annotate src/thread/pth/SDL_syscond.c @ 1338:604d73db6802

Removed uses of stdlib.h and string.h
author Sam Lantinga <slouken@libsdl.org>
date Tue, 07 Feb 2006 09:29:18 +0000
parents 3692456e7b0f
children c71e05b4dc2e
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
1338
604d73db6802 Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents: 1336
diff changeset
9 #include "SDL_stdlib.h"
329
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 #include "SDL_error.h"
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 #include "SDL_thread.h"
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 #include "SDL_syscond_c.h"
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 #include "SDL_sysmutex_c.h"
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 /* Create a condition variable */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 SDL_cond * SDL_CreateCond(void)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18 SDL_cond *cond;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 329
diff changeset
20 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
21 if ( cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 if ( pth_cond_init(&(cond->condpth_p)) < 0 ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23 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
24 SDL_free(cond);
329
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 cond = NULL;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 } else {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 SDL_OutOfMemory();
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 return(cond);
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
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 /* Destroy a condition variable */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 void SDL_DestroyCond(SDL_cond *cond)
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 if ( cond ) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 329
diff changeset
37 SDL_free(cond);
329
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 }
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 /* 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
42 int SDL_CondSignal(SDL_cond *cond)
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 int retval;
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 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 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
53 SDL_SetError("pth_cond_notify() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 retval = -1;
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 return retval;
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
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 /* 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
60 int SDL_CondBroadcast(SDL_cond *cond)
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 int retval;
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 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 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
71 SDL_SetError("pth_cond_notify() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 retval = -1;
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 return retval;
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
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 /* 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
78 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
79 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
80
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 Typical use:
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 Thread A:
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 SDL_LockMutex(lock);
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 while ( ! condition ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 SDL_CondWait(cond);
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 SDL_UnlockMutex(lock);
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 Thread B:
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 SDL_LockMutex(lock);
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 condition = true;
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 SDL_UnlockMutex(lock);
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 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
98 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 int retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 pth_event_t ev;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 int sec;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106 }
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 retval = 0;
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 sec = ms/1000;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111 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
112
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 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
114 SDL_SetError("pth_cond_await() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 retval = -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 }
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 pth_event_free(ev, PTH_FREE_ALL);
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 return retval;
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
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 /* Wait on the condition variable forever */
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 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
125 {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 int retval;
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 if ( ! cond ) {
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 SDL_SetError("Passed a NULL condition variable");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130 return -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133 retval = 0;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 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
135 SDL_SetError("pth_cond_await() failed");
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136 retval = -1;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137 }
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
138 return retval;
1d74ddc90cb2 Patrice's fixes for GNU Pthread support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 }