Semaphore.h 759 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // Copyright @ 2014 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2014-12
  4. //
  5. #ifndef __SEMAPHORE_H
  6. #define __SEMAPHORE_H
  7. #include "Types.h"
  8. namespace tzc {
  9. class DECLDLL Semaphore {
  10. public:
  11. Semaphore(TZ_Uint32 iMax = 4096);
  12. ~Semaphore();
  13. void Signal();
  14. void Wait();
  15. TZ_Int32 Count();
  16. const TZ_Int32 Max();
  17. TZ_BOOL Wait(TZ_LONG lMseconds);
  18. private:
  19. #if defined(WIN32) || defined(WINDOWS)
  20. HANDLE hd;
  21. #elif defined(LINUX)
  22. pthread_mutex_t m_mutex;
  23. pthread_cond_t m_cond;
  24. #endif
  25. volatile TZ_Int32 m_iCount;
  26. const TZ_Int32 m_iMax;
  27. };
  28. inline TZ_Int32 Semaphore::Count()
  29. {
  30. return m_iCount;
  31. }
  32. inline const TZ_Int32 Semaphore::Max()
  33. {
  34. return m_iMax;
  35. }
  36. } // namespace tzc
  37. #endif /* ----- #ifndef __SEMAPHORE_H ----- */