1 // Created by: Kirill Gavrilov
2 // Copyright (c) 2018 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
24 #include "Standard_Condition.hxx"
29 //! clock_gettime() wrapper.
30 static void conditionGetRealTime (struct timespec& theTime)
32 #if defined(__APPLE__)
34 gettimeofday (&aTime, NULL);
35 theTime.tv_sec = aTime.tv_sec;
36 theTime.tv_nsec = aTime.tv_usec * 1000;
38 clock_gettime (CLOCK_REALTIME, &theTime);
44 // =======================================================================
45 // function : Standard_Condition
47 // =======================================================================
48 Standard_Condition::Standard_Condition (bool theIsSet)
50 : myEvent((void* )::CreateEvent (0, true, theIsSet, NULL))
56 pthread_mutex_init(&myMutex, 0);
57 pthread_cond_init (&myCond, 0);
61 // =======================================================================
62 // function : ~Standard_Condition
64 // =======================================================================
65 Standard_Condition::~Standard_Condition()
68 ::CloseHandle ((HANDLE )myEvent);
70 pthread_mutex_destroy(&myMutex);
71 pthread_cond_destroy (&myCond);
75 // =======================================================================
78 // =======================================================================
79 void Standard_Condition::Set()
82 ::SetEvent ((HANDLE )myEvent);
84 pthread_mutex_lock(&myMutex);
86 pthread_cond_broadcast(&myCond);
87 pthread_mutex_unlock (&myMutex);
91 // =======================================================================
94 // =======================================================================
95 void Standard_Condition::Reset()
98 ::ResetEvent ((HANDLE )myEvent);
100 pthread_mutex_lock (&myMutex);
102 pthread_mutex_unlock (&myMutex);
106 // =======================================================================
109 // =======================================================================
110 void Standard_Condition::Wait()
113 ::WaitForSingleObject ((HANDLE )myEvent, INFINITE);
115 pthread_mutex_lock (&myMutex);
118 pthread_cond_wait (&myCond, &myMutex);
120 pthread_mutex_unlock (&myMutex);
124 // =======================================================================
127 // =======================================================================
128 bool Standard_Condition::Wait (int theTimeMilliseconds)
131 return (::WaitForSingleObject ((HANDLE )myEvent, (DWORD )theTimeMilliseconds) != WAIT_TIMEOUT);
133 bool isSignalled = true;
134 pthread_mutex_lock (&myMutex);
137 struct timespec aNow;
138 struct timespec aTimeout;
139 conditionGetRealTime (aNow);
140 aTimeout.tv_sec = (theTimeMilliseconds / 1000);
141 aTimeout.tv_nsec = (theTimeMilliseconds - aTimeout.tv_sec * 1000) * 1000000;
142 if (aTimeout.tv_nsec > 1000000000)
144 aTimeout.tv_sec += 1;
145 aTimeout.tv_nsec -= 1000000000;
147 aTimeout.tv_sec += aNow.tv_sec;
148 aTimeout.tv_nsec += aNow.tv_nsec;
149 isSignalled = (pthread_cond_timedwait (&myCond, &myMutex, &aTimeout) != ETIMEDOUT);
151 pthread_mutex_unlock (&myMutex);
156 // =======================================================================
159 // =======================================================================
160 bool Standard_Condition::Check()
163 return (::WaitForSingleObject ((HANDLE )myEvent, (DWORD )0) != WAIT_TIMEOUT);
165 bool isSignalled = true;
166 pthread_mutex_lock (&myMutex);
169 struct timespec aNow;
170 struct timespec aTimeout;
171 conditionGetRealTime (aNow);
172 aTimeout.tv_sec = aNow.tv_sec;
173 aTimeout.tv_nsec = aNow.tv_nsec + 100;
174 isSignalled = (pthread_cond_timedwait (&myCond, &myMutex, &aTimeout) != ETIMEDOUT);
176 pthread_mutex_unlock (&myMutex);
181 // =======================================================================
182 // function : CheckReset
184 // =======================================================================
185 bool Standard_Condition::CheckReset()
188 const bool wasSignalled = (::WaitForSingleObject ((HANDLE )myEvent, (DWORD )0) != WAIT_TIMEOUT);
189 ::ResetEvent ((HANDLE )myEvent);
192 pthread_mutex_lock (&myMutex);
193 bool wasSignalled = myFlag;
196 struct timespec aNow;
197 struct timespec aTimeout;
198 conditionGetRealTime (aNow);
199 aTimeout.tv_sec = aNow.tv_sec;
200 aTimeout.tv_nsec = aNow.tv_nsec + 100;
201 wasSignalled = (pthread_cond_timedwait (&myCond, &myMutex, &aTimeout) != ETIMEDOUT);
204 pthread_mutex_unlock (&myMutex);