Condition.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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. WaitForSingleObject(_sem,(DWORD)ms);
  57. }
  58. inline void signal() const
  59. throw()
  60. {
  61. ReleaseSemaphore(_sem,1,NULL);
  62. }
  63. private:
  64. HANDLE _sem;
  65. };
  66. } // namespace ZeroTier
  67. #else // !__WINDOWS__
  68. #include <time.h>
  69. #include <stdlib.h>
  70. #include <pthread.h>
  71. #include "Utils.hpp"
  72. namespace ZeroTier {
  73. class Condition : NonCopyable
  74. {
  75. public:
  76. Condition()
  77. throw()
  78. {
  79. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  80. pthread_cond_init(&_cond,(const pthread_condattr_t *)0);
  81. }
  82. ~Condition()
  83. {
  84. pthread_cond_destroy(&_cond);
  85. pthread_mutex_destroy(&_mh);
  86. }
  87. inline void wait() const
  88. throw()
  89. {
  90. pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
  91. pthread_cond_wait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh));
  92. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  93. }
  94. inline void wait(unsigned long ms) const
  95. throw()
  96. {
  97. uint64_t when = Utils::now() + (uint64_t)ms;
  98. struct timespec ts;
  99. ts.tv_sec = (unsigned long)(when / 1000);
  100. ts.tv_nsec = (unsigned long)(when % 1000) * 1000000;
  101. pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
  102. pthread_cond_timedwait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh),&ts);
  103. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  104. }
  105. inline void signal() const
  106. throw()
  107. {
  108. pthread_cond_signal(const_cast <pthread_cond_t *>(&_cond));
  109. }
  110. private:
  111. pthread_cond_t _cond;
  112. pthread_mutex_t _mh;
  113. };
  114. } // namespace ZeroTier
  115. #endif // !__WINDOWS__
  116. #endif