1/**************************************************************************\ 2 * Copyright (c) Kongsberg Oil & Gas Technologies AS 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 notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * Neither the name of the copyright holder nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31\**************************************************************************/ 32 33/* This file should only be included from condvar.c */ 34 35#include <stdio.h> 36 37#include <Inventor/C/base/time.h> 38 39#include "threads/mutexp.h" 40 41static int 42internal_condvar_struct_init(cc_condvar * condvar_struct) 43{ 44 int status = pthread_cond_init(&(condvar_struct->pthread.condid), NULL); 45 if (status != 0) { 46 if (COIN_DEBUG) 47 cc_debugerror_post("internal_condvar_struct_init", 48 "pthread_cond_init() error: %d\n", status); 49 return CC_ERROR; 50 } 51 return CC_OK; 52} 53 54static int 55internal_condvar_struct_clean(cc_condvar * condvar_struct) 56{ 57 int status; 58 int ret = CC_OK; 59 status = pthread_cond_destroy(&(condvar_struct->pthread.condid)); 60 if (status != 0) { 61 if (COIN_DEBUG) 62 cc_debugerror_post("internal_condvar_struct_clean", 63 "pthread_cond_destroy() error: %d\n", status); 64 ret = CC_ERROR; 65 } 66 return ret; 67} 68 69static int 70internal_condvar_wait(cc_condvar * condvar, cc_mutex * mutex) 71{ 72 int status; 73 74 status = pthread_cond_wait(&condvar->pthread.condid, 75 &mutex->pthread.mutexid); 76 if (status != 0) { 77 if (COIN_DEBUG) 78 cc_debugerror_post("internal_condvar_wait", 79 "pthread_cond_wait() error: %d\n", status); 80 return CC_ERROR; 81 } 82 return CC_OK; 83} 84 85static int 86internal_condvar_timed_wait(cc_condvar * condvar, cc_mutex * mutex, cc_time period) 87{ 88 struct timespec timeout; 89 cc_time when; 90 int status, ret; 91 double sec; 92 93 when = cc_time_gettimeofday() + period; 94 sec = floor(when); 95 timeout.tv_sec = (int) sec; 96#if defined(HAVE_PTHREAD_TIMESPEC_NSEC) 97 timeout.tv_nsec = (int) ((when - sec) * 1000000000.0); 98#else 99 timeout.tv_usec = (int) ((when - sec) * 1000000.0); 100#endif 101 status = pthread_cond_timedwait(&condvar->pthread.condid, 102 &mutex->pthread.mutexid, &timeout); 103 ret = CC_OK; 104 if (status != 0) { 105 if (status == ETIMEDOUT) { 106 ret = CC_TIMEOUT; 107 } 108 else { 109 ret = CC_ERROR; 110 if (COIN_DEBUG) { 111 cc_debugerror_post("internal_condvar_timed_wait", 112 "pthread_cond_timedwait() error: %d", status); 113 switch (status) { 114 case EINTR: cc_debugerror_post("internal_condvar_timed_wait", 115 "EINTR\n"); break; 116 case EBUSY: cc_debugerror_post("internal_condvar_timed_wait", 117 "EBUSY\n"); break; 118 default: cc_debugerror_post("internal_condvar_timed_wait", 119 "default\n"); break; 120 } 121 } 122 } 123 } 124 return ret; 125} 126 127static int 128internal_condvar_wake_one(cc_condvar * condvar) 129{ 130 int status; 131 status = pthread_cond_signal(&condvar->pthread.condid); 132 if (status != 0) { 133 if (COIN_DEBUG) 134 cc_debugerror_post("internal_condvar_wake_one", 135 "pthread_cond_signal() error: %d\n", status); 136 return CC_ERROR; 137 } 138 return CC_OK; 139} 140 141static int 142internal_condvar_wake_all(cc_condvar * condvar) 143{ 144 int status = pthread_cond_broadcast(&condvar->pthread.condid); 145 if (status != 0) { 146 if (COIN_DEBUG) 147 cc_debugerror_post("internal_condvar_wake_all", 148 "pthread_cond_broadcast() error: %d\n", status); 149 return CC_ERROR; 150 } 151 return CC_OK; 152} 153