1 /* 2 * Generic event loop functionality. 3 * 4 * Copyright (C) 2018, 2023 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include "event-loop.h" 23 24 #include <pthread.h> 25 #include <pthread-l4.h> 26 27 28 29 /* Main loop for event threads. */ 30 31 void *Event_loop::event_loop(void *data) 32 { 33 Event_loop *self = reinterpret_cast<Event_loop *>(data); 34 35 while (1) 36 self->handle(); 37 38 return 0; 39 } 40 41 /* Event loop initiation. */ 42 43 void Event_loop::start() 44 { 45 pthread_attr_t thread_attr; 46 struct sched_param sp; 47 48 /* Thread initialisation boilerplate. */ 49 50 if (pthread_attr_init(&thread_attr)) 51 return; 52 53 sp.sched_priority = _priority; 54 pthread_attr_setschedparam(&thread_attr, &sp); 55 pthread_attr_setschedpolicy(&thread_attr, SCHED_L4); 56 pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED); 57 58 /* Provide this instance as the private data. */ 59 60 pthread_create(&_pthread, &thread_attr, event_loop, this); 61 }