正在显示
2 个修改的文件
包含
30 行增加
和
46 行删除
| @@ -46,7 +46,6 @@ | @@ -46,7 +46,6 @@ | ||
| 46 | #include <sys/mman.h> | 46 | #include <sys/mman.h> |
| 47 | #include "common.h" | 47 | #include "common.h" |
| 48 | 48 | ||
| 49 | - | ||
| 50 | /* How much space to leave between the stacks, at each end */ | 49 | /* How much space to leave between the stacks, at each end */ |
| 51 | #define REDZONE _ST_PAGE_SIZE | 50 | #define REDZONE _ST_PAGE_SIZE |
| 52 | 51 | ||
| @@ -75,8 +74,9 @@ _st_stack_t *_st_stack_new(int stack_size) | @@ -75,8 +74,9 @@ _st_stack_t *_st_stack_new(int stack_size) | ||
| 75 | } | 74 | } |
| 76 | 75 | ||
| 77 | /* Make a new thread stack object. */ | 76 | /* Make a new thread stack object. */ |
| 78 | - if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL) | 77 | + if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL) { |
| 79 | return NULL; | 78 | return NULL; |
| 79 | + } | ||
| 80 | extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0; | 80 | extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0; |
| 81 | ts->vaddr_size = stack_size + 2*REDZONE + extra; | 81 | ts->vaddr_size = stack_size + 2*REDZONE + extra; |
| 82 | ts->vaddr = _st_new_stk_segment(ts->vaddr_size); | 82 | ts->vaddr = _st_new_stk_segment(ts->vaddr_size); |
| @@ -103,21 +103,20 @@ _st_stack_t *_st_stack_new(int stack_size) | @@ -103,21 +103,20 @@ _st_stack_t *_st_stack_new(int stack_size) | ||
| 103 | return ts; | 103 | return ts; |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | - | ||
| 107 | /* | 106 | /* |
| 108 | * Free the stack for the current thread | 107 | * Free the stack for the current thread |
| 109 | */ | 108 | */ |
| 110 | void _st_stack_free(_st_stack_t *ts) | 109 | void _st_stack_free(_st_stack_t *ts) |
| 111 | { | 110 | { |
| 112 | - if (!ts) | 111 | + if (!ts) { |
| 113 | return; | 112 | return; |
| 113 | + } | ||
| 114 | 114 | ||
| 115 | /* Put the stack on the free list */ | 115 | /* Put the stack on the free list */ |
| 116 | ST_APPEND_LINK(&ts->links, _st_free_stacks.prev); | 116 | ST_APPEND_LINK(&ts->links, _st_free_stacks.prev); |
| 117 | _st_num_free_stacks++; | 117 | _st_num_free_stacks++; |
| 118 | } | 118 | } |
| 119 | 119 | ||
| 120 | - | ||
| 121 | static char *_st_new_stk_segment(int size) | 120 | static char *_st_new_stk_segment(int size) |
| 122 | { | 121 | { |
| 123 | #ifdef MALLOC_STACK | 122 | #ifdef MALLOC_STACK |
| @@ -127,28 +126,29 @@ static char *_st_new_stk_segment(int size) | @@ -127,28 +126,29 @@ static char *_st_new_stk_segment(int size) | ||
| 127 | int mmap_flags = MAP_PRIVATE; | 126 | int mmap_flags = MAP_PRIVATE; |
| 128 | void *vaddr; | 127 | void *vaddr; |
| 129 | 128 | ||
| 130 | -#if defined (MD_USE_SYSV_ANON_MMAP) | 129 | + #if defined (MD_USE_SYSV_ANON_MMAP) |
| 131 | if (zero_fd < 0) { | 130 | if (zero_fd < 0) { |
| 132 | - if ((zero_fd = open("/dev/zero", O_RDWR, 0)) < 0) | 131 | + if ((zero_fd = open("/dev/zero", O_RDWR, 0)) < 0) { |
| 133 | return NULL; | 132 | return NULL; |
| 133 | + } | ||
| 134 | fcntl(zero_fd, F_SETFD, FD_CLOEXEC); | 134 | fcntl(zero_fd, F_SETFD, FD_CLOEXEC); |
| 135 | } | 135 | } |
| 136 | -#elif defined (MD_USE_BSD_ANON_MMAP) | 136 | + #elif defined (MD_USE_BSD_ANON_MMAP) |
| 137 | mmap_flags |= MAP_ANON; | 137 | mmap_flags |= MAP_ANON; |
| 138 | -#else | ||
| 139 | -#error Unknown OS | ||
| 140 | -#endif | 138 | + #else |
| 139 | + #error Unknown OS | ||
| 140 | + #endif | ||
| 141 | 141 | ||
| 142 | vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, mmap_flags, zero_fd, 0); | 142 | vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, mmap_flags, zero_fd, 0); |
| 143 | - if (vaddr == (void *)MAP_FAILED) | 143 | + if (vaddr == (void *)MAP_FAILED) { |
| 144 | return NULL; | 144 | return NULL; |
| 145 | + } | ||
| 145 | 146 | ||
| 146 | -#endif /* MALLOC_STACK */ | 147 | +#endif |
| 147 | 148 | ||
| 148 | return (char *)vaddr; | 149 | return (char *)vaddr; |
| 149 | } | 150 | } |
| 150 | 151 | ||
| 151 | - | ||
| 152 | /* Not used */ | 152 | /* Not used */ |
| 153 | #if 0 | 153 | #if 0 |
| 154 | void _st_delete_stk_segment(char *vaddr, int size) | 154 | void _st_delete_stk_segment(char *vaddr, int size) |
| @@ -166,8 +166,9 @@ int st_randomize_stacks(int on) | @@ -166,8 +166,9 @@ int st_randomize_stacks(int on) | ||
| 166 | int wason = _st_randomize_stacks; | 166 | int wason = _st_randomize_stacks; |
| 167 | 167 | ||
| 168 | _st_randomize_stacks = on; | 168 | _st_randomize_stacks = on; |
| 169 | - if (on) | 169 | + if (on) { |
| 170 | srandom((unsigned int) st_utime()); | 170 | srandom((unsigned int) st_utime()); |
| 171 | + } | ||
| 171 | 172 | ||
| 172 | return wason; | 173 | return wason; |
| 173 | } | 174 | } |
| @@ -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) { |
-
请 注册 或 登录 后发表评论