/Src/Dependencies/Boost/boost/interprocess/sync/xsi/advanced_xsi_semaphore.hpp

http://hadesmem.googlecode.com/ · C++ Header · 193 lines · 74 code · 24 blank · 95 comment · 20 complexity · 88b58066575d8c1a7f3ec57f57f56e9b MD5 · raw file

  1. /*
  2. * Provide an simpler and easier to understand interface to the System V
  3. * semaphore system calls. There are 7 routines available to the user:
  4. *
  5. * id = sem_create(key, initval); # create with initial value or open
  6. * id = sem_open(key); # open (must already exist)
  7. * sem_wait(id); # wait = P = down by 1
  8. * sem_signal(id); # signal = V = up by 1
  9. * sem_op(id, amount); # wait if (amount < 0)
  10. * # signal if (amount > 0)
  11. * sem_close(id); # close
  12. * sem_rm(id); # remove (delete)
  13. *
  14. * We create and use a 3-member set for the requested semaphore.
  15. * The first member, [0], is the actual semaphore value, and the second
  16. * member, [1], is a counter used to know when all processes have finished
  17. * with the semaphore. The counter is initialized to a large number,
  18. * decremented on every create or open and incremented on every close.
  19. * This way we can use the "adjust" feature provided by System V so that
  20. * any process that exit's without calling sem_close() is accounted
  21. * for. It doesn't help us if the last process does this (as we have
  22. * no way of getting control to remove the semaphore) but it will
  23. * work if any process other than the last does an exit (intentional
  24. * or unintentional).
  25. * The third member, [2], of the semaphore set is used as a lock variable
  26. * to avoid any race conditions in the sem_create() and sem_close()
  27. * functions.
  28. */
  29. #ifndef BOOST_INTERPROCESS_SYNC_XSI_ADVANCED_XSI_SEMAPHORE_HPP
  30. #define BOOST_INTERPROCESS_SYNC_XSI_ADVANCED_XSI_SEMAPHORE_HPP
  31. #include <sys/ipc.h>
  32. #include <sys/sem.h>
  33. #include <errno.h>
  34. namespace boost {
  35. namespace interprocess {
  36. namespace xsi {
  37. // Create a semaphore with a specified initial value.
  38. // If the semaphore already exists, we don't initialize it (of course).
  39. // We return the semaphore ID if all OK, else -1.
  40. inline bool advanced_sem_open_or_create(::key_t key, int initval, int &semid, int perm)
  41. {
  42. semid = -1;
  43. int id, semval;
  44. union semun {
  45. int val;
  46. ::semid_ds *buf;
  47. ushort *array;
  48. } semctl_arg;
  49. if (key == IPC_PRIVATE)
  50. return false; //not intended for private semaphores
  51. else if (key == (::key_t) -1)
  52. return false; //probably an ftok() error by caller
  53. again:
  54. if ((id = ::semget(key, 3, (perm & 0x01FF) | IPC_CREAT)) < 0)
  55. return false; //permission problem or tables full
  56. // When the semaphore is created, we know that the value of all
  57. // 3 members is 0.
  58. // Get a lock on the semaphore by waiting for [2] to equal 0,
  59. // then increment it.
  60. //
  61. // There is a race condition here. There is a possibility that
  62. // between the semget() above and the ::semop() below, another
  63. // process can call our sem_close() function which can remove
  64. // the semaphore if that process is the last one using it.
  65. // Therefore, we handle the error condition of an invalid
  66. // semaphore ID specially below, and if it does happen, we just
  67. // go back and create it again.
  68. struct sembuf op_lock[2] = {
  69. {2, 0, 0}, // wait for [2] (lock) to equal 0
  70. {2, 1, SEM_UNDO} // then increment [2] to 1 - this locks it
  71. // UNDO to release the lock if processes exits
  72. // before explicitly unlocking
  73. };
  74. if (::semop(id, &op_lock[0], 2) < 0) {
  75. if (errno == EINVAL)
  76. goto again;
  77. }
  78. // Get the value of the process counter. If it equals 0,
  79. // then no one has initialized the semaphore yet.
  80. if ((semval = ::semctl(id, 1, GETVAL, 0)) < 0)
  81. return false;
  82. if (semval == 0) {
  83. // We could initialize by doing a SETALL, but that
  84. // would clear the adjust value that we set when we
  85. // locked the semaphore above. Instead, we'll do 2
  86. // system calls to initialize [0] and [1].
  87. semctl_arg.val = initval;
  88. if (::semctl(id, 0, SETVAL, semctl_arg) < 0)
  89. return false;
  90. semctl_arg.val = 1;
  91. if (::semctl(id, 1, SETVAL, semctl_arg) < 0)
  92. return false;
  93. }
  94. // Decrement the process counter and then release the lock.
  95. struct sembuf op_unlock[1] = {
  96. 2, -1, 0/*SEM_UNDO*/ // decrement [2] (lock) back to 0
  97. };
  98. if (::semop(id, &op_unlock[0], 1) < 0)
  99. return false;
  100. semid = id;
  101. return true;
  102. }
  103. // Open a semaphore that must already exist.
  104. // This function should be used, instead of sem_create(), if the caller
  105. // knows that the semaphore must already exist. For example a client
  106. // from a client-server pair would use this, if its the server's
  107. // responsibility to create the semaphore.
  108. // We return the semaphore ID if all OK, else -1.
  109. /*
  110. inline bool advanced_sem_open(key_t key, int &semid)
  111. {
  112. semid = -1;
  113. if (key == IPC_PRIVATE)
  114. return false; // not intended for private semaphores
  115. else if (key == (::key_t) -1)
  116. return false; // probably an ftok() error by caller
  117. if ((semid = ::semget(key, 3, 0)) < 0)
  118. return false; // doesn't exist, or tables full
  119. // Decrement the process counter. We don't need a lock
  120. struct sembuf op_open[1] = {
  121. 1, -1, SEM_UNDO // decrement [1] (proc counter) with undo on exit
  122. };
  123. if (::semop(id, &op_open[0], 1) < 0)
  124. return false;
  125. return true;
  126. }
  127. */
  128. /****************************************************************************
  129. * Remove a semaphore.
  130. * This call is intended to be called by a server, for example,
  131. * when it is being shut down, as we do an IPC_RMID on the semaphore,
  132. * regardless whether other processes may be using it or not.
  133. * Most other processes should use sem_close() below.
  134. */
  135. inline bool advanced_sem_rm(int id)
  136. {
  137. if (::semctl(id, 0, IPC_RMID, 0) < 0)
  138. return false;
  139. return true;
  140. }
  141. /****************************************************************************
  142. * General semaphore operation. Increment or decrement by a user-specified
  143. * amount (positive or negative; amount can't be zero).
  144. */
  145. inline bool advanced_sem_op(int id, int value, bool undo = true)
  146. {
  147. ::sembuf op_op[1] = {
  148. 0, 99, 0 // decrement or increment [0] with undo on exit
  149. // the 99 is set to the actual amount to add
  150. // or subtract (positive or negative)
  151. };
  152. if(undo){
  153. op_op[0].sem_flg = SEM_UNDO;
  154. }
  155. if ((op_op[0].sem_op = value) == 0)
  156. return false;
  157. if (::semop(id, &op_op[0], 1) < 0)
  158. return false;
  159. return true;
  160. }
  161. } //namespace xsi {
  162. } //namespace interprocess {
  163. } //namespace boost {
  164. #endif //BOOST_INTERPROCESS_SYNC_XSI_ADVANCED_XSI_SEMAPHORE_HPP