00001 #ifndef __J2K__PThread_Lock_HPP__
00002 #define __J2K__PThread_Lock_HPP__
00003
00004 /*
00005 * When constructed, this object acquires (by default) an
00006 * exclusive lock from the Mutex passed to it in its constructor.
00007 * When the object goes out of scope, it releases the lock. This
00008 * makes it convenient to synchronize whole methods, because the
00009 * lock will be released even if an exception is thrown. For
00010 * example:
00011 *
00012 * class Foo {
00013 * public:
00014 * void bar() {
00015 * Lock lock(mutex);
00016 * // do some stuff that could throw exceptions...
00017 * }
00018 *
00019 * private:
00020 * Mutex mutex;
00021 * };
00022 *
00023 * This class is not reference-counted; each instance is intended to be
00024 * encapsulated by one method, as a local variable.
00025 */
00026
00027 #include <j2k/Fred/Boolean.hpp>
00028 #include <j2k/Fred/StdTypes.hpp>
00029
00030 class Lock {
00031 public:
00032 Lock(Mutex& mutex, BOOL autoLock = TRUE);
00033
00034 // Releases the Mutex's lock, if it holds one.
00035 virtual ~Lock();
00036
00037 // Get the Mutex's lock, if it doesn't hold one.
00038 void get();
00039
00040 // Releases the Mutex's lock, if it holds one.
00041 void release();
00042
00043 /* Waits for another thread to call notify() or notifyAll()
00044 * on a Lock for the same Mutex.
00045 * The calling thread must already have the lock.
00046 */
00047 void wait();
00048
00049 /* Notifies one thread waiting for this mutex.
00050 * The calling thread must already have the lock.
00051 */
00052 void notify();
00053
00054 /* Notifies all threads waiting for this mutex.
00055 * The calling thread must already have the lock.
00056 */
00057 void notifyAll();
00058
00059 private:
00060 Lock();
00061 Lock(const Lock&);
00062 Lock& operator=(Lock&);
00063
00064 Mutex& mutex;
00065 BOOL locked;
00066 };
00067
00068 #endif
1.2.11.1 written by Dimitri van Heesch,
© 1997-2001