comparison src/thread/pth/SDL_syscond.c @ 329:1d74ddc90cb2

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