about summary refs log tree commit diff
path: root/src/engine/external/glfw/lib/thread.c
blob: 8094332c084150e1cafffa09d798d2beecbadf34 (plain)
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
//========================================================================
// GLFW - An OpenGL framework
// File:        thread.c
// Platform:    Any
// 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"



//************************************************************************
//****                  GLFW internal functions                       ****
//************************************************************************

//========================================================================
// _glfwGetThreadPointer() - Find pointer to thread with a matching ID
//========================================================================

_GLFWthread * _glfwGetThreadPointer( int ID )
{
    _GLFWthread *t;

    for( t = &_glfwThrd.First; t != NULL; t = t->Next )
    {
        if( t->ID == ID )
        {
            break;
        }
    }

    return t;
}


//========================================================================
// _glfwAppendThread() - Append thread to thread list
//========================================================================

void _glfwAppendThread( _GLFWthread * t )
{
    _GLFWthread *t_tmp;

    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;
}


//========================================================================
// _glfwRemoveThread() - Remove thread from thread list
//========================================================================

void _glfwRemoveThread( _GLFWthread * t )
{
    if( t->Previous != NULL )
    {
        t->Previous->Next = t->Next;
    }
    if( t->Next != NULL )
    {
        t->Next->Previous = t->Previous;
    }
    free( (void *) t );
}



//************************************************************************
//****                     GLFW user functions                        ****
//************************************************************************

//========================================================================
// glfwCreateThread() - Create a new thread
//========================================================================

GLFWAPI GLFWthread GLFWAPIENTRY glfwCreateThread( GLFWthreadfun fun,
    void *arg )
{
    // Is GLFW initialized?
    if( !_glfwInitialized )
    {
        return -1;
    }

    // Return the GLFW thread ID
    return _glfwPlatformCreateThread( fun, arg );
}


//========================================================================
// glfwDestroyThread() - Kill a thread. NOTE: THIS IS A VERY DANGEROUS
// OPERATION, AND SHOULD NOT BE USED EXCEPT IN EXTREME SITUATIONS!
//========================================================================

GLFWAPI void GLFWAPIENTRY glfwDestroyThread( GLFWthread ID )
{
    // Is GLFW initialized?
    if( !_glfwInitialized )
    {
        return;
    }

    // Is it a valid thread? (killing the main thread is not allowed)
    if( ID < 1 )
    {
        return;
    }

    _glfwPlatformDestroyThread( ID );
}


//========================================================================
// glfwWaitThread() - Wait for a thread to die
//========================================================================

GLFWAPI int GLFWAPIENTRY glfwWaitThread( GLFWthread ID, int waitmode )
{
    // Is GLFW initialized?
    if( !_glfwInitialized )
    {
        return GL_TRUE;
    }

    // Is it a valid thread? (waiting for the main thread is not allowed)
    if( ID < 1 )
    {
        return GL_TRUE;
    }

    return _glfwPlatformWaitThread( ID, waitmode );
}


//========================================================================
// glfwGetThreadID() - Return the thread ID for the current thread
//========================================================================

GLFWAPI GLFWthread GLFWAPIENTRY glfwGetThreadID( void )
{
    // Is GLFW initialized?
    if( !_glfwInitialized )
    {
        return 0;
    }

    return _glfwPlatformGetThreadID();
}


//========================================================================
// glfwCreateMutex() - Create a mutual exclusion object
//========================================================================

GLFWAPI GLFWmutex GLFWAPIENTRY glfwCreateMutex( void )
{
    // Is GLFW initialized?
    if( !_glfwInitialized )
    {
        return (GLFWmutex) 0;
    }

    return _glfwPlatformCreateMutex();
}


//========================================================================
// glfwDestroyMutex() - Destroy a mutual exclusion object
//========================================================================

GLFWAPI void GLFWAPIENTRY glfwDestroyMutex( GLFWmutex mutex )
{
    // Initialized & valid mutex (no real way of assuring this)?
    if( !_glfwInitialized || !mutex )
    {
        return;
    }

    _glfwPlatformDestroyMutex( mutex );
}


//========================================================================
// glfwLockMutex() - Request access to a mutex
//========================================================================

GLFWAPI void GLFWAPIENTRY glfwLockMutex( GLFWmutex mutex )
{
    // Initialized & valid mutex (no real way of assuring this)?
    if( !_glfwInitialized && !mutex )
    {
        return;
    }

    _glfwPlatformLockMutex( mutex );
}


//========================================================================
// glfwUnlockMutex() - Release a mutex
//========================================================================

GLFWAPI void GLFWAPIENTRY glfwUnlockMutex( GLFWmutex mutex )
{
    // Initialized & valid mutex (no real way of assuring this)?
    if( !_glfwInitialized && !mutex )
    {
        return;
    }

    _glfwPlatformUnlockMutex( mutex );
}


//========================================================================
// glfwCreateCond() - Create a new condition variable object
//========================================================================

GLFWAPI GLFWcond GLFWAPIENTRY glfwCreateCond( void )
{
    // Is GLFW initialized?
    if( !_glfwInitialized )
    {
        return (GLFWcond) 0;
    }

    return _glfwPlatformCreateCond();
}


//========================================================================
// glfwDestroyCond() - Destroy a condition variable object
//========================================================================

GLFWAPI void GLFWAPIENTRY glfwDestroyCond( GLFWcond cond )
{
    // Initialized & valid condition variable?
    if( !_glfwInitialized || !cond )
    {
        return;
    }

    _glfwPlatformDestroyCond( cond );
}


//========================================================================
// glfwWaitCond() - Wait for a condition to be raised
//========================================================================

GLFWAPI void GLFWAPIENTRY glfwWaitCond( GLFWcond cond, GLFWmutex mutex,
    double timeout )
{
    // Initialized & valid condition variable and mutex?
    if( !_glfwInitialized || !cond || !mutex )
    {
        return;
    }

    _glfwPlatformWaitCond( cond, mutex, timeout );
}


//========================================================================
// glfwSignalCond() - Signal a condition to one waiting thread
//========================================================================

GLFWAPI void GLFWAPIENTRY glfwSignalCond( GLFWcond cond )
{
    // Initialized & valid condition variable?
    if( !_glfwInitialized || !cond )
    {
        return;
    }

    _glfwPlatformSignalCond( cond );
}


//========================================================================
// glfwBroadcastCond() - Broadcast a condition to all waiting threads
//========================================================================

GLFWAPI void GLFWAPIENTRY glfwBroadcastCond( GLFWcond cond )
{
    // Initialized & valid condition variable?
    if( !_glfwInitialized || !cond )
    {
        return;
    }

    _glfwPlatformBroadcastCond( cond );
}


//========================================================================
// glfwGetNumberOfProcessors() - Return the number of processors in the
// system. This information can be useful for determining the optimal
// number of threads to use for performing a certain task.
//========================================================================

GLFWAPI int GLFWAPIENTRY glfwGetNumberOfProcessors( void )
{
    // Is GLFW initialized?
    if( !_glfwInitialized )
    {
        return 0;
    }

    return _glfwPlatformGetNumberOfProcessors();
}