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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
//========================================================================
// GLFW - An OpenGL framework
// File: win32_thread.c
// Platform: Windows
// API version: 2.6
// WWW: http://glfw.sourceforge.net
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Camilla Berglund
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
//************************************************************************
// This is an implementation of POSIX "compatible" condition variables for
// Win32, as described by Douglas C. Schmidt and Irfan Pyarali:
// http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
//************************************************************************
enum {
_GLFW_COND_SIGNAL = 0,
_GLFW_COND_BROADCAST = 1
};
typedef struct {
// Signal and broadcast event HANDLEs
HANDLE events[ 2 ];
// Count of the number of waiters
unsigned int waiters_count;
// Serialize access to <waiters_count>
CRITICAL_SECTION waiters_count_lock;
} _GLFWcond;
//************************************************************************
//**** GLFW internal functions ****
//************************************************************************
//========================================================================
// _glfwNewThread() - This is simply a "wrapper" for calling the user
// thread function.
//========================================================================
DWORD WINAPI _glfwNewThread( LPVOID lpParam )
{
GLFWthreadfun threadfun;
_GLFWthread *t;
// Get pointer to thread information for current thread
t = _glfwGetThreadPointer( _glfwPlatformGetThreadID() );
if( t == NULL )
{
return 0;
}
// Get user thread function pointer
threadfun = t->Function;
// Call the user thread function
threadfun( (void *) lpParam );
// Remove thread from thread list
ENTER_THREAD_CRITICAL_SECTION
_glfwRemoveThread( t );
LEAVE_THREAD_CRITICAL_SECTION
// When the thread function returns, the thread will die...
return 0;
}
//************************************************************************
//**** GLFW user functions ****
//************************************************************************
//========================================================================
// _glfwPlatformCreateThread() - Create a new thread
//========================================================================
GLFWthread _glfwPlatformCreateThread( GLFWthreadfun fun, void *arg )
{
GLFWthread ID;
_GLFWthread *t, *t_tmp;
HANDLE hThread;
DWORD dwThreadId;
// Enter critical section
ENTER_THREAD_CRITICAL_SECTION
// Create a new thread information memory area
t = (_GLFWthread *) malloc( sizeof(_GLFWthread) );
if( t == NULL )
{
// Leave critical section
LEAVE_THREAD_CRITICAL_SECTION
return -1;
}
// Get a new unique thread id
ID = _glfwThrd.NextID ++;
// Store thread information in the thread list
t->Function = fun;
t->ID = ID;
// Create thread
hThread = CreateThread(
NULL, // Default security attributes
0, // Default stack size (1 MB)
_glfwNewThread, // Thread function (a wrapper function)
(LPVOID)arg, // Argument to thread is the user argument
0, // Default creation flags
&dwThreadId // Returned thread identifier
);
// Did the thread creation fail?
if( hThread == NULL )
{
free( (void *) t );
LEAVE_THREAD_CRITICAL_SECTION
return -1;
}
// Store more thread information in the thread list
t->Handle = hThread;
t->WinID = dwThreadId;
// Append thread to thread list
t_tmp = &_glfwThrd.First;
while( t_tmp->Next != NULL )
{
t_tmp = t_tmp->Next;
}
t_tmp->Next = t;
t->Previous = t_tmp;
t->Next = NULL;
// Leave critical section
LEAVE_THREAD_CRITICAL_SECTION
// Return the GLFW thread ID
return ID;
}
//========================================================================
// _glfwPlatformDestroyThread() - Kill a thread. NOTE: THIS IS A VERY
// DANGEROUS OPERATION, AND SHOULD NOT BE USED EXCEPT IN EXTREME
// SITUATIONS!
//========================================================================
void _glfwPlatformDestroyThread( GLFWthread ID )
{
_GLFWthread *t;
// Enter critical section
ENTER_THREAD_CRITICAL_SECTION
// Get thread information pointer
t = _glfwGetThreadPointer( ID );
if( t == NULL )
{
LEAVE_THREAD_CRITICAL_SECTION
return;
}
// Simply murder the process, no mercy!
if( TerminateThread( t->Handle, 0 ) )
{
// Close thread handle
CloseHandle( t->Handle );
// Remove thread from thread list
_glfwRemoveThread( t );
}
// Leave critical section
LEAVE_THREAD_CRITICAL_SECTION
}
//========================================================================
// _glfwPlatformWaitThread() - Wait for a thread to die
//========================================================================
int _glfwPlatformWaitThread( GLFWthread ID, int waitmode )
{
DWORD result;
HANDLE hThread;
_GLFWthread *t;
// Enter critical section
ENTER_THREAD_CRITICAL_SECTION
// Get thread information pointer
t = _glfwGetThreadPointer( ID );
// Is the thread already dead?
if( t == NULL )
{
LEAVE_THREAD_CRITICAL_SECTION
return GL_TRUE;
}
// Get thread handle
hThread = t->Handle;
// Leave critical section
LEAVE_THREAD_CRITICAL_SECTION
// Wait for thread to die
if( waitmode == GLFW_WAIT )
{
result = WaitForSingleObject( hThread, INFINITE );
}
else if( waitmode == GLFW_NOWAIT )
{
result = WaitForSingleObject( hThread, 0 );
}
else
{
return GL_FALSE;
}
// Did we have a time-out?
if( result == WAIT_TIMEOUT )
{
return GL_FALSE;
}
return GL_TRUE;
}
//========================================================================
// _glfwPlatformGetThreadID() - Return the thread ID for the current
// thread
//========================================================================
GLFWthread _glfwPlatformGetThreadID( void )
{
_GLFWthread *t;
GLFWthread ID = -1;
DWORD WinID;
// Get Windows thread ID
WinID = GetCurrentThreadId();
// Enter critical section (to avoid an inconsistent thread list)
ENTER_THREAD_CRITICAL_SECTION
// Loop through entire list of threads to find the matching Windows
// thread ID
for( t = &_glfwThrd.First; t != NULL; t = t->Next )
{
if( t->WinID == WinID )
{
ID = t->ID;
break;
}
}
// Leave critical section
LEAVE_THREAD_CRITICAL_SECTION
// Return the found GLFW thread identifier
return ID;
}
//========================================================================
// _glfwPlatformCreateMutex() - Create a mutual exclusion object
//========================================================================
GLFWmutex _glfwPlatformCreateMutex( void )
{
CRITICAL_SECTION *mutex;
// Allocate memory for mutex
mutex = (CRITICAL_SECTION *) malloc( sizeof(CRITICAL_SECTION) );
if( !mutex )
{
return NULL;
}
// Initialize mutex
InitializeCriticalSection( mutex );
// Cast to GLFWmutex and return
return (GLFWmutex) mutex;
}
//========================================================================
// glfwDestroyMutex() - Destroy a mutual exclusion object
//========================================================================
void _glfwPlatformDestroyMutex( GLFWmutex mutex )
{
// Destroy mutex
DeleteCriticalSection( (CRITICAL_SECTION *) mutex );
free( mutex );
}
//========================================================================
// _glfwPlatformLockMutex() - Request access to a mutex
//========================================================================
void _glfwPlatformLockMutex( GLFWmutex mutex )
{
// Wait for mutex to be released
EnterCriticalSection( (CRITICAL_SECTION *) mutex );
}
//========================================================================
// _glfwPlatformUnlockMutex() - Release a mutex
//========================================================================
void _glfwPlatformUnlockMutex( GLFWmutex mutex )
{
// Release mutex
LeaveCriticalSection( (CRITICAL_SECTION *) mutex );
}
//========================================================================
// _glfwPlatformCreateCond() - Create a new condition variable object
//========================================================================
GLFWcond _glfwPlatformCreateCond( void )
{
_GLFWcond *cond;
// Allocate memory for condition variable
cond = (_GLFWcond *) malloc( sizeof(_GLFWcond) );
if( !cond )
{
return NULL;
}
// Initialize condition variable
cond->waiters_count = 0;
cond->events[ _GLFW_COND_SIGNAL ] = CreateEvent( NULL, FALSE,
FALSE, NULL );
cond->events[ _GLFW_COND_BROADCAST ] = CreateEvent( NULL, TRUE,
FALSE, NULL );
InitializeCriticalSection( &cond->waiters_count_lock );
// Cast to GLFWcond and return
return (GLFWcond) cond;
}
//========================================================================
// _glfwPlatformDestroyCond() - Destroy a condition variable object
//========================================================================
void _glfwPlatformDestroyCond( GLFWcond cond )
{
// Close the condition variable handles
CloseHandle( ((_GLFWcond *)cond)->events[ _GLFW_COND_SIGNAL ] );
CloseHandle( ((_GLFWcond *)cond)->events[ _GLFW_COND_BROADCAST ] );
// Delete critical section
DeleteCriticalSection( &((_GLFWcond *)cond)->waiters_count_lock );
// Free memory for condition variable
free( (void *) cond );
}
//========================================================================
// _glfwPlatformWaitCond() - Wait for a condition to be raised
//========================================================================
void _glfwPlatformWaitCond( GLFWcond cond, GLFWmutex mutex,
double timeout )
{
_GLFWcond *cv = (_GLFWcond *) cond;
int result, last_waiter;
DWORD timeout_ms;
// Avoid race conditions
EnterCriticalSection( &cv->waiters_count_lock );
cv->waiters_count ++;
LeaveCriticalSection( &cv->waiters_count_lock );
// It's ok to release the mutex here since Win32 manual-reset events
// maintain state when used with SetEvent()
LeaveCriticalSection( (CRITICAL_SECTION *) mutex );
// Translate timeout into milliseconds
if( timeout >= GLFW_INFINITY )
{
timeout_ms = INFINITE;
}
else
{
timeout_ms = (DWORD) (1000.0 * timeout + 0.5);
if( timeout_ms <= 0 )
{
timeout_ms = 1;
}
}
// Wait for either event to become signaled due to glfwSignalCond or
// glfwBroadcastCond being called
result = WaitForMultipleObjects( 2, cv->events, FALSE, timeout_ms );
// Check if we are the last waiter
EnterCriticalSection( &cv->waiters_count_lock );
cv->waiters_count --;
last_waiter = (result == WAIT_OBJECT_0 + _GLFW_COND_BROADCAST) &&
(cv->waiters_count == 0);
LeaveCriticalSection( &cv->waiters_count_lock );
// Some thread called glfwBroadcastCond
if( last_waiter )
{
// We're the last waiter to be notified or to stop waiting, so
// reset the manual event
ResetEvent( cv->events[ _GLFW_COND_BROADCAST ] );
}
// Reacquire the mutex
EnterCriticalSection( (CRITICAL_SECTION *) mutex );
}
//========================================================================
// _glfwPlatformSignalCond() - Signal a condition to one waiting thread
//========================================================================
void _glfwPlatformSignalCond( GLFWcond cond )
{
_GLFWcond *cv = (_GLFWcond *) cond;
int have_waiters;
// Avoid race conditions
EnterCriticalSection( &cv->waiters_count_lock );
have_waiters = cv->waiters_count > 0;
LeaveCriticalSection( &cv->waiters_count_lock );
if( have_waiters )
{
SetEvent( cv->events[ _GLFW_COND_SIGNAL ] );
}
}
//========================================================================
// _glfwPlatformBroadcastCond() - Broadcast a condition to all waiting
// threads
//========================================================================
void _glfwPlatformBroadcastCond( GLFWcond cond )
{
_GLFWcond *cv = (_GLFWcond *) cond;
int have_waiters;
// Avoid race conditions
EnterCriticalSection( &cv->waiters_count_lock );
have_waiters = cv->waiters_count > 0;
LeaveCriticalSection( &cv->waiters_count_lock );
if( have_waiters )
{
SetEvent( cv->events[ _GLFW_COND_BROADCAST ] );
}
}
//========================================================================
// _glfwPlatformGetNumberOfProcessors() - Return the number of processors
// in the system.
//========================================================================
int _glfwPlatformGetNumberOfProcessors( void )
{
SYSTEM_INFO si;
// Get hardware system information
GetSystemInfo( &si );
return (int) si.dwNumberOfProcessors;
}
|