00001 /* 00002 * psync.h 00003 * 00004 * Abstract synchronisation semaphore class. 00005 * 00006 * Portable Windows Library 00007 * 00008 * Copyright (c) 1993-1998 Equivalence Pty. Ltd. 00009 * Copyright (c) 2005 Post Increment 00010 * 00011 * The contents of this file are subject to the Mozilla Public License 00012 * Version 1.0 (the "License"); you may not use this file except in 00013 * compliance with the License. You may obtain a copy of the License at 00014 * http://www.mozilla.org/MPL/ 00015 * 00016 * Software distributed under the License is distributed on an "AS IS" 00017 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00018 * the License for the specific language governing rights and limitations 00019 * under the License. 00020 * 00021 * The Original Code is Portable Windows Library. 00022 * 00023 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 00024 * 00025 * Portions are Copyright (C) 1993 Free Software Foundation, Inc. 00026 * All Rights Reserved. 00027 * 00028 * Contributor(s): ______________________________________. 00029 * 00030 * $Log: psync.h,v $ 00031 * Revision 1.7 2007/08/17 07:29:21 csoutheren 00032 * Fix build on MacOSX 00033 * 00034 * Revision 1.6 2007/08/17 05:29:19 csoutheren 00035 * Add field to Linux showing locking thread to assist in debugging 00036 * 00037 * Revision 1.5 2007/05/07 14:05:09 csoutheren 00038 * Add PSyncNULL 00039 * 00040 * Revision 1.4 2005/11/25 03:43:47 csoutheren 00041 * Fixed function argument comments to be compatible with Doxygen 00042 * 00043 * Revision 1.3 2005/11/14 22:29:13 csoutheren 00044 * Reverted Wait and Signal to non-const - there is no way we can guarantee that all 00045 * descendant classes everywhere will be changed over, so we have to keep the 00046 * original API 00047 * 00048 * Revision 1.2 2005/11/04 06:56:10 csoutheren 00049 * Added new class PSync as abstract base class for all mutex/sempahore classes 00050 * Changed PCriticalSection to use Wait/Signal rather than Enter/Leave 00051 * Changed Wait/Signal to be const member functions 00052 * Renamed PMutex to PTimedMutex and made PMutex synonym for PCriticalSection. 00053 * This allows use of very efficient mutex primitives in 99% of cases where timed waits 00054 * are not needed 00055 * 00056 * Revision 1.1 2005/11/04 06:34:20 csoutheren 00057 * Added new class PSync as abstract base class for all mutex/sempahore classes 00058 * Changed PCriticalSection to use Wait/Signal rather than Enter/Leave 00059 * Changed Wait/Signal to be const member functions 00060 * Renamed PMutex to PTimedMutex and made PMutex synonym for PCriticalSection. 00061 * This allows use of very efficient mutex primitives in 99% of cases where timed waits 00062 * are not needed 00063 * 00064 */ 00065 00066 #ifndef _PSYNC 00067 #define _PSYNC 00068 00069 #ifdef P_USE_PRAGMA 00070 #pragma interface 00071 #endif 00072 00073 #include <ptlib/contain.h> 00074 00075 class PSync : public PObject 00076 { 00077 public: 00082 virtual void Wait() = 0; 00083 00086 virtual void Signal() = 0; 00088 00089 #ifdef P_PTHREADS 00090 PSync() 00091 : lockerId(pthread_t(-1)) { } 00092 protected: 00093 pthread_t lockerId; 00094 #endif 00095 }; 00096 00097 class PSyncNULL : public PSync 00098 { 00099 public: 00100 virtual void Wait() { } 00101 virtual void Signal() { } 00102 }; 00103 00123 class PWaitAndSignal { 00124 public: 00129 inline PWaitAndSignal( 00130 const PSync & sem, 00131 BOOL wait = TRUE 00132 ) : sync((PSync &)sem) 00133 { if (wait) sync.Wait(); } 00134 00139 ~PWaitAndSignal() 00140 { sync.Signal(); } 00141 00142 protected: 00143 PSync & sync; 00144 }; 00145 00146 #endif // PSYNC 00147