jsem.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jlib.hpp"
  14. #include "jsem.hpp"
  15. #include "jisem.hpp"
  16. #include "jmutex.hpp"
  17. #ifndef _WIN32
  18. #include <sys/time.h>
  19. #include <semaphore.h>
  20. #ifndef USE_OLD_SEMAPHORE_CODE
  21. Semaphore::Semaphore(unsigned initialCount)
  22. {
  23. sem_init(&sem, 0, initialCount);
  24. }
  25. Semaphore::~Semaphore()
  26. {
  27. sem_destroy(&sem);
  28. }
  29. void Semaphore::reinit(unsigned initialCount)
  30. {
  31. sem_destroy(&sem);
  32. sem_init(&sem, 0, initialCount);
  33. }
  34. void Semaphore::wait()
  35. {
  36. sem_wait(&sem);
  37. }
  38. bool Semaphore::wait(unsigned timeout)
  39. {
  40. if (timeout==(unsigned)-1) {
  41. sem_wait(&sem);
  42. return true;
  43. }
  44. //Ensure uncontended case is handled without calling gettimeofday
  45. if (sem_trywait(&sem) == 0)
  46. return true;
  47. timeval cur;
  48. gettimeofday(&cur, NULL);
  49. timespec abs;
  50. abs.tv_sec = cur.tv_sec + timeout/1000;
  51. abs.tv_nsec = (cur.tv_usec + timeout%1000*1000)*1000;
  52. if (abs.tv_nsec>=1000000000) {
  53. abs.tv_nsec-=1000000000;
  54. abs.tv_sec++;
  55. }
  56. int ret = sem_timedwait(&sem, &abs);
  57. if (ret < 0)
  58. return false;
  59. return true;
  60. }
  61. void Semaphore::signal()
  62. {
  63. sem_post(&sem);
  64. }
  65. void Semaphore::signal(unsigned n)
  66. {
  67. for (unsigned i=0; i < n; i++)
  68. sem_post(&sem);
  69. }
  70. #else
  71. //Old semaphore code based on condition variables.
  72. Semaphore::Semaphore(unsigned initialCount)
  73. {
  74. init();
  75. count = initialCount;
  76. }
  77. Semaphore::~Semaphore()
  78. {
  79. pthread_mutex_destroy(&mx);
  80. pthread_cond_destroy(&cond);
  81. }
  82. void Semaphore::init()
  83. {
  84. pthread_mutex_init(&mx, NULL);
  85. pthread_cond_init(&cond, NULL);
  86. }
  87. void Semaphore::reinit(unsigned initialCount)
  88. {
  89. pthread_mutex_lock(&mx);
  90. count = initialCount;
  91. pthread_mutex_unlock(&mx);
  92. }
  93. void Semaphore::wait()
  94. {
  95. pthread_mutex_lock(&mx);
  96. if (--count<0)
  97. pthread_cond_wait(&cond, &mx);
  98. pthread_mutex_unlock(&mx);
  99. }
  100. bool Semaphore::wait(unsigned timeout)
  101. {
  102. if (timeout==(unsigned)-1) {
  103. wait();
  104. return true;
  105. }
  106. pthread_mutex_lock(&mx);
  107. if (--count<0) {
  108. timespec abs;
  109. timeval cur;
  110. gettimeofday(&cur, NULL);
  111. abs.tv_sec = cur.tv_sec + timeout/1000;
  112. abs.tv_nsec = (cur.tv_usec + timeout%1000*1000)*1000;
  113. if (abs.tv_nsec>=1000000000) {
  114. abs.tv_nsec-=1000000000;
  115. abs.tv_sec++;
  116. }
  117. if (pthread_cond_timedwait(&cond, &mx, &abs)==ETIMEDOUT) {
  118. count++; // not waiting
  119. pthread_mutex_unlock(&mx);
  120. return false;
  121. }
  122. }
  123. pthread_mutex_unlock(&mx);
  124. return true;
  125. }
  126. void Semaphore::signal()
  127. {
  128. pthread_mutex_lock(&mx);
  129. if (count++<0) // only signal if someone waiting
  130. pthread_cond_signal(&cond);
  131. pthread_mutex_unlock(&mx);
  132. }
  133. void Semaphore::signal(unsigned n)
  134. {
  135. pthread_mutex_lock(&mx);
  136. while ((count<0)&&n) {
  137. count++;
  138. pthread_cond_signal(&cond); // this shouldn't switch til mutex unlocked
  139. n--;
  140. }
  141. count += n;
  142. pthread_mutex_unlock(&mx);
  143. }
  144. #endif
  145. #endif