]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re/util/include/event
update
[l4.git] / l4 / pkg / l4re / util / include / event
1 // vi:ft=cpp:
2 /**
3  * \file
4  */
5 /*
6  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
7  *               Alexander Warg <warg@os.inf.tu-dresden.de>
8  *     economic rights: Technische Universität Dresden (Germany)
9  *
10  * This file is part of TUD:OS and distributed under the terms of the
11  * GNU General Public License 2.
12  * Please see the COPYING-GPL-2 file for details.
13  *
14  * As a special exception, you may use this file as part of a free software
15  * library without restriction.  Specifically, if other files instantiate
16  * templates or use macros or inline functions from this file, or you compile
17  * this file and link it with other files to produce an executable, this
18  * file does not by itself cause the resulting executable to be covered by
19  * the GNU General Public License.  This exception does not however
20  * invalidate any other reasons why the executable file might be covered by
21  * the GNU General Public License.
22  */
23 #pragma once
24
25 #include <l4/re/cap_alloc>
26 #include <l4/re/util/cap_alloc>
27 #include <l4/re/env>
28 #include <l4/re/rm>
29 #include <l4/re/util/event_buffer>
30 #include <l4/sys/factory>
31
32 namespace L4Re { namespace Util {
33
34 /**
35  * \brief Convenience wrapper for getting access to an event object.
36  *
37  * After calling init() the class supplies the event-buffer and the
38  * associated IRQ object.
39  */
40 template< typename PAYLOAD >
41 class Event_t
42 {
43 public:
44   /**
45    * \brief Modes of operation.
46    */
47   enum Mode
48   {
49     Mode_irq,      ///< Create an IRQ and attach, to get notifications.
50     Mode_polling,  ///< Do not use an IRQ.
51   };
52
53   /**
54    * \brief Initialise an event object.
55    *
56    * \param event   Capability to event.
57    * \param env     Optional: Pointer to L4Re-Environment
58    * \param ca      Optional: Pointer to capability allocator.
59    * \return 0 on success, error code on error
60    */
61   int init(L4::Cap<L4Re::Event> event, Mode mode = Mode_irq,
62            L4Re::Env const *env = L4Re::Env::env(),
63            L4Re::Cap_alloc *ca = L4Re::Cap_alloc::get_cap_alloc(L4Re::Util::cap_alloc))
64   {
65     Auto_cap<L4Re::Dataspace>::Cap ev_ds = ca->alloc<L4Re::Dataspace>();
66     if (!ev_ds.is_valid())
67       return -L4_ENOMEM;
68
69     int r;
70
71     Auto_del_cap<L4::Irq>::Cap ev_irq;
72
73     if (mode == Mode_irq)
74       {
75         ev_irq = ca->alloc<L4::Irq>();
76         if (!ev_irq.is_valid())
77           return -L4_ENOMEM;
78
79         if ((r = l4_error(env->factory()->create_irq(ev_irq.get()))))
80           return r;
81
82         if ((r = l4_error(event->bind(0, ev_irq.get()))))
83           return r;
84       }
85
86     if ((r = event->get_buffer(ev_ds.get())))
87       return r;
88
89     long sz = ev_ds->size();
90     if (sz < 0)
91       return sz;
92
93     Rm::Auto_region<void*> buf;
94
95     if ((r = env->rm()->attach(&buf, sz, L4Re::Rm::Search_addr, ev_ds.get())))
96       return r;
97
98     _ev_buffer = L4Re::Event_buffer_t<PAYLOAD>(buf.get(), sz);
99     _ev_ds     = ev_ds;
100     _ev_irq    = ev_irq;
101     _buf       = buf;
102
103     return 0;
104   }
105
106   /**
107    * \brief Get event buffer.
108    * \return Event buffer object.
109    */
110   L4Re::Event_buffer_t<PAYLOAD> &buffer() { return _ev_buffer; }
111
112   /**
113    * \brief Get event IRQ.
114    * \return Event IRQ.
115    */
116   L4::Cap<L4::Irq> irq() const { return _ev_irq.get(); }
117
118 private:
119   Auto_cap<L4Re::Dataspace>::Cap _ev_ds;
120   Auto_del_cap<L4::Irq>::Cap _ev_irq;
121   L4Re::Event_buffer_t<PAYLOAD> _ev_buffer;
122   Rm::Auto_region<void*> _buf;
123 };
124
125 typedef Event_t<Default_event_payload> Event;
126
127 }}