Condition.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2014 ZeroTier Networks LLC
  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_CONDITION_HPP
  28. #define ZT_CONDITION_HPP
  29. #include "../node/Constants.hpp"
  30. #include "../node/NonCopyable.hpp"
  31. #ifdef __WINDOWS__
  32. #include <Windows.h>
  33. #include <stdlib.h>
  34. namespace ZeroTier {
  35. class Condition : NonCopyable
  36. {
  37. public:
  38. Condition()
  39. throw()
  40. {
  41. _sem = CreateSemaphore(NULL,0,1,NULL);
  42. }
  43. ~Condition()
  44. {
  45. CloseHandle(_sem);
  46. }
  47. inline void wait() const
  48. throw()
  49. {
  50. WaitForSingleObject(_sem,INFINITE);
  51. }
  52. inline void wait(unsigned long ms) const
  53. throw()
  54. {
  55. if (ms)
  56. WaitForSingleObject(_sem,(DWORD)ms);
  57. else WaitForSingleObject(_sem,INFINITE);
  58. }
  59. inline void signal() const
  60. throw()
  61. {
  62. ReleaseSemaphore(_sem,1,NULL);
  63. }
  64. private:
  65. HANDLE _sem;
  66. };
  67. } // namespace ZeroTier
  68. #else // !__WINDOWS__
  69. #include <time.h>
  70. #include <stdlib.h>
  71. #include <pthread.h>
  72. #include "../node/Utils.hpp"
  73. namespace ZeroTier {
  74. class Condition : NonCopyable
  75. {
  76. public:
  77. Condition()
  78. throw()
  79. {
  80. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  81. pthread_cond_init(&_cond,(const pthread_condattr_t *)0);
  82. }
  83. ~Condition()
  84. {
  85. pthread_cond_destroy(&_cond);
  86. pthread_mutex_destroy(&_mh);
  87. }
  88. inline void wait() const
  89. throw()
  90. {
  91. pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
  92. pthread_cond_wait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh));
  93. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  94. }
  95. inline void wait(unsigned long ms) const
  96. throw()
  97. {
  98. uint64_t when = Utils::now() + (uint64_t)ms;
  99. struct timespec ts;
  100. ts.tv_sec = (unsigned long)(when / 1000);
  101. ts.tv_nsec = (unsigned long)(when % 1000) * 1000000;
  102. pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
  103. pthread_cond_timedwait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh),&ts);
  104. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  105. }
  106. inline void signal() const
  107. throw()
  108. {
  109. pthread_cond_signal(const_cast <pthread_cond_t *>(&_cond));
  110. }
  111. private:
  112. pthread_cond_t _cond;
  113. pthread_mutex_t _mh;
  114. };
  115. } // namespace ZeroTier
  116. #endif // !__WINDOWS__
  117. #endif