• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP
12 #define BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP
13 
14 #ifndef BOOST_CONFIG_HPP
15 #  include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 #  pragma once
20 #endif
21 
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/creation_tags.hpp>
25 #include <boost/interprocess/exceptions.hpp>
26 #include <boost/interprocess/permissions.hpp>
27 #include <boost/interprocess/detail/interprocess_tester.hpp>
28 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
29 
30 #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
31 #include <boost/interprocess/sync/posix/named_semaphore.hpp>
32 //Experimental...
33 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
34    #include <boost/interprocess/sync/windows/named_semaphore.hpp>
35    #define BOOST_INTERPROCESS_USE_WINDOWS
36 #else
37 #include <boost/interprocess/sync/shm/named_semaphore.hpp>
38 #endif
39 
40 //!\file
41 //!Describes a named semaphore class for inter-process synchronization
42 
43 namespace boost {
44 namespace interprocess {
45 
46 //!A semaphore with a global name, so it can be found from different
47 //!processes. Allows several resource sharing patterns and efficient
48 //!acknowledgment mechanisms.
49 class named_semaphore
50 {
51    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
52 
53    //Non-copyable
54    named_semaphore();
55    named_semaphore(const named_semaphore &);
56    named_semaphore &operator=(const named_semaphore &);
57    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
58 
59    public:
60    //!Creates a global semaphore with a name, and an initial count.
61    //!If the semaphore can't be created throws interprocess_exception
62    named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
63 
64    //!Opens or creates a global semaphore with a name, and an initial count.
65    //!If the semaphore is created, this call is equivalent to
66    //!named_semaphore(create_only_t, ...)
67    //!If the semaphore is already created, this call is equivalent to
68    //!named_semaphore(open_only_t, ... )
69    //!and initialCount is ignored.
70    named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
71 
72    //!Opens a global semaphore with a name if that semaphore is previously.
73    //!created. If it is not previously created this function throws
74    //!interprocess_exception.
75    named_semaphore(open_only_t, const char *name);
76 
77    //!Destroys *this and indicates that the calling process is finished using
78    //!the resource. The destructor function will deallocate
79    //!any system resources allocated by the system for use by this process for
80    //!this resource. The resource can still be opened again calling
81    //!the open constructor overload. To erase the resource from the system
82    //!use remove().
83    ~named_semaphore();
84 
85    //!Increments the semaphore count. If there are processes/threads blocked waiting
86    //!for the semaphore, then one of these processes will return successfully from
87    //!its wait function. If there is an error an interprocess_exception exception is thrown.
88    void post();
89 
90    //!Decrements the semaphore. If the semaphore value is not greater than zero,
91    //!then the calling process/thread blocks until it can decrement the counter.
92    //!If there is an error an interprocess_exception exception is thrown.
93    void wait();
94 
95    //!Decrements the semaphore if the semaphore's value is greater than zero
96    //!and returns true. If the value is not greater than zero returns false.
97    //!If there is an error an interprocess_exception exception is thrown.
98    bool try_wait();
99 
100    //!Decrements the semaphore if the semaphore's value is greater
101    //!than zero and returns true. Otherwise, waits for the semaphore
102    //!to the posted or the timeout expires. If the timeout expires, the
103    //!function returns false. If the semaphore is posted the function
104    //!returns true. If there is an error throws sem_exception
105    bool timed_wait(const boost::posix_time::ptime &abs_time);
106 
107    //!Erases a named semaphore from the system.
108    //!Returns false on error. Never throws.
109    static bool remove(const char *name);
110 
111    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
112    private:
113    friend class ipcdetail::interprocess_tester;
114    void dont_close_on_destruction();
115 
116    #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
117       typedef ipcdetail::posix_named_semaphore   impl_t;
118    #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
119       #undef BOOST_INTERPROCESS_USE_WINDOWS
120       typedef ipcdetail::windows_named_semaphore impl_t;
121    #else
122       typedef ipcdetail::shm_named_semaphore     impl_t;
123    #endif
124    impl_t m_sem;
125    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
126 };
127 
128 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
129 
named_semaphore(create_only_t,const char * name,unsigned int initialCount,const permissions & perm)130 inline named_semaphore::named_semaphore
131    (create_only_t, const char *name, unsigned int initialCount, const permissions &perm)
132    :  m_sem(create_only, name, initialCount, perm)
133 {}
134 
named_semaphore(open_or_create_t,const char * name,unsigned int initialCount,const permissions & perm)135 inline named_semaphore::named_semaphore
136    (open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm)
137    :  m_sem(open_or_create, name, initialCount, perm)
138 {}
139 
named_semaphore(open_only_t,const char * name)140 inline named_semaphore::named_semaphore(open_only_t, const char *name)
141    :  m_sem(open_only, name)
142 {}
143 
~named_semaphore()144 inline named_semaphore::~named_semaphore()
145 {}
146 
dont_close_on_destruction()147 inline void named_semaphore::dont_close_on_destruction()
148 {  ipcdetail::interprocess_tester::dont_close_on_destruction(m_sem);  }
149 
wait()150 inline void named_semaphore::wait()
151 {  m_sem.wait();  }
152 
post()153 inline void named_semaphore::post()
154 {  m_sem.post();  }
155 
try_wait()156 inline bool named_semaphore::try_wait()
157 {  return m_sem.try_wait();  }
158 
timed_wait(const boost::posix_time::ptime & abs_time)159 inline bool named_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
160 {  return m_sem.timed_wait(abs_time);  }
161 
remove(const char * name)162 inline bool named_semaphore::remove(const char *name)
163 {  return impl_t::remove(name);   }
164 
165 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
166 
167 }  //namespace interprocess {
168 }  //namespace boost {
169 
170 
171 #include <boost/interprocess/detail/config_end.hpp>
172 
173 #endif   //BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP
174