Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * Native Client threads library | 8 * Native Client threads library |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 #include "native_client/src/untrusted/pthread/pthread.h" | 26 #include "native_client/src/untrusted/pthread/pthread.h" |
| 27 #include "native_client/src/untrusted/pthread/pthread_internal.h" | 27 #include "native_client/src/untrusted/pthread/pthread_internal.h" |
| 28 #include "native_client/src/untrusted/pthread/pthread_types.h" | 28 #include "native_client/src/untrusted/pthread/pthread_types.h" |
| 29 | 29 |
| 30 #include "native_client/src/untrusted/valgrind/dynamic_annotations.h" | 30 #include "native_client/src/untrusted/valgrind/dynamic_annotations.h" |
| 31 | 31 |
| 32 #if defined(NACL_IN_IRT) | 32 #if defined(NACL_IN_IRT) |
| 33 # include "native_client/src/untrusted/irt/irt_private.h" | 33 # include "native_client/src/untrusted/irt/irt_private.h" |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 #define FUN_TO_VOID_PTR(a) ((void*)((uintptr_t) a)) | 36 #define FUN_TO_VOID_PTR(a) ((void *) ((uintptr_t) a)) |
|
Roland McGrath
2012/12/04 00:04:20
This also has superfluous parens around the second
Mark Seaborn
2012/12/05 05:10:57
Done.
| |
| 37 | 37 |
| 38 /* | 38 /* |
| 39 * ABI tables for underyling NaCl thread interfaces. | 39 * ABI tables for underyling NaCl thread interfaces. |
| 40 */ | 40 */ |
| 41 static struct nacl_irt_thread irt_thread; | 41 static struct nacl_irt_thread irt_thread; |
| 42 | 42 |
| 43 /* | 43 /* |
| 44 * These days, the thread_create() syscall/IRT call will align the | 44 * These days, the thread_create() syscall/IRT call will align the |
| 45 * stack for us, but for compatibility with older, released x86 | 45 * stack for us, but for compatibility with older, released x86 |
| 46 * versions of NaCl where thread_create() does not align the stack, we | 46 * versions of NaCl where thread_create() does not align the stack, we |
| 47 * align the stack ourselves. | 47 * align the stack ourselves. |
| 48 */ | 48 */ |
| 49 #if defined(__i386__) | 49 #if defined(__i386__) |
| 50 static const uint32_t kStackAlignment = 32; | 50 static const uint32_t kStackAlignment = 32; |
| 51 static const uint32_t kStackPadBelowAlign = 4; /* Return address size */ | 51 static const uint32_t kStackPadBelowAlign = 4; /* Return address size */ |
| 52 #elif defined(__x86_64__) | 52 #elif defined(__x86_64__) |
| 53 static const uint32_t kStackAlignment = 32; | 53 static const uint32_t kStackAlignment = 32; |
| 54 static const uint32_t kStackPadBelowAlign = 8; /* Return address size */ | 54 static const uint32_t kStackPadBelowAlign = 8; /* Return address size */ |
| 55 #else | 55 #else |
| 56 static const uint32_t kStackAlignment = 1; | 56 static const uint32_t kStackAlignment = 1; |
| 57 static const uint32_t kStackPadBelowAlign = 0; | 57 static const uint32_t kStackPadBelowAlign = 0; |
| 58 #endif | 58 #endif |
| 59 | 59 |
| 60 #define TDB_SIZE (sizeof(struct nc_combined_tdb)) | 60 #define TDB_SIZE (sizeof(struct nc_combined_tdb)) |
| 61 | 61 |
| 62 static inline char* align(uint32_t offset, uint32_t alignment) { | 62 static inline char *align(uint32_t offset, uint32_t alignment) { |
| 63 return (char*) ((offset + alignment - 1) & ~(alignment - 1)); | 63 return (char *) ((offset + alignment - 1) & ~(alignment - 1)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 /* Thread management global variables */ | 66 /* Thread management global variables */ |
| 67 const int __nc_kMaxCachedMemoryBlocks = 50; | 67 const int __nc_kMaxCachedMemoryBlocks = 50; |
| 68 | 68 |
| 69 int __nc_thread_initialized; | 69 int __nc_thread_initialized; |
| 70 | 70 |
| 71 /* mutex used to synchronize thread management code */ | 71 /* Mutex used to synchronize thread management code */ |
| 72 pthread_mutex_t __nc_thread_management_lock; | 72 pthread_mutex_t __nc_thread_management_lock; |
| 73 | 73 |
| 74 /* condition variable that gets signaled when all the threads | 74 /* |
| 75 * Condition variable that gets signaled when all the threads | |
| 75 * except the main thread have terminated | 76 * except the main thread have terminated |
| 76 */ | 77 */ |
| 77 static pthread_cond_t __nc_last_thread_cond; | 78 static pthread_cond_t __nc_last_thread_cond; |
| 78 static pthread_t __nc_initial_thread_id; | 79 static pthread_t __nc_initial_thread_id; |
| 79 | 80 |
| 80 /* number of threads currently running in this NaCl module */ | 81 /* Number of threads currently running in this NaCl module */ |
| 81 int __nc_running_threads_counter = 1; | 82 int __nc_running_threads_counter = 1; |
| 82 | 83 |
| 83 /* we have two queues of memory blocks - one for each type */ | 84 /* We have two queues of memory blocks - one for each type */ |
| 84 STAILQ_HEAD(tailhead, entry) __nc_thread_memory_blocks[2]; | 85 STAILQ_HEAD(tailhead, entry) __nc_thread_memory_blocks[2]; |
| 85 /* We need a counter for each queue to keep track of number of blocks */ | 86 /* We need a counter for each queue to keep track of number of blocks */ |
| 86 int __nc_memory_block_counter[2]; | 87 int __nc_memory_block_counter[2]; |
| 87 | 88 |
| 88 #define NODE_TO_PAYLOAD(TlsNode) \ | 89 #define NODE_TO_PAYLOAD(TlsNode) \ |
| 89 ((char*)(TlsNode) + sizeof(nc_thread_memory_block_t)) | 90 ((char *) (TlsNode) + sizeof(nc_thread_memory_block_t)) |
| 90 | 91 |
| 91 /* Internal functions */ | 92 /* Internal functions */ |
| 92 | 93 |
| 93 static inline void nc_abort(void) { | 94 static inline void nc_abort(void) { |
| 94 while (1) *(volatile int *) 0 = 0; /* Crash. */ | 95 while (1) *(volatile int *) 0 = 0; /* Crash. */ |
| 95 } | 96 } |
| 96 | 97 |
| 97 static inline nc_thread_descriptor_t *nc_get_tdb(void) { | 98 static inline nc_thread_descriptor_t *nc_get_tdb(void) { |
| 98 /* | 99 /* |
| 99 * Fetch the thread-specific data pointer. This is usually just | 100 * Fetch the thread-specific data pointer. This is usually just |
| 100 * a wrapper around __libnacl_irt_tls.tls_get() but we don't use | 101 * a wrapper around __libnacl_irt_tls.tls_get() but we don't use |
| 101 * that here so that the IRT build can override the definition. | 102 * that here so that the IRT build can override the definition. |
| 102 */ | 103 */ |
| 103 return (void *) ((char *) __nacl_read_tp() + __nacl_tp_tdb_offset(TDB_SIZE)); | 104 return (void *) ((char *) __nacl_read_tp() + __nacl_tp_tdb_offset(TDB_SIZE)); |
| 104 } | 105 } |
| 105 | 106 |
| 106 static void nc_thread_starter(void) { | 107 static void nc_thread_starter(void) { |
| 107 nc_thread_descriptor_t *tdb = nc_get_tdb(); | 108 nc_thread_descriptor_t *tdb = nc_get_tdb(); |
| 108 __newlib_thread_init(); | 109 __newlib_thread_init(); |
| 109 #if defined(NACL_IN_IRT) | 110 #if defined(NACL_IN_IRT) |
| 110 g_is_irt_internal_thread = 1; | 111 g_is_irt_internal_thread = 1; |
| 111 #endif | 112 #endif |
| 112 void *retval = tdb->start_func(tdb->state); | 113 void *retval = tdb->start_func(tdb->state); |
| 113 /* if the function returns, terminate the thread */ | 114 /* If the function returns, terminate the thread */ |
|
Roland McGrath
2012/12/04 00:04:20
If you're going to capitalize, you might as well p
Mark Seaborn
2012/12/05 05:10:57
Done.
| |
| 114 pthread_exit(retval); | 115 pthread_exit(retval); |
| 115 /* NOTREACHED */ | 116 /* NOTREACHED */ |
| 116 /* TODO(gregoryd) - add assert */ | 117 /* TODO(gregoryd) - add assert */ |
| 117 } | 118 } |
| 118 | 119 |
| 119 static nc_thread_memory_block_t* nc_allocate_memory_block_mu( | 120 static nc_thread_memory_block_t *nc_allocate_memory_block_mu( |
| 120 nc_thread_memory_block_type_t type, | 121 nc_thread_memory_block_type_t type, |
| 121 int required_size) { | 122 int required_size) { |
| 122 struct tailhead *head; | 123 struct tailhead *head; |
| 123 nc_thread_memory_block_t *node; | 124 nc_thread_memory_block_t *node; |
| 124 /* assume the lock is held!!! */ | 125 /* assume the lock is held!!! */ |
| 125 if (type >= MAX_MEMORY_TYPE) | 126 if (type >= MAX_MEMORY_TYPE) |
| 126 return NULL; | 127 return NULL; |
| 127 head = &__nc_thread_memory_blocks[type]; | 128 head = &__nc_thread_memory_blocks[type]; |
| 128 | 129 |
| 129 /* We need to know the size even if we find a free node - to memset it to 0 */ | 130 /* We need to know the size even if we find a free node - to memset it to 0 */ |
| 130 switch (type) { | 131 switch (type) { |
| 131 case THREAD_STACK_MEMORY: | 132 case THREAD_STACK_MEMORY: |
| 132 required_size = required_size + kStackAlignment - 1; | 133 required_size = required_size + kStackAlignment - 1; |
| 133 break; | 134 break; |
| 134 case TLS_AND_TDB_MEMORY: | 135 case TLS_AND_TDB_MEMORY: |
| 135 break; | 136 break; |
| 136 case MAX_MEMORY_TYPE: | 137 case MAX_MEMORY_TYPE: |
| 137 default: | 138 default: |
| 138 return NULL; | 139 return NULL; |
| 139 } | 140 } |
| 140 | 141 |
| 141 if (!STAILQ_EMPTY(head)) { | 142 if (!STAILQ_EMPTY(head)) { |
| 142 /* try to get one from queue */ | 143 /* Try to get one from queue */ |
| 143 nc_thread_memory_block_t *node = STAILQ_FIRST(head); | 144 nc_thread_memory_block_t *node = STAILQ_FIRST(head); |
| 144 | 145 |
| 145 /* | 146 /* |
| 146 * On average the memory blocks will be marked as not used in the same order | 147 * On average the memory blocks will be marked as not used in the same order |
| 147 * as they are added to the queue, therefore there is no need to check the | 148 * as they are added to the queue, therefore there is no need to check the |
| 148 * next queue entries if the first one is still in use. | 149 * next queue entries if the first one is still in use. |
| 149 */ | 150 */ |
| 150 if (0 == node->is_used && node->size >= required_size) { | 151 if (0 == node->is_used && node->size >= required_size) { |
| 151 /* This will only re-use the first node possibly, and could be | 152 /* |
| 153 * This will only re-use the first node possibly, and could be | |
| 152 * improved to provide the stack with a best-fit algorithm if needed. | 154 * improved to provide the stack with a best-fit algorithm if needed. |
| 153 * TODO: we should scan all nodes to see if there is one that fits | 155 * TODO: we should scan all nodes to see if there is one that fits |
| 154 * before allocating another. | 156 * before allocating another. |
| 155 * http://code.google.com/p/nativeclient/issues/detail?id=1569 | 157 * http://code.google.com/p/nativeclient/issues/detail?id=1569 |
| 156 */ | 158 */ |
| 157 int size = node->size; | 159 int size = node->size; |
| 158 STAILQ_REMOVE_HEAD(head, entries); | 160 STAILQ_REMOVE_HEAD(head, entries); |
| 159 --__nc_memory_block_counter[type]; | 161 --__nc_memory_block_counter[type]; |
| 160 | 162 |
| 161 memset(node, 0,sizeof(*node)); | 163 memset(node, 0,sizeof(*node)); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 183 } else { | 185 } else { |
| 184 /* | 186 /* |
| 185 * Stop once we find a block that is still in use, | 187 * Stop once we find a block that is still in use, |
| 186 * since probably there is no point to continue | 188 * since probably there is no point to continue |
| 187 */ | 189 */ |
| 188 break; | 190 break; |
| 189 } | 191 } |
| 190 } | 192 } |
| 191 | 193 |
| 192 } | 194 } |
| 193 /* no available blocks of the required type/size - allocate one */ | 195 /* No available blocks of the required type/size - allocate one */ |
| 194 node = malloc(MEMORY_BLOCK_ALLOCATION_SIZE(required_size)); | 196 node = malloc(MEMORY_BLOCK_ALLOCATION_SIZE(required_size)); |
| 195 if (NULL != node) { | 197 if (NULL != node) { |
| 196 memset(node, 0, sizeof(*node)); | 198 memset(node, 0, sizeof(*node)); |
| 197 node->size = required_size; | 199 node->size = required_size; |
| 198 node->is_used = 1; | 200 node->is_used = 1; |
| 199 } | 201 } |
| 200 return node; | 202 return node; |
| 201 } | 203 } |
| 202 | 204 |
| 203 static void nc_free_memory_block_mu(nc_thread_memory_block_type_t type, | 205 static void nc_free_memory_block_mu(nc_thread_memory_block_type_t type, |
| 204 nc_thread_memory_block_t* node) { | 206 nc_thread_memory_block_t *node) { |
| 205 /* assume the lock is held !!! */ | 207 /* assume the lock is held !!! */ |
| 206 struct tailhead *head = &__nc_thread_memory_blocks[type]; | 208 struct tailhead *head = &__nc_thread_memory_blocks[type]; |
| 207 STAILQ_INSERT_TAIL(head, node, entries); | 209 STAILQ_INSERT_TAIL(head, node, entries); |
| 208 ++__nc_memory_block_counter[type]; | 210 ++__nc_memory_block_counter[type]; |
| 209 } | 211 } |
| 210 | 212 |
| 211 static void nc_release_basic_data_mu(nc_basic_thread_data_t *basic_data) { | 213 static void nc_release_basic_data_mu(nc_basic_thread_data_t *basic_data) { |
| 212 /* join_condvar can be initialized only if tls_node exists */ | 214 /* join_condvar can be initialized only if tls_node exists */ |
| 213 pthread_cond_destroy(&basic_data->join_condvar); | 215 pthread_cond_destroy(&basic_data->join_condvar); |
| 214 free(basic_data); | 216 free(basic_data); |
| 215 } | 217 } |
| 216 | 218 |
| 217 static void nc_release_tls_node(nc_thread_memory_block_t *block, | 219 static void nc_release_tls_node(nc_thread_memory_block_t *block, |
| 218 nc_thread_descriptor_t *tdb) { | 220 nc_thread_descriptor_t *tdb) { |
| 219 if (block) { | 221 if (block) { |
| 220 if (NULL != tdb->basic_data) { | 222 if (NULL != tdb->basic_data) { |
| 221 tdb->basic_data->tdb = NULL; | 223 tdb->basic_data->tdb = NULL; |
| 222 } | 224 } |
| 223 block->is_used = 0; | 225 block->is_used = 0; |
| 224 nc_free_memory_block_mu(TLS_AND_TDB_MEMORY, block); | 226 nc_free_memory_block_mu(TLS_AND_TDB_MEMORY, block); |
| 225 } | 227 } |
| 226 } | 228 } |
| 227 | 229 |
| 228 /* Initialize a newly allocated TDB to some default values */ | 230 /* Initialize a newly allocated TDB to some default values */ |
| 229 static int nc_tdb_init(nc_thread_descriptor_t *tdb, | 231 static int nc_tdb_init(nc_thread_descriptor_t *tdb, |
| 230 nc_basic_thread_data_t * basic_data) { | 232 nc_basic_thread_data_t *basic_data) { |
| 231 tdb->tls_base = tdb; | 233 tdb->tls_base = tdb; |
| 232 tdb->basic_data = basic_data; | 234 tdb->basic_data = basic_data; |
| 233 basic_data->tdb = tdb; | 235 basic_data->tdb = tdb; |
| 234 tdb->basic_data->retval = 0; | 236 tdb->basic_data->retval = 0; |
| 235 tdb->basic_data->status = THREAD_RUNNING; | 237 tdb->basic_data->status = THREAD_RUNNING; |
| 236 | 238 |
| 237 tdb->joinable = PTHREAD_CREATE_JOINABLE; | 239 tdb->joinable = PTHREAD_CREATE_JOINABLE; |
| 238 tdb->join_waiting = 0; | 240 tdb->join_waiting = 0; |
| 239 | 241 |
| 240 tdb->tls_node = NULL; | 242 tdb->tls_node = NULL; |
| 241 tdb->stack_node = NULL; | 243 tdb->stack_node = NULL; |
| 242 | 244 |
| 243 tdb->start_func = NULL; | 245 tdb->start_func = NULL; |
| 244 tdb->state = NULL; | 246 tdb->state = NULL; |
| 245 | 247 |
| 246 tdb->irt_thread_data = NULL; | 248 tdb->irt_thread_data = NULL; |
| 247 | 249 |
| 248 /* Imitate PTHREAD_COND_INITIALIZER - we cannot use it directly here, | 250 /* |
| 251 * Imitate PTHREAD_COND_INITIALIZER - we cannot use it directly here, | |
| 249 * since this is not variable initialization. | 252 * since this is not variable initialization. |
| 250 */ | 253 */ |
| 251 nc_pthread_condvar_ctor(&basic_data->join_condvar); | 254 nc_pthread_condvar_ctor(&basic_data->join_condvar); |
| 252 return 0; | 255 return 0; |
| 253 } | 256 } |
| 254 | 257 |
| 255 /* Initializes all globals except for the initial thread structure. */ | 258 /* Initializes all globals except for the initial thread structure. */ |
| 256 void __nc_initialize_globals(void) { | 259 void __nc_initialize_globals(void) { |
| 257 /* | 260 /* |
| 258 * Fetch the ABI tables from the IRT. If we don't have these, all is lost. | 261 * Fetch the ABI tables from the IRT. If we don't have these, all is lost. |
| 259 */ | 262 */ |
| 260 __nc_initialize_interfaces(&irt_thread); | 263 __nc_initialize_interfaces(&irt_thread); |
| 261 | 264 |
| 262 if (pthread_mutex_init(&__nc_thread_management_lock, NULL) != 0) | 265 if (pthread_mutex_init(&__nc_thread_management_lock, NULL) != 0) |
| 263 nc_abort(); | 266 nc_abort(); |
| 264 | 267 |
| 265 /* Tell ThreadSanitizer to not generate happens-before arcs between uses of | 268 /* |
| 266 this mutex. Otherwise we miss to many real races. | 269 * Tell ThreadSanitizer to not generate happens-before arcs between uses of |
| 267 When not running under ThreadSanitizer, this is just a call to an empty | 270 * this mutex. Otherwise we miss to many real races. |
| 268 function. */ | 271 * When not running under ThreadSanitizer, this is just a call to an empty |
| 272 * function. | |
| 273 */ | |
| 269 ANNOTATE_NOT_HAPPENS_BEFORE_MUTEX(&__nc_thread_management_lock); | 274 ANNOTATE_NOT_HAPPENS_BEFORE_MUTEX(&__nc_thread_management_lock); |
| 270 | 275 |
| 271 if (pthread_cond_init(&__nc_last_thread_cond, NULL) != 0) | 276 if (pthread_cond_init(&__nc_last_thread_cond, NULL) != 0) |
| 272 nc_abort(); | 277 nc_abort(); |
| 273 STAILQ_INIT(&__nc_thread_memory_blocks[0]); | 278 STAILQ_INIT(&__nc_thread_memory_blocks[0]); |
| 274 STAILQ_INIT(&__nc_thread_memory_blocks[1]); | 279 STAILQ_INIT(&__nc_thread_memory_blocks[1]); |
| 275 | 280 |
| 276 __nc_thread_initialized = 1; | 281 __nc_thread_initialized = 1; |
| 277 } | 282 } |
| 278 | 283 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 299 tdb->basic_data.status = THREAD_RUNNING; | 304 tdb->basic_data.status = THREAD_RUNNING; |
| 300 pthread_cond_t condvar_init = PTHREAD_COND_INITIALIZER; | 305 pthread_cond_t condvar_init = PTHREAD_COND_INITIALIZER; |
| 301 tdb->basic_data.join_condvar = condvar_init; | 306 tdb->basic_data.join_condvar = condvar_init; |
| 302 | 307 |
| 303 tdb->tdb.basic_data = &tdb->basic_data; | 308 tdb->tdb.basic_data = &tdb->basic_data; |
| 304 tdb->basic_data.tdb = &tdb->tdb; | 309 tdb->basic_data.tdb = &tdb->tdb; |
| 305 } | 310 } |
| 306 | 311 |
| 307 #else | 312 #else |
| 308 | 313 |
| 309 /* Will be called from the library startup code, | 314 /* |
| 315 * Will be called from the library startup code, | |
| 310 * which always happens on the application's main thread | 316 * which always happens on the application's main thread |
| 311 */ | 317 */ |
| 312 void __pthread_initialize(void) { | 318 void __pthread_initialize(void) { |
| 313 __pthread_initialize_minimal(TDB_SIZE); | 319 __pthread_initialize_minimal(TDB_SIZE); |
| 314 | 320 |
| 315 struct nc_combined_tdb *tdb = (struct nc_combined_tdb *) nc_get_tdb(); | 321 struct nc_combined_tdb *tdb = (struct nc_combined_tdb *) nc_get_tdb(); |
| 316 nc_tdb_init(&tdb->tdb, &tdb->basic_data); | 322 nc_tdb_init(&tdb->tdb, &tdb->basic_data); |
| 317 __nc_initial_thread_id = &tdb->basic_data; | 323 __nc_initial_thread_id = &tdb->basic_data; |
| 318 | 324 |
| 319 __nc_initialize_globals(); | 325 __nc_initialize_globals(); |
| 320 | 326 |
| 321 __nc_futex_init(); | 327 __nc_futex_init(); |
| 322 } | 328 } |
| 323 | 329 |
| 324 #endif | 330 #endif |
| 325 | 331 |
| 326 | 332 |
| 327 /* pthread functions */ | 333 /* pthread functions */ |
| 328 | 334 |
| 329 int pthread_create(pthread_t *thread_id, | 335 int pthread_create(pthread_t *thread_id, |
| 330 const pthread_attr_t *attr, | 336 const pthread_attr_t *attr, |
| 331 void *(*start_routine) (void *), | 337 void *(*start_routine)(void *), |
| 332 void *arg) { | 338 void *arg) { |
| 333 int retval = EAGAIN; | 339 int retval = EAGAIN; |
| 334 void *esp; | 340 void *esp; |
| 335 /* declare the variables outside of the while scope */ | 341 /* Declare the variables outside of the while scope */ |
| 336 nc_thread_memory_block_t *stack_node = NULL; | 342 nc_thread_memory_block_t *stack_node = NULL; |
| 337 char *thread_stack = NULL; | 343 char *thread_stack = NULL; |
| 338 nc_thread_descriptor_t *new_tdb = NULL; | 344 nc_thread_descriptor_t *new_tdb = NULL; |
| 339 nc_basic_thread_data_t *new_basic_data = NULL; | 345 nc_basic_thread_data_t *new_basic_data = NULL; |
| 340 nc_thread_memory_block_t *tls_node = NULL; | 346 nc_thread_memory_block_t *tls_node = NULL; |
| 341 size_t stacksize = PTHREAD_STACK_DEFAULT; | 347 size_t stacksize = PTHREAD_STACK_DEFAULT; |
| 342 void *new_tp; | 348 void *new_tp; |
| 343 | 349 |
| 344 /* TODO(gregoryd) - right now a single lock is used, try to optimize? */ | 350 /* TODO(gregoryd) - right now a single lock is used, try to optimize? */ |
| 345 pthread_mutex_lock(&__nc_thread_management_lock); | 351 pthread_mutex_lock(&__nc_thread_management_lock); |
| 346 | 352 |
| 347 do { | 353 do { |
| 348 /* Allocate the combined TLS + TDB block---see tls.h for explanation. */ | 354 /* Allocate the combined TLS + TDB block---see tls.h for explanation. */ |
| 349 | 355 |
| 350 tls_node = nc_allocate_memory_block_mu(TLS_AND_TDB_MEMORY, | 356 tls_node = nc_allocate_memory_block_mu(TLS_AND_TDB_MEMORY, |
| 351 __nacl_tls_combined_size(TDB_SIZE)); | 357 __nacl_tls_combined_size(TDB_SIZE)); |
| 352 if (NULL == tls_node) | 358 if (NULL == tls_node) |
| 353 break; | 359 break; |
| 354 | 360 |
| 355 new_tp = __nacl_tls_data_bss_initialize_from_template( | 361 new_tp = __nacl_tls_data_bss_initialize_from_template( |
| 356 NODE_TO_PAYLOAD(tls_node), TDB_SIZE); | 362 NODE_TO_PAYLOAD(tls_node), TDB_SIZE); |
| 357 | 363 |
| 358 new_tdb = (nc_thread_descriptor_t *) | 364 new_tdb = (nc_thread_descriptor_t *) |
| 359 ((char *) new_tp + __nacl_tp_tdb_offset(TDB_SIZE)); | 365 ((char *) new_tp + __nacl_tp_tdb_offset(TDB_SIZE)); |
| 360 | 366 |
| 361 /* TODO(gregoryd): consider creating a pool of basic_data structs, | 367 /* |
| 368 * TODO(gregoryd): consider creating a pool of basic_data structs, | |
| 362 * similar to stack and TLS+TDB (probably when adding the support for | 369 * similar to stack and TLS+TDB (probably when adding the support for |
| 363 * variable stack size). | 370 * variable stack size). |
| 364 */ | 371 */ |
| 365 new_basic_data = malloc(sizeof(*new_basic_data)); | 372 new_basic_data = malloc(sizeof(*new_basic_data)); |
| 366 if (NULL == new_basic_data) { | 373 if (NULL == new_basic_data) { |
| 367 /* | 374 /* |
| 368 * The tdb should be zero intialized. | 375 * The tdb should be zero intialized. |
| 369 * This just re-emphasizes this requirement. | 376 * This just re-emphasizes this requirement. |
| 370 */ | 377 */ |
| 371 new_tdb->basic_data = NULL; | 378 new_tdb->basic_data = NULL; |
| 372 break; | 379 break; |
| 373 } | 380 } |
| 374 | 381 |
| 375 nc_tdb_init(new_tdb, new_basic_data); | 382 nc_tdb_init(new_tdb, new_basic_data); |
| 376 new_tdb->tls_node = tls_node; | 383 new_tdb->tls_node = tls_node; |
| 377 | 384 |
| 378 /* all the required members of the tdb must be initialized before | 385 /* |
| 386 * All the required members of the tdb must be initialized before | |
| 379 * the thread is started and actually before the global lock is released, | 387 * the thread is started and actually before the global lock is released, |
| 380 * since another thread can call pthread_join() or pthread_detach() | 388 * since another thread can call pthread_join() or pthread_detach() |
| 381 */ | 389 */ |
| 382 new_tdb->start_func = start_routine; | 390 new_tdb->start_func = start_routine; |
| 383 new_tdb->state = arg; | 391 new_tdb->state = arg; |
| 384 if (attr != NULL) { | 392 if (attr != NULL) { |
| 385 new_tdb->joinable = attr->joinable; | 393 new_tdb->joinable = attr->joinable; |
| 386 stacksize = attr->stacksize; | 394 stacksize = attr->stacksize; |
| 387 } | 395 } |
| 388 | 396 |
| 389 /* Allocate the stack for the thread */ | 397 /* Allocate the stack for the thread */ |
| 390 stack_node = nc_allocate_memory_block_mu(THREAD_STACK_MEMORY, stacksize); | 398 stack_node = nc_allocate_memory_block_mu(THREAD_STACK_MEMORY, stacksize); |
| 391 if (NULL == stack_node) { | 399 if (NULL == stack_node) { |
| 392 retval = EAGAIN; | 400 retval = EAGAIN; |
| 393 break; | 401 break; |
| 394 } | 402 } |
| 395 thread_stack = align((uint32_t) NODE_TO_PAYLOAD(stack_node), | 403 thread_stack = align((uint32_t) NODE_TO_PAYLOAD(stack_node), |
| 396 kStackAlignment); | 404 kStackAlignment); |
| 397 new_tdb->stack_node = stack_node; | 405 new_tdb->stack_node = stack_node; |
| 398 | 406 |
| 399 retval = 0; | 407 retval = 0; |
| 400 } while (0); | 408 } while (0); |
| 401 | 409 |
| 402 if (0 != retval) { | 410 if (0 != retval) { |
| 403 pthread_mutex_unlock(&__nc_thread_management_lock); | 411 pthread_mutex_unlock(&__nc_thread_management_lock); |
| 404 goto ret; /* error */ | 412 goto ret; /* error */ |
| 405 } | 413 } |
| 406 | 414 |
| 407 /* Speculatively increase the thread count. If thread creation | 415 /* |
| 408 fails, we will decrease it back. This way the thread count will | 416 * Speculatively increase the thread count. If thread creation |
| 409 never be lower than the actual number of threads, but can briefly be | 417 * fails, we will decrease it back. This way the thread count will |
| 410 higher than that. */ | 418 * never be lower than the actual number of threads, but can briefly |
| 419 * be higher than that. | |
| 420 */ | |
| 411 ++__nc_running_threads_counter; | 421 ++__nc_running_threads_counter; |
| 412 | 422 |
| 413 /* Save the new thread id. This can not be done after the syscall, | 423 /* |
| 414 because the child thread could have already finished by that | 424 * Save the new thread id. This can not be done after the syscall, |
| 415 time. If thread creation fails, it will be overriden with -1 later.*/ | 425 * because the child thread could have already finished by that |
| 426 * time. If thread creation fails, it will be overriden with -1 | |
| 427 * later. | |
| 428 */ | |
| 416 *thread_id = new_basic_data; | 429 *thread_id = new_basic_data; |
| 417 | 430 |
| 418 pthread_mutex_unlock(&__nc_thread_management_lock); | 431 pthread_mutex_unlock(&__nc_thread_management_lock); |
| 419 | 432 |
| 420 /* | 433 /* |
| 421 * Calculate the top-of-stack location. The very first location is a | 434 * Calculate the top-of-stack location. The very first location is a |
| 422 * zero address of architecture-dependent width, needed to satisfy the | 435 * zero address of architecture-dependent width, needed to satisfy the |
| 423 * normal ABI alignment requirements for the stack. (On some machines | 436 * normal ABI alignment requirements for the stack. (On some machines |
| 424 * this is the dummy return address of the thread-start function.) | 437 * this is the dummy return address of the thread-start function.) |
| 425 * | 438 * |
| 426 * Both thread_stack and stacksize are multiples of 16. | 439 * Both thread_stack and stacksize are multiples of 16. |
| 427 */ | 440 */ |
| 428 esp = (void *) (thread_stack + stacksize - kStackPadBelowAlign); | 441 esp = (void *) (thread_stack + stacksize - kStackPadBelowAlign); |
| 429 memset(esp, 0, kStackPadBelowAlign); | 442 memset(esp, 0, kStackPadBelowAlign); |
| 430 | 443 |
| 431 /* start the thread */ | 444 /* Start the thread */ |
| 432 retval = irt_thread.thread_create( | 445 retval = irt_thread.thread_create( |
| 433 FUN_TO_VOID_PTR(nc_thread_starter), esp, new_tp); | 446 FUN_TO_VOID_PTR(nc_thread_starter), esp, new_tp); |
| 434 if (0 != retval) { | 447 if (0 != retval) { |
| 435 pthread_mutex_lock(&__nc_thread_management_lock); | 448 pthread_mutex_lock(&__nc_thread_management_lock); |
| 436 /* TODO(gregoryd) : replace with atomic decrement? */ | 449 /* TODO(gregoryd) : replace with atomic decrement? */ |
| 437 --__nc_running_threads_counter; | 450 --__nc_running_threads_counter; |
| 438 pthread_mutex_unlock(&__nc_thread_management_lock); | 451 pthread_mutex_unlock(&__nc_thread_management_lock); |
| 439 goto ret; | 452 goto ret; |
| 440 } | 453 } |
| 441 | 454 |
| 442 assert(0 == retval); | 455 assert(0 == retval); |
| 443 | 456 |
| 444 ret: | 457 ret: |
| 445 if (0 != retval) { | 458 if (0 != retval) { |
| 446 /* failed to create a thread */ | 459 /* Failed to create a thread */ |
| 447 pthread_mutex_lock(&__nc_thread_management_lock); | 460 pthread_mutex_lock(&__nc_thread_management_lock); |
| 448 | 461 |
| 449 nc_release_tls_node(tls_node, new_tdb); | 462 nc_release_tls_node(tls_node, new_tdb); |
| 450 if (new_basic_data) { | 463 if (new_basic_data) { |
| 451 nc_release_basic_data_mu(new_basic_data); | 464 nc_release_basic_data_mu(new_basic_data); |
| 452 } | 465 } |
| 453 if (stack_node) { | 466 if (stack_node) { |
| 454 stack_node->is_used = 0; | 467 stack_node->is_used = 0; |
| 455 nc_free_memory_block_mu(THREAD_STACK_MEMORY, stack_node); | 468 nc_free_memory_block_mu(THREAD_STACK_MEMORY, stack_node); |
| 456 } | 469 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 468 while (1 != __nc_running_threads_counter) { | 481 while (1 != __nc_running_threads_counter) { |
| 469 pthread_cond_wait(&__nc_last_thread_cond, &__nc_thread_management_lock); | 482 pthread_cond_wait(&__nc_last_thread_cond, &__nc_thread_management_lock); |
| 470 } | 483 } |
| 471 ANNOTATE_CONDVAR_LOCK_WAIT(&__nc_last_thread_cond, | 484 ANNOTATE_CONDVAR_LOCK_WAIT(&__nc_last_thread_cond, |
| 472 &__nc_thread_management_lock); | 485 &__nc_thread_management_lock); |
| 473 | 486 |
| 474 pthread_mutex_unlock(&__nc_thread_management_lock); | 487 pthread_mutex_unlock(&__nc_thread_management_lock); |
| 475 return 0; | 488 return 0; |
| 476 } | 489 } |
| 477 | 490 |
| 478 void pthread_exit (void* retval) { | 491 void pthread_exit(void *retval) { |
| 479 /* get all we need from the tdb before releasing it */ | 492 /* Get all we need from the tdb before releasing it */ |
| 480 nc_thread_descriptor_t *tdb = nc_get_tdb(); | 493 nc_thread_descriptor_t *tdb = nc_get_tdb(); |
| 481 nc_thread_memory_block_t *stack_node = tdb->stack_node; | 494 nc_thread_memory_block_t *stack_node = tdb->stack_node; |
| 482 int32_t *is_used = &stack_node->is_used; | 495 int32_t *is_used = &stack_node->is_used; |
| 483 nc_basic_thread_data_t *basic_data = tdb->basic_data; | 496 nc_basic_thread_data_t *basic_data = tdb->basic_data; |
| 484 int joinable = tdb->joinable; | 497 int joinable = tdb->joinable; |
| 485 | 498 |
| 486 /* call the destruction functions for TSD */ | 499 /* Call the destruction functions for TSD */ |
| 487 __nc_tsd_exit(); | 500 __nc_tsd_exit(); |
| 488 | 501 |
| 489 __newlib_thread_exit(); | 502 __newlib_thread_exit(); |
| 490 | 503 |
| 491 __nc_futex_thread_exit(); | 504 __nc_futex_thread_exit(); |
| 492 | 505 |
| 493 if (__nc_initial_thread_id != basic_data) { | 506 if (__nc_initial_thread_id != basic_data) { |
| 494 pthread_mutex_lock(&__nc_thread_management_lock); | 507 pthread_mutex_lock(&__nc_thread_management_lock); |
| 495 --__nc_running_threads_counter; | 508 --__nc_running_threads_counter; |
| 496 pthread_mutex_unlock(&__nc_thread_management_lock); | 509 pthread_mutex_unlock(&__nc_thread_management_lock); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 512 /* | 525 /* |
| 513 * We can release TLS+TDB - thread id and its return value are still | 526 * We can release TLS+TDB - thread id and its return value are still |
| 514 * kept in basic_data | 527 * kept in basic_data |
| 515 */ | 528 */ |
| 516 nc_release_tls_node(tdb->tls_node, tdb); | 529 nc_release_tls_node(tdb->tls_node, tdb); |
| 517 | 530 |
| 518 if (!joinable) { | 531 if (!joinable) { |
| 519 nc_release_basic_data_mu(basic_data); | 532 nc_release_basic_data_mu(basic_data); |
| 520 } | 533 } |
| 521 | 534 |
| 522 /* now add the stack to the list but keep it marked as used */ | 535 /* Now add the stack to the list but keep it marked as used */ |
| 523 nc_free_memory_block_mu(THREAD_STACK_MEMORY, stack_node); | 536 nc_free_memory_block_mu(THREAD_STACK_MEMORY, stack_node); |
| 524 | 537 |
| 525 if (1 == __nc_running_threads_counter) { | 538 if (1 == __nc_running_threads_counter) { |
| 526 pthread_cond_signal(&__nc_last_thread_cond); | 539 pthread_cond_signal(&__nc_last_thread_cond); |
| 527 } | 540 } |
| 528 | 541 |
| 529 pthread_mutex_unlock(&__nc_thread_management_lock); | 542 pthread_mutex_unlock(&__nc_thread_management_lock); |
| 530 irt_thread.thread_exit(is_used); | 543 irt_thread.thread_exit(is_used); |
| 531 nc_abort(); | 544 nc_abort(); |
| 532 } | 545 } |
| 533 | 546 |
| 534 int pthread_join(pthread_t thread_id, void **thread_return) { | 547 int pthread_join(pthread_t thread_id, void **thread_return) { |
| 535 int retval = 0; | 548 int retval = 0; |
| 536 nc_basic_thread_data_t *basic_data = thread_id; | 549 nc_basic_thread_data_t *basic_data = thread_id; |
| 537 if (pthread_self() == thread_id) { | 550 if (pthread_self() == thread_id) { |
| 538 return EDEADLK; | 551 return EDEADLK; |
| 539 } | 552 } |
| 540 | 553 |
| 541 pthread_mutex_lock(&__nc_thread_management_lock); | 554 pthread_mutex_lock(&__nc_thread_management_lock); |
| 542 | 555 |
| 543 if (basic_data->tdb != NULL) { | 556 if (basic_data->tdb != NULL) { |
| 544 /* The thread is still running */ | 557 /* The thread is still running */ |
| 545 nc_thread_descriptor_t *joined_tdb = basic_data->tdb; | 558 nc_thread_descriptor_t *joined_tdb = basic_data->tdb; |
| 546 if (!joined_tdb->joinable || joined_tdb->join_waiting) { | 559 if (!joined_tdb->joinable || joined_tdb->join_waiting) { |
| 547 /* the thread is detached or another thread is waiting to join */ | 560 /* The thread is detached or another thread is waiting to join */ |
| 548 retval = EINVAL; | 561 retval = EINVAL; |
| 549 goto ret; | 562 goto ret; |
| 550 } | 563 } |
| 551 joined_tdb->join_waiting = 1; | 564 joined_tdb->join_waiting = 1; |
| 552 /* wait till the thread terminates */ | 565 /* Wait till the thread terminates */ |
| 553 while (THREAD_TERMINATED != basic_data->status) { | 566 while (THREAD_TERMINATED != basic_data->status) { |
| 554 pthread_cond_wait(&basic_data->join_condvar, | 567 pthread_cond_wait(&basic_data->join_condvar, |
| 555 &__nc_thread_management_lock); | 568 &__nc_thread_management_lock); |
| 556 } | 569 } |
| 557 } | 570 } |
| 558 ANNOTATE_CONDVAR_LOCK_WAIT(&basic_data->join_condvar, | 571 ANNOTATE_CONDVAR_LOCK_WAIT(&basic_data->join_condvar, |
| 559 &__nc_thread_management_lock); | 572 &__nc_thread_management_lock); |
| 560 /* The thread has already terminated */ | 573 /* The thread has already terminated */ |
| 561 /* save the return value */ | 574 /* Save the return value */ |
| 562 if (thread_return != NULL) { | 575 if (thread_return != NULL) { |
| 563 *thread_return = basic_data->retval; | 576 *thread_return = basic_data->retval; |
| 564 } | 577 } |
| 565 | 578 |
| 566 /* release the resources */ | 579 /* Release the resources */ |
| 567 nc_release_basic_data_mu(basic_data); | 580 nc_release_basic_data_mu(basic_data); |
| 568 retval = 0; | 581 retval = 0; |
| 569 | 582 |
| 570 ret: | 583 ret: |
| 571 pthread_mutex_unlock(&__nc_thread_management_lock); | 584 pthread_mutex_unlock(&__nc_thread_management_lock); |
| 572 | 585 |
| 573 return retval; | 586 return retval; |
| 574 | 587 |
| 575 } | 588 } |
| 576 | 589 |
| 577 int pthread_detach(pthread_t thread_id) { | 590 int pthread_detach(pthread_t thread_id) { |
| 578 int retval = 0; | 591 int retval = 0; |
| 579 nc_basic_thread_data_t *basic_data = thread_id; | 592 nc_basic_thread_data_t *basic_data = thread_id; |
| 580 nc_thread_descriptor_t *detached_tdb; | 593 nc_thread_descriptor_t *detached_tdb; |
| 581 /* TODO(gregoryd) - can be optimized using InterlockedExchange | 594 /* |
| 582 * once it's available */ | 595 * TODO(gregoryd) - can be optimized using InterlockedExchange |
| 596 * once it's available | |
| 597 */ | |
| 583 pthread_mutex_lock(&__nc_thread_management_lock); | 598 pthread_mutex_lock(&__nc_thread_management_lock); |
| 584 detached_tdb = basic_data->tdb; | 599 detached_tdb = basic_data->tdb; |
| 585 | 600 |
| 586 if (NULL == detached_tdb) { | 601 if (NULL == detached_tdb) { |
| 587 /* The thread has already terminated */ | 602 /* The thread has already terminated */ |
| 588 nc_release_basic_data_mu(basic_data); | 603 nc_release_basic_data_mu(basic_data); |
| 589 } else { | 604 } else { |
| 590 if (!detached_tdb->join_waiting) { | 605 if (!detached_tdb->join_waiting) { |
| 591 if (detached_tdb->joinable) { | 606 if (detached_tdb->joinable) { |
| 592 detached_tdb->joinable = 0; | 607 detached_tdb->joinable = 0; |
| 593 } else { | 608 } else { |
| 594 /* already detached */ | 609 /* Already detached */ |
| 595 retval = EINVAL; | 610 retval = EINVAL; |
| 596 } | 611 } |
| 597 } else { | 612 } else { |
| 598 /* another thread is already waiting to join - do nothing */ | 613 /* Another thread is already waiting to join - do nothing */ |
| 599 } | 614 } |
| 600 } | 615 } |
| 601 pthread_mutex_unlock(&__nc_thread_management_lock); | 616 pthread_mutex_unlock(&__nc_thread_management_lock); |
| 602 return retval; | 617 return retval; |
| 603 } | 618 } |
| 604 | 619 |
| 605 int pthread_kill(pthread_t thread_id, | 620 int pthread_kill(pthread_t thread_id, |
| 606 int sig) { | 621 int sig) { |
| 607 /* This function is currently unimplemented. */ | 622 /* This function is currently unimplemented. */ |
| 608 return ENOSYS; | 623 return ENOSYS; |
| 609 } | 624 } |
| 610 | 625 |
| 611 pthread_t pthread_self(void) { | 626 pthread_t pthread_self(void) { |
| 612 /* get the tdb pointer from gs and use it to return the thread handle*/ | 627 /* Get the tdb pointer from gs and use it to return the thread handle*/ |
| 613 nc_thread_descriptor_t *tdb = nc_get_tdb(); | 628 nc_thread_descriptor_t *tdb = nc_get_tdb(); |
| 614 return tdb->basic_data; | 629 return tdb->basic_data; |
| 615 } | 630 } |
| 616 | 631 |
| 617 int pthread_equal (pthread_t thread1, pthread_t thread2) { | 632 int pthread_equal(pthread_t thread1, pthread_t thread2) { |
| 618 return (thread1 == thread2); | 633 return (thread1 == thread2); |
| 619 } | 634 } |
| 620 | 635 |
| 621 int pthread_setschedprio(pthread_t thread_id, int prio) { | 636 int pthread_setschedprio(pthread_t thread_id, int prio) { |
| 622 if (thread_id != pthread_self()) { | 637 if (thread_id != pthread_self()) { |
| 623 /* | 638 /* |
| 624 * We can only support changing our own priority. | 639 * We can only support changing our own priority. |
| 625 */ | 640 */ |
| 626 return EPERM; | 641 return EPERM; |
| 627 } | 642 } |
| 628 return irt_thread.thread_nice(prio); | 643 return irt_thread.thread_nice(prio); |
| 629 } | 644 } |
| 630 | 645 |
| 631 int pthread_attr_init (pthread_attr_t *attr) { | 646 int pthread_attr_init(pthread_attr_t *attr) { |
| 632 if (NULL == attr) { | 647 if (NULL == attr) { |
| 633 return EINVAL; | 648 return EINVAL; |
| 634 } | 649 } |
| 635 attr->joinable = PTHREAD_CREATE_JOINABLE; | 650 attr->joinable = PTHREAD_CREATE_JOINABLE; |
| 636 attr->stacksize = PTHREAD_STACK_DEFAULT; | 651 attr->stacksize = PTHREAD_STACK_DEFAULT; |
| 637 return 0; | 652 return 0; |
| 638 } | 653 } |
| 639 | 654 |
| 640 int pthread_attr_destroy (pthread_attr_t *attr) { | 655 int pthread_attr_destroy(pthread_attr_t *attr) { |
| 641 if (NULL == attr) { | 656 if (NULL == attr) { |
| 642 return EINVAL; | 657 return EINVAL; |
| 643 } | 658 } |
| 644 /* nothing to destroy */ | 659 /* Nothing to destroy */ |
| 645 return 0; | 660 return 0; |
| 646 } | 661 } |
| 647 | 662 |
| 648 int pthread_attr_setdetachstate (pthread_attr_t *attr, | 663 int pthread_attr_setdetachstate(pthread_attr_t *attr, |
| 649 int detachstate) { | 664 int detachstate) { |
| 650 if (NULL == attr) { | 665 if (NULL == attr) { |
| 651 return EINVAL; | 666 return EINVAL; |
| 652 } | 667 } |
| 653 attr->joinable = detachstate; | 668 attr->joinable = detachstate; |
| 654 return 0; | 669 return 0; |
| 655 } | 670 } |
| 656 | 671 |
| 657 int pthread_attr_getdetachstate (pthread_attr_t *attr, | 672 int pthread_attr_getdetachstate(pthread_attr_t *attr, |
| 658 int *detachstate) { | 673 int *detachstate) { |
| 659 if (NULL == attr) { | 674 if (NULL == attr) { |
| 660 return EINVAL; | 675 return EINVAL; |
| 661 } | 676 } |
| 662 return attr->joinable; | 677 return attr->joinable; |
| 663 } | 678 } |
| 664 | 679 |
| 665 int pthread_attr_setstacksize(pthread_attr_t *attr, | 680 int pthread_attr_setstacksize(pthread_attr_t *attr, |
| 666 size_t stacksize) { | 681 size_t stacksize) { |
| 667 if (NULL == attr) { | 682 if (NULL == attr) { |
| 668 return EINVAL; | 683 return EINVAL; |
| 669 } | 684 } |
| 670 if (PTHREAD_STACK_MIN < stacksize) { | 685 if (PTHREAD_STACK_MIN < stacksize) { |
| 671 attr->stacksize = stacksize; | 686 attr->stacksize = stacksize; |
| 672 } else { | 687 } else { |
| 673 attr->stacksize = PTHREAD_STACK_MIN; | 688 attr->stacksize = PTHREAD_STACK_MIN; |
| 674 } | 689 } |
| 675 return 0; | 690 return 0; |
| 676 } | 691 } |
| 677 | 692 |
| 678 int pthread_attr_getstacksize(pthread_attr_t *attr, | 693 int pthread_attr_getstacksize(pthread_attr_t *attr, |
| 679 size_t *stacksize) { | 694 size_t *stacksize) { |
| 680 if (NULL == attr) { | 695 if (NULL == attr) { |
| 681 return EINVAL; | 696 return EINVAL; |
| 682 } | 697 } |
| 683 *stacksize = attr->stacksize; | 698 *stacksize = attr->stacksize; |
| 684 return 0; | 699 return 0; |
| 685 } | 700 } |
| 686 | 701 |
| 687 void __local_lock_init(_LOCK_T* lock); | 702 void __local_lock_init(_LOCK_T *lock); |
| 688 void __local_lock_init_recursive(_LOCK_T* lock); | 703 void __local_lock_init_recursive(_LOCK_T *lock); |
| 689 void __local_lock_close(_LOCK_T* lock); | 704 void __local_lock_close(_LOCK_T *lock); |
| 690 void __local_lock_close_recursive(_LOCK_T* lock); | 705 void __local_lock_close_recursive(_LOCK_T *lock); |
| 691 void __local_lock_acquire(_LOCK_T* lock); | 706 void __local_lock_acquire(_LOCK_T *lock); |
| 692 void __local_lock_acquire_recursive(_LOCK_T* lock); | 707 void __local_lock_acquire_recursive(_LOCK_T *lock); |
| 693 int __local_lock_try_acquire(_LOCK_T* lock); | 708 int __local_lock_try_acquire(_LOCK_T *lock); |
| 694 int __local_lock_try_acquire_recursive(_LOCK_T* lock); | 709 int __local_lock_try_acquire_recursive(_LOCK_T *lock); |
| 695 void __local_lock_release(_LOCK_T* lock); | 710 void __local_lock_release(_LOCK_T *lock); |
| 696 void __local_lock_release_recursive(_LOCK_T* lock); | 711 void __local_lock_release_recursive(_LOCK_T *lock); |
| 697 | 712 |
| 698 void __local_lock_init(_LOCK_T* lock) { | 713 void __local_lock_init(_LOCK_T *lock) { |
| 699 if (lock != NULL) { | 714 if (lock != NULL) { |
| 700 pthread_mutexattr_t attr; | 715 pthread_mutexattr_t attr; |
| 701 pthread_mutexattr_init(&attr); | 716 pthread_mutexattr_init(&attr); |
| 702 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_FAST_NP); | 717 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_FAST_NP); |
| 703 pthread_mutex_init((pthread_mutex_t*)lock, &attr); | 718 pthread_mutex_init((pthread_mutex_t*)lock, &attr); |
| 704 } | 719 } |
| 705 } | 720 } |
| 706 | 721 |
| 707 void __local_lock_init_recursive(_LOCK_T* lock) { | 722 void __local_lock_init_recursive(_LOCK_T *lock) { |
| 708 if (lock != NULL) { | 723 if (lock != NULL) { |
| 709 pthread_mutexattr_t attr; | 724 pthread_mutexattr_t attr; |
| 710 pthread_mutexattr_init(&attr); | 725 pthread_mutexattr_init(&attr); |
| 711 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); | 726 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); |
| 712 pthread_mutex_init((pthread_mutex_t*)lock, &attr); | 727 pthread_mutex_init((pthread_mutex_t*)lock, &attr); |
| 713 } | 728 } |
| 714 } | 729 } |
| 715 | 730 |
| 716 void __local_lock_close(_LOCK_T* lock) { | 731 void __local_lock_close(_LOCK_T *lock) { |
| 717 if (lock != NULL) { | 732 if (lock != NULL) { |
| 718 pthread_mutex_destroy((pthread_mutex_t*)lock); | 733 pthread_mutex_destroy((pthread_mutex_t*)lock); |
| 719 } | 734 } |
| 720 } | 735 } |
| 721 | 736 |
| 722 void __local_lock_close_recursive(_LOCK_T* lock) { | 737 void __local_lock_close_recursive(_LOCK_T *lock) { |
| 723 __local_lock_close(lock); | 738 __local_lock_close(lock); |
| 724 } | 739 } |
| 725 | 740 |
| 726 void __local_lock_acquire(_LOCK_T* lock) { | 741 void __local_lock_acquire(_LOCK_T *lock) { |
| 727 if (!__nc_thread_initialized) { | 742 if (!__nc_thread_initialized) { |
| 728 /* | 743 /* |
| 729 * pthread library is not initialized yet - there is only one thread. | 744 * pthread library is not initialized yet - there is only one thread. |
| 730 * Calling pthread_mutex_lock will cause an access violation because it | 745 * Calling pthread_mutex_lock will cause an access violation because it |
| 731 * will attempt to access the TDB which is not initialized yet | 746 * will attempt to access the TDB which is not initialized yet |
| 732 */ | 747 */ |
| 733 return; | 748 return; |
| 734 } | 749 } |
| 735 if (lock != NULL) { | 750 if (lock != NULL) { |
| 736 pthread_mutex_lock((pthread_mutex_t*)lock); | 751 pthread_mutex_lock((pthread_mutex_t*)lock); |
| 737 } | 752 } |
| 738 } | 753 } |
| 739 | 754 |
| 740 void __local_lock_acquire_recursive(_LOCK_T* lock) { | 755 void __local_lock_acquire_recursive(_LOCK_T *lock) { |
| 741 __local_lock_acquire(lock); | 756 __local_lock_acquire(lock); |
| 742 } | 757 } |
| 743 | 758 |
| 744 int __local_lock_try_acquire(_LOCK_T* lock) { | 759 int __local_lock_try_acquire(_LOCK_T *lock) { |
| 745 if (!__nc_thread_initialized) { | 760 if (!__nc_thread_initialized) { |
| 746 /* | 761 /* |
| 747 * pthread library is not initialized yet - there is only one thread. | 762 * pthread library is not initialized yet - there is only one thread. |
| 748 * Calling pthread_mutex_lock will cause an access violation because it | 763 * Calling pthread_mutex_lock will cause an access violation because it |
| 749 * will attempt to access the TDB which is not initialized yet | 764 * will attempt to access the TDB which is not initialized yet |
| 750 */ | 765 */ |
| 751 return 0; | 766 return 0; |
| 752 } | 767 } |
| 753 | 768 |
| 754 if (lock != NULL) { | 769 if (lock != NULL) { |
| 755 return pthread_mutex_trylock((pthread_mutex_t*)lock); | 770 return pthread_mutex_trylock((pthread_mutex_t*)lock); |
| 756 } else { | 771 } else { |
| 757 return EINVAL; | 772 return EINVAL; |
| 758 } | 773 } |
| 759 } | 774 } |
| 760 | 775 |
| 761 int __local_lock_try_acquire_recursive(_LOCK_T* lock) { | 776 int __local_lock_try_acquire_recursive(_LOCK_T *lock) { |
| 762 return __local_lock_try_acquire(lock); | 777 return __local_lock_try_acquire(lock); |
| 763 } | 778 } |
| 764 | 779 |
| 765 void __local_lock_release(_LOCK_T* lock) { | 780 void __local_lock_release(_LOCK_T *lock) { |
| 766 if (!__nc_thread_initialized) { | 781 if (!__nc_thread_initialized) { |
| 767 /* | 782 /* |
| 768 * pthread library is not initialized yet - there is only one thread. | 783 * pthread library is not initialized yet - there is only one thread. |
| 769 * Calling pthread_mutex_lock will cause an access violation because it | 784 * Calling pthread_mutex_lock will cause an access violation because it |
| 770 * will attempt to access the TDB which is not initialized yet | 785 * will attempt to access the TDB which is not initialized yet |
| 771 * NOTE: there is no race condition here because the value of the counter | 786 * NOTE: there is no race condition here because the value of the counter |
| 772 * cannot change while the lock is held - the startup process is | 787 * cannot change while the lock is held - the startup process is |
| 773 * single-threaded. | 788 * single-threaded. |
| 774 */ | 789 */ |
| 775 return; | 790 return; |
| 776 } | 791 } |
| 777 | 792 |
| 778 if (lock != NULL) { | 793 if (lock != NULL) { |
| 779 pthread_mutex_unlock((pthread_mutex_t*)lock); | 794 pthread_mutex_unlock((pthread_mutex_t*)lock); |
| 780 } | 795 } |
| 781 } | 796 } |
| 782 | 797 |
| 783 void __local_lock_release_recursive(_LOCK_T* lock) { | 798 void __local_lock_release_recursive(_LOCK_T *lock) { |
| 784 __local_lock_release(lock); | 799 __local_lock_release(lock); |
| 785 } | 800 } |
| 786 | 801 |
| 787 /* | 802 /* |
| 788 * We include this directly in this file rather than compiling it | 803 * We include this directly in this file rather than compiling it |
| 789 * separately because there is some code (e.g. libstdc++) that uses weak | 804 * separately because there is some code (e.g. libstdc++) that uses weak |
| 790 * references to all pthread functions, but conditionalizes its calls only | 805 * references to all pthread functions, but conditionalizes its calls only |
| 791 * on one symbol. So if these functions are in another file in a library | 806 * on one symbol. So if these functions are in another file in a library |
| 792 * archive, they might not be linked in by static linking. | 807 * archive, they might not be linked in by static linking. |
| 793 */ | 808 */ |
| 794 #include "native_client/src/untrusted/pthread/nc_tsd.c" | 809 #include "native_client/src/untrusted/pthread/nc_tsd.c" |
| OLD | NEW |