# HG changeset patch # User Paul Boddie # Date 1526675691 -7200 # Node ID 951e3acf12cecf8b0c49f7b6b5f87c86c2c6469b # Parent 22fa3b147e4ca2e5f64806ba5970073270efc714 Introduced a generic event loop as the basis of the input event loop. diff -r 22fa3b147e4c -r 951e3acf12ce pkg/devices/input/include/input-event-loop.h --- a/pkg/devices/input/include/input-event-loop.h Fri May 18 18:43:30 2018 +0200 +++ b/pkg/devices/input/include/input-event-loop.h Fri May 18 22:34:51 2018 +0200 @@ -23,66 +23,53 @@ #ifdef __cplusplus +#include + #include #include #include -#include -#include - -/* POSIX-thread-compatible handler function type. */ - -typedef void (*Event_handler)(L4Re::Event_buffer::Event &, void *); +typedef L4Re::Event_buffer::Event Event_type; /* Input event loop abstraction. */ -class Input_event_loop +class Input_event_loop : public Event_loop { - /* Notification interrupt capability. */ - - L4::Cap _irq; - + /* Handler function type. */ + + typedef void (*Event_handler)(Event_type &, void *); + /* External handler function. */ - + Event_handler _handler; - /* Private data for the handler. */ - - void *_priv; - /* Event buffer and notification interrupt capability. */ L4Re::Event_buffer _event_buffer; - /* Thread properties. */ + /* Notification interrupt capability. */ - int _priority; - pthread_t _pthread; + L4::Cap _irq; public: - /* Initialise the event loop with a notification interrupt, thread priority, - event handler function and private data, and an event buffer. */ + /* Initialise the event loop with an event handler function and private data, + an event buffer, a notification interrupt, and a thread priority. */ - explicit Input_event_loop(L4::Cap irq, - Event_handler handler, void *priv, + explicit Input_event_loop(Event_handler handler, void *priv, L4Re::Event_buffer event_buffer, + L4::Cap irq, int priority=0x20) - : _irq(irq), _handler(handler), _priv(priv), _event_buffer(event_buffer), - _priority(priority) + : _handler(handler), Event_loop(priv, priority), _event_buffer(event_buffer), _irq(irq) { } - /* Static main loop function for event-handling thread. */ - - static void *event_loop(void *data); - /* Event handler method. */ - void handle(); + virtual void handle(); /* Initiation function. */ - void start(); + virtual void start(l4_umword_t label=0xDF00); }; #endif diff -r 22fa3b147e4c -r 951e3acf12ce pkg/devices/input/src/client/Makefile --- a/pkg/devices/input/src/client/Makefile Fri May 18 18:43:30 2018 +0200 +++ b/pkg/devices/input/src/client/Makefile Fri May 18 22:34:51 2018 +0200 @@ -8,6 +8,6 @@ PRIVATE_INCDIR += $(PKGDIR)/input/include -REQUIRES_LIBS := l4re_c l4re_c-util +REQUIRES_LIBS := l4re_c l4re_c-util libdevice-util include $(L4DIR)/mk/lib.mk diff -r 22fa3b147e4c -r 951e3acf12ce pkg/devices/input/src/client/input-event-loop.cc --- a/pkg/devices/input/src/client/input-event-loop.cc Fri May 18 18:43:30 2018 +0200 +++ b/pkg/devices/input/src/client/input-event-loop.cc Fri May 18 22:34:51 2018 +0200 @@ -19,25 +19,10 @@ * Boston, MA 02110-1301, USA */ -#include - -#include -#include +#include "input-event-loop.h" #include -/* Main loop for event threads. */ - -void * -Input_event_loop::event_loop(void *data) -{ - Input_event_loop *self = reinterpret_cast(data); - - while (1) self->handle(); - - return 0; -} - /* Handler for incoming interrupts signalling the presence of events. */ void @@ -58,24 +43,9 @@ /* Event loop initiation. */ void -Input_event_loop::start() +Input_event_loop::start(l4_umword_t label) { - pthread_attr_t thread_attr; - struct sched_param sp; - - /* Thread initialisation boilerplate. */ - - if (pthread_attr_init(&thread_attr)) - return; + Event_loop::start(); - sp.sched_priority = _priority; - pthread_attr_setschedparam(&thread_attr, &sp); - pthread_attr_setschedpolicy(&thread_attr, SCHED_L4); - pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED); - - /* Provide this instance as the private data. */ - - pthread_create(&_pthread, &thread_attr, event_loop, this); - - _irq->bind_thread(Pthread::L4::cap(_pthread), 0xDEAD); + _irq->bind_thread(Pthread::L4::cap(_pthread), label); } diff -r 22fa3b147e4c -r 951e3acf12ce pkg/devices/util/include/event-loop.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/devices/util/include/event-loop.h Fri May 18 22:34:51 2018 +0200 @@ -0,0 +1,65 @@ +/* + * Generic event loop functionality. + * + * (c) 2018 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#pragma once + +#ifdef __cplusplus + +#include +#include + +/* Event loop abstraction. */ + +class Event_loop +{ +protected: + /* Private data for the handler. */ + + void *_priv; + + /* Thread properties. */ + + int _priority; + pthread_t _pthread; + +public: + /* Initialise the event loop with an event handler function and private data, + plus thread priority. */ + + explicit Event_loop(void *priv, int priority=0x20) + : _priv(priv), _priority(priority) + { + } + + /* Static main loop function for event-handling thread. */ + + static void *event_loop(void *data); + + /* Event handler method. */ + + virtual void handle() = 0; + + /* Initiation function. */ + + virtual void start(); +}; + +#endif diff -r 22fa3b147e4c -r 951e3acf12ce pkg/devices/util/src/Makefile --- a/pkg/devices/util/src/Makefile Fri May 18 18:43:30 2018 +0200 +++ b/pkg/devices/util/src/Makefile Fri May 18 22:34:51 2018 +0200 @@ -4,7 +4,7 @@ TARGET = libdevice_util.o.a PC_FILENAME := libdevice-util -SRC_CC := dataspace.cc memory.cc +SRC_CC := dataspace.cc event-loop.cc memory.cc PRIVATE_INCDIR += $(PKGDIR)/util/include diff -r 22fa3b147e4c -r 951e3acf12ce pkg/devices/util/src/event-loop.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/devices/util/src/event-loop.cc Fri May 18 22:34:51 2018 +0200 @@ -0,0 +1,62 @@ +/* + * Generic event loop functionality. + * + * (c) 2018 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include "event-loop.h" + +#include +#include + +#include + +/* Main loop for event threads. */ + +void * +Event_loop::event_loop(void *data) +{ + Event_loop *self = reinterpret_cast(data); + + while (1) self->handle(); + + return 0; +} + +/* Event loop initiation. */ + +void +Event_loop::start() +{ + pthread_attr_t thread_attr; + struct sched_param sp; + + /* Thread initialisation boilerplate. */ + + if (pthread_attr_init(&thread_attr)) + return; + + sp.sched_priority = _priority; + pthread_attr_setschedparam(&thread_attr, &sp); + pthread_attr_setschedpolicy(&thread_attr, SCHED_L4); + pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED); + + /* Provide this instance as the private data. */ + + pthread_create(&_pthread, &thread_attr, event_loop, this); +} diff -r 22fa3b147e4c -r 951e3acf12ce pkg/landfall-examples/input_event_client/input_event_client.cc --- a/pkg/landfall-examples/input_event_client/input_event_client.cc Fri May 18 18:43:30 2018 +0200 +++ b/pkg/landfall-examples/input_event_client/input_event_client.cc Fri May 18 22:34:51 2018 +0200 @@ -301,7 +301,7 @@ /* Create an event handler and wait for events. */ - Input_event_loop loop(irq, handler, priv, event_buffer); + Input_event_loop loop(handler, priv, event_buffer, irq); loop.start(); l4_sleep_forever();