winlin

research st: refine sync.

@@ -44,14 +44,12 @@ @@ -44,14 +44,12 @@
44 #include <errno.h> 44 #include <errno.h>
45 #include "common.h" 45 #include "common.h"
46 46
47 -  
48 extern time_t _st_curr_time; 47 extern time_t _st_curr_time;
49 extern st_utime_t _st_last_tset; 48 extern st_utime_t _st_last_tset;
50 extern int _st_active_count; 49 extern int _st_active_count;
51 50
52 static st_utime_t (*_st_utime)(void) = NULL; 51 static st_utime_t (*_st_utime)(void) = NULL;
53 52
54 -  
55 /***************************************** 53 /*****************************************
56 * Time functions 54 * Time functions
57 */ 55 */
@@ -62,14 +60,13 @@ st_utime_t st_utime(void) @@ -62,14 +60,13 @@ st_utime_t st_utime(void)
62 #ifdef MD_GET_UTIME 60 #ifdef MD_GET_UTIME
63 MD_GET_UTIME(); 61 MD_GET_UTIME();
64 #else 62 #else
65 -#error Unknown OS 63 + #error Unknown OS
66 #endif 64 #endif
67 } 65 }
68 66
69 return (*_st_utime)(); 67 return (*_st_utime)();
70 } 68 }
71 69
72 -  
73 int st_set_utime_function(st_utime_t (*func)(void)) 70 int st_set_utime_function(st_utime_t (*func)(void))
74 { 71 {
75 if (_st_active_count) { 72 if (_st_active_count) {
@@ -82,13 +79,11 @@ int st_set_utime_function(st_utime_t (*func)(void)) @@ -82,13 +79,11 @@ int st_set_utime_function(st_utime_t (*func)(void))
82 return 0; 79 return 0;
83 } 80 }
84 81
85 -  
86 st_utime_t st_utime_last_clock(void) 82 st_utime_t st_utime_last_clock(void)
87 { 83 {
88 return _ST_LAST_CLOCK; 84 return _ST_LAST_CLOCK;
89 } 85 }
90 86
91 -  
92 int st_timecache_set(int on) 87 int st_timecache_set(int on)
93 { 88 {
94 int wason = (_st_curr_time) ? 1 : 0; 89 int wason = (_st_curr_time) ? 1 : 0;
@@ -96,22 +91,22 @@ int st_timecache_set(int on) @@ -96,22 +91,22 @@ int st_timecache_set(int on)
96 if (on) { 91 if (on) {
97 _st_curr_time = time(NULL); 92 _st_curr_time = time(NULL);
98 _st_last_tset = st_utime(); 93 _st_last_tset = st_utime();
99 - } else 94 + } else {
100 _st_curr_time = 0; 95 _st_curr_time = 0;
  96 + }
101 97
102 return wason; 98 return wason;
103 } 99 }
104 100
105 -  
106 time_t st_time(void) 101 time_t st_time(void)
107 { 102 {
108 - if (_st_curr_time) 103 + if (_st_curr_time) {
109 return _st_curr_time; 104 return _st_curr_time;
  105 + }
110 106
111 return time(NULL); 107 return time(NULL);
112 } 108 }
113 109
114 -  
115 int st_usleep(st_utime_t usecs) 110 int st_usleep(st_utime_t usecs)
116 { 111 {
117 _st_thread_t *me = _ST_CURRENT_THREAD(); 112 _st_thread_t *me = _ST_CURRENT_THREAD();
@@ -125,8 +120,9 @@ int st_usleep(st_utime_t usecs) @@ -125,8 +120,9 @@ int st_usleep(st_utime_t usecs)
125 if (usecs != ST_UTIME_NO_TIMEOUT) { 120 if (usecs != ST_UTIME_NO_TIMEOUT) {
126 me->state = _ST_ST_SLEEPING; 121 me->state = _ST_ST_SLEEPING;
127 _ST_ADD_SLEEPQ(me, usecs); 122 _ST_ADD_SLEEPQ(me, usecs);
128 - } else 123 + } else {
129 me->state = _ST_ST_SUSPENDED; 124 me->state = _ST_ST_SUSPENDED;
  125 + }
130 126
131 _ST_SWITCH_CONTEXT(me); 127 _ST_SWITCH_CONTEXT(me);
132 128
@@ -139,18 +135,14 @@ int st_usleep(st_utime_t usecs) @@ -139,18 +135,14 @@ int st_usleep(st_utime_t usecs)
139 return 0; 135 return 0;
140 } 136 }
141 137
142 -  
143 int st_sleep(int secs) 138 int st_sleep(int secs)
144 { 139 {
145 - return st_usleep((secs >= 0) ? secs * (st_utime_t) 1000000LL :  
146 - ST_UTIME_NO_TIMEOUT); 140 + return st_usleep((secs >= 0) ? secs * (st_utime_t) 1000000LL : ST_UTIME_NO_TIMEOUT);
147 } 141 }
148 142
149 -  
150 /***************************************** 143 /*****************************************
151 * Condition variable functions 144 * Condition variable functions
152 */ 145 */
153 -  
154 _st_cond_t *st_cond_new(void) 146 _st_cond_t *st_cond_new(void)
155 { 147 {
156 _st_cond_t *cvar; 148 _st_cond_t *cvar;
@@ -163,7 +155,6 @@ _st_cond_t *st_cond_new(void) @@ -163,7 +155,6 @@ _st_cond_t *st_cond_new(void)
163 return cvar; 155 return cvar;
164 } 156 }
165 157
166 -  
167 int st_cond_destroy(_st_cond_t *cvar) 158 int st_cond_destroy(_st_cond_t *cvar)
168 { 159 {
169 if (cvar->wait_q.next != &cvar->wait_q) { 160 if (cvar->wait_q.next != &cvar->wait_q) {
@@ -176,7 +167,6 @@ int st_cond_destroy(_st_cond_t *cvar) @@ -176,7 +167,6 @@ int st_cond_destroy(_st_cond_t *cvar)
176 return 0; 167 return 0;
177 } 168 }
178 169
179 -  
180 int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout) 170 int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout)
181 { 171 {
182 _st_thread_t *me = _ST_CURRENT_THREAD(); 172 _st_thread_t *me = _ST_CURRENT_THREAD();
@@ -192,8 +182,9 @@ int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout) @@ -192,8 +182,9 @@ int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout)
192 me->state = _ST_ST_COND_WAIT; 182 me->state = _ST_ST_COND_WAIT;
193 ST_APPEND_LINK(&me->wait_links, &cvar->wait_q); 183 ST_APPEND_LINK(&me->wait_links, &cvar->wait_q);
194 184
195 - if (timeout != ST_UTIME_NO_TIMEOUT) 185 + if (timeout != ST_UTIME_NO_TIMEOUT) {
196 _ST_ADD_SLEEPQ(me, timeout); 186 _ST_ADD_SLEEPQ(me, timeout);
  187 + }
197 188
198 _ST_SWITCH_CONTEXT(me); 189 _ST_SWITCH_CONTEXT(me);
199 190
@@ -214,13 +205,11 @@ int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout) @@ -214,13 +205,11 @@ int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout)
214 return rv; 205 return rv;
215 } 206 }
216 207
217 -  
218 int st_cond_wait(_st_cond_t *cvar) 208 int st_cond_wait(_st_cond_t *cvar)
219 { 209 {
220 return st_cond_timedwait(cvar, ST_UTIME_NO_TIMEOUT); 210 return st_cond_timedwait(cvar, ST_UTIME_NO_TIMEOUT);
221 } 211 }
222 212
223 -  
224 static int _st_cond_signal(_st_cond_t *cvar, int broadcast) 213 static int _st_cond_signal(_st_cond_t *cvar, int broadcast)
225 { 214 {
226 _st_thread_t *thread; 215 _st_thread_t *thread;
@@ -229,37 +218,35 @@ static int _st_cond_signal(_st_cond_t *cvar, int broadcast) @@ -229,37 +218,35 @@ static int _st_cond_signal(_st_cond_t *cvar, int broadcast)
229 for (q = cvar->wait_q.next; q != &cvar->wait_q; q = q->next) { 218 for (q = cvar->wait_q.next; q != &cvar->wait_q; q = q->next) {
230 thread = _ST_THREAD_WAITQ_PTR(q); 219 thread = _ST_THREAD_WAITQ_PTR(q);
231 if (thread->state == _ST_ST_COND_WAIT) { 220 if (thread->state == _ST_ST_COND_WAIT) {
232 - if (thread->flags & _ST_FL_ON_SLEEPQ) 221 + if (thread->flags & _ST_FL_ON_SLEEPQ) {
233 _ST_DEL_SLEEPQ(thread); 222 _ST_DEL_SLEEPQ(thread);
  223 + }
234 224
235 /* Make thread runnable */ 225 /* Make thread runnable */
236 thread->state = _ST_ST_RUNNABLE; 226 thread->state = _ST_ST_RUNNABLE;
237 _ST_ADD_RUNQ(thread); 227 _ST_ADD_RUNQ(thread);
238 - if (!broadcast) 228 + if (!broadcast) {
239 break; 229 break;
240 } 230 }
241 } 231 }
  232 + }
242 233
243 return 0; 234 return 0;
244 } 235 }
245 236
246 -  
247 int st_cond_signal(_st_cond_t *cvar) 237 int st_cond_signal(_st_cond_t *cvar)
248 { 238 {
249 return _st_cond_signal(cvar, 0); 239 return _st_cond_signal(cvar, 0);
250 } 240 }
251 241
252 -  
253 int st_cond_broadcast(_st_cond_t *cvar) 242 int st_cond_broadcast(_st_cond_t *cvar)
254 { 243 {
255 return _st_cond_signal(cvar, 1); 244 return _st_cond_signal(cvar, 1);
256 } 245 }
257 246
258 -  
259 /***************************************** 247 /*****************************************
260 * Mutex functions 248 * Mutex functions
261 */ 249 */
262 -  
263 _st_mutex_t *st_mutex_new(void) 250 _st_mutex_t *st_mutex_new(void)
264 { 251 {
265 _st_mutex_t *lock; 252 _st_mutex_t *lock;
@@ -273,7 +260,6 @@ _st_mutex_t *st_mutex_new(void) @@ -273,7 +260,6 @@ _st_mutex_t *st_mutex_new(void)
273 return lock; 260 return lock;
274 } 261 }
275 262
276 -  
277 int st_mutex_destroy(_st_mutex_t *lock) 263 int st_mutex_destroy(_st_mutex_t *lock)
278 { 264 {
279 if (lock->owner != NULL || lock->wait_q.next != &lock->wait_q) { 265 if (lock->owner != NULL || lock->wait_q.next != &lock->wait_q) {
@@ -286,7 +272,6 @@ int st_mutex_destroy(_st_mutex_t *lock) @@ -286,7 +272,6 @@ int st_mutex_destroy(_st_mutex_t *lock)
286 return 0; 272 return 0;
287 } 273 }
288 274
289 -  
290 int st_mutex_lock(_st_mutex_t *lock) 275 int st_mutex_lock(_st_mutex_t *lock)
291 { 276 {
292 _st_thread_t *me = _ST_CURRENT_THREAD(); 277 _st_thread_t *me = _ST_CURRENT_THREAD();
@@ -325,7 +310,6 @@ int st_mutex_lock(_st_mutex_t *lock) @@ -325,7 +310,6 @@ int st_mutex_lock(_st_mutex_t *lock)
325 return 0; 310 return 0;
326 } 311 }
327 312
328 -  
329 int st_mutex_unlock(_st_mutex_t *lock) 313 int st_mutex_unlock(_st_mutex_t *lock)
330 { 314 {
331 _st_thread_t *thread; 315 _st_thread_t *thread;
@@ -353,7 +337,6 @@ int st_mutex_unlock(_st_mutex_t *lock) @@ -353,7 +337,6 @@ int st_mutex_unlock(_st_mutex_t *lock)
353 return 0; 337 return 0;
354 } 338 }
355 339
356 -  
357 int st_mutex_trylock(_st_mutex_t *lock) 340 int st_mutex_trylock(_st_mutex_t *lock)
358 { 341 {
359 if (lock->owner != NULL) { 342 if (lock->owner != NULL) {