jsem.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. void getEndTime(timespec & abs, unsigned timeout)
  21. {
  22. timeval cur;
  23. gettimeofday(&cur, NULL);
  24. abs.tv_sec = cur.tv_sec + timeout/1000;
  25. abs.tv_nsec = (cur.tv_usec + timeout%1000*1000)*1000;
  26. if (abs.tv_nsec>=1000000000) {
  27. abs.tv_nsec-=1000000000;
  28. abs.tv_sec++;
  29. }
  30. }
  31. #ifndef USE_OLD_SEMAPHORE_CODE
  32. Semaphore::Semaphore(unsigned initialCount)
  33. {
  34. sem_init(&sem, 0, initialCount);
  35. }
  36. Semaphore::~Semaphore()
  37. {
  38. sem_destroy(&sem);
  39. }
  40. void Semaphore::reinit(unsigned initialCount)
  41. {
  42. sem_destroy(&sem);
  43. sem_init(&sem, 0, initialCount);
  44. }
  45. void Semaphore::wait()
  46. {
  47. sem_wait(&sem);
  48. }
  49. bool Semaphore::tryWait()
  50. {
  51. return sem_trywait(&sem);
  52. }
  53. bool Semaphore::wait(unsigned timeout)
  54. {
  55. if (timeout==(unsigned)-1) {
  56. sem_wait(&sem);
  57. return true;
  58. }
  59. //Ensure uncontended case is handled without calling gettimeofday
  60. if (sem_trywait(&sem) == 0)
  61. return true;
  62. if (timeout==0)
  63. return false;
  64. timespec abs;
  65. getEndTime(abs, timeout);
  66. int ret = sem_timedwait(&sem, &abs);
  67. if (ret < 0)
  68. return false;
  69. return true;
  70. }
  71. void Semaphore::signal()
  72. {
  73. sem_post(&sem);
  74. }
  75. void Semaphore::signal(unsigned n)
  76. {
  77. for (unsigned i=0; i < n; i++)
  78. sem_post(&sem);
  79. }
  80. #else
  81. //Old semaphore code based on condition variables.
  82. Semaphore::Semaphore(unsigned initialCount)
  83. {
  84. init();
  85. count = initialCount;
  86. }
  87. Semaphore::~Semaphore()
  88. {
  89. pthread_mutex_destroy(&mx);
  90. pthread_cond_destroy(&cond);
  91. }
  92. void Semaphore::init()
  93. {
  94. pthread_mutex_init(&mx, NULL);
  95. pthread_cond_init(&cond, NULL);
  96. }
  97. void Semaphore::reinit(unsigned initialCount)
  98. {
  99. pthread_mutex_lock(&mx);
  100. count = initialCount;
  101. pthread_mutex_unlock(&mx);
  102. }
  103. bool Semaphore::tryWait()
  104. {
  105. bool signalled = false;
  106. pthread_mutex_lock(&mx);
  107. if (count > 0)
  108. {
  109. count--;
  110. signalled = true;
  111. }
  112. pthread_mutex_unlock(&mx);
  113. return signalled;
  114. }
  115. void Semaphore::wait()
  116. {
  117. pthread_mutex_lock(&mx);
  118. if (--count<0)
  119. pthread_cond_wait(&cond, &mx);
  120. pthread_mutex_unlock(&mx);
  121. }
  122. bool Semaphore::wait(unsigned timeout)
  123. {
  124. if (timeout==(unsigned)-1) {
  125. wait();
  126. return true;
  127. }
  128. pthread_mutex_lock(&mx);
  129. if (--count<0) {
  130. timespec abs;
  131. timeval cur;
  132. gettimeofday(&cur, NULL);
  133. abs.tv_sec = cur.tv_sec + timeout/1000;
  134. abs.tv_nsec = (cur.tv_usec + timeout%1000*1000)*1000;
  135. if (abs.tv_nsec>=1000000000) {
  136. abs.tv_nsec-=1000000000;
  137. abs.tv_sec++;
  138. }
  139. if (pthread_cond_timedwait(&cond, &mx, &abs)==ETIMEDOUT) {
  140. count++; // not waiting
  141. pthread_mutex_unlock(&mx);
  142. return false;
  143. }
  144. }
  145. pthread_mutex_unlock(&mx);
  146. return true;
  147. }
  148. void Semaphore::signal()
  149. {
  150. pthread_mutex_lock(&mx);
  151. if (count++<0) // only signal if someone waiting
  152. pthread_cond_signal(&cond);
  153. pthread_mutex_unlock(&mx);
  154. }
  155. void Semaphore::signal(unsigned n)
  156. {
  157. pthread_mutex_lock(&mx);
  158. while ((count<0)&&n) {
  159. count++;
  160. pthread_cond_signal(&cond); // this shouldn't switch til mutex unlocked
  161. n--;
  162. }
  163. count += n;
  164. pthread_mutex_unlock(&mx);
  165. }
  166. #endif
  167. #endif