Condition.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "NonCopyable.hpp"
  30. #if defined(__APPLE__) || defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  31. #include <time.h>
  32. #include <stdlib.h>
  33. #include <pthread.h>
  34. #include "Utils.hpp"
  35. namespace ZeroTier {
  36. class Condition : NonCopyable
  37. {
  38. public:
  39. Condition()
  40. throw()
  41. {
  42. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  43. pthread_cond_init(&_cond,(const pthread_condattr_t *)0);
  44. }
  45. ~Condition()
  46. {
  47. pthread_cond_destroy(&_cond);
  48. pthread_mutex_destroy(&_mh);
  49. }
  50. inline void wait() const
  51. throw()
  52. {
  53. pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
  54. pthread_cond_wait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh));
  55. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  56. }
  57. inline void wait(unsigned long ms) const
  58. throw()
  59. {
  60. uint64_t when = Utils::now() + (uint64_t)ms;
  61. struct timespec ts;
  62. ts.tv_sec = (unsigned long)(when / 1000);
  63. ts.tv_nsec = (unsigned long)(when % 1000) * 1000000;
  64. pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
  65. pthread_cond_timedwait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh),&ts);
  66. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  67. }
  68. inline void signal() const
  69. throw()
  70. {
  71. pthread_cond_signal(const_cast <pthread_cond_t *>(&_cond));
  72. }
  73. private:
  74. pthread_cond_t _cond;
  75. pthread_mutex_t _mh;
  76. };
  77. } // namespace ZeroTier
  78. #endif // Apple / Linux
  79. #ifdef _WIN32
  80. #include <stdlib.h>
  81. #include <Windows.h>
  82. namespace ZeroTier {
  83. error need windoze;
  84. // On Windows this will probably be implemented via Semaphores
  85. } // namespace ZeroTier
  86. #endif // _WIN32
  87. #endif