1 | /* |
2 | * Legal Notice |
3 | * |
4 | * This document and associated source code (the "Work") is a part of a |
5 | * benchmark specification maintained by the TPC. |
6 | * |
7 | * The TPC reserves all right, title, and interest to the Work as provided |
8 | * under U.S. and international laws, including without limitation all patent |
9 | * and trademark rights therein. |
10 | * |
11 | * No Warranty |
12 | * |
13 | * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION |
14 | * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE |
15 | * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER |
16 | * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY, |
17 | * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES, |
18 | * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR |
19 | * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF |
20 | * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE. |
21 | * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT, |
22 | * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT |
23 | * WITH REGARD TO THE WORK. |
24 | * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO |
25 | * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE |
26 | * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS |
27 | * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, |
28 | * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY, |
29 | * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT |
30 | * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD |
31 | * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. |
32 | * |
33 | * Contributors |
34 | * - Christopher Chan-Nui |
35 | */ |
36 | |
37 | #include "utilities/condition.h" |
38 | |
39 | #include <cstring> |
40 | |
41 | #include <iostream> |
42 | #include <sstream> |
43 | #include <stdexcept> |
44 | #include <cerrno> |
45 | #include <cstdlib> |
46 | #ifndef WIN32 |
47 | #include <sys/time.h> |
48 | #endif |
49 | |
50 | #include "utilities/error.h" |
51 | |
52 | using std::exit; |
53 | using std::strerror; |
54 | |
55 | namespace TPCE { |
56 | |
57 | #ifdef WIN32 |
58 | |
59 | CCondition::CCondition(CMutex &pairedmutex) : mutex_(pairedmutex), cond_() { |
60 | InitializeConditionVariable(&cond_); |
61 | } |
62 | |
63 | #else |
64 | |
65 | CCondition::CCondition(CMutex &pairedmutex) : mutex_(pairedmutex), cond_() { |
66 | int rc = pthread_cond_init(&cond_, NULL); |
67 | if (rc != 0) { |
68 | std::ostringstream strm; |
69 | strm << "pthread_cond_init error: " << strerror(rc) << "(" << rc << ")" ; |
70 | throw std::runtime_error(strm.str()); |
71 | } |
72 | } |
73 | #endif |
74 | |
75 | void CCondition::lock() { |
76 | mutex_.lock(); |
77 | } |
78 | |
79 | void CCondition::unlock() { |
80 | mutex_.unlock(); |
81 | } |
82 | |
83 | #ifdef WIN32 |
84 | |
85 | void CCondition::wait() const { |
86 | SleepConditionVariableCS(&cond_, mutex_.mutex(), INFINITE); |
87 | } |
88 | |
89 | void CCondition::signal() { |
90 | WakeConditionVariable(&cond_); |
91 | } |
92 | |
93 | void CCondition::broadcast() { |
94 | WakeAllConditionVariable(&cond_); |
95 | } |
96 | |
97 | bool CCondition::timedwait(long timeout /*us*/) const { |
98 | if (timeout < 0) { |
99 | wait(); |
100 | return true; |
101 | } |
102 | |
103 | int rc = SleepConditionVariableCS(&cond_, mutex_.mutex(), timeout / 1000); |
104 | if (rc == 0) { |
105 | int rc2 = GetLastError(); |
106 | if (rc2 == WAIT_TIMEOUT) { |
107 | return false; |
108 | } else { |
109 | std::ostringstream strm; |
110 | strm << "SleepConditionVariableCS error: " << strerror(rc) << "(" << rc << ")" ; |
111 | throw std::runtime_error(strm.str()); |
112 | } |
113 | } |
114 | return true; |
115 | } |
116 | |
117 | #else |
118 | |
119 | void CCondition::wait() const { |
120 | int rc = pthread_cond_wait(&cond_, mutex_.mutex()); |
121 | if (rc != 0) { |
122 | std::ostringstream strm; |
123 | strm << "pthread_cond_wait error: " << strerror(rc) << "(" << rc << ")" ; |
124 | throw std::runtime_error(strm.str()); |
125 | } |
126 | } |
127 | |
128 | void CCondition::signal() { |
129 | int rc = pthread_cond_signal(&cond_); |
130 | if (rc != 0) { |
131 | std::ostringstream strm; |
132 | strm << "pthread_cond_signal error: " << strerror(rc) << "(" << rc << ")" ; |
133 | throw std::runtime_error(strm.str()); |
134 | } |
135 | } |
136 | |
137 | void CCondition::broadcast() { |
138 | int rc = pthread_cond_broadcast(&cond_); |
139 | if (rc != 0) { |
140 | std::ostringstream strm; |
141 | strm << "pthread_cond_broadcast error: " << strerror(rc) << "(" << rc << ")" ; |
142 | throw std::runtime_error(strm.str()); |
143 | } |
144 | } |
145 | |
146 | bool CCondition::timedwait(const struct timespec &timeout) const { |
147 | int rc = pthread_cond_timedwait(&cond_, mutex_.mutex(), &timeout); |
148 | if (rc == ETIMEDOUT) { |
149 | return false; |
150 | } else if (rc != 0) { |
151 | std::ostringstream strm; |
152 | strm << "pthread_cond_timedwait error: " << strerror(rc) << "(" << rc << ")" ; |
153 | throw std::runtime_error(strm.str()); |
154 | } |
155 | return true; |
156 | } |
157 | |
158 | bool CCondition::timedwait(long timeout /*us*/) const { |
159 | if (timeout < 0) { |
160 | wait(); |
161 | return true; |
162 | } |
163 | |
164 | const int nsec_in_sec = 1000000000; |
165 | const int usec_in_sec = 1000000; |
166 | const int usec_in_nsec = 1000; |
167 | struct timeval tv; |
168 | struct timespec ts; |
169 | |
170 | gettimeofday(&tv, NULL); |
171 | |
172 | ts.tv_sec = tv.tv_sec + static_cast<long>(timeout / usec_in_sec); |
173 | ts.tv_nsec = (tv.tv_usec + static_cast<long>(timeout % usec_in_sec) * usec_in_nsec); |
174 | if (ts.tv_nsec > nsec_in_sec) { |
175 | ts.tv_sec += ts.tv_nsec / nsec_in_sec; |
176 | ts.tv_nsec = ts.tv_nsec % nsec_in_sec; |
177 | } |
178 | return timedwait(ts); |
179 | } |
180 | #endif |
181 | |
182 | } // namespace TPCE |
183 | |