Adding in the rest of build 38.
[nativeclient.git] / tools / nc_threads / nc_condvar.c
blob1de53557b60c726c316bcd404fc3c695a9647770
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Native Client condition variable API
37 #include <unistd.h>
38 #include "pthread.h"
39 #include "pthread_types.h"
41 void pthread_cond_validate(pthread_cond_t* cond) {
42 nc_spinlock_lock(&cond->spinlock);
44 if (NC_INVALID_HANDLE == cond->handle) {
45 pthread_cond_init(cond, NULL);
48 nc_spinlock_unlock(&cond->spinlock);
52 /* Initialize condition variable COND using attributes ATTR, or use
53 the default values if later is NULL. */
54 int pthread_cond_init (pthread_cond_t *cond,
55 pthread_condattr_t *cond_attr) {
56 cond->handle = __nacl_create_condvar();
58 /* 0 for success, 1 for failure */
59 return (cond->handle < 0);
62 /* Destroy condition variable COND. */
63 int pthread_cond_destroy (pthread_cond_t *cond) {
64 pthread_cond_validate(cond);
65 int retval = close(cond->handle);
66 cond->handle = NC_INVALID_HANDLE;
67 return retval;
70 /* Wake up one thread waiting for condition variable COND. */
71 int pthread_cond_signal (pthread_cond_t *cond) {
72 pthread_cond_validate(cond);
73 return __nacl_signal_condvar(cond->handle);
77 int pthread_cond_broadcast (pthread_cond_t *cond) {
78 pthread_cond_validate(cond);
79 return __nacl_broadcast_condvar(cond->handle);
82 int pthread_cond_wait (pthread_cond_t *cond,
83 pthread_mutex_t *mutex) {
84 pthread_cond_validate(cond);
85 return __nacl_wait_condvar(cond->handle, mutex->mutex_handle);
88 int pthread_cond_timedwait_abs(pthread_cond_t *cond,
89 pthread_mutex_t *mutex,
90 struct timespec *abstime) {
91 pthread_cond_validate(cond);
92 return __nacl_absolute_timed_wait_condvar(cond->handle,
93 mutex->mutex_handle,
94 abstime);
98 int nc_pthread_condvar_ctor(pthread_cond_t * cond)
100 cond->handle = NC_INVALID_HANDLE;
101 cond->spinlock = 0;
102 return 1;