annotate src/thread/pth/SDL_syscond.c @ 1379:c0a74f199ecf

Use only safe string functions
author Sam Lantinga <slouken@libsdl.org>
date Sun, 19 Feb 2006 23:46:34 +0000
parents 19418e4422cb
children d910939febfa
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_sysmutex_c.h"
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
12 struct SDL_cond
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
13 {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
14 pth_cond_t condpth_p;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
15 };
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
16
329
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 /* Create a condition variable */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18 SDL_cond * SDL_CreateCond(void)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 SDL_cond *cond;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 329
diff changeset
22 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
23 if ( cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 if ( pth_cond_init(&(cond->condpth_p)) < 0 ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 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
26 SDL_free(cond);
329
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 cond = NULL;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 } else {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 SDL_OutOfMemory();
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 return(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
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 /* Destroy a condition variable */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 void SDL_DestroyCond(SDL_cond *cond)
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 if ( cond ) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 329
diff changeset
39 SDL_free(cond);
329
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 }
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
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43 /* 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
44 int SDL_CondSignal(SDL_cond *cond)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 int retval;
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 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 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
55 SDL_SetError("pth_cond_notify() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56 retval = -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 return retval;
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
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 /* 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
62 int SDL_CondBroadcast(SDL_cond *cond)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 int retval;
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 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 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
73 SDL_SetError("pth_cond_notify() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 retval = -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 return retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 }
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 /* 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
80 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
81 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
82
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 Typical use:
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 Thread A:
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 SDL_LockMutex(lock);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 while ( ! condition ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 SDL_CondWait(cond);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90 SDL_UnlockMutex(lock);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 Thread B:
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93 SDL_LockMutex(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 condition = true;
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 SDL_UnlockMutex(lock);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98 */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 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
100 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 int retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102 pth_event_t ev;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103 int sec;
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 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 sec = ms/1000;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 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
114
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 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
116 SDL_SetError("pth_cond_await() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117 retval = -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118 }
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 pth_event_free(ev, PTH_FREE_ALL);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 return retval;
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
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125 /* Wait on the condition variable forever */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 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
127 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128 int retval;
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 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136 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
137 SDL_SetError("pth_cond_await() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
138 retval = -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140 return retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
141 }