diff options
Diffstat (limited to 'CPP/Windows/Synchronization.h')
| -rw-r--r-- | CPP/Windows/Synchronization.h | 50 |
1 files changed, 19 insertions, 31 deletions
diff --git a/CPP/Windows/Synchronization.h b/CPP/Windows/Synchronization.h index 7d2e8d2..afd03d2 100644 --- a/CPP/Windows/Synchronization.h +++ b/CPP/Windows/Synchronization.h | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | // Windows/Synchronization.h | 1 | // Windows/Synchronization.h |
| 2 | 2 | ||
| 3 | #ifndef __WINDOWS_SYNCHRONIZATION_H | 3 | #ifndef ZIP7_INC_WINDOWS_SYNCHRONIZATION_H |
| 4 | #define __WINDOWS_SYNCHRONIZATION_H | 4 | #define ZIP7_INC_WINDOWS_SYNCHRONIZATION_H |
| 5 | 5 | ||
| 6 | #include "../../C/Threads.h" | 6 | #include "../../C/Threads.h" |
| 7 | 7 | ||
| @@ -32,14 +32,14 @@ public: | |||
| 32 | WRes Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL, LPSECURITY_ATTRIBUTES sa = NULL) | 32 | WRes Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL, LPSECURITY_ATTRIBUTES sa = NULL) |
| 33 | { | 33 | { |
| 34 | _object = ::CreateEvent(sa, BoolToBOOL(manualReset), BoolToBOOL(initiallyOwn), name); | 34 | _object = ::CreateEvent(sa, BoolToBOOL(manualReset), BoolToBOOL(initiallyOwn), name); |
| 35 | if (name == NULL && _object != 0) | 35 | if (name == NULL && _object != NULL) |
| 36 | return 0; | 36 | return 0; |
| 37 | return ::GetLastError(); | 37 | return ::GetLastError(); |
| 38 | } | 38 | } |
| 39 | WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) | 39 | WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) |
| 40 | { | 40 | { |
| 41 | _object = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name); | 41 | _object = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name); |
| 42 | if (_object != 0) | 42 | if (_object != NULL) |
| 43 | return 0; | 43 | return 0; |
| 44 | return ::GetLastError(); | 44 | return ::GetLastError(); |
| 45 | } | 45 | } |
| @@ -227,8 +227,8 @@ public: | |||
| 227 | } | 227 | } |
| 228 | WRes Create() | 228 | WRes Create() |
| 229 | { | 229 | { |
| 230 | RINOK(::pthread_mutex_init(&_mutex, 0)); | 230 | RINOK(::pthread_mutex_init(&_mutex, NULL)) |
| 231 | WRes ret = ::pthread_cond_init(&_cond, 0); | 231 | const WRes ret = ::pthread_cond_init(&_cond, NULL); |
| 232 | _isValid = 1; | 232 | _isValid = 1; |
| 233 | return ret; | 233 | return ret; |
| 234 | } | 234 | } |
| @@ -246,8 +246,8 @@ public: | |||
| 246 | } | 246 | } |
| 247 | WRes LeaveAndSignal() | 247 | WRes LeaveAndSignal() |
| 248 | { | 248 | { |
| 249 | WRes res1 = ::pthread_cond_broadcast(&_cond); | 249 | const WRes res1 = ::pthread_cond_broadcast(&_cond); |
| 250 | WRes res2 = ::pthread_mutex_unlock(&_mutex); | 250 | const WRes res2 = ::pthread_mutex_unlock(&_mutex); |
| 251 | return (res2 ? res2 : res1); | 251 | return (res2 ? res2 : res1); |
| 252 | } | 252 | } |
| 253 | }; | 253 | }; |
| @@ -268,6 +268,7 @@ struct CBaseHandle_WFMO MY_UNCOPYABLE | |||
| 268 | CSynchro *_sync; | 268 | CSynchro *_sync; |
| 269 | 269 | ||
| 270 | CBaseHandle_WFMO(): _sync(NULL) {} | 270 | CBaseHandle_WFMO(): _sync(NULL) {} |
| 271 | virtual ~CBaseHandle_WFMO(); | ||
| 271 | 272 | ||
| 272 | operator CHandle_WFMO() { return this; } | 273 | operator CHandle_WFMO() { return this; } |
| 273 | virtual bool IsSignaledAndUpdate() = 0; | 274 | virtual bool IsSignaledAndUpdate() = 0; |
| @@ -283,7 +284,7 @@ public: | |||
| 283 | 284 | ||
| 284 | // bool IsCreated() { return (this->_sync != NULL); } | 285 | // bool IsCreated() { return (this->_sync != NULL); } |
| 285 | // CBaseEvent_WFMO() { ; } | 286 | // CBaseEvent_WFMO() { ; } |
| 286 | ~CBaseEvent_WFMO() { Close(); } | 287 | // ~CBaseEvent_WFMO() Z7_override { Close(); } |
| 287 | 288 | ||
| 288 | WRes Close() { this->_sync = NULL; return 0; } | 289 | WRes Close() { this->_sync = NULL; return 0; } |
| 289 | 290 | ||
| @@ -299,37 +300,30 @@ public: | |||
| 299 | 300 | ||
| 300 | WRes Set() | 301 | WRes Set() |
| 301 | { | 302 | { |
| 302 | RINOK(this->_sync->Enter()); | 303 | RINOK(this->_sync->Enter()) |
| 303 | this->_state = true; | 304 | this->_state = true; |
| 304 | return this->_sync->LeaveAndSignal(); | 305 | return this->_sync->LeaveAndSignal(); |
| 305 | } | 306 | } |
| 306 | 307 | ||
| 307 | WRes Reset() | 308 | WRes Reset() |
| 308 | { | 309 | { |
| 309 | RINOK(this->_sync->Enter()); | 310 | RINOK(this->_sync->Enter()) |
| 310 | this->_state = false; | 311 | this->_state = false; |
| 311 | return this->_sync->Leave(); | 312 | return this->_sync->Leave(); |
| 312 | } | 313 | } |
| 313 | 314 | ||
| 314 | virtual bool IsSignaledAndUpdate() | 315 | virtual bool IsSignaledAndUpdate() Z7_override; |
| 315 | { | ||
| 316 | if (this->_state == false) | ||
| 317 | return false; | ||
| 318 | if (this->_manual_reset == false) | ||
| 319 | this->_state = false; | ||
| 320 | return true; | ||
| 321 | } | ||
| 322 | }; | 316 | }; |
| 323 | 317 | ||
| 324 | 318 | ||
| 325 | class CManualResetEvent_WFMO: public CBaseEvent_WFMO | 319 | class CManualResetEvent_WFMO Z7_final: public CBaseEvent_WFMO |
| 326 | { | 320 | { |
| 327 | public: | 321 | public: |
| 328 | WRes Create(CSynchro *sync, bool initiallyOwn = false) { return CBaseEvent_WFMO::Create(sync, true, initiallyOwn); } | 322 | WRes Create(CSynchro *sync, bool initiallyOwn = false) { return CBaseEvent_WFMO::Create(sync, true, initiallyOwn); } |
| 329 | }; | 323 | }; |
| 330 | 324 | ||
| 331 | 325 | ||
| 332 | class CAutoResetEvent_WFMO: public CBaseEvent_WFMO | 326 | class CAutoResetEvent_WFMO Z7_final: public CBaseEvent_WFMO |
| 333 | { | 327 | { |
| 334 | public: | 328 | public: |
| 335 | WRes Create(CSynchro *sync) { return CBaseEvent_WFMO::Create(sync, false, false); } | 329 | WRes Create(CSynchro *sync) { return CBaseEvent_WFMO::Create(sync, false, false); } |
| @@ -340,7 +334,7 @@ public: | |||
| 340 | }; | 334 | }; |
| 341 | 335 | ||
| 342 | 336 | ||
| 343 | class CSemaphore_WFMO : public CBaseHandle_WFMO | 337 | class CSemaphore_WFMO Z7_final: public CBaseHandle_WFMO |
| 344 | { | 338 | { |
| 345 | UInt32 _count; | 339 | UInt32 _count; |
| 346 | UInt32 _maxCount; | 340 | UInt32 _maxCount; |
| @@ -365,11 +359,11 @@ public: | |||
| 365 | if (releaseCount < 1) | 359 | if (releaseCount < 1) |
| 366 | return EINVAL; | 360 | return EINVAL; |
| 367 | 361 | ||
| 368 | RINOK(this->_sync->Enter()); | 362 | RINOK(this->_sync->Enter()) |
| 369 | UInt32 newCount = this->_count + releaseCount; | 363 | UInt32 newCount = this->_count + releaseCount; |
| 370 | if (newCount > this->_maxCount) | 364 | if (newCount > this->_maxCount) |
| 371 | { | 365 | { |
| 372 | RINOK(this->_sync->Leave()); | 366 | RINOK(this->_sync->Leave()) |
| 373 | return ERROR_TOO_MANY_POSTS; // EINVAL | 367 | return ERROR_TOO_MANY_POSTS; // EINVAL |
| 374 | } | 368 | } |
| 375 | this->_count = newCount; | 369 | this->_count = newCount; |
| @@ -377,13 +371,7 @@ public: | |||
| 377 | return this->_sync->LeaveAndSignal(); | 371 | return this->_sync->LeaveAndSignal(); |
| 378 | } | 372 | } |
| 379 | 373 | ||
| 380 | virtual bool IsSignaledAndUpdate() | 374 | virtual bool IsSignaledAndUpdate() Z7_override; |
| 381 | { | ||
| 382 | if (this->_count == 0) | ||
| 383 | return false; | ||
| 384 | this->_count--; | ||
| 385 | return true; | ||
| 386 | } | ||
| 387 | }; | 375 | }; |
| 388 | 376 | ||
| 389 | #endif // _WIN32 | 377 | #endif // _WIN32 |
