schedule.h (3051B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2024 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 /* 21 * @author Tobias Frisch 22 * @file schedule.h 23 */ 24 25 #ifndef SCHEDULE_H_ 26 #define SCHEDULE_H_ 27 28 #include <glib-2.0/glib.h> 29 #include <gnunet/gnunet_util_lib.h> 30 #include <pthread.h> 31 32 typedef struct MESSENGER_Semaphore { 33 pthread_mutex_t mutex; 34 pthread_cond_t condition; 35 unsigned int counter; 36 } MESSENGER_Semaphore; 37 38 typedef struct MESSENGER_SignalHandle { 39 #ifdef MESSENGER_APPLICATION_NO_EVENT_FD 40 int pipe_fds [2]; 41 #else 42 int event_fd; 43 #endif 44 } MESSENGER_SignalHandle; 45 46 typedef enum MESSENGER_ScheduleSignal : unsigned char { 47 MESSENGER_SCHEDULE_SIGNAL_RUN = 1, 48 MESSENGER_SCHEDULE_SIGNAL_LOCK = 2, 49 } MESSENGER_ScheduleSignal; 50 51 typedef struct MESSENGER_Schedule { 52 MESSENGER_SignalHandle push_signal; 53 54 MESSENGER_Semaphore push_sem; 55 MESSENGER_Semaphore sync_sem; 56 gboolean locked; 57 58 GSourceFunc function; 59 gpointer data; 60 61 struct GNUNET_SCHEDULER_Task *task; 62 guint poll; 63 } MESSENGER_Schedule; 64 65 /** 66 * Initializes a schedule to synchronize a current thread 67 * with another certain thread via pipes and mutexes. 68 */ 69 void 70 schedule_init(MESSENGER_Schedule *schedule); 71 72 /** 73 * Completes initialization of a schedule to synchronize 74 * another thread with the GNUnet scheduler. 75 */ 76 void 77 schedule_load_gnunet(MESSENGER_Schedule *schedule); 78 79 /** 80 * Completes initialization of a schedule to synchronize 81 * another thread with the GLib/GTK scheduler. 82 */ 83 void 84 schedule_load_glib(MESSENGER_Schedule *schedule); 85 86 /** 87 * Cleanup a schedule and all of its resources for 88 * its synchronization. 89 */ 90 void 91 schedule_cleanup(MESSENGER_Schedule *schedule); 92 93 /** 94 * Calls a given function from the thread of a given 95 * schedule and waits for its completion until then 96 * in the current thread. 97 */ 98 void 99 schedule_sync_run(MESSENGER_Schedule *schedule, 100 GSourceFunc function, 101 gpointer data); 102 103 /** 104 * Locks the thread of a given schedule to wait 105 * until the schedule gets unlocked again from 106 * the current thread. Execution only continues 107 * in the current thread afterwards. 108 */ 109 void 110 schedule_sync_lock(MESSENGER_Schedule *schedule); 111 112 /** 113 * Unlocks the thread of a given schedule from 114 * the current thread. Execution continues in 115 * parallel again afterwards. 116 */ 117 void 118 schedule_sync_unlock(MESSENGER_Schedule *schedule); 119 120 #endif /* SCHEDULE_H_ */