BinarySemaphore.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_BINARYSEMAPHORE_HPP
  28. #define ZT_BINARYSEMAPHORE_HPP
  29. #include <stdio.h>
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include "Constants.hpp"
  33. #include "NonCopyable.hpp"
  34. #ifdef __WINDOWS__
  35. #include <Windows.h>
  36. namespace ZeroTier {
  37. class BinarySemaphore : NonCopyable
  38. {
  39. public:
  40. BinarySemaphore() throw() { _sem = CreateSemaphore(NULL,0,1,NULL); }
  41. ~BinarySemaphore() { CloseHandle(_sem); }
  42. inline void wait() { WaitForSingleObject(_sem,INFINITE); }
  43. inline void post() { ReleaseSemaphore(_sem,1,NULL); }
  44. private:
  45. HANDLE _sem;
  46. };
  47. } // namespace ZeroTier
  48. #else // !__WINDOWS__
  49. #include <pthread.h>
  50. namespace ZeroTier {
  51. class BinarySemaphore : NonCopyable
  52. {
  53. public:
  54. BinarySemaphore()
  55. {
  56. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  57. pthread_cond_init(&_cond,(const pthread_condattr_t *)0);
  58. _f = false;
  59. }
  60. ~BinarySemaphore()
  61. {
  62. pthread_cond_destroy(&_cond);
  63. pthread_mutex_destroy(&_mh);
  64. }
  65. inline void wait()
  66. {
  67. pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
  68. while (!_f)
  69. pthread_cond_wait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh));
  70. _f = false;
  71. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  72. }
  73. inline void post()
  74. {
  75. pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
  76. _f = true;
  77. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  78. pthread_cond_signal(const_cast <pthread_cond_t *>(&_cond));
  79. }
  80. private:
  81. pthread_cond_t _cond;
  82. pthread_mutex_t _mh;
  83. volatile bool _f;
  84. };
  85. } // namespace ZeroTier
  86. #endif // !__WINDOWS__
  87. #endif