2
0

psemaphore.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Filename: psemaphore.h
  2. // Created by: drose (13Oct08)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #ifndef PSEMAPHORE_H
  15. #define PSEMAPHORE_H
  16. #include "pandabase.h"
  17. #include "pmutex.h"
  18. #include "conditionVar.h"
  19. ////////////////////////////////////////////////////////////////////
  20. // Class : Semaphore
  21. // Description : A classic semaphore synchronization primitive.
  22. //
  23. // A semaphore manages an internal counter which is
  24. // decremented by each acquire() call and incremented by
  25. // each release() call. The counter can never go below
  26. // zero; when acquire() finds that it is zero, it
  27. // blocks, waiting until some other thread calls
  28. // release().
  29. ////////////////////////////////////////////////////////////////////
  30. class EXPCL_PANDA_PIPELINE Semaphore {
  31. PUBLISHED:
  32. INLINE Semaphore(int initial_count = 1);
  33. INLINE ~Semaphore();
  34. private:
  35. INLINE Semaphore(const Semaphore &copy);
  36. INLINE void operator = (const Semaphore &copy);
  37. PUBLISHED:
  38. BLOCKING INLINE void acquire();
  39. BLOCKING INLINE bool try_acquire();
  40. INLINE int release();
  41. INLINE int get_count() const;
  42. void output(ostream &out) const;
  43. private:
  44. Mutex _lock;
  45. ConditionVar _cvar;
  46. int _count;
  47. };
  48. INLINE ostream &
  49. operator << (ostream &out, const Semaphore &sem) {
  50. sem.output(out);
  51. return out;
  52. }
  53. #include "psemaphore.I"
  54. #endif