Mercurial > sdl-ios-xcode
comparison src/thread/pthread/SDL_syscond.c @ 1895:c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 10 Jul 2006 21:04:37 +0000 |
parents | d910939febfa |
children | 99210400e8b9 |
comparison
equal
deleted
inserted
replaced
1894:c69cee13dd76 | 1895:c121d94672cb |
---|---|
29 #include "SDL_thread.h" | 29 #include "SDL_thread.h" |
30 #include "SDL_sysmutex_c.h" | 30 #include "SDL_sysmutex_c.h" |
31 | 31 |
32 struct SDL_cond | 32 struct SDL_cond |
33 { | 33 { |
34 pthread_cond_t cond; | 34 pthread_cond_t cond; |
35 }; | 35 }; |
36 | 36 |
37 /* Create a condition variable */ | 37 /* Create a condition variable */ |
38 SDL_cond * SDL_CreateCond(void) | 38 SDL_cond * |
39 SDL_CreateCond(void) | |
39 { | 40 { |
40 SDL_cond *cond; | 41 SDL_cond *cond; |
41 | 42 |
42 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond)); | 43 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond)); |
43 if ( cond ) { | 44 if (cond) { |
44 if ( pthread_cond_init(&cond->cond, NULL) < 0 ) { | 45 if (pthread_cond_init(&cond->cond, NULL) < 0) { |
45 SDL_SetError("pthread_cond_init() failed"); | 46 SDL_SetError("pthread_cond_init() failed"); |
46 SDL_free(cond); | 47 SDL_free(cond); |
47 cond = NULL; | 48 cond = NULL; |
48 } | 49 } |
49 } | 50 } |
50 return(cond); | 51 return (cond); |
51 } | 52 } |
52 | 53 |
53 /* Destroy a condition variable */ | 54 /* Destroy a condition variable */ |
54 void SDL_DestroyCond(SDL_cond *cond) | 55 void |
56 SDL_DestroyCond(SDL_cond * cond) | |
55 { | 57 { |
56 if ( cond ) { | 58 if (cond) { |
57 pthread_cond_destroy(&cond->cond); | 59 pthread_cond_destroy(&cond->cond); |
58 SDL_free(cond); | 60 SDL_free(cond); |
59 } | 61 } |
60 } | 62 } |
61 | 63 |
62 /* Restart one of the threads that are waiting on the condition variable */ | 64 /* Restart one of the threads that are waiting on the condition variable */ |
63 int SDL_CondSignal(SDL_cond *cond) | 65 int |
66 SDL_CondSignal(SDL_cond * cond) | |
64 { | 67 { |
65 int retval; | 68 int retval; |
66 | 69 |
67 if ( ! cond ) { | 70 if (!cond) { |
68 SDL_SetError("Passed a NULL condition variable"); | 71 SDL_SetError("Passed a NULL condition variable"); |
69 return -1; | 72 return -1; |
70 } | 73 } |
71 | 74 |
72 retval = 0; | 75 retval = 0; |
73 if ( pthread_cond_signal(&cond->cond) != 0 ) { | 76 if (pthread_cond_signal(&cond->cond) != 0) { |
74 SDL_SetError("pthread_cond_signal() failed"); | 77 SDL_SetError("pthread_cond_signal() failed"); |
75 retval = -1; | 78 retval = -1; |
76 } | 79 } |
77 return retval; | 80 return retval; |
78 } | 81 } |
79 | 82 |
80 /* Restart all threads that are waiting on the condition variable */ | 83 /* Restart all threads that are waiting on the condition variable */ |
81 int SDL_CondBroadcast(SDL_cond *cond) | 84 int |
85 SDL_CondBroadcast(SDL_cond * cond) | |
82 { | 86 { |
83 int retval; | 87 int retval; |
84 | 88 |
85 if ( ! cond ) { | 89 if (!cond) { |
86 SDL_SetError("Passed a NULL condition variable"); | 90 SDL_SetError("Passed a NULL condition variable"); |
87 return -1; | 91 return -1; |
88 } | 92 } |
89 | 93 |
90 retval = 0; | 94 retval = 0; |
91 if ( pthread_cond_broadcast(&cond->cond) != 0 ) { | 95 if (pthread_cond_broadcast(&cond->cond) != 0) { |
92 SDL_SetError("pthread_cond_broadcast() failed"); | 96 SDL_SetError("pthread_cond_broadcast() failed"); |
93 retval = -1; | 97 retval = -1; |
94 } | 98 } |
95 return retval; | 99 return retval; |
96 } | 100 } |
97 | 101 |
98 int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms) | 102 int |
103 SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms) | |
99 { | 104 { |
100 int retval; | 105 int retval; |
101 struct timeval delta; | 106 struct timeval delta; |
102 struct timespec abstime; | 107 struct timespec abstime; |
103 | 108 |
104 if ( ! cond ) { | 109 if (!cond) { |
105 SDL_SetError("Passed a NULL condition variable"); | 110 SDL_SetError("Passed a NULL condition variable"); |
106 return -1; | 111 return -1; |
107 } | 112 } |
108 | 113 |
109 gettimeofday(&delta, NULL); | 114 gettimeofday(&delta, NULL); |
110 | 115 |
111 abstime.tv_sec = delta.tv_sec + (ms/1000); | 116 abstime.tv_sec = delta.tv_sec + (ms / 1000); |
112 abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000; | 117 abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000; |
113 if ( abstime.tv_nsec > 1000000000 ) { | 118 if (abstime.tv_nsec > 1000000000) { |
114 abstime.tv_sec += 1; | 119 abstime.tv_sec += 1; |
115 abstime.tv_nsec -= 1000000000; | 120 abstime.tv_nsec -= 1000000000; |
116 } | 121 } |
117 | 122 |
118 tryagain: | 123 tryagain: |
119 retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime); | 124 retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime); |
120 switch (retval) { | 125 switch (retval) { |
121 case EINTR: | 126 case EINTR: |
122 goto tryagain; | 127 goto tryagain; |
123 break; | 128 break; |
124 case ETIMEDOUT: | 129 case ETIMEDOUT: |
125 retval = SDL_MUTEX_TIMEDOUT; | 130 retval = SDL_MUTEX_TIMEDOUT; |
126 break; | 131 break; |
127 case 0: | 132 case 0: |
128 break; | 133 break; |
129 default: | 134 default: |
130 SDL_SetError("pthread_cond_timedwait() failed"); | 135 SDL_SetError("pthread_cond_timedwait() failed"); |
131 retval = -1; | 136 retval = -1; |
132 break; | 137 break; |
133 } | 138 } |
134 return retval; | 139 return retval; |
135 } | 140 } |
136 | 141 |
137 /* Wait on the condition variable, unlocking the provided mutex. | 142 /* Wait on the condition variable, unlocking the provided mutex. |
138 The mutex must be locked before entering this function! | 143 The mutex must be locked before entering this function! |
139 */ | 144 */ |
140 int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex) | 145 int |
146 SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex) | |
141 { | 147 { |
142 int retval; | 148 int retval; |
143 | 149 |
144 if ( ! cond ) { | 150 if (!cond) { |
145 SDL_SetError("Passed a NULL condition variable"); | 151 SDL_SetError("Passed a NULL condition variable"); |
146 return -1; | 152 return -1; |
147 } | 153 } |
148 | 154 |
149 retval = 0; | 155 retval = 0; |
150 if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) { | 156 if (pthread_cond_wait(&cond->cond, &mutex->id) != 0) { |
151 SDL_SetError("pthread_cond_wait() failed"); | 157 SDL_SetError("pthread_cond_wait() failed"); |
152 retval = -1; | 158 retval = -1; |
153 } | 159 } |
154 return retval; | 160 return retval; |
155 } | 161 } |
162 | |
163 /* vi: set ts=4 sw=4 expandtab: */ |