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 | /* Private data for the handler. */ |
paul@9 | 35 | |
paul@9 | 36 | void *_priv; |
paul@9 | 37 | |
paul@9 | 38 | /* Thread properties. */ |
paul@9 | 39 | |
paul@9 | 40 | int _priority; |
paul@9 | 41 | pthread_t _pthread; |
paul@9 | 42 | |
paul@9 | 43 | public: |
paul@9 | 44 | /* Initialise the event loop with an event handler function and private data, |
paul@9 | 45 | plus thread priority. */ |
paul@9 | 46 | |
paul@9 | 47 | explicit Event_loop(void *priv, int priority=0x20) |
paul@9 | 48 | : _priv(priv), _priority(priority) |
paul@9 | 49 | { |
paul@9 | 50 | } |
paul@9 | 51 | |
paul@9 | 52 | /* Static main loop function for event-handling thread. */ |
paul@9 | 53 | |
paul@9 | 54 | static void *event_loop(void *data); |
paul@9 | 55 | |
paul@9 | 56 | /* Event handler method. */ |
paul@9 | 57 | |
paul@9 | 58 | virtual void handle() = 0; |
paul@9 | 59 | |
paul@9 | 60 | /* Initiation function. */ |
paul@9 | 61 | |
paul@9 | 62 | virtual void start(); |
paul@9 | 63 | }; |
paul@9 | 64 | |
paul@9 | 65 | #endif |