home *** CD-ROM | disk | FTP | other *** search
- /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Copyright (C) 1994, by WATCOM International Inc. All rights %
- % reserved. No part of this software may be reproduced or %
- % used in any form or by any means - graphic, electronic or %
- % mechanical, including photocopying, recording, taping or %
- % information storage and retrieval systems - except with the %
- % written permission of WATCOM International Inc. %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- */
-
- #ifndef _WSEMAPHR_HPP_INCLUDED
- #define _WSEMAPHR_HPP_INCLUDED
-
- #define _WSEMAPHORE_HPP_INCLUDED
-
- #ifndef _WNO_PRAGMA_PUSH
- #pragma pack(push,8);
- #pragma enum int;
- #endif
-
- //
- // Notes on semaphore usage:
- //
- // The easy way to do mutual exclusion inside a function is as follows:
- //
- // 1) Declare a semaphore of the appropriate type as a static or
- // global.
- // 2) Declare a WSemaphoreLock as an auto, pointing it to the
- // semaphore.
- //
- // void myclass::myfunc()
- // {
- // static WCriticalSection critSect;
- // WSemaphoreLock lock( &critSect );
- //
- // .....
- // }
- //
- // Then each time you enter the function, the semaphore is locked
- // and then automatically unlocked when you leave.
- //
- // Alternatively, you can do this:
- //
- // static WCriticalSection critSect;
- //
- // critSect.Enter();
- //
- // critSect.Exit();
- //
- // But always declare your critical sections as static variables!
- //
-
- #ifndef _WDEF_HPP_INCLUDED
- # include "wdef.hpp"
- #endif
- #ifndef _WOBJECT_HPP_INCLUDED
- # include "wobject.hpp"
- #endif
-
- enum WSemaphoreHandle { NULLHSEMAPHORE = 0, LASTHSEMAPHORE = LAST_16TO32BIT };
-
- //
- // WBaseSemaphore
- //
- // The abstract base class for all the semaphores.
- //
-
- class WCMCLASS WBaseSemaphore : public WObject {
- WDeclareSubclass( WBaseSemaphore, WObject );
-
- friend class WSemaphoreLock;
-
- /***********************************************************
- * Constructors and Destructors
- ***********************************************************/
-
- protected:
-
- WBaseSemaphore( WSemaphoreHandle handle=NULLHSEMAPHORE,
- WBool canDelete=TRUE )
- { _handle = handle; _canDelete = canDelete; }
-
- public:
-
- virtual ~WBaseSemaphore() {}
-
- protected:
-
- /***********************************************************
- * Properties
- ***********************************************************/
-
- public:
-
- // Handle
- //
- // Returns the system handle associated with the
- // semaphore.
-
- WSemaphoreHandle GetHandle() const { return _handle; }
-
- /***********************************************************
- * Others
- ***********************************************************/
-
- // WSemaphoreLock calls these to acquire/release a lock.
-
- // Lock
-
- virtual WBool Lock( WULong timeout=0xFFFFFFFF ) = 0;
-
- // Unlock
-
- virtual WBool Unlock() = 0;
-
- /***********************************************************
- * Data Members
- ***********************************************************/
-
- protected:
-
- WBool _canDelete;
- WSemaphoreHandle _handle;
-
- };
-
- //
- // WCriticalSection
- //
- // Defines a critical section.
- //
-
- class WCMCLASS WCriticalSection : public WBaseSemaphore {
- WDeclareSubclass( WCriticalSection, WBaseSemaphore );
-
- public:
-
- /***********************************************************
- * Constructors and Destructors
- ***********************************************************/
-
- WCriticalSection( WBool allowTimeouts=FALSE );
-
- virtual ~WCriticalSection();
-
- /***********************************************************
- * Properties
- ***********************************************************/
-
- /***********************************************************
- * Methods
- ***********************************************************/
-
- // Enter
-
- WBool Enter( WULong timeout=0xFFFFFFFF ) { return Lock( timeout ); }
-
- // Exit
-
- WBool Exit() { return Unlock(); }
-
- /***********************************************************
- * Overrides
- ***********************************************************/
-
- protected:
-
- virtual WBool Lock( WULong timeout=0xFFFFFFFF );
-
- virtual WBool Unlock();
-
- /***********************************************************
- * Static
- ***********************************************************/
-
- public:
-
- static WCriticalSection & GetDefaultCriticalSection();
-
- /***********************************************************
- * Data
- ***********************************************************/
-
- private:
-
- WBool _allowTimeouts;
- };
-
- //
- // WMutexSemaphore
- //
- // Defines a mutual-exclusion semaphore.
- //
-
- class WCMCLASS WMutexSemaphore : public WBaseSemaphore {
- WDeclareSubclass( WMutexSemaphore, WBaseSemaphore );
-
- public:
-
- /***********************************************************
- * Constructors and Destructors
- ***********************************************************/
-
- WMutexSemaphore();
-
- WMutexSemaphore( const WChar *name, WBool own=FALSE );
-
- WMutexSemaphore( WSemaphoreHandle handle, WBool canDelete=FALSE );
-
- virtual ~WMutexSemaphore();
-
- /***********************************************************
- * Properties
- ***********************************************************/
-
- /***********************************************************
- * Methods
- ***********************************************************/
-
- // Acquire
- //
- // Wait for a semaphore to be acquired/signalled, with
- // a timeout in milliseconds. By default the timeout
- // is infinite.
-
- WBool Acquire( WULong timeout=0xFFFFFFFF );
-
- // Close
- //
- // Closes the semaphore.
-
- WBool Close();
-
- // Create
- //
- // Creates a new mutex semaphore of the given name. If
- // the sem already exists, simply returns a handle to
- // it and "own" is ignored, otherwise if "own" is true
- // then the semaphore is initially owned by this thread.
-
- WBool Create( const WChar *name, WBool own=FALSE );
-
- // Open
- //
- // Open a semaphore that already exists.
-
- WBool Open( const WChar *name );
-
- // Release
- //
- // Release a semaphore after acquiring it.
- //
-
- WBool Release();
-
- /***********************************************************
- * Overrides
- ***********************************************************/
-
- protected:
-
- virtual WBool Lock( WULong t=0xFFFFFFFF ) { return Acquire( t ); }
- virtual WBool Unlock() { return Release(); }
- };
-
- //
- // WCountSemaphore
- //
- // Defines a counting semaphore.
- //
-
- class WCMCLASS WCountSemaphore : public WBaseSemaphore {
- WDeclareSubclass( WCountSemaphore, WBaseSemaphore );
-
- public:
-
- /***********************************************************
- * Constructors and Destructors
- ***********************************************************/
-
- WCountSemaphore();
-
- WCountSemaphore( const WChar * name, WLong initialCount=1,
- WLong maximumCount=1 );
-
- virtual ~WCountSemaphore();
-
- /***********************************************************
- * Properties
- ***********************************************************/
-
- /***********************************************************
- * Methods
- ***********************************************************/
-
- // Acquire
- //
- // Wait for a semaphore to be acquired/signalled, with
- // a timeout in milliseconds. By default the timeout
- // is infinite.
-
- WBool Acquire( WULong timeout=0xFFFFFFFF );
-
- // Close
- //
- // Closes the semaphore.
-
- WBool Close();
-
- // Create
- //
- // Creates a new count semaphore of the given name. If
- // the sem already exists, simply returns a handle to
- // it and "initialCount" and "maximumCount" are ignored,
- // otherwise "initialCount" specifies an initial count for
- // the WCountSemaphore object and "maximumCount" specifies
- // the maximum count for the WCountSemaphore object.
- // "maximumCount" must be greater than zero and "initialCount"
- // must be greater than or equal to zero and less than or equal
- // to "maximumCount".
-
- WBool Create( const WChar * name, WLong initialCount=1,
- WLong maximumCount=1 );
-
- // Open
- //
- // Open a semaphore that already exists.
-
- WBool Open( const WChar *name );
-
- // Release
- //
- // Release a semaphore after acquiring it.
- //
-
- WBool Release();
-
- // Wait
- //
- // Wait on an count semaphore. Can specify a timeout in
- // milliseconds, which is set to infinite by default.
-
- WBool Wait( WULong timeout=0xFFFFFFFF );
-
- /***********************************************************
- * Overrides
- ***********************************************************/
-
- protected:
-
- virtual WBool Lock( WULong timeout=0xFFFFFFFF );
- virtual WBool Unlock();
- };
-
- //
- // WEventSemaphore
- //
- // Defines an event semaphore.
- //
-
- class WCMCLASS WEventSemaphore : public WBaseSemaphore {
- WDeclareSubclass( WEventSemaphore, WBaseSemaphore );
-
- public:
-
- /***********************************************************
- * Constructors and Destructors
- ***********************************************************/
-
- WEventSemaphore();
-
- WEventSemaphore( const WChar *name, WBool initialReset=TRUE,
- WBool manualReset=TRUE );
-
- WEventSemaphore( WSemaphoreHandle handle, WBool canDelete=FALSE );
-
- virtual ~WEventSemaphore();
-
- /***********************************************************
- * Properties
- ***********************************************************/
-
- /***********************************************************
- * Methods
- ***********************************************************/
-
- // Close
- //
- // Closes the semaphore.
-
- WBool Close();
-
- // Create
- //
- // Creates a new event semaphore of the given name. If
- // the sem already exists, simply returns a handle to
- // it and the other parms are ignored. The initiallyReset
- // parm controls the initial state. If manualReset is
- // TRUE, then the semaphore must be explicitly reset after
- // it is triggered, otherwise a single thread is allowed
- // to proceed before the semaphore resets itself.
-
- WBool Create( const WChar *name, WBool initiallyReset=TRUE,
- WBool manualReset=TRUE );
-
- // Open
- //
- // Open a semaphore that already exists.
-
- WBool Open( const WChar *name );
-
- // Pulse
- //
- // "Pulses" the semaphore: sets it to release any
- // waiting threads (or a single thread if autoreset)
- // and then resets it.
-
- WBool Pulse();
-
- // Reset
- //
- // Sets the semaphore to the "reset" state, which means
- // any thread that waits on it will block.
-
- WBool Reset();
-
- // Set
- //
- // Sets the semaphore to the "set" state, which releases
- // any waiting threads.
-
- WBool Set();
-
- // Wait
- //
- // Wait on an event semaphore. Can specify a timeout in
- // milliseconds, which is set to infinite by default.
- // If blockMessageProcessing is TRUE, then no messages will be
- // responded to by this thread until the call to Wait() finishes.
-
- WBool Wait( WULong timeout=0xFFFFFFFF,
- WBool blockMessageProcessing=TRUE );
-
- /***********************************************************
- * Overrides
- ***********************************************************/
-
- protected:
-
- virtual WBool Lock( WULong t=0xFFFFFFFF );
- virtual WBool Unlock();
- };
-
- //
- // WSemaphoreLock
- //
- // A convenience class which encapsulates waiting for a
- // semaphore to be acquired and then releasing it on destruction.
- //
-
- class WCMCLASS WSemaphoreLock : public WObject {
- WDeclareSubclass( WSemaphoreLock, WObject );
-
- public:
-
- /***********************************************************
- * Constructors and Destructors
- ***********************************************************/
-
- WSemaphoreLock( WBaseSemaphore *semaphore );
-
- WSemaphoreLock( WBaseSemaphore & semaphore );
-
- virtual ~WSemaphoreLock();
-
- /***********************************************************
- * Properties
- ***********************************************************/
-
- /***********************************************************
- * Methods
- ***********************************************************/
-
- // Unlock
- //
- // Prematurely release the semaphore.
-
- WBool Unlock();
-
- /***********************************************************
- * Data Members
- ***********************************************************/
-
- private:
-
- WBaseSemaphore *_semaphore;
- };
-
- #ifndef _WNO_PRAGMA_PUSH
- #pragma enum pop;
- #pragma pack(pop);
- #endif
-
- #endif // _WSEMAPHORE_HPP_INCLUDED
-