| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 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 condition variable API | 8 * Native Client condition variable API |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <errno.h> | 11 #include <errno.h> |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 | 13 |
| 14 #include "native_client/src/untrusted/nacl/nacl_irt.h" | 14 #include "native_client/src/untrusted/nacl/nacl_irt.h" |
| 15 #include "native_client/src/untrusted/pthread/pthread.h" | 15 #include "native_client/src/untrusted/pthread/pthread.h" |
| 16 #include "native_client/src/untrusted/pthread/pthread_internal.h" | 16 #include "native_client/src/untrusted/pthread/pthread_internal.h" |
| 17 #include "native_client/src/untrusted/pthread/pthread_types.h" | 17 #include "native_client/src/untrusted/pthread/pthread_types.h" |
| 18 | 18 |
| 19 static int nc_thread_cond_init(pthread_cond_t *cond, | 19 static int nc_thread_cond_init(pthread_cond_t *cond, |
| 20 const pthread_condattr_t *cond_attr) { | 20 const pthread_condattr_t *cond_attr) { |
| 21 return __nc_irt_cond.cond_create(&cond->handle); | 21 return __nc_irt_cond.cond_create(&cond->handle); |
| 22 } | 22 } |
| 23 | 23 |
| 24 /* TODO(gregoryd): make this static? */ | 24 /* TODO(gregoryd): make this static? */ |
| 25 void pthread_cond_validate(pthread_cond_t* cond) { | 25 void pthread_cond_validate(pthread_cond_t *cond) { |
| 26 if (nc_token_acquire(&cond->token)) { | 26 if (nc_token_acquire(&cond->token)) { |
| 27 nc_thread_cond_init(cond, NULL); | 27 nc_thread_cond_init(cond, NULL); |
| 28 nc_token_release(&cond->token); | 28 nc_token_release(&cond->token); |
| 29 } | 29 } |
| 30 } | 30 } |
| 31 | 31 |
| 32 | 32 |
| 33 /* | 33 /* |
| 34 * Initialize condition variable COND using attributes ATTR, or use | 34 * Initialize condition variable COND using attributes ATTR, or use |
| 35 * the default values if later is NULL. | 35 * the default values if later is NULL. |
| 36 */ | 36 */ |
| 37 int pthread_cond_init (pthread_cond_t *cond, | 37 int pthread_cond_init(pthread_cond_t *cond, |
| 38 const pthread_condattr_t *cond_attr) { | 38 const pthread_condattr_t *cond_attr) { |
| 39 int retval; | 39 int retval; |
| 40 nc_token_init(&cond->token, 1); | 40 nc_token_init(&cond->token, 1); |
| 41 retval = nc_thread_cond_init(cond, cond_attr); | 41 retval = nc_thread_cond_init(cond, cond_attr); |
| 42 nc_token_release(&cond->token); | 42 nc_token_release(&cond->token); |
| 43 if (0 != retval) | 43 if (0 != retval) |
| 44 return EAGAIN; | 44 return EAGAIN; |
| 45 return 0; | 45 return 0; |
| 46 } | 46 } |
| 47 | 47 |
| 48 /* | 48 /* |
| 49 * Destroy condition variable COND. | 49 * Destroy condition variable COND. |
| 50 */ | 50 */ |
| 51 int pthread_cond_destroy (pthread_cond_t *cond) { | 51 int pthread_cond_destroy(pthread_cond_t *cond) { |
| 52 int retval; | 52 int retval; |
| 53 pthread_cond_validate(cond); | 53 pthread_cond_validate(cond); |
| 54 retval = __nc_irt_cond.cond_destroy(cond->handle); | 54 retval = __nc_irt_cond.cond_destroy(cond->handle); |
| 55 cond->handle = NC_INVALID_HANDLE; | 55 cond->handle = NC_INVALID_HANDLE; |
| 56 return retval; | 56 return retval; |
| 57 } | 57 } |
| 58 | 58 |
| 59 /* | 59 /* |
| 60 * Wake up one thread waiting for condition variable COND. | 60 * Wake up one thread waiting for condition variable COND. |
| 61 */ | 61 */ |
| 62 int pthread_cond_signal (pthread_cond_t *cond) { | 62 int pthread_cond_signal(pthread_cond_t *cond) { |
| 63 pthread_cond_validate(cond); | 63 pthread_cond_validate(cond); |
| 64 return __nc_irt_cond.cond_signal(cond->handle); | 64 return __nc_irt_cond.cond_signal(cond->handle); |
| 65 } | 65 } |
| 66 | 66 |
| 67 int pthread_cond_broadcast (pthread_cond_t *cond) { | 67 int pthread_cond_broadcast(pthread_cond_t *cond) { |
| 68 pthread_cond_validate(cond); | 68 pthread_cond_validate(cond); |
| 69 return __nc_irt_cond.cond_broadcast(cond->handle); | 69 return __nc_irt_cond.cond_broadcast(cond->handle); |
| 70 } | 70 } |
| 71 | 71 |
| 72 int pthread_cond_wait (pthread_cond_t *cond, | 72 int pthread_cond_wait(pthread_cond_t *cond, |
| 73 pthread_mutex_t *mutex) { | 73 pthread_mutex_t *mutex) { |
| 74 pthread_cond_validate(cond); | 74 pthread_cond_validate(cond); |
| 75 int retval = __nc_irt_cond.cond_wait(cond->handle, mutex->mutex_handle); | 75 int retval = __nc_irt_cond.cond_wait(cond->handle, mutex->mutex_handle); |
| 76 if (retval == 0) { | 76 if (retval == 0) { |
| 77 mutex->owner_thread_id = pthread_self(); | 77 mutex->owner_thread_id = pthread_self(); |
| 78 mutex->recursion_counter = 1; | 78 mutex->recursion_counter = 1; |
| 79 } | 79 } |
| 80 return retval; | 80 return retval; |
| 81 } | 81 } |
| 82 | 82 |
| 83 int pthread_cond_timedwait_abs(pthread_cond_t *cond, | 83 int pthread_cond_timedwait_abs(pthread_cond_t *cond, |
| 84 pthread_mutex_t *mutex, | 84 pthread_mutex_t *mutex, |
| 85 const struct timespec *abstime) { | 85 const struct timespec *abstime) { |
| 86 pthread_cond_validate(cond); | 86 pthread_cond_validate(cond); |
| 87 int retval = __nc_irt_cond.cond_timed_wait_abs(cond->handle, | 87 int retval = __nc_irt_cond.cond_timed_wait_abs(cond->handle, |
| 88 mutex->mutex_handle, | 88 mutex->mutex_handle, |
| 89 abstime); | 89 abstime); |
| 90 if (retval == 0 || retval == ETIMEDOUT) { | 90 if (retval == 0 || retval == ETIMEDOUT) { |
| 91 mutex->owner_thread_id = pthread_self(); | 91 mutex->owner_thread_id = pthread_self(); |
| 92 mutex->recursion_counter = 1; | 92 mutex->recursion_counter = 1; |
| 93 } | 93 } |
| 94 return retval; | 94 return retval; |
| 95 } | 95 } |
| 96 | 96 |
| 97 int nc_pthread_condvar_ctor(pthread_cond_t *cond) { | 97 int nc_pthread_condvar_ctor(pthread_cond_t *cond) { |
| 98 nc_token_init(&cond->token, 0); | 98 nc_token_init(&cond->token, 0); |
| 99 cond->handle = NC_INVALID_HANDLE; | 99 cond->handle = NC_INVALID_HANDLE; |
| 100 return 1; | 100 return 1; |
| 101 } | 101 } |
| OLD | NEW |