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 /*! \file Inventor/C/threads/common.h */
34 
35 /*!
36   \struct cc_condvar common.h Inventor/C/threads/common.h
37   \ingroup threads
38   \brief The structure for a conditional variable.
39 */
40 
41 /*!
42   \typedef struct cc_condvar cc_condvar
43   \ingroup threads
44   \brief The type definition for the conditional variable structure.
45 */
46 
47 /*! \file condvar.h */
48 #include <Inventor/C/threads/condvar.h>
49 
50 #include <cstdlib>
51 #include <cassert>
52 #include <cerrno>
53 
54 #ifdef HAVE_SYS_TIME_H
55 #include <sys/time.h>
56 #endif /* HAVE_SYS_TIME_H */
57 #ifdef HAVE_UNISTD_H
58 #include <unistd.h>
59 #endif /* HAVE_UNISTD_H */
60 
61 #include <Inventor/C/threads/mutex.h>
62 #include <Inventor/C/errors/debugerror.h>
63 
64 #include "threads/condvarp.h"
65 
66 /* ********************************************************************** */
67 #ifdef USE_PTHREAD
68 #include "condvar_pthread.icc"
69 #endif /* USE_PTHREAD */
70 
71 #ifdef USE_W32THREAD
72 #include "condvar_win32.icc"
73 #endif /* USE_W32THREAD */
74 
75 /*
76   \internal
77 */
78 
79 void
cc_condvar_struct_init(cc_condvar * condvar_struct)80 cc_condvar_struct_init(cc_condvar * condvar_struct)
81 {
82   int ok;
83   ok = internal_condvar_struct_init(condvar_struct);
84   assert(ok == CC_OK);
85 }
86 
87 /*
88   \internal
89 */
90 void
cc_condvar_struct_clean(cc_condvar * condvar_struct)91 cc_condvar_struct_clean(cc_condvar * condvar_struct)
92 {
93   int ok;
94   assert(condvar_struct != NULL);
95   ok = internal_condvar_struct_clean(condvar_struct);
96   assert(ok == CC_OK);
97 }
98 
99 /* ********************************************************************** */
100 
101 /*! Constructs a conditional variable. */
102 
103 cc_condvar *
cc_condvar_construct(void)104 cc_condvar_construct(void)
105 {
106   cc_condvar * condvar;
107   condvar = (cc_condvar *) malloc(sizeof(cc_condvar));
108   assert(condvar != NULL);
109   cc_condvar_struct_init(condvar);
110   return condvar;
111 }
112 
113 /*! Destroys the given \a condvar. */
114 
115 void
cc_condvar_destruct(cc_condvar * condvar)116 cc_condvar_destruct(cc_condvar * condvar)
117 {
118   assert((condvar != NULL));
119   cc_condvar_struct_clean(condvar);
120   free(condvar);
121 }
122 
123 /*! Wait indefinitely for the \a condvar conditional variable
124     using the specified \a mutex lock. */
125 
126 int
cc_condvar_wait(cc_condvar * condvar,cc_mutex * mutex)127 cc_condvar_wait(cc_condvar * condvar, cc_mutex * mutex)
128 {
129   int ok;
130   assert(condvar != NULL);
131   ok = internal_condvar_wait(condvar, mutex);
132   assert(ok == CC_OK);
133   return ok;
134 }
135 
136 /*! Wait for no more than the \a period for the \a condvar
137     conditional variable using the specified \a mutex lock. */
138 
139 int
cc_condvar_timed_wait(cc_condvar * condvar,cc_mutex * mutex,cc_time period)140 cc_condvar_timed_wait(cc_condvar * condvar,
141                       cc_mutex * mutex,
142                       cc_time period)
143 {
144   int ret;
145   assert(condvar != NULL);
146   ret = internal_condvar_timed_wait(condvar, mutex, period);
147   assert(ret == CC_OK || ret == CC_TIMEOUT);
148   return ret;
149 }
150 
151 /*! Wake one thread waiting for the \a condvar conditional variable. */
152 
153 void
cc_condvar_wake_one(cc_condvar * condvar)154 cc_condvar_wake_one(cc_condvar * condvar)
155 {
156   int ok;
157   assert(condvar != NULL);
158   ok = internal_condvar_wake_one(condvar);
159   assert(ok == CC_OK);
160 }
161 
162 /*! Wake all threads waiting for the \a condvar conditional variable. */
163 
164 void
cc_condvar_wake_all(cc_condvar * condvar)165 cc_condvar_wake_all(cc_condvar * condvar)
166 {
167   int ok;
168   assert(condvar != NULL);
169 
170   ok = internal_condvar_wake_all(condvar);
171   assert(ok == CC_OK);
172 }
173 
174