jsem.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. timespec abs;
  63. getEndTime(abs, timeout);
  64. int ret = sem_timedwait(&sem, &abs);
  65. if (ret < 0)
  66. return false;
  67. return true;
  68. }
  69. void Semaphore::signal()
  70. {
  71. sem_post(&sem);
  72. }
  73. void Semaphore::signal(unsigned n)
  74. {
  75. for (unsigned i=0; i < n; i++)
  76. sem_post(&sem);
  77. }
  78. #else
  79. //Old semaphore code based on condition variables.
  80. Semaphore::Semaphore(unsigned initialCount)
  81. {
  82. init();
  83. count = initialCount;
  84. }
  85. Semaphore::~Semaphore()
  86. {
  87. pthread_mutex_destroy(&mx);
  88. pthread_cond_destroy(&cond);
  89. }
  90. void Semaphore::init()
  91. {
  92. pthread_mutex_init(&mx, NULL);
  93. pthread_cond_init(&cond, NULL);
  94. }
  95. void Semaphore::reinit(unsigned initialCount)
  96. {
  97. pthread_mutex_lock(&mx);
  98. count = initialCount;
  99. pthread_mutex_unlock(&mx);
  100. }
  101. bool Semaphore::tryWait()
  102. {
  103. bool signalled = false;
  104. pthread_mutex_lock(&mx);
  105. if (count > 0)
  106. {
  107. count--;
  108. signalled = true;
  109. }
  110. pthread_mutex_unlock(&mx);
  111. return signalled;
  112. }
  113. void Semaphore::wait()
  114. {
  115. pthread_mutex_lock(&mx);
  116. if (--count<0)
  117. pthread_cond_wait(&cond, &mx);
  118. pthread_mutex_unlock(&mx);
  119. }
  120. bool Semaphore::wait(unsigned timeout)
  121. {
  122. if (timeout==(unsigned)-1) {
  123. wait();
  124. return true;
  125. }
  126. pthread_mutex_lock(&mx);
  127. if (--count<0) {
  128. timespec abs;
  129. timeval cur;
  130. gettimeofday(&cur, NULL);
  131. abs.tv_sec = cur.tv_sec + timeout/1000;
  132. abs.tv_nsec = (cur.tv_usec + timeout%1000*1000)*1000;
  133. if (abs.tv_nsec>=1000000000) {
  134. abs.tv_nsec-=1000000000;
  135. abs.tv_sec++;
  136. }
  137. if (pthread_cond_timedwait(&cond, &mx, &abs)==ETIMEDOUT) {
  138. count++; // not waiting
  139. pthread_mutex_unlock(&mx);
  140. return false;
  141. }
  142. }
  143. pthread_mutex_unlock(&mx);
  144. return true;
  145. }
  146. void Semaphore::signal()
  147. {
  148. pthread_mutex_lock(&mx);
  149. if (count++<0) // only signal if someone waiting
  150. pthread_cond_signal(&cond);
  151. pthread_mutex_unlock(&mx);
  152. }
  153. void Semaphore::signal(unsigned n)
  154. {
  155. pthread_mutex_lock(&mx);
  156. while ((count<0)&&n) {
  157. count++;
  158. pthread_cond_signal(&cond); // this shouldn't switch til mutex unlocked
  159. n--;
  160. }
  161. count += n;
  162. pthread_mutex_unlock(&mx);
  163. }
  164. #endif
  165. #endif