other-licenses/7zstub/src/Windows/Synchronization.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/Windows/Synchronization.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,114 @@
     1.4 +// Windows/Synchronization.h
     1.5 +
     1.6 +#ifndef __WINDOWS_SYNCHRONIZATION_H
     1.7 +#define __WINDOWS_SYNCHRONIZATION_H
     1.8 +
     1.9 +#include "Defs.h"
    1.10 +#include "Handle.h"
    1.11 +
    1.12 +namespace NWindows {
    1.13 +namespace NSynchronization {
    1.14 +
    1.15 +class CObject: public CHandle
    1.16 +{
    1.17 +public:
    1.18 +  bool Lock(DWORD timeoutInterval = INFINITE)
    1.19 +    { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0); }
    1.20 +};
    1.21 +
    1.22 +class CBaseEvent: public CObject
    1.23 +{
    1.24 +public:
    1.25 +  bool Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL,
    1.26 +      LPSECURITY_ATTRIBUTES securityAttributes = NULL)
    1.27 +  {
    1.28 +    _handle = ::CreateEvent(securityAttributes, BoolToBOOL(manualReset),
    1.29 +        BoolToBOOL(initiallyOwn), name);
    1.30 +    return (_handle != 0);
    1.31 +  }
    1.32 +
    1.33 +  bool Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name)
    1.34 +  {
    1.35 +    _handle = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name);
    1.36 +    return (_handle != 0);
    1.37 +  }
    1.38 +
    1.39 +  bool Set() { return BOOLToBool(::SetEvent(_handle)); }
    1.40 +  bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); }
    1.41 +  bool Reset() { return BOOLToBool(::ResetEvent(_handle)); }
    1.42 +};
    1.43 +
    1.44 +class CEvent: public CBaseEvent
    1.45 +{
    1.46 +public:
    1.47 +  CEvent() {};
    1.48 +  CEvent(bool manualReset, bool initiallyOwn, 
    1.49 +      LPCTSTR name = NULL, LPSECURITY_ATTRIBUTES securityAttributes = NULL);
    1.50 +};
    1.51 +
    1.52 +class CManualResetEvent: public CEvent
    1.53 +{
    1.54 +public:
    1.55 +  CManualResetEvent(bool initiallyOwn = false, LPCTSTR name = NULL, 
    1.56 +      LPSECURITY_ATTRIBUTES securityAttributes = NULL):
    1.57 +    CEvent(true, initiallyOwn, name, securityAttributes) {};
    1.58 +};
    1.59 +
    1.60 +class CAutoResetEvent: public CEvent
    1.61 +{
    1.62 +public:
    1.63 +  CAutoResetEvent(bool initiallyOwn = false, LPCTSTR name = NULL, 
    1.64 +      LPSECURITY_ATTRIBUTES securityAttributes = NULL):
    1.65 +    CEvent(false, initiallyOwn, name, securityAttributes) {};
    1.66 +};
    1.67 +
    1.68 +class CMutex: public CObject
    1.69 +{
    1.70 +public:
    1.71 +  bool Create(bool initiallyOwn, LPCTSTR name = NULL,
    1.72 +      LPSECURITY_ATTRIBUTES securityAttributes = NULL)
    1.73 +  {
    1.74 +    _handle = ::CreateMutex(securityAttributes, BoolToBOOL(initiallyOwn), name);
    1.75 +    return (_handle != 0);
    1.76 +  }
    1.77 +  bool Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name)
    1.78 +  {
    1.79 +    _handle = ::OpenMutex(desiredAccess, BoolToBOOL(inheritHandle), name);
    1.80 +    return (_handle != 0);
    1.81 +  }
    1.82 +  bool Release() { return BOOLToBool(::ReleaseMutex(_handle)); }
    1.83 +};
    1.84 +
    1.85 +class CMutexLock
    1.86 +{
    1.87 +  CMutex &_object;
    1.88 +public:
    1.89 +  CMutexLock(CMutex &object): _object(object) { _object.Lock(); } 
    1.90 +  ~CMutexLock() { _object.Release(); }
    1.91 +};
    1.92 +
    1.93 +class CCriticalSection
    1.94 +{
    1.95 +  CRITICAL_SECTION _object;
    1.96 +  // void Initialize() { ::InitializeCriticalSection(&_object); }
    1.97 +  // void Delete() { ::DeleteCriticalSection(&_object); }
    1.98 +public:
    1.99 +  CCriticalSection() { ::InitializeCriticalSection(&_object); }
   1.100 +  ~CCriticalSection() { ::DeleteCriticalSection(&_object); }
   1.101 +  void Enter() { ::EnterCriticalSection(&_object); }
   1.102 +  void Leave() { ::LeaveCriticalSection(&_object); }
   1.103 +};
   1.104 +
   1.105 +class CCriticalSectionLock
   1.106 +{
   1.107 +  CCriticalSection &_object;
   1.108 +  void Unlock()  { _object.Leave(); }
   1.109 +public:
   1.110 +  CCriticalSectionLock(CCriticalSection &object): _object(object) 
   1.111 +    {_object.Enter(); } 
   1.112 +  ~CCriticalSectionLock() { Unlock(); }
   1.113 +};
   1.114 +
   1.115 +}}
   1.116 +
   1.117 +#endif

mercurial