jsem.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef __JSEM__
  14. #define __JSEM__
  15. #include "jiface.hpp"
  16. #ifdef _WIN32
  17. class jlib_decl Semaphore
  18. {
  19. protected:
  20. Semaphore(const char *name)
  21. {
  22. hSem = CreateSemaphore(NULL, 0, 0x7fffffff, name);
  23. }
  24. public:
  25. Semaphore(unsigned initialCount = 0U)
  26. {
  27. hSem = CreateSemaphore(NULL, initialCount, 0x7fffffff, NULL);
  28. }
  29. ~Semaphore()
  30. {
  31. CloseHandle(hSem);
  32. }
  33. bool tryWait()
  34. {
  35. return (WaitForSingleObject(hSem, 0) == WAIT_OBJECT_0);
  36. }
  37. void wait()
  38. {
  39. WaitForSingleObject(hSem, INFINITE);
  40. }
  41. void reinit(unsigned initialCount=0U)
  42. {
  43. CloseHandle(hSem);
  44. hSem = CreateSemaphore(NULL, initialCount, 0x7fffffff, NULL);
  45. }
  46. bool wait(unsigned timeout)
  47. {
  48. return (WaitForSingleObject(hSem, (timeout==(unsigned)-1)?INFINITE:timeout)==WAIT_OBJECT_0);
  49. }
  50. void signal()
  51. {
  52. ReleaseSemaphore(hSem,1,NULL);
  53. }
  54. void signal(unsigned count)
  55. {
  56. ReleaseSemaphore(hSem,count,NULL);
  57. }
  58. protected:
  59. HANDLE hSem;
  60. };
  61. class jlib_decl Semaphore_Array
  62. {
  63. public:
  64. Semaphore_Array(int num_sem, unsigned initialCount = 0U)
  65. { hSem = new HANDLE [num_sem];
  66. sem_count=num_sem;
  67. for (int i=0; i<num_sem; i++) {
  68. hSem[i] = CreateSemaphore(NULL, initialCount, 0x7fffffff, NULL);
  69. }
  70. }
  71. ~Semaphore_Array()
  72. { for (int i =0; i< sem_count; i++) {
  73. CloseHandle(hSem[i]);
  74. }
  75. delete [] hSem;
  76. }
  77. int wait_one_of()
  78. {
  79. return WaitForMultipleObjects(sem_count,hSem, false, INFINITE);
  80. }
  81. int wait_one_of(unsigned timeout)
  82. {
  83. return WaitForMultipleObjects(sem_count,hSem, false, (timeout==(unsigned)-1)?INFINITE:timeout);
  84. }
  85. void signal(int i)
  86. {
  87. ReleaseSemaphore(hSem[i],1,NULL);
  88. }
  89. void signal(int i,unsigned count)
  90. {
  91. ReleaseSemaphore(hSem[i],count,NULL);
  92. }
  93. protected:
  94. HANDLE * hSem;
  95. int sem_count;
  96. };
  97. #else
  98. #include <semaphore.h>
  99. #ifdef __APPLE__
  100. // sem_timedwait is not available in OSX, so continue to use old code
  101. #define USE_OLD_SEMAPHORE_CODE
  102. #endif
  103. class jlib_decl Semaphore
  104. {
  105. public:
  106. Semaphore(unsigned initialCount=0U);
  107. ~Semaphore();
  108. bool tryWait();
  109. void wait();
  110. bool wait(unsigned timeout); // in msecs
  111. void signal();
  112. void signal(unsigned count);
  113. void reinit(unsigned initialCount=0U);
  114. #ifndef USE_OLD_SEMAPHORE_CODE
  115. protected:
  116. sem_t sem;
  117. #else
  118. protected:
  119. void init();
  120. protected:
  121. MutexId mx;
  122. pthread_cond_t cond;
  123. int count;
  124. #endif
  125. };
  126. #endif
  127. #endif