1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
//
// Multi-threading support code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2025 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
// https://www.fltk.org/bugs.php
//
#include <config.h>
#include <FL/Fl.H>
#include "Fl_System_Driver.H"
#include <stdlib.h>
/*
From Bill:
I would prefer that FLTK contain the minimal amount of extra
stuff for doing threads. There are other portable thread
wrapper libraries out there and FLTK should not be providing
another. This file is an attempt to make minimal additions
and make them self-contained in this source file.
From Mike:
Starting with 1.1.8, we now have a callback so that you can
process awake() messages as they come in.
The API:
Fl::lock() - recursive lock. You must call this before the
first call to Fl::wait()/run() to initialize the thread
system. The lock is locked all the time except when
Fl::wait() is waiting for events.
Fl::unlock() - release the recursive lock.
Fl::awake(void*) - Causes Fl::wait() to return (with the lock
locked) even if there are no events ready.
Fl::awake(void (*cb)(void *), void*) - Call a function
in the main thread from within another thread of execution.
Fl::thread_message() - returns an argument sent to an
Fl::awake() call, or returns NULL if none. WARNING: the
current implementation only has a one-entry queue and only
returns the most recent value!
From Matt:
25 years later, we have blazing fast CPUs with 24 cores and more.
Fl::awake(void*) is no longer useful as "the last value" could have
been overwritten by another thread before the main thread gets to it.
Also, the ring buffer may potentially fill up much faster than expected,
so I introduced Fl::awake_once(void (*cb)(void *), void*) which removes
duplicate entries in the queue.
*/
#ifndef FL_DOXYGEN
static constexpr int AWAKE_RING_SIZE = 1024;
Fl_Awake_Handler *Fl_System_Driver::awake_ring_ = nullptr;
void **Fl_System_Driver::awake_data_ = nullptr;
int Fl_System_Driver::awake_ring_size_ = 0;
int Fl_System_Driver::awake_ring_head_ = 0;
int Fl_System_Driver::awake_ring_tail_ = 0;
bool Fl_System_Driverawake_pending_ = false;
#endif
/**
\cond DriverDev
\addtogroup DriverDeveloper
\{
*/
/**
\brief Adds an awake handler for use in awake().
\internal Adds an awake handler for use in awake().
\param[in] func The function to call when the main thread is awake.
\param[in] data The user data to pass to the function.
\param[in] once If true, the handler will be added only once, removing any
existing handler with the same function pointer and data pointer.
\return 0 on success, -1 if the ring buffer is full.
*/
int Fl_System_Driver::push_awake_handler(Fl_Awake_Handler func, void *data, bool once)
{
int ret = 0;
Fl::system_driver()->lock_ring();
// Allocate the ring buffers if we have not done so yet.
if (!awake_ring_) {
awake_ring_size_ = AWAKE_RING_SIZE;
awake_ring_ = (Fl_Awake_Handler*)malloc(awake_ring_size_*sizeof(Fl_Awake_Handler));
awake_data_ = (void**)malloc(awake_ring_size_*sizeof(void*));
// explicitly initialize the head and tail indices
awake_ring_head_= awake_ring_tail_ = 0;
}
// If we want to add the handler only once, go through the list of existing
// handlers and remove any handler with the same function pointer
// and data pointer.
if (once) {
int src = awake_ring_tail_;
int dst = awake_ring_tail_;
while (src != awake_ring_head_) {
if ((awake_ring_[src] != func) || (awake_data_[src] != data)) {
if (src != dst) {
awake_ring_[dst] = awake_ring_[src];
awake_data_[dst] = awake_data_[src];
}
dst++;
if (dst >= awake_ring_size_) dst = 0; // wrap around
}
src++;
if (src >= awake_ring_size_) src = 0; // wrap around
}
awake_ring_head_ = dst;
}
// The next head index we will want (not the current index):
// We use this to check if the ring-buffer is full or not
// (and to update awake_ring_head_ if we do use the current index.)
int next_head = awake_ring_head_ + 1;
if (next_head >= awake_ring_size_) {
next_head = 0;
}
// check that the ring buffer is not full
if (next_head == awake_ring_tail_) {
// ring is full. Return -1 as an error indicator.
ret = -1;
} else {
awake_ring_[awake_ring_head_] = func;
awake_data_[awake_ring_head_] = data;
awake_ring_head_ = next_head;
}
Fl::system_driver()->unlock_ring();
return ret;
}
/**
\brief Gets the last stored awake handler for use in awake().
\internal Used in the main event loop when an Awake message is received.
*/
int Fl_System_Driver::pop_awake_handler(Fl_Awake_Handler &func, void *&data)
{
int ret = 0;
Fl::system_driver()->lock_ring();
if ((!awake_ring_) || (awake_ring_head_ == awake_ring_tail_)) {
ret = -1;
} else {
func = awake_ring_[awake_ring_tail_];
data = awake_data_[awake_ring_tail_];
++awake_ring_tail_;
if (awake_ring_tail_ >= awake_ring_size_) {
awake_ring_tail_ = 0;
}
}
Fl::system_driver()->unlock_ring();
return ret;
}
/**
\brief Checks if the awake ring buffer is empty.
\internal Used in the main event loop when an Awake message is received.
*/
bool Fl_System_Driver::awake_ring_empty() {
Fl::system_driver()->lock_ring();
bool retval = (awake_ring_head_ == awake_ring_tail_);
Fl::system_driver()->unlock_ring();
return retval;
}
/**
\}
\endcond
*/
/**
\brief Notifies the main GUI thread from a worker thread.
In FLTK, worker threads can update the UI, but all UI changes must be wrapped
between Fl::lock() and Fl::unlock(). After calling Fl::unlock(), the worker
thread should call Fl::awake() to signal the main thread that
updates are pending.
\note Worker threads must not create, show, or hide windows.
\see \ref advanced_multithreading
\see Fl::awake(Fl_Awake_Handler, void*)
\see Fl::awake_once(Fl_Awake_Handler, void*)
*/
void Fl::awake() {
Fl::system_driver()->awake(nullptr);
}
/**
\brief Awake the main GUI thread and leave a message pointer.
\deprecated Use Fl::awake() or Fl::awake(Fl_Awake_Handler, void*) instead.
This method is deprecated. The API can not ensure that Fl::thread_message()
returns the messages sent by Fl::awake(void *v) complete and in the correct
order.
Use Fl::awake() instead if you do not need to send a specific message.
Use Fl::awake(Fl_Awake_Handler, void*) or Fl::awake_once(Fl_Awake_Handler, void*)
if you need to send a message to the main thread and ensure that all messages
are processed in the order they were sent.
\see \ref advanced_multithreading
\see Fl::awake()
\see Fl::awake(Fl_Awake_Handler, void*)
\see Fl::awake_once(Fl_Awake_Handler, void*)
*/
void Fl::awake(void *v) {
Fl::system_driver()->awake(v);
}
/**
\brief Schedules a callback to be executed by the main thread, then wakes up the main thread.
This function lets a worker thread request that a specific callback function
be run by the main thread, passing optional user data. The callback will be
executed during the main thread's next event handling cycle.
The queue holding the list of handlers is limited to 1024 entries.
If the queue is full, the function will return -1 and the callback will not be
scheduled. However the main thread will still be woken up to process any
other pending events.
\note If user_data points to dynamically allocated memory, it is the
responsibility of the caller to ensure that the memory is valid until the
callback is executed. The callback will be executed during the main thread's
next event handling cycle, but depending on the sytems load, this may take
several seconds.
\return 0 if the callback was successfully scheduled
\return -1 if the queue is full.
\see Fl::awake()
\see Fl::awake_once(Fl_Awake_Handler, void*)
\see \ref advanced_multithreading
*/
int Fl::awake(Fl_Awake_Handler handler, void *user_data) {
int ret = Fl_System_Driver::push_awake_handler(handler, user_data, false);
Fl::awake();
return ret;
}
/**
\brief Schedules a callback to be executed once by the main thread, then wakes up the main thread.
This function lets a worker thread request that a specific callback function
be run by the main thread, passing optional user data. If a callback with the
same user_data is already scheduled, the previous entry will be removed and
the new entry will be appended to the list.
\return 0 if the callback was successfully scheduled
\return -1 if the queue is full.
\see Fl::awake()
\see Fl::awake(Fl_Awake_Handler, void*)
\see \ref advanced_multithreading
*/
int Fl::awake_once(Fl_Awake_Handler handler, void *user_data) {
// TODO: remove any previous entry with the same handler and user_data
int ret = Fl_System_Driver::push_awake_handler(handler, user_data, true);
Fl::awake();
return ret;
}
/**
\brief Returns the last message sent by a child thread.
\deprecated Use Fl::awake(Fl_Awake_Handler, void*) or
Fl::awake_once(Fl_Awake_Handler, void*) instead.
The thread_message() method returns the last message
that was sent from a child by the Fl::awake(void*) method.
This method is deprecated. The API can not ensure that Fl::thread_message()
returns the messages sent by Fl::awake(void *v) complete and in the correct
order.
\see \ref advanced_multithreading
\see Fl::awake()
\see Fl::awake(Fl_Awake_Handler, void*)
\see Fl::awake_once(Fl_Awake_Handler, void*)
*/
void* Fl::thread_message() {
return Fl::system_driver()->thread_message();
}
/**
\brief Acquire the global UI lock for FLTK.
The lock() method blocks the current thread until it
can safely access FLTK widgets and data. Child threads should
call this method prior to updating any widgets or accessing
data. The main thread must call Fl::lock() once before any windows are shown
to initialize the threading support in FLTK. The initial Fl::lock() call
will return non-zero if threading is not available on the platform.
Child threads enclose calls to FLTK functions between Fl::lock() and
Fl::unlock() accessing FLTK. When a child thread has finshed accessing FLTK
and wants the main thread to update the UI, it should call Fl::awake().
Child threads can never create, show, or hide windows.
When the wait() method is waiting
for input or timeouts, child threads are given access to FLTK.
Similarly, when the main thread needs to do processing, it will
wait until all child threads have called unlock() before processing
additional data.
\return 0 if threading is available on the platform; non-zero
otherwise.
\see \ref advanced_multithreading
\see Fl::lock()
\see Fl::awake()
*/
int Fl::lock() {
return Fl::system_driver()->lock();
}
/**
\brief Release the global UI lock set by Fl::lock().
The unlock() method releases the lock that was set using the lock() method.
Child threads should call this method as soon as they are finished
accessing FLTK.
\see \ref advanced_multithreading
\see Fl::lock()
*/
void Fl::unlock() {
Fl::system_driver()->unlock();
}
|