PageRenderTime 20ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 30#ifndef BOOST_INTERPROCESS_SYNC_XSI_ADVANCED_XSI_SEMAPHORE_HPP
 31#define BOOST_INTERPROCESS_SYNC_XSI_ADVANCED_XSI_SEMAPHORE_HPP
 32
 33#include <sys/ipc.h>
 34#include <sys/sem.h>
 35#include <errno.h>
 36
 37namespace boost {
 38namespace interprocess {
 39namespace xsi {
 40
 41// Create a semaphore with a specified initial value.
 42// If the semaphore already exists, we don't initialize it (of course).
 43// We return the semaphore ID if all OK, else -1.
 44
 45inline bool advanced_sem_open_or_create(::key_t key, int initval, int &semid, int perm)
 46{
 47   semid = -1;
 48   int id, semval;
 49   union semun {
 50      int         val;
 51      ::semid_ds *buf;
 52      ushort     *array;
 53   } semctl_arg;
 54
 55   if (key == IPC_PRIVATE)
 56      return false; //not intended for private semaphores
 57
 58   else if (key == (::key_t) -1)
 59      return false; //probably an ftok() error by caller
 60
 61   again:
 62   if ((id = ::semget(key, 3, (perm & 0x01FF) | IPC_CREAT)) < 0)
 63      return false;   //permission problem or tables full
 64
 65   // When the semaphore is created, we know that the value of all
 66   // 3 members is 0.
 67   // Get a lock on the semaphore by waiting for [2] to equal 0,
 68   // then increment it.
 69   //
 70   // There is a race condition here.  There is a possibility that
 71   // between the semget() above and the ::semop() below, another
 72   // process can call our sem_close() function which can remove
 73   // the semaphore if that process is the last one using it.
 74   // Therefore, we handle the error condition of an invalid
 75   // semaphore ID specially below, and if it does happen, we just
 76   // go back and create it again.
 77   struct sembuf op_lock[2] = {
 78      {2, 0, 0},        // wait for [2] (lock) to equal 0
 79      {2, 1, SEM_UNDO}  // then increment [2] to 1 - this locks it
 80                        // UNDO to release the lock if processes exits
 81                        // before explicitly unlocking
 82   };
 83
 84   if (::semop(id, &op_lock[0], 2) < 0) {
 85      if (errno == EINVAL)
 86         goto again;
 87   }
 88
 89   // Get the value of the process counter.  If it equals 0,
 90   // then no one has initialized the semaphore yet.
 91   if ((semval = ::semctl(id, 1, GETVAL, 0)) < 0)
 92      return false;
 93
 94   if (semval == 0) {
 95      // We could initialize by doing a SETALL, but that
 96      // would clear the adjust value that we set when we
 97      // locked the semaphore above.  Instead, we'll do 2
 98      // system calls to initialize [0] and [1].
 99      semctl_arg.val = initval;
100      if (::semctl(id, 0, SETVAL, semctl_arg) < 0)
101         return false;
102
103      semctl_arg.val = 1;
104      if (::semctl(id, 1, SETVAL, semctl_arg) < 0)
105         return false;
106   }
107
108   // Decrement the process counter and then release the lock.
109   struct sembuf op_unlock[1] = {
110      2, -1, 0/*SEM_UNDO*/ // decrement [2] (lock) back to 0
111   };
112
113   if (::semop(id, &op_unlock[0], 1) < 0)
114      return false;
115
116   semid = id;
117   return true;
118}
119
120// Open a semaphore that must already exist.
121// This function should be used, instead of sem_create(), if the caller
122// knows that the semaphore must already exist.  For example a client
123// from a client-server pair would use this, if its the server's
124// responsibility to create the semaphore.
125// We return the semaphore ID if all OK, else -1.
126/*
127inline bool advanced_sem_open(key_t key, int &semid)
128{
129   semid = -1;
130   if (key == IPC_PRIVATE)
131      return false; // not intended for private semaphores
132
133   else if (key == (::key_t) -1)
134      return false;  // probably an ftok() error by caller
135
136   if ((semid = ::semget(key, 3, 0)) < 0)
137      return false;     // doesn't exist, or tables full
138
139   // Decrement the process counter.  We don't need a lock
140   struct sembuf op_open[1] = {
141      1, -1, SEM_UNDO   // decrement [1] (proc counter) with undo on exit
142   };
143
144   if (::semop(id, &op_open[0], 1) < 0)
145      return false;
146
147   return true;
148}
149*/
150/****************************************************************************
151 * Remove a semaphore.
152 * This call is intended to be called by a server, for example,
153 * when it is being shut down, as we do an IPC_RMID on the semaphore,
154 * regardless whether other processes may be using it or not.
155 * Most other processes should use sem_close() below.
156 */
157
158inline bool advanced_sem_rm(int id)
159{
160   if (::semctl(id, 0, IPC_RMID, 0) < 0)
161      return false;
162   return true;
163}
164
165
166/****************************************************************************
167 * General semaphore operation.  Increment or decrement by a user-specified
168 * amount (positive or negative; amount can't be zero).
169 */
170
171inline bool advanced_sem_op(int id, int value, bool undo = true)
172{
173   ::sembuf op_op[1] = {
174      0, 99, 0 // decrement or increment [0] with undo on exit
175               // the 99 is set to the actual amount to add
176               // or subtract (positive or negative)
177   };
178   if(undo){
179      op_op[0].sem_flg = SEM_UNDO;
180   }
181   if ((op_op[0].sem_op = value) == 0)
182      return false;
183
184   if (::semop(id, &op_op[0], 1) < 0)
185      return false;
186   return true;
187}
188
189}  //namespace xsi {
190}  //namespace interprocess {
191}  //namespace boost {
192
193#endif //BOOST_INTERPROCESS_SYNC_XSI_ADVANCED_XSI_SEMAPHORE_HPP