00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _DECAF_UTIL_CONCURRENT_SEMAPHORE_H_
00019 #define _DECAF_UTIL_CONCURRENT_SEMAPHORE_H_
00020
00021 #include <decaf/util/Config.h>
00022
00023 #include <decaf/lang/exceptions/InterruptedException.h>
00024 #include <decaf/lang/exceptions/RuntimeException.h>
00025 #include <decaf/lang/exceptions/IllegalArgumentException.h>
00026
00027 #include <decaf/util/Collection.h>
00028 #include <decaf/util/concurrent/TimeUnit.h>
00029
00030 namespace decaf {
00031 namespace util {
00032 namespace concurrent {
00033
00034 class SemSync;
00035
00141 class DECAF_API Semaphore {
00142 private:
00143
00144 SemSync* sync;
00145
00146 private:
00147
00148 Semaphore( const Semaphore& );
00149 Semaphore& operator= ( const Semaphore& );
00150
00151 public:
00152
00159 Semaphore( int permits );
00160
00169 Semaphore( int permits, bool fair );
00170
00171 virtual ~Semaphore();
00172
00197 void acquire();
00198
00216 void acquireUninterruptibly();
00217
00236 bool tryAcquire();
00237
00274 bool tryAcquire( long long timeout, const TimeUnit& unit );
00275
00289 void release();
00290
00321 void acquire( int permits );
00322
00343 void acquireUninterruptibly( int permits );
00344
00369 bool tryAcquire( int permits );
00370
00413 bool tryAcquire( int permits, long long timeout, const TimeUnit& unit );
00414
00431 void release( int permits );
00432
00440 int availablePermits() const;
00441
00449 int drainPermits();
00450
00454 bool isFair() const;
00455
00462 std::string toString() const;
00463
00471 int getQueueLength() const;
00472
00476 bool hasQueuedThreads() const;
00477
00478 protected:
00479
00491 void reducePermits(int reduceBy);
00492
00500 decaf::util::Collection<decaf::lang::Thread*>* getQueuedThreads() const;
00501
00502 };
00503
00504 }}}
00505
00506 #endif