paul@9 | 1 | /* |
paul@9 | 2 | * Generic event loop functionality. |
paul@9 | 3 | * |
paul@9 | 4 | * (c) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@9 | 5 | * |
paul@9 | 6 | * This program is free software; you can redistribute it and/or |
paul@9 | 7 | * modify it under the terms of the GNU General Public License as |
paul@9 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@9 | 9 | * the License, or (at your option) any later version. |
paul@9 | 10 | * |
paul@9 | 11 | * This program is distributed in the hope that it will be useful, |
paul@9 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@9 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@9 | 14 | * GNU General Public License for more details. |
paul@9 | 15 | * |
paul@9 | 16 | * You should have received a copy of the GNU General Public License |
paul@9 | 17 | * along with this program; if not, write to the Free Software |
paul@9 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@9 | 19 | * Boston, MA 02110-1301, USA |
paul@9 | 20 | */ |
paul@9 | 21 | |
paul@9 | 22 | #pragma once |
paul@9 | 23 | |
paul@9 | 24 | #ifdef __cplusplus |
paul@9 | 25 | |
paul@9 | 26 | #include <pthread.h> |
paul@9 | 27 | #include <pthread-l4.h> |
paul@9 | 28 | |
paul@9 | 29 | /* Event loop abstraction. */ |
paul@9 | 30 | |
paul@9 | 31 | class Event_loop |
paul@9 | 32 | { |
paul@9 | 33 | protected: |
paul@9 | 34 | /* Thread properties. */ |
paul@9 | 35 | |
paul@9 | 36 | int _priority; |
paul@9 | 37 | pthread_t _pthread; |
paul@9 | 38 | |
paul@9 | 39 | public: |
paul@10 | 40 | /* Initialise the event loop with a thread priority. */ |
paul@9 | 41 | |
paul@10 | 42 | explicit Event_loop(int priority=0x20) |
paul@10 | 43 | : _priority(priority) |
paul@9 | 44 | { |
paul@9 | 45 | } |
paul@9 | 46 | |
paul@9 | 47 | /* Static main loop function for event-handling thread. */ |
paul@9 | 48 | |
paul@9 | 49 | static void *event_loop(void *data); |
paul@9 | 50 | |
paul@9 | 51 | /* Event handler method. */ |
paul@9 | 52 | |
paul@9 | 53 | virtual void handle() = 0; |
paul@9 | 54 | |
paul@9 | 55 | /* Initiation function. */ |
paul@9 | 56 | |
paul@9 | 57 | virtual void start(); |
paul@9 | 58 | }; |
paul@9 | 59 | |
paul@10 | 60 | |
paul@10 | 61 | |
paul@10 | 62 | /* Event loop with handler. */ |
paul@10 | 63 | |
paul@10 | 64 | template <class T> |
paul@10 | 65 | class Event_handler_loop : public Event_loop |
paul@10 | 66 | { |
paul@10 | 67 | protected: |
paul@10 | 68 | /* Event type. */ |
paul@10 | 69 | |
paul@10 | 70 | typedef T Event_type; |
paul@10 | 71 | |
paul@10 | 72 | /* Handler function type. */ |
paul@10 | 73 | |
paul@10 | 74 | typedef void (*Event_handler)(Event_type &); |
paul@10 | 75 | |
paul@10 | 76 | /* External handler function. */ |
paul@10 | 77 | |
paul@10 | 78 | Event_handler _handler; |
paul@10 | 79 | |
paul@10 | 80 | public: |
paul@10 | 81 | /* Initialise the event loop with a thread priority. */ |
paul@10 | 82 | |
paul@10 | 83 | explicit Event_handler_loop(int priority=0x20) |
paul@10 | 84 | : Event_loop(priority) |
paul@10 | 85 | { |
paul@10 | 86 | } |
paul@10 | 87 | |
paul@10 | 88 | /* Attach a handler to the loop. */ |
paul@10 | 89 | |
paul@10 | 90 | virtual void attach(Event_handler handler) |
paul@10 | 91 | { |
paul@10 | 92 | _handler = handler; |
paul@10 | 93 | } |
paul@10 | 94 | }; |
paul@10 | 95 | |
paul@11 | 96 | /* Event loop with handler and null event type. */ |
paul@11 | 97 | |
paul@11 | 98 | template <> |
paul@11 | 99 | class Event_handler_loop<void> : public Event_loop |
paul@11 | 100 | { |
paul@11 | 101 | protected: |
paul@11 | 102 | /* Event type. */ |
paul@11 | 103 | |
paul@11 | 104 | typedef void Event_type; |
paul@11 | 105 | |
paul@11 | 106 | /* Handler function type. */ |
paul@11 | 107 | |
paul@11 | 108 | typedef void (*Event_handler)(); |
paul@11 | 109 | |
paul@11 | 110 | /* External handler function. */ |
paul@11 | 111 | |
paul@11 | 112 | Event_handler _handler; |
paul@11 | 113 | |
paul@11 | 114 | public: |
paul@11 | 115 | /* Initialise the event loop with a thread priority. */ |
paul@11 | 116 | |
paul@11 | 117 | explicit Event_handler_loop(int priority=0x20) |
paul@11 | 118 | : Event_loop(priority) |
paul@11 | 119 | { |
paul@11 | 120 | } |
paul@11 | 121 | |
paul@11 | 122 | /* Attach a handler to the loop. */ |
paul@11 | 123 | |
paul@11 | 124 | virtual void attach(Event_handler handler) |
paul@11 | 125 | { |
paul@11 | 126 | _handler = handler; |
paul@11 | 127 | } |
paul@11 | 128 | }; |
paul@11 | 129 | |
paul@9 | 130 | #endif |