paul@9 | 1 | /* |
paul@9 | 2 | * Generic event loop functionality. |
paul@9 | 3 | * |
paul@31 | 4 | * Copyright (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 | #include "event-loop.h" |
paul@9 | 23 | |
paul@9 | 24 | #include <pthread.h> |
paul@9 | 25 | #include <pthread-l4.h> |
paul@9 | 26 | |
paul@9 | 27 | #include <l4/re/event> |
paul@9 | 28 | |
paul@9 | 29 | /* Main loop for event threads. */ |
paul@9 | 30 | |
paul@9 | 31 | void * |
paul@9 | 32 | Event_loop::event_loop(void *data) |
paul@9 | 33 | { |
paul@9 | 34 | Event_loop *self = reinterpret_cast<Event_loop *>(data); |
paul@9 | 35 | |
paul@9 | 36 | while (1) self->handle(); |
paul@9 | 37 | |
paul@9 | 38 | return 0; |
paul@9 | 39 | } |
paul@9 | 40 | |
paul@9 | 41 | /* Event loop initiation. */ |
paul@9 | 42 | |
paul@9 | 43 | void |
paul@9 | 44 | Event_loop::start() |
paul@9 | 45 | { |
paul@9 | 46 | pthread_attr_t thread_attr; |
paul@9 | 47 | struct sched_param sp; |
paul@9 | 48 | |
paul@9 | 49 | /* Thread initialisation boilerplate. */ |
paul@9 | 50 | |
paul@9 | 51 | if (pthread_attr_init(&thread_attr)) |
paul@9 | 52 | return; |
paul@9 | 53 | |
paul@9 | 54 | sp.sched_priority = _priority; |
paul@9 | 55 | pthread_attr_setschedparam(&thread_attr, &sp); |
paul@9 | 56 | pthread_attr_setschedpolicy(&thread_attr, SCHED_L4); |
paul@9 | 57 | pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED); |
paul@9 | 58 | |
paul@9 | 59 | /* Provide this instance as the private data. */ |
paul@9 | 60 | |
paul@9 | 61 | pthread_create(&_pthread, &thread_attr, event_loop, this); |
paul@9 | 62 | } |