Condition.hpp 3.1 KB

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