PageRenderTime 40ms CodeModel.GetById 32ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 105 lines | 45 code | 16 blank | 44 comment | 11 complexity | 5ae0ced9a5f2f684b4d4481495d3da58 MD5 | raw file
  1#ifndef BOOST_INTERPROCESS_SYNC_XSI_SIMPLE_XSI_SEMAPHORE_HPP
  2#define BOOST_INTERPROCESS_SYNC_XSI_SIMPLE_XSI_SEMAPHORE_HPP
  3
  4/*
  5 * Provide an simpler and easier to understand interface to the System V
  6 * semaphore system calls.  There are 7 routines available to the user:
  7 *
  8 *      id = sem_create(key, initval);  # create with initial value or open
  9 *      id = sem_open(key);             # open (must already exist)
 10 *      sem_wait(id);                   # wait = P = down by 1
 11 *      sem_signal(id);                 # signal = V = up by 1
 12 *      sem_op(id, amount);             # wait   if (amount < 0)
 13 *                                      # signal if (amount > 0)
 14 *      sem_close(id);                  # close
 15 *      sem_rm(id);                     # remove (delete)
 16 *
 17 * We create and use a 3-member set for the requested semaphore.
 18 * The first member, [0], is the actual semaphore value, and the second
 19 * member, [1], is a counter used to know when all processes have finished
 20 * with the semaphore.  The counter is initialized to a large number,
 21 * decremented on every create or open and incremented on every close.
 22 * This way we can use the "adjust" feature provided by System V so that
 23 * any process that exit's without calling sem_close() is accounted
 24 * for.  It doesn't help us if the last process does this (as we have
 25 * no way of getting control to remove the semaphore) but it will
 26 * work if any process other than the last does an exit (intentional
 27 * or unintentional).
 28 * The third member, [2], of the semaphore set is used as a lock variable
 29 * to avoid any race conditions in the sem_create() and sem_close()
 30 * functions.
 31 */
 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 simple_sem_open_or_create(::key_t key, int initval, int &semid, int perm)
 46{
 47   int id, semval;
 48   semid = -1;
 49
 50   if (key == IPC_PRIVATE)
 51      return false; //not intended for private semaphores
 52
 53   else if (key == (::key_t) -1)
 54      return false; //probably an ftok() error by caller
 55
 56   again:
 57   if ((id = ::semget(key, 1, (perm & 0x01FF) | IPC_CREAT)) < 0)
 58      return false;   //permission problem or tables full
 59
 60   semid = id;
 61   return true;
 62}
 63
 64/****************************************************************************
 65 * Remove a semaphore.
 66 * This call is intended to be called by a server, for example,
 67 * when it is being shut down, as we do an IPC_RMID on the semaphore,
 68 * regardless whether other processes may be using it or not.
 69 * Most other processes should use sem_close() below.
 70 */
 71
 72inline bool simple_sem_rm(int id)
 73{
 74   if (::semctl(id, 0, IPC_RMID, 0) < 0)
 75      return false;
 76   return true;
 77}
 78
 79
 80/****************************************************************************
 81 * General semaphore operation.  Increment or decrement by a user-specified
 82 * amount (positive or negative; amount can't be zero).
 83 */
 84
 85inline bool simple_sem_op(int id, int value, bool undo = true)
 86{
 87   ::sembuf op_op[1] = {
 88      0, 99, 0 // decrement or increment [0] with undo on exit
 89               // the 99 is set to the actual amount to add
 90               // or subtract (positive or negative)
 91   };
 92   if(undo){
 93      op_op[0].sem_flg = SEM_UNDO;
 94   }
 95   if ((op_op[0].sem_op = value) == 0)
 96      return false;
 97
 98   if (::semop(id, &op_op[0], 1) < 0)
 99      return false;
100   return true;
101}
102
103}  //namespace xsi {
104}  //namespace interprocess {
105}  //namespace boost {