L4Re - L4 Runtime Environment
event_buffer
1 // vi:set ft=cpp: -*- Mode: C++ -*-
2 /*
3  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
4  * Alexander Warg <warg@os.inf.tu-dresden.de>
5  * economic rights: Technische Universit├Ąt Dresden (Germany)
6  *
7  * This file is part of TUD:OS and distributed under the terms of the
8  * GNU General Public License 2.
9  * Please see the COPYING-GPL-2 file for details.
10  *
11  * As a special exception, you may use this file as part of a free software
12  * library without restriction. Specifically, if other files instantiate
13  * templates or use macros or inline functions from this file, or you compile
14  * this file and link it with other files to produce an executable, this
15  * file does not by itself cause the resulting executable to be covered by
16  * the GNU General Public License. This exception does not however
17  * invalidate any other reasons why the executable file might be covered by
18  * the GNU General Public License.
19  */
20 
21 #pragma once
22 
23 #include <l4/re/event>
24 #include <l4/re/event-sys.h>
25 #include <l4/re/rm>
26 
27 #include <cstring>
28 
29 namespace L4Re { namespace Util {
30 
35 template< typename PAYLOAD >
36 class Event_buffer_t : public L4Re::Event_buffer_t<PAYLOAD>
37 {
38 private:
39  void *_buf;
40 public:
46  void *buf() const noexcept { return _buf; }
47 
57  {
58  l4_addr_t sz = ds->size();
59  _buf = 0;
60 
61  long r = rm->attach(&_buf, sz,
62  L4Re::Rm::F::Search_addr | L4Re::Rm::F::RW,
64  if (r < 0)
65  return r;
66 
68  return 0;
69  }
70 
78  long detach(L4::Cap<L4Re::Rm> rm) noexcept
79  {
81  if (_buf)
82  return rm->detach(_buf, &ds);
83  return 0;
84  }
85 
86 };
87 
92 template< typename PAYLOAD >
93 class Event_buffer_consumer_t : public Event_buffer_t<PAYLOAD>
94 {
95 public:
96 
103  template< typename CB, typename D >
104  void foreach_available_event(CB const &cb, D data = D())
105  {
106  typename Event_buffer_t<PAYLOAD>::Event *e;
107  while ((e = Event_buffer_t<PAYLOAD>::next()))
108  {
109  cb(e, data);
110  e->free();
111  }
112  }
113 
124  template< typename CB, typename D >
126  L4::Cap<L4::Thread> thread,
127  CB const &cb, D data = D())
128  {
129 
130  if (l4_error(irq->bind_thread(thread, 0)))
131  return;
132 
133  while (1)
134  {
135  long r;
137  l4_utcb());
138  if (r)
139  continue;
140 
141  foreach_available_event(cb, data);
142  }
143  }
144 };
145 
148 
149 }}
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition: capability.h:52
l4_umword_t l4_ipc_error(l4_msgtag_t tag, l4_utcb_t *utcb) L4_NOTHROW
Get the error code for an object invocation.
Definition: ipc.h:503
Event buffer class.
Definition: event:198
L4Re C++ Interfaces.
Definition: cmd_control:15
#define L4_IPC_NEVER
never timeout
Definition: __timeout.h:82
Region mapper interface.
void foreach_available_event(CB const &cb, D data=D())
Call function on every available event.
Definition: event_buffer:104
void * buf() const noexcept
Return the buffer.
Definition: event_buffer:46
void process(L4::Cap< L4::Irq > irq, L4::Cap< L4::Thread > thread, CB const &cb, D data=D())
Continuously wait for events and process them.
Definition: event_buffer:125
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition: utcb.h:340
An event buffer consumer.
Definition: event_buffer:93
void free() noexcept
Free the entry.
Definition: event:213
long attach(L4::Cap< L4Re::Dataspace > ds, L4::Cap< L4Re::Rm > rm) noexcept
Attach event buffer from address space.
Definition: event_buffer:56
long l4_error(l4_msgtag_t tag) L4_NOTHROW
Return error code of a system call return message tag or the tag label.
Definition: ipc.h:520
Event structure used in buffer.
Definition: event:205
l4_msgtag_t l4_irq_receive(l4_cap_idx_t irq, l4_timeout_t to) L4_NOTHROW
Unmask and wait for specified IRQ.
Definition: irq.h:359
long detach(L4::Cap< L4Re::Rm > rm) noexcept
Detach event buffer from address space.
Definition: event_buffer:78
Search for a suitable address range.
Definition: rm:105
unsigned long l4_addr_t
Address type.
Definition: l4int.h:45
Event_buffer utility class.
Definition: event_buffer:36
Cap< T > make_cap_rw(L4::Cap< T > cap) noexcept
Make an L4::Ipc::Cap<T> for the given capability with L4_CAP_FPAGE_RW rights.
Definition: ipc_types:634
l4_msgtag_t bind_thread(Ipc::Opt< Ipc::Cap< Thread > > t, l4_umword_t label)
Bind a thread to an IPC receive endpoint.