1.1 --- a/pkg/devices/input/include/input-event-loop.h Fri May 18 18:43:30 2018 +0200
1.2 +++ b/pkg/devices/input/include/input-event-loop.h Fri May 18 22:34:51 2018 +0200
1.3 @@ -23,66 +23,53 @@
1.4
1.5 #ifdef __cplusplus
1.6
1.7 +#include <l4/devices/event-loop.h>
1.8 +
1.9 #include <l4/re/event>
1.10 #include <l4/sys/capability>
1.11 #include <l4/sys/icu>
1.12
1.13 -#include <pthread.h>
1.14 -#include <pthread-l4.h>
1.15 -
1.16 -/* POSIX-thread-compatible handler function type. */
1.17 -
1.18 -typedef void (*Event_handler)(L4Re::Event_buffer::Event &, void *);
1.19 +typedef L4Re::Event_buffer::Event Event_type;
1.20
1.21 /* Input event loop abstraction. */
1.22
1.23 -class Input_event_loop
1.24 +class Input_event_loop : public Event_loop
1.25 {
1.26 - /* Notification interrupt capability. */
1.27 -
1.28 - L4::Cap<L4::Irq> _irq;
1.29 -
1.30 + /* Handler function type. */
1.31 +
1.32 + typedef void (*Event_handler)(Event_type &, void *);
1.33 +
1.34 /* External handler function. */
1.35 -
1.36 +
1.37 Event_handler _handler;
1.38
1.39 - /* Private data for the handler. */
1.40 -
1.41 - void *_priv;
1.42 -
1.43 /* Event buffer and notification interrupt capability. */
1.44
1.45 L4Re::Event_buffer _event_buffer;
1.46
1.47 - /* Thread properties. */
1.48 + /* Notification interrupt capability. */
1.49
1.50 - int _priority;
1.51 - pthread_t _pthread;
1.52 + L4::Cap<L4::Irq> _irq;
1.53
1.54 public:
1.55 - /* Initialise the event loop with a notification interrupt, thread priority,
1.56 - event handler function and private data, and an event buffer. */
1.57 + /* Initialise the event loop with an event handler function and private data,
1.58 + an event buffer, a notification interrupt, and a thread priority. */
1.59
1.60 - explicit Input_event_loop(L4::Cap<L4::Irq> irq,
1.61 - Event_handler handler, void *priv,
1.62 + explicit Input_event_loop(Event_handler handler, void *priv,
1.63 L4Re::Event_buffer event_buffer,
1.64 + L4::Cap<L4::Irq> irq,
1.65 int priority=0x20)
1.66 - : _irq(irq), _handler(handler), _priv(priv), _event_buffer(event_buffer),
1.67 - _priority(priority)
1.68 + : _handler(handler), Event_loop(priv, priority), _event_buffer(event_buffer), _irq(irq)
1.69 {
1.70 }
1.71
1.72 - /* Static main loop function for event-handling thread. */
1.73 -
1.74 - static void *event_loop(void *data);
1.75 -
1.76 /* Event handler method. */
1.77
1.78 - void handle();
1.79 + virtual void handle();
1.80
1.81 /* Initiation function. */
1.82
1.83 - void start();
1.84 + virtual void start(l4_umword_t label=0xDF00);
1.85 };
1.86
1.87 #endif
2.1 --- a/pkg/devices/input/src/client/Makefile Fri May 18 18:43:30 2018 +0200
2.2 +++ b/pkg/devices/input/src/client/Makefile Fri May 18 22:34:51 2018 +0200
2.3 @@ -8,6 +8,6 @@
2.4
2.5 PRIVATE_INCDIR += $(PKGDIR)/input/include
2.6
2.7 -REQUIRES_LIBS := l4re_c l4re_c-util
2.8 +REQUIRES_LIBS := l4re_c l4re_c-util libdevice-util
2.9
2.10 include $(L4DIR)/mk/lib.mk
3.1 --- a/pkg/devices/input/src/client/input-event-loop.cc Fri May 18 18:43:30 2018 +0200
3.2 +++ b/pkg/devices/input/src/client/input-event-loop.cc Fri May 18 22:34:51 2018 +0200
3.3 @@ -19,25 +19,10 @@
3.4 * Boston, MA 02110-1301, USA
3.5 */
3.6
3.7 -#include <l4/devices/input-event-loop.h>
3.8 -
3.9 -#include <pthread.h>
3.10 -#include <pthread-l4.h>
3.11 +#include "input-event-loop.h"
3.12
3.13 #include <l4/re/event>
3.14
3.15 -/* Main loop for event threads. */
3.16 -
3.17 -void *
3.18 -Input_event_loop::event_loop(void *data)
3.19 -{
3.20 - Input_event_loop *self = reinterpret_cast<Input_event_loop *>(data);
3.21 -
3.22 - while (1) self->handle();
3.23 -
3.24 - return 0;
3.25 -}
3.26 -
3.27 /* Handler for incoming interrupts signalling the presence of events. */
3.28
3.29 void
3.30 @@ -58,24 +43,9 @@
3.31 /* Event loop initiation. */
3.32
3.33 void
3.34 -Input_event_loop::start()
3.35 +Input_event_loop::start(l4_umword_t label)
3.36 {
3.37 - pthread_attr_t thread_attr;
3.38 - struct sched_param sp;
3.39 -
3.40 - /* Thread initialisation boilerplate. */
3.41 -
3.42 - if (pthread_attr_init(&thread_attr))
3.43 - return;
3.44 + Event_loop::start();
3.45
3.46 - sp.sched_priority = _priority;
3.47 - pthread_attr_setschedparam(&thread_attr, &sp);
3.48 - pthread_attr_setschedpolicy(&thread_attr, SCHED_L4);
3.49 - pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED);
3.50 -
3.51 - /* Provide this instance as the private data. */
3.52 -
3.53 - pthread_create(&_pthread, &thread_attr, event_loop, this);
3.54 -
3.55 - _irq->bind_thread(Pthread::L4::cap(_pthread), 0xDEAD);
3.56 + _irq->bind_thread(Pthread::L4::cap(_pthread), label);
3.57 }
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/pkg/devices/util/include/event-loop.h Fri May 18 22:34:51 2018 +0200
4.3 @@ -0,0 +1,65 @@
4.4 +/*
4.5 + * Generic event loop functionality.
4.6 + *
4.7 + * (c) 2018 Paul Boddie <paul@boddie.org.uk>
4.8 + *
4.9 + * This program is free software; you can redistribute it and/or
4.10 + * modify it under the terms of the GNU General Public License as
4.11 + * published by the Free Software Foundation; either version 2 of
4.12 + * the License, or (at your option) any later version.
4.13 + *
4.14 + * This program is distributed in the hope that it will be useful,
4.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.17 + * GNU General Public License for more details.
4.18 + *
4.19 + * You should have received a copy of the GNU General Public License
4.20 + * along with this program; if not, write to the Free Software
4.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
4.22 + * Boston, MA 02110-1301, USA
4.23 + */
4.24 +
4.25 +#pragma once
4.26 +
4.27 +#ifdef __cplusplus
4.28 +
4.29 +#include <pthread.h>
4.30 +#include <pthread-l4.h>
4.31 +
4.32 +/* Event loop abstraction. */
4.33 +
4.34 +class Event_loop
4.35 +{
4.36 +protected:
4.37 + /* Private data for the handler. */
4.38 +
4.39 + void *_priv;
4.40 +
4.41 + /* Thread properties. */
4.42 +
4.43 + int _priority;
4.44 + pthread_t _pthread;
4.45 +
4.46 +public:
4.47 + /* Initialise the event loop with an event handler function and private data,
4.48 + plus thread priority. */
4.49 +
4.50 + explicit Event_loop(void *priv, int priority=0x20)
4.51 + : _priv(priv), _priority(priority)
4.52 + {
4.53 + }
4.54 +
4.55 + /* Static main loop function for event-handling thread. */
4.56 +
4.57 + static void *event_loop(void *data);
4.58 +
4.59 + /* Event handler method. */
4.60 +
4.61 + virtual void handle() = 0;
4.62 +
4.63 + /* Initiation function. */
4.64 +
4.65 + virtual void start();
4.66 +};
4.67 +
4.68 +#endif
5.1 --- a/pkg/devices/util/src/Makefile Fri May 18 18:43:30 2018 +0200
5.2 +++ b/pkg/devices/util/src/Makefile Fri May 18 22:34:51 2018 +0200
5.3 @@ -4,7 +4,7 @@
5.4 TARGET = libdevice_util.o.a
5.5 PC_FILENAME := libdevice-util
5.6
5.7 -SRC_CC := dataspace.cc memory.cc
5.8 +SRC_CC := dataspace.cc event-loop.cc memory.cc
5.9
5.10 PRIVATE_INCDIR += $(PKGDIR)/util/include
5.11
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/pkg/devices/util/src/event-loop.cc Fri May 18 22:34:51 2018 +0200
6.3 @@ -0,0 +1,62 @@
6.4 +/*
6.5 + * Generic event loop functionality.
6.6 + *
6.7 + * (c) 2018 Paul Boddie <paul@boddie.org.uk>
6.8 + *
6.9 + * This program is free software; you can redistribute it and/or
6.10 + * modify it under the terms of the GNU General Public License as
6.11 + * published by the Free Software Foundation; either version 2 of
6.12 + * the License, or (at your option) any later version.
6.13 + *
6.14 + * This program is distributed in the hope that it will be useful,
6.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6.17 + * GNU General Public License for more details.
6.18 + *
6.19 + * You should have received a copy of the GNU General Public License
6.20 + * along with this program; if not, write to the Free Software
6.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
6.22 + * Boston, MA 02110-1301, USA
6.23 + */
6.24 +
6.25 +#include "event-loop.h"
6.26 +
6.27 +#include <pthread.h>
6.28 +#include <pthread-l4.h>
6.29 +
6.30 +#include <l4/re/event>
6.31 +
6.32 +/* Main loop for event threads. */
6.33 +
6.34 +void *
6.35 +Event_loop::event_loop(void *data)
6.36 +{
6.37 + Event_loop *self = reinterpret_cast<Event_loop *>(data);
6.38 +
6.39 + while (1) self->handle();
6.40 +
6.41 + return 0;
6.42 +}
6.43 +
6.44 +/* Event loop initiation. */
6.45 +
6.46 +void
6.47 +Event_loop::start()
6.48 +{
6.49 + pthread_attr_t thread_attr;
6.50 + struct sched_param sp;
6.51 +
6.52 + /* Thread initialisation boilerplate. */
6.53 +
6.54 + if (pthread_attr_init(&thread_attr))
6.55 + return;
6.56 +
6.57 + sp.sched_priority = _priority;
6.58 + pthread_attr_setschedparam(&thread_attr, &sp);
6.59 + pthread_attr_setschedpolicy(&thread_attr, SCHED_L4);
6.60 + pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED);
6.61 +
6.62 + /* Provide this instance as the private data. */
6.63 +
6.64 + pthread_create(&_pthread, &thread_attr, event_loop, this);
6.65 +}
7.1 --- a/pkg/landfall-examples/input_event_client/input_event_client.cc Fri May 18 18:43:30 2018 +0200
7.2 +++ b/pkg/landfall-examples/input_event_client/input_event_client.cc Fri May 18 22:34:51 2018 +0200
7.3 @@ -301,7 +301,7 @@
7.4
7.5 /* Create an event handler and wait for events. */
7.6
7.7 - Input_event_loop loop(irq, handler, priv, event_buffer);
7.8 + Input_event_loop loop(handler, priv, event_buffer, irq);
7.9
7.10 loop.start();
7.11 l4_sleep_forever();