annotate src/thread/pth/SDL_syscond.c @ 1358:c71e05b4dc2e

More header massaging... works great on Windows. ;-)
author Sam Lantinga <slouken@libsdl.org>
date Fri, 10 Feb 2006 06:48:43 +0000
parents 604d73db6802
children 19418e4422cb
rev   line source
329
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 * GNU pth conditions variables
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 *
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4 * Patrice Mandin
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 #include <pth.h>
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9 #include "SDL_thread.h"
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 #include "SDL_syscond_c.h"
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 #include "SDL_sysmutex_c.h"
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 /* Create a condition variable */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14 SDL_cond * SDL_CreateCond(void)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 SDL_cond *cond;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 329
diff changeset
18 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
329
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 if ( cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 if ( pth_cond_init(&(cond->condpth_p)) < 0 ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21 SDL_SetError("pthread_cond_init() failed");
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 329
diff changeset
22 SDL_free(cond);
329
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23 cond = NULL;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 } else {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 SDL_OutOfMemory();
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 return(cond);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 /* Destroy a condition variable */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 void SDL_DestroyCond(SDL_cond *cond)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 if ( cond ) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 329
diff changeset
35 SDL_free(cond);
329
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39 /* Restart one of the threads that are waiting on the condition variable */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 int SDL_CondSignal(SDL_cond *cond)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
42 int retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 if ( pth_cond_notify(&(cond->condpth_p), FALSE) != 0 ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 SDL_SetError("pth_cond_notify() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 retval = -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 return retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 /* Restart all threads that are waiting on the condition variable */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 int SDL_CondBroadcast(SDL_cond *cond)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60 int retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 if ( pth_cond_notify(&(cond->condpth_p), TRUE) != 0 ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 SDL_SetError("pth_cond_notify() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 retval = -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 return retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 /* Wait on the condition variable for at most 'ms' milliseconds.
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 The mutex must be locked before entering this function!
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 The mutex is unlocked during the wait, and locked again after the wait.
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
79 Typical use:
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 Thread A:
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82 SDL_LockMutex(lock);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 while ( ! condition ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 SDL_CondWait(cond);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 SDL_UnlockMutex(lock);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 Thread B:
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 SDL_LockMutex(lock);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90 ...
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 condition = true;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 ...
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93 SDL_UnlockMutex(lock);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97 int retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98 pth_event_t ev;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 int sec;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 sec = ms/1000;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 ev = pth_event(PTH_EVENT_TIME, pth_timeout(sec,(ms-sec*1000)*1000));
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111 if ( pth_cond_await(&(cond->condpth_p), &(mutex->mutexpth_p), ev) != 0 ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 SDL_SetError("pth_cond_await() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 retval = -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 pth_event_free(ev, PTH_FREE_ALL);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118 return retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121 /* Wait on the condition variable forever */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 int retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 if ( pth_cond_await(&(cond->condpth_p), &(mutex->mutexpth_p), NULL) != 0 ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133 SDL_SetError("pth_cond_await() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 retval = -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136 return retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137 }