Boost logo

Boost-Commit :

From: igaztanaga_at_[hidden]
Date: 2007-11-24 14:47:59


Author: igaztanaga
Date: 2007-11-24 14:47:58 EST (Sat, 24 Nov 2007)
New Revision: 41342
URL: http://svn.boost.org/trac/boost/changeset/41342

Log:
Added workaround for systems without SEM_FAILED and semaphores based on filesystem
Added:
   trunk/boost/interprocess/detail/tmp_dir_helpers.hpp (contents, props changed)
Text files modified:
   trunk/boost/interprocess/detail/workaround.hpp | 10 +++++
   trunk/boost/interprocess/shared_memory_object.hpp | 73 ++++-----------------------------------
   trunk/boost/interprocess/sync/posix/semaphore_wrapper.hpp | 61 ++++++++++++++++----------------
   trunk/libs/interprocess/proj/vc7ide/interprocesslib.vcproj | 3 +
   4 files changed, 51 insertions(+), 96 deletions(-)

Added: trunk/boost/interprocess/detail/tmp_dir_helpers.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/interprocess/detail/tmp_dir_helpers.hpp 2007-11-24 14:47:58 EST (Sat, 24 Nov 2007)
@@ -0,0 +1,78 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Ion Gaztanaga 2007. 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_DETAIL_TMP_DIR_HELPERS_HPP
+#define BOOST_INTERPROCESS_DETAIL_TMP_DIR_HELPERS_HPP
+
+#include <boost/interprocess/detail/config_begin.hpp>
+#include <boost/interprocess/detail/workaround.hpp>
+#include <boost/interprocess/detail/os_file_functions.hpp>
+#include <boost/interprocess/errors.hpp>
+#include <boost/interprocess/exceptions.hpp>
+#include <string>
+
+namespace boost {
+namespace interprocess {
+namespace detail {
+
+inline void tmp_filename(const char *filename, std::string &tmp_name)
+{
+ const char *tmp_dir = get_temporary_path();
+ if(!tmp_dir){
+ error_info err = system_error_code();
+ throw interprocess_exception(err);
+ }
+ tmp_name = tmp_dir;
+
+ //Remove final null.
+ tmp_name += "/boost_interprocess/";
+ tmp_name += filename;
+}
+
+inline void create_tmp_dir_and_get_filename(const char *filename, std::string &tmp_name)
+{
+ const char *tmp_path = get_temporary_path();
+ if(!tmp_path){
+ error_info err = system_error_code();
+ throw interprocess_exception(err);
+ }
+
+ tmp_name = tmp_path;
+ tmp_name += "/boost_interprocess";
+
+ //Create the temporary directory.
+ //If fails, check that it's because already exists
+ if(!create_directory(tmp_name.c_str())){
+ error_info info(system_error_code());
+ if(info.get_error_code() != already_exists_error){
+ throw interprocess_exception(info);
+ }
+ }
+
+ //Add filename
+ tmp_name += '/';
+ tmp_name += filename;
+}
+
+inline void add_leading_slash(const char *name, std::string &new_name)
+{
+ if(name[0] != '/'){
+ new_name = '/';
+ }
+ new_name += name;
+}
+
+} //namespace boost {
+} //namespace interprocess {
+} //namespace detail {
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //ifndef BOOST_INTERPROCESS_DETAIL_TMP_DIR_HELPERS_HPP

Modified: trunk/boost/interprocess/detail/workaround.hpp
==============================================================================
--- trunk/boost/interprocess/detail/workaround.hpp (original)
+++ trunk/boost/interprocess/detail/workaround.hpp 2007-11-24 14:47:58 EST (Sat, 24 Nov 2007)
@@ -78,6 +78,16 @@
       #endif
    #endif
 
+ #ifdef BOOST_INTERPROCESS_POSIX_SEMAPHORES
+ //Some systems have filesystem-based shared memory, so the
+ //portable "/semname" format does not work due to permission issues
+ //For those systems we need to form a path to a temporary directory:
+ // hp-ux tru64 vms
+ #if defined(__hpux) || defined(__osf__) || defined(__vms)
+ #define BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SEMAPHORES
+ #endif
+ #endif
+
 #endif
 
 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)

Modified: trunk/boost/interprocess/shared_memory_object.hpp
==============================================================================
--- trunk/boost/interprocess/shared_memory_object.hpp (original)
+++ trunk/boost/interprocess/shared_memory_object.hpp 2007-11-24 14:47:58 EST (Sat, 24 Nov 2007)
@@ -19,6 +19,7 @@
 #include <boost/interprocess/interprocess_fwd.hpp>
 #include <boost/interprocess/exceptions.hpp>
 #include <boost/interprocess/detail/os_file_functions.hpp>
+#include <boost/interprocess/detail/tmp_dir_helpers.hpp>
 #include <cstddef>
 #include <string>
 #include <cstdio> //std::remove
@@ -30,7 +31,7 @@
 # include <unistd.h> //ftruncate, close
 # include <sys/stat.h> //mode_t, S_IRWXG, S_IRWXO, S_IRWXU,
 #else
-# include<boost/interprocess/detail/os_file_functions.hpp>
+//
 #endif
 
 //!\file
@@ -140,14 +141,6 @@
    //!Closes a previously opened file mapping. Never throws.
    void priv_close();
 
- //!Create a temporary file name
- static void priv_tmp_filename(const char *name, std::string &tmp_name);
-
- //!Create a temporary file name
- static void priv_create_tmp_dir_and_get_filename(const char *name, std::string &tmp_name);
-
- static void priv_add_leading_slash(const char *name, std::string &new_name);
-
    //!Closes a previously opened file mapping. Never throws.
    bool priv_open_or_create(detail::create_enum_t type, const char *filename, mode_t mode);
 
@@ -184,56 +177,6 @@
 inline mode_t shared_memory_object::get_mode() const
 { return m_mode; }
 
-inline void shared_memory_object::priv_tmp_filename
- (const char *filename, std::string &tmp_name)
-{
- const char *tmp_dir = detail::get_temporary_path();
- if(!tmp_dir){
- error_info err = system_error_code();
- throw interprocess_exception(err);
- }
- tmp_name = tmp_dir;
-
- //Remove final null.
- tmp_name += "/boost_interprocess/";
- tmp_name += filename;
-}
-
-inline void shared_memory_object::priv_create_tmp_dir_and_get_filename
- (const char *filename, std::string &tmp_name)
-{
- const char *tmp_path = detail::get_temporary_path();
- if(!tmp_path){
- error_info err = system_error_code();
- throw interprocess_exception(err);
- }
-
- tmp_name = tmp_path;
- tmp_name += "/boost_interprocess";
-
- //Create the temporary directory.
- //If fails, check that it's because already exists
- if(!detail::create_directory(tmp_name.c_str())){
- error_info info(system_error_code());
- if(info.get_error_code() != already_exists_error){
- throw interprocess_exception(info);
- }
- }
-
- //Add filename
- tmp_name += '/';
- tmp_name += filename;
-}
-
-inline void shared_memory_object::priv_add_leading_slash
- (const char *name, std::string &new_name)
-{
- if(name[0] != '/'){
- new_name = '/';
- }
- new_name += name;
-}
-
 #if !defined(BOOST_INTERPROCESS_POSIX_SHARED_MEMORY_OBJECTS)
 
 inline bool shared_memory_object::priv_open_or_create
@@ -241,7 +184,7 @@
 {
    m_filename = filename;
    std::string shmfile;
- priv_create_tmp_dir_and_get_filename(filename, shmfile);
+ detail::create_tmp_dir_and_get_filename(filename, shmfile);
 
    //Set accesses
    if (mode != read_write && mode != read_only){
@@ -282,7 +225,7 @@
    try{
       //Make sure a temporary path is created for shared memory
       std::string shmfile;
- priv_tmp_filename(filename, shmfile);
+ detail::tmp_filename(filename, shmfile);
       return std::remove(shmfile.c_str()) == 0;
    }
    catch(...){
@@ -314,9 +257,9 @@
     mode_t mode)
 {
    #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SHARED_MEMORY
- priv_add_leading_slash(filename, m_filename);
+ detail::add_leading_slash(filename, m_filename);
    #else
- priv_create_tmp_dir_and_get_filename(filename, m_filename);
+ detail::create_tmp_dir_and_get_filename(filename, m_filename);
    #endif
 
    //Create new mapping
@@ -369,9 +312,9 @@
    try{
       std::string file_str;
       #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SHARED_MEMORY
- priv_add_leading_slash(filename, file_str);
+ detail::add_leading_slash(filename, file_str);
       #else
- priv_tmp_filename(filename, file_str);
+ detail::tmp_filename(filename, file_str);
       #endif
       return 0 != shm_unlink(file_str.c_str());
    }

Modified: trunk/boost/interprocess/sync/posix/semaphore_wrapper.hpp
==============================================================================
--- trunk/boost/interprocess/sync/posix/semaphore_wrapper.hpp (original)
+++ trunk/boost/interprocess/sync/posix/semaphore_wrapper.hpp 2007-11-24 14:47:58 EST (Sat, 24 Nov 2007)
@@ -15,9 +15,16 @@
 #include <boost/interprocess/exceptions.hpp>
 #include <boost/interprocess/creation_tags.hpp>
 #include <boost/interprocess/detail/os_file_functions.hpp>
+#include <boost/interprocess/detail/tmp_dir_helpers.hpp>
 #include <string>
 #include <semaphore.h>
 
+#ifdef SEM_FAILED
+#define BOOST_INTERPROCESS_POSIX_SEM_FAILED SEM_FAILED
+#else
+#define BOOST_INTERPROCESS_POSIX_SEM_FAILED ((sem_t*)(-1))
+#endif
+
 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
 #else
@@ -37,14 +44,12 @@
    (sem_t *&handle, detail::create_enum_t type, const char *origname, mode_t mode,
     unsigned int count)
 {
- bool slash_added = origname[0] != '/';
- //First add preceding "/"
    std::string name;
-
- if(slash_added){
- name = '/';
- }
- name += origname;
+ #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SEMAPHORES
+ detail::add_leading_slash(origname, name);
+ #else
+ detail::create_tmp_dir_and_get_filename(origname, name);
+ #endif
 
    //Create new mapping
    int oflag = 0;
@@ -83,14 +88,10 @@
       handle = sem_open(name.c_str(), oflag);
 
    //Check for error
- if(handle == SEM_FAILED){
+ if(handle == BOOST_INTERPROCESS_POSIX_SEM_FAILED){
       throw interprocess_exception(error_info(errno));
    }
 
- if(slash_added){
- name.erase(name.begin());
- }
-
    return true;
 }
 
@@ -102,16 +103,16 @@
    }
 }
 
-inline bool semaphore_unlink(const char *name)
+inline bool semaphore_unlink(const char *semname)
 {
    try{
- std::string str;
- //First add preceding "/"
- if(name[0] != '/'){
- str = '/';
- }
- str += name;
- return 0 != sem_unlink(str.c_str());
+ std::string sem_str;
+ #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SEMAPHORES
+ detail::add_leading_slash(semname, sem_str);
+ #else
+ detail::tmp_filename(semname, sem_str);
+ #endif
+ return 0 != sem_unlink(sem_str.c_str());
    }
    catch(...){
       return false;
@@ -121,7 +122,10 @@
 inline void semaphore_init(sem_t *handle, int initialCount)
 {
    int ret = sem_init(handle, 1, initialCount);
- if(ret != 0){
+ //According to SUSV3 version 2003 edition, the return value of a successful
+ //sem_init call is not defined, but -1 is returned on failure.
+ //In the future, a successful call might be required to return 0.
+ if(ret == -1){
       throw interprocess_exception(system_error_code());
    }
 }
@@ -190,14 +194,7 @@
    return false;
    #endif //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
 }
-/*
-inline int semaphore_get_count(sem_t *handle)
-{
- int count;
- sem_getvalue(handle, &ret);
- return count;
-}
-*/
+
 
 class named_semaphore_wrapper
 {
@@ -212,7 +209,7 @@
 
    ~named_semaphore_wrapper()
    {
- if(mp_sem != SEM_FAILED)
+ if(mp_sem != BOOST_INTERPROCESS_POSIX_SEM_FAILED)
          semaphore_close(mp_sem);
    }
 
@@ -234,7 +231,7 @@
    private:
    friend class detail::interprocess_tester;
    void dont_close_on_destruction()
- { mp_sem = SEM_FAILED; }
+ { mp_sem = BOOST_INTERPROCESS_POSIX_SEM_FAILED; }
 
    sem_t *mp_sem;
 };
@@ -272,4 +269,6 @@
 } //namespace interprocess {
 } //namespace boost {
 
+#undef BOOST_INTERPROCESS_POSIX_SEM_FAILED
+
 #endif //#ifndef BOOST_INTERPROCESS_POSIX_SEMAPHORE_WRAPPER_HPP

Modified: trunk/libs/interprocess/proj/vc7ide/interprocesslib.vcproj
==============================================================================
--- trunk/libs/interprocess/proj/vc7ide/interprocesslib.vcproj (original)
+++ trunk/libs/interprocess/proj/vc7ide/interprocesslib.vcproj 2007-11-24 14:47:58 EST (Sat, 24 Nov 2007)
@@ -435,6 +435,9 @@
                                 RelativePath="..\..\..\..\boost\interprocess\detail\segment_manager_helper.hpp">
                         </File>
                         <File
+ RelativePath="..\..\..\..\boost\interprocess\detail\tmp_dir_helpers.hpp">
+ </File>
+ <File
                                 RelativePath="..\..\..\..\boost\interprocess\detail\type_traits.hpp">
                         </File>
                         <File


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk