// set vi:ft=cpp: /* * (c) 2008-2009 Adam Lackorzynski , * Alexander Warg * economic rights: Technische Universität Dresden (Germany) * * This file is part of TUD:OS and distributed under the terms of the * GNU General Public License 2. * Please see the COPYING-GPL-2 file for details. * * As a special exception, you may use this file as part of a free software * library without restriction. Specifically, if other files instantiate * templates or use macros or inline functions from this file, or you compile * this file and link it with other files to produce an executable, this * file does not by itself cause the resulting executable to be covered by * the GNU General Public License. This exception does not however * invalidate any other reasons why the executable file might be covered by * the GNU General Public License. */ #pragma once #include #include #include #include namespace L4Re { namespace Util { /** * \brief Event_buffer utility class. * \ingroup api_l4re_util */ template< typename PAYLOAD > class Event_buffer_t : public L4Re::Event_buffer_t { private: void *_buf; public: /** * \brief Return the buffer. * * \return Pointer to the event buffer. */ void *buf() const throw() { return _buf; } /** * \brief Attach event buffer from address space. * * \param ds Dataspace of the event buffer. * \param rm Region manager to attach buffer to. * * \return 0 on success, negative error code otherwise. */ long attach(L4::Cap ds, L4::Cap rm) throw() { l4_addr_t sz = ds->size(); _buf = 0; long r = rm->attach(&_buf, sz, L4Re::Rm::Search_addr, ds); if (r < 0) return r; *(L4Re::Event_buffer_t*)this = L4Re::Event_buffer_t(_buf, sz); return 0; } /** * \brief Detach event buffer from address space. * * \param rm Region manager to detach buffer from. * \param buf Buffer pointer. * * \return 0 on success, negative error code otherwise. */ long detach(L4::Cap rm, void *buf) throw() { L4::Cap ds; return rm->detach(buf, &ds); } }; /** * \brief An event buffer consumer. * \ingroup api_l4re_util */ template< typename PAYLOAD > class Event_buffer_consumer_t : public Event_buffer_t { public: /** * \brief Call function on every available event. * * \param cb Function callback. */ template< typename CB, typename D > void foreach_available_event(CB const &cb, D data = D()) { typename Event_buffer_t::Event *e; while ((e = Event_buffer_t::next())) { cb(e, data); e->free(); } } /** * \brief Continuously wait for events and process them. * * \param irq Event signal to wait for. * \param thread Thread capability of the thread calling this function. * \param cb Callback function that is called for each received event. * * \note This function never returns. */ template< typename CB, typename D > void process(L4::Cap irq, L4::Cap thread, CB const &cb, D data = D()) { if (l4_error(irq->attach(0, thread))) return; while (1) { long r; r = l4_ipc_error(l4_irq_receive(irq.cap(), L4_IPC_NEVER), l4_utcb()); if (r) continue; foreach_available_event(cb, data); } } }; typedef Event_buffer_t Event_buffer; typedef Event_buffer_consumer_t Event_buffer_consumer; }}