SheafSystem  0.0.0.0
thread_condition_variable.cc
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Implementation for class THREAD_CONDITION_VARIABLE
19 
20 #ifndef THREAD_CONDITION_VARIABLE_H
21 #include "SheafSystem/thread_condition_variable.h"
22 #endif
23 
24 #ifdef _PTHREADS
25 
26 #include "SheafSystem/std_iostream.h"
27 
28 #include "SheafSystem/assert_contract.h"
29 
31 sheaf::thread_condition_variable::
32 thread_condition_variable()
33 {
34 
35  // body:
36 
37 #ifdef _PTHREADS
38  int rtncode = pthread_cond_init(&pthread_cond,0);
39  if (rtncode == 0)
40  rtncode = pthread_mutex_init(&pthread_mutex,0);
41 
42  // postconditions:
43 
44  ensure(rtncode == 0);
45 #endif
46 
47  ensure(invariant());
48 }
49 
51 sheaf::thread_condition_variable::
52 ~thread_condition_variable()
53 {
54 
55 #ifdef _PTHREADS
56 
57  // body:
58 
59  int rtncode = pthread_cond_destroy(&pthread_cond);
60  if (rtncode == 0)
61  rtncode = pthread_mutex_destroy(&pthread_mutex);
62 
63  // postconditions:
64 
65  ensure(rtncode == 0);
66 
67 #endif
68 }
69 
70 
72 bool
73 sheaf::thread_condition_variable::
74 invariant() const
75 {
76 
77  return true;
78 }
79 
80 
82 void
83 sheaf::thread_condition_variable::
84 broadcast()
85 {
86 
87 #ifdef _PTHREADS
88 
89  // body:
90 
91  int rtncode = pthread_cond_broadcast(&pthread_cond);
92 
93  // postconditions:
94 
95  ensure(rtncode == 0);
96 
97 #endif
98 }
99 
100 
102 void
103 sheaf::thread_condition_variable::
104 signal()
105 {
106 
107 #ifdef _PTHREADS
108 
109  // body:
110 
111  int rtncode = pthread_cond_signal(&pthread_cond);
112 
113  // postconditions:
114 
115  ensure(rtncode == 0);
116 
117 #endif
118 }
119 
120 
122 void
123 sheaf::thread_condition_variable::
124 timedwait(timespec* xtime)
125 {
126 
127  // preconditions:
128 
129  require(xtime != 0);
130 
131 #ifdef _PHTHREADS
132 
133  // body:
134 
135  int rtncode = pthread_cond_timedwait(&pthread_cond, &pthread_mutex, xtime);
136 
137  // postcondition:
138 
139  ensure(rtncode == 0);
140 
141 #endif
142 
143 }
144 
145 
147 void
148 sheaf::thread_condition_variable::
149 wait()
150 {
151 
152 #ifdef _PTHREADS
153 
154  // body:
155 
156  int rtncode = pthread_cond_wait(&pthread_cond, &pthread_mutex);
157 
158  // postconditions:
159 
160  ensure(rtncode == 0);
161 
162 #endif
163 }
164 
165 
167 void
168 sheaf::thread_condition_variable::
169 lock_mutex()
170 {
171 
172 #ifdef _PTHREADS
173 
174  // body:
175 
176  int rtncode;
177 
178  rtncode = pthread_mutex_lock(&pthread_mutex);
179 
180  // postconditions:
181 
182  ensure(rtncode == 0);
183 
184 #endif
185 }
186 
187 
189 void
190 sheaf::thread_condition_variable::
191 unlock_mutex()
192 {
193 
194 #ifdef _PTHREADS
195 
196  // body:
197 
198  int rtncode = pthread_mutex_unlock(&pthread_mutex);
199 
200  // postconditions:
201 
202  ensure(rtncode == 0);
203 
204 #endif
205 }
206 
207 #endif // _PTHREADS