diff options
Diffstat (limited to 'boost/interprocess/sync/xsi')
-rw-r--r-- | boost/interprocess/sync/xsi/advanced_xsi_semaphore.hpp | 201 | ||||
-rw-r--r-- | boost/interprocess/sync/xsi/simple_xsi_semaphore.hpp | 124 | ||||
-rw-r--r-- | boost/interprocess/sync/xsi/xsi_named_mutex.hpp | 238 |
3 files changed, 0 insertions, 563 deletions
diff --git a/boost/interprocess/sync/xsi/advanced_xsi_semaphore.hpp b/boost/interprocess/sync/xsi/advanced_xsi_semaphore.hpp deleted file mode 100644 index 49562f5cbd..0000000000 --- a/boost/interprocess/sync/xsi/advanced_xsi_semaphore.hpp +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Provide an simpler and easier to understand interface to the System V - * semaphore system calls. There are 7 routines available to the user: - * - * id = sem_create(key, initval); # create with initial value or open - * id = sem_open(key); # open (must already exist) - * sem_wait(id); # wait = P = down by 1 - * sem_signal(id); # signal = V = up by 1 - * sem_op(id, amount); # wait if (amount < 0) - * # signal if (amount > 0) - * sem_close(id); # close - * sem_rm(id); # remove (delete) - * - * We create and use a 3-member set for the requested semaphore. - * The first member, [0], is the actual semaphore value, and the second - * member, [1], is a counter used to know when all processes have finished - * with the semaphore. The counter is initialized to a large number, - * decremented on every create or open and incremented on every close. - * This way we can use the "adjust" feature provided by System V so that - * any process that exit's without calling sem_close() is accounted - * for. It doesn't help us if the last process does this (as we have - * no way of getting control to remove the semaphore) but it will - * work if any process other than the last does an exit (intentional - * or unintentional). - * The third member, [2], of the semaphore set is used as a lock variable - * to avoid any race conditions in the sem_create() and sem_close() - * functions. - */ - -#ifndef BOOST_INTERPROCESS_SYNC_XSI_ADVANCED_XSI_SEMAPHORE_HPP -#define BOOST_INTERPROCESS_SYNC_XSI_ADVANCED_XSI_SEMAPHORE_HPP - -#ifndef BOOST_CONFIG_HPP -# include <boost/config.hpp> -#endif -# -#if defined(BOOST_HAS_PRAGMA_ONCE) -# pragma once -#endif - -#include <sys/ipc.h> -#include <sys/sem.h> -#include <errno.h> - -namespace boost { -namespace interprocess { -namespace xsi { - -// Create a semaphore with a specified initial value. -// If the semaphore already exists, we don't initialize it (of course). -// We return the semaphore ID if all OK, else -1. - -inline bool advanced_sem_open_or_create(::key_t key, int initval, int &semid, int perm) -{ - semid = -1; - int id, semval; - union semun { - int val; - ::semid_ds *buf; - ushort *array; - } semctl_arg; - - if (key == IPC_PRIVATE) - return false; //not intended for private semaphores - - else if (key == (::key_t) -1) - return false; //probably an ftok() error by caller - - again: - if ((id = ::semget(key, 3, (perm & 0x01FF) | IPC_CREAT)) < 0) - return false; //permission problem or tables full - - // When the semaphore is created, we know that the value of all - // 3 members is 0. - // Get a lock on the semaphore by waiting for [2] to equal 0, - // then increment it. - // - // There is a race condition here. There is a possibility that - // between the semget() above and the ::semop() below, another - // process can call our sem_close() function which can remove - // the semaphore if that process is the last one using it. - // Therefore, we handle the error condition of an invalid - // semaphore ID specially below, and if it does happen, we just - // go back and create it again. - struct sembuf op_lock[2] = { - {2, 0, 0}, // wait for [2] (lock) to equal 0 - {2, 1, SEM_UNDO} // then increment [2] to 1 - this locks it - // UNDO to release the lock if processes exits - // before explicitly unlocking - }; - - if (::semop(id, &op_lock[0], 2) < 0) { - if (errno == EINVAL) - goto again; - } - - // Get the value of the process counter. If it equals 0, - // then no one has initialized the semaphore yet. - if ((semval = ::semctl(id, 1, GETVAL, 0)) < 0) - return false; - - if (semval == 0) { - // We could initialize by doing a SETALL, but that - // would clear the adjust value that we set when we - // locked the semaphore above. Instead, we'll do 2 - // system calls to initialize [0] and [1]. - semctl_arg.val = initval; - if (::semctl(id, 0, SETVAL, semctl_arg) < 0) - return false; - - semctl_arg.val = 1; - if (::semctl(id, 1, SETVAL, semctl_arg) < 0) - return false; - } - - // Decrement the process counter and then release the lock. - struct sembuf op_unlock[1] = { - 2, -1, 0/*SEM_UNDO*/ // decrement [2] (lock) back to 0 - }; - - if (::semop(id, &op_unlock[0], 1) < 0) - return false; - - semid = id; - return true; -} - -// Open a semaphore that must already exist. -// This function should be used, instead of sem_create(), if the caller -// knows that the semaphore must already exist. For example a client -// from a client-server pair would use this, if its the server's -// responsibility to create the semaphore. -// We return the semaphore ID if all OK, else -1. -/* -inline bool advanced_sem_open(key_t key, int &semid) -{ - semid = -1; - if (key == IPC_PRIVATE) - return false; // not intended for private semaphores - - else if (key == (::key_t) -1) - return false; // probably an ftok() error by caller - - if ((semid = ::semget(key, 3, 0)) < 0) - return false; // doesn't exist, or tables full - - // Decrement the process counter. We don't need a lock - struct sembuf op_open[1] = { - 1, -1, SEM_UNDO // decrement [1] (proc counter) with undo on exit - }; - - if (::semop(id, &op_open[0], 1) < 0) - return false; - - return true; -} -*/ -/**************************************************************************** - * Remove a semaphore. - * This call is intended to be called by a server, for example, - * when it is being shut down, as we do an IPC_RMID on the semaphore, - * regardless whether other processes may be using it or not. - * Most other processes should use sem_close() below. - */ - -inline bool advanced_sem_rm(int id) -{ - if (::semctl(id, 0, IPC_RMID, 0) < 0) - return false; - return true; -} - - -/**************************************************************************** - * General semaphore operation. Increment or decrement by a user-specified - * amount (positive or negative; amount can't be zero). - */ - -inline bool advanced_sem_op(int id, int value, bool undo = true) -{ - ::sembuf op_op[1] = { - 0, 99, 0 // decrement or increment [0] with undo on exit - // the 99 is set to the actual amount to add - // or subtract (positive or negative) - }; - if(undo){ - op_op[0].sem_flg = SEM_UNDO; - } - if ((op_op[0].sem_op = value) == 0) - return false; - - if (::semop(id, &op_op[0], 1) < 0) - return false; - return true; -} - -} //namespace xsi { -} //namespace interprocess { -} //namespace boost { - -#endif //BOOST_INTERPROCESS_SYNC_XSI_ADVANCED_XSI_SEMAPHORE_HPP diff --git a/boost/interprocess/sync/xsi/simple_xsi_semaphore.hpp b/boost/interprocess/sync/xsi/simple_xsi_semaphore.hpp deleted file mode 100644 index 3d6fd882c6..0000000000 --- a/boost/interprocess/sync/xsi/simple_xsi_semaphore.hpp +++ /dev/null @@ -1,124 +0,0 @@ -////////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2011-2012. Distributed under the Boost -// Software License, Version 1.0. (See accompanying file -// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/interprocess for documentation. -// -////////////////////////////////////////////////////////////////////////////// -#ifndef BOOST_INTERPROCESS_SYNC_XSI_SIMPLE_XSI_SEMAPHORE_HPP -#define BOOST_INTERPROCESS_SYNC_XSI_SIMPLE_XSI_SEMAPHORE_HPP - -#ifndef BOOST_CONFIG_HPP -# include <boost/config.hpp> -#endif -# -#if defined(BOOST_HAS_PRAGMA_ONCE) -# pragma once -#endif - -/* - * Provide an simpler and easier to understand interface to the System V - * semaphore system calls. There are 7 routines available to the user: - * - * id = sem_create(key, initval); # create with initial value or open - * id = sem_open(key); # open (must already exist) - * sem_wait(id); # wait = P = down by 1 - * sem_signal(id); # signal = V = up by 1 - * sem_op(id, amount); # wait if (amount < 0) - * # signal if (amount > 0) - * sem_close(id); # close - * sem_rm(id); # remove (delete) - * - * We create and use a 3-member set for the requested semaphore. - * The first member, [0], is the actual semaphore value, and the second - * member, [1], is a counter used to know when all processes have finished - * with the semaphore. The counter is initialized to a large number, - * decremented on every create or open and incremented on every close. - * This way we can use the "adjust" feature provided by System V so that - * any process that exit's without calling sem_close() is accounted - * for. It doesn't help us if the last process does this (as we have - * no way of getting control to remove the semaphore) but it will - * work if any process other than the last does an exit (intentional - * or unintentional). - * The third member, [2], of the semaphore set is used as a lock variable - * to avoid any race conditions in the sem_create() and sem_close() - * functions. - */ - -#include <sys/ipc.h> -#include <sys/sem.h> -#include <errno.h> - -namespace boost { -namespace interprocess { -namespace xsi { - -// Create a semaphore with a specified initial value. -// If the semaphore already exists, we don't initialize it (of course). -// We return the semaphore ID if all OK, else -1. - -inline bool simple_sem_open_or_create(::key_t key, int initval, int &semid, int perm) -{ - int id, semval; - semid = -1; - - if (key == IPC_PRIVATE) - return false; //not intended for private semaphores - - else if (key == (::key_t) -1) - return false; //probably an ftok() error by caller - - again: - if ((id = ::semget(key, 1, (perm & 0x01FF) | IPC_CREAT)) < 0) - return false; //permission problem or tables full - - semid = id; - return true; -} - -/**************************************************************************** - * Remove a semaphore. - * This call is intended to be called by a server, for example, - * when it is being shut down, as we do an IPC_RMID on the semaphore, - * regardless whether other processes may be using it or not. - * Most other processes should use sem_close() below. - */ - -inline bool simple_sem_rm(int id) -{ - if (::semctl(id, 0, IPC_RMID, 0) < 0) - return false; - return true; -} - - -/**************************************************************************** - * General semaphore operation. Increment or decrement by a user-specified - * amount (positive or negative; amount can't be zero). - */ - -inline bool simple_sem_op(int id, int value, bool undo = true) -{ - ::sembuf op_op[1] = { - 0, 99, 0 // decrement or increment [0] with undo on exit - // the 99 is set to the actual amount to add - // or subtract (positive or negative) - }; - if(undo){ - op_op[0].sem_flg = SEM_UNDO; - } - if ((op_op[0].sem_op = value) == 0) - return false; - - if (::semop(id, &op_op[0], 1) < 0) - return false; - return true; -} - -} //namespace xsi { -} //namespace interprocess { -} //namespace boost { - -#endif //BOOST_INTERPROCESS_SYNC_XSI_SIMPLE_XSI_SEMAPHORE_HPP diff --git a/boost/interprocess/sync/xsi/xsi_named_mutex.hpp b/boost/interprocess/sync/xsi/xsi_named_mutex.hpp deleted file mode 100644 index 7d8cf50a83..0000000000 --- a/boost/interprocess/sync/xsi/xsi_named_mutex.hpp +++ /dev/null @@ -1,238 +0,0 @@ -////////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2009-2012. Distributed under the Boost -// Software License, Version 1.0. (See accompanying file -// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/interprocess for documentation. -// -////////////////////////////////////////////////////////////////////////////// - -#ifndef BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP -#define BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP - -#ifndef BOOST_CONFIG_HPP -# include <boost/config.hpp> -#endif -# -#if defined(BOOST_HAS_PRAGMA_ONCE) -# pragma once -#endif - -#include <boost/interprocess/detail/config_begin.hpp> -#include <boost/interprocess/detail/workaround.hpp> - -#if defined(BOOST_INTERPROCESS_WINDOWS) -#error "This header can't be used in Windows operating systems" -#endif - -#include <boost/move/utility_core.hpp> -#include <boost/interprocess/creation_tags.hpp> -#include <boost/interprocess/exceptions.hpp> -#include <boost/interprocess/detail/utilities.hpp> -#include <boost/interprocess/detail/os_file_functions.hpp> -#include <boost/interprocess/interprocess_fwd.hpp> -#include <boost/interprocess/exceptions.hpp> -#include <boost/interprocess/sync/xsi/basic_xsi_semaphore.hpp> -#include <boost/interprocess/detail/simple_swap.hpp> - -#include <cstddef> -#include <boost/assert.hpp> -#include <boost/cstdint.hpp> -#include <string> - - -//!\file -//!Describes a class representing a xsi-based named_mutex. - -namespace boost { -namespace interprocess { - -//!A class that wraps a XSI (System V)-based named semaphore -//!that undoes the operation if the process crashes. -class xsi_named_mutex -{ - #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) - //Non-copyable and non-assignable - xsi_named_mutex(xsi_named_mutex &); - xsi_named_mutex &operator=(xsi_named_mutex &); - #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED - - public: - BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_named_mutex) - - //!Default constructor. - //!Represents an empty xsi_named_mutex. - xsi_named_mutex(); - - //!Tries to create a new XSI-based named mutex with a key obtained from a call to ftok (with path - //!"path" and id "id"), and permissions "perm". - //!If the named mutex previously exists, it tries to open it. - //!Otherwise throws an error. - xsi_named_mutex(open_or_create_t, const char *path, boost::uint8_t id, int perm = 0666) - { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, path, id, perm); } - - //!Moves the ownership of "moved"'s named mutex to *this. - //!After the call, "moved" does not represent any named mutex - //!Does not throw - xsi_named_mutex(BOOST_RV_REF(xsi_named_mutex) moved) - { this->swap(moved); } - - //!Moves the ownership of "moved"'s named mutex to *this. - //!After the call, "moved" does not represent any named mutex. - //!Does not throw - xsi_named_mutex &operator=(BOOST_RV_REF(xsi_named_mutex) moved) - { - xsi_named_mutex tmp(boost::move(moved)); - this->swap(tmp); - return *this; - } - - //!Swaps two xsi_named_mutex. Does not throw - void swap(xsi_named_mutex &other); - - //!Destroys *this. The named mutex is still valid after - //!destruction. use remove() to destroy the named mutex. - ~xsi_named_mutex(); - - //!Returns the path used to construct the - //!named mutex. - const char *get_path() const; - - //!Returns access - //!permissions - int get_permissions() const; - - //!Returns the mapping handle. - //!Never throws - mapping_handle_t get_mapping_handle() const; - - //!Erases a XSI-based named mutex from the system. - //!Returns false on error. Never throws - bool remove(); - - void lock(); - - void unlock(); - - #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) - private: - - //!Closes a previously opened file mapping. Never throws. - void priv_close(); - - //!Closes a previously opened file mapping. Never throws. - bool priv_open_or_create( ipcdetail::create_enum_t type - , const char *path - , boost::uint8_t id - , int perm); - int m_semid; - key_t m_key; - boost::uint8_t m_id; - int m_perm; - std::string m_path; - #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED -}; - -#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) - -inline xsi_named_mutex::xsi_named_mutex() - : m_semid(-1), m_key(-1), m_id(0), m_perm(0), m_path() -{} - -inline xsi_named_mutex::~xsi_named_mutex() -{ this->priv_close(); } - -inline const char *xsi_named_mutex::get_path() const -{ return m_path.c_str(); } - -inline void xsi_named_mutex::swap(xsi_named_mutex &other) -{ - (simple_swap)(m_key, other.m_key); - (simple_swap)(m_id, other.m_id); - (simple_swap)(m_semid, other.m_semid); - (simple_swap)(m_perm, other.m_perm); - m_path.swap(other.m_path); -} - -inline mapping_handle_t xsi_named_mutex::get_mapping_handle() const -{ mapping_handle_t mhnd = { m_semid, true}; return mhnd; } - -inline int xsi_named_mutex::get_permissions() const -{ return m_perm; } - -inline bool xsi_named_mutex::priv_open_or_create - (ipcdetail::create_enum_t type, const char *path, boost::uint8_t id, int perm) -{ - key_t key; - if(path){ - key = ::ftok(path, id); - if(((key_t)-1) == key){ - error_info err = system_error_code(); - throw interprocess_exception(err); - } - } - else{ - key = IPC_PRIVATE; - } - - perm &= 0x01FF; - - int semid; - if(!xsi::simple_sem_open_or_create(key, 1, semid, perm)){ - error_info err = system_error_code(); - throw interprocess_exception(err); - } - - m_perm = perm; - m_semid = semid; - m_path = path ? path : ""; - m_id = id; - m_key = key; - - return true; -} - -inline void xsi_named_mutex::priv_close() -{ -} - -inline void xsi_named_mutex::lock() -{ - if(!xsi::simple_sem_op(m_semid, -1)){ - error_info err = system_error_code(); - throw interprocess_exception(err); - } -} - -inline void xsi_named_mutex::unlock() -{ - bool success = xsi::simple_sem_op(m_semid, 1); - (void)success; - BOOST_ASSERT(success); -} - -inline bool xsi_named_mutex::remove() -{ - if(m_semid != -1){ - int ret = ::semctl(m_semid, IPC_RMID, 0); - if(-1 == ret) - return false; - //Now put it in default-constructed state - m_semid = -1; - m_key = -1; - m_id = 0; - m_perm = 0; - m_path.clear(); - } - return false; -} - -#endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED - -} //namespace interprocess { -} //namespace boost { - -#include <boost/interprocess/detail/config_end.hpp> - -#endif //BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP |