Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_INTERPROCESS_SHM_NAMED_SEMAPHORE_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_SHM_NAMED_SEMAPHORE_HPP
|
Chris@16
|
13
|
Chris@101
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
15 # include <boost/config.hpp>
|
Chris@101
|
16 #endif
|
Chris@101
|
17 #
|
Chris@101
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
19 # pragma once
|
Chris@16
|
20 #endif
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
23 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
24 #include <boost/interprocess/creation_tags.hpp>
|
Chris@16
|
25 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
26 #include <boost/interprocess/permissions.hpp>
|
Chris@16
|
27 #include <boost/interprocess/detail/interprocess_tester.hpp>
|
Chris@16
|
28 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
29 #include <boost/interprocess/shared_memory_object.hpp>
|
Chris@16
|
30 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
|
Chris@16
|
31 #include <boost/interprocess/sync/interprocess_semaphore.hpp>
|
Chris@16
|
32 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
|
Chris@16
|
33
|
Chris@16
|
34 namespace boost {
|
Chris@16
|
35 namespace interprocess {
|
Chris@16
|
36 namespace ipcdetail {
|
Chris@16
|
37
|
Chris@16
|
38 class shm_named_semaphore
|
Chris@16
|
39 {
|
Chris@101
|
40 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
41
|
Chris@16
|
42 //Non-copyable
|
Chris@16
|
43 shm_named_semaphore();
|
Chris@16
|
44 shm_named_semaphore(const shm_named_semaphore &);
|
Chris@16
|
45 shm_named_semaphore &operator=(const shm_named_semaphore &);
|
Chris@101
|
46 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
47
|
Chris@16
|
48 public:
|
Chris@16
|
49 shm_named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
|
Chris@16
|
50
|
Chris@16
|
51 shm_named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
|
Chris@16
|
52
|
Chris@16
|
53 shm_named_semaphore(open_only_t, const char *name);
|
Chris@16
|
54
|
Chris@16
|
55 ~shm_named_semaphore();
|
Chris@16
|
56
|
Chris@16
|
57 void post();
|
Chris@16
|
58 void wait();
|
Chris@16
|
59 bool try_wait();
|
Chris@16
|
60 bool timed_wait(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
61
|
Chris@16
|
62 static bool remove(const char *name);
|
Chris@16
|
63
|
Chris@101
|
64 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
65 private:
|
Chris@16
|
66 friend class interprocess_tester;
|
Chris@16
|
67 void dont_close_on_destruction();
|
Chris@16
|
68
|
Chris@16
|
69 interprocess_semaphore *semaphore() const
|
Chris@16
|
70 { return static_cast<interprocess_semaphore*>(m_shmem.get_user_address()); }
|
Chris@16
|
71
|
Chris@16
|
72 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
|
Chris@16
|
73 open_create_impl_t m_shmem;
|
Chris@16
|
74 typedef named_creation_functor<interprocess_semaphore, int> construct_func_t;
|
Chris@101
|
75 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
76 };
|
Chris@16
|
77
|
Chris@16
|
78 inline shm_named_semaphore::~shm_named_semaphore()
|
Chris@16
|
79 {}
|
Chris@16
|
80
|
Chris@16
|
81 inline void shm_named_semaphore::dont_close_on_destruction()
|
Chris@16
|
82 { interprocess_tester::dont_close_on_destruction(m_shmem); }
|
Chris@16
|
83
|
Chris@16
|
84 inline shm_named_semaphore::shm_named_semaphore
|
Chris@16
|
85 (create_only_t, const char *name, unsigned int initialCount, const permissions &perm)
|
Chris@16
|
86 : m_shmem (create_only
|
Chris@16
|
87 ,name
|
Chris@16
|
88 ,sizeof(interprocess_semaphore) +
|
Chris@16
|
89 open_create_impl_t::ManagedOpenOrCreateUserOffset
|
Chris@16
|
90 ,read_write
|
Chris@16
|
91 ,0
|
Chris@16
|
92 ,construct_func_t(DoCreate, initialCount)
|
Chris@16
|
93 ,perm)
|
Chris@16
|
94 {}
|
Chris@16
|
95
|
Chris@16
|
96 inline shm_named_semaphore::shm_named_semaphore
|
Chris@16
|
97 (open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm)
|
Chris@16
|
98 : m_shmem (open_or_create
|
Chris@16
|
99 ,name
|
Chris@16
|
100 ,sizeof(interprocess_semaphore) +
|
Chris@16
|
101 open_create_impl_t::ManagedOpenOrCreateUserOffset
|
Chris@16
|
102 ,read_write
|
Chris@16
|
103 ,0
|
Chris@16
|
104 ,construct_func_t(DoOpenOrCreate, initialCount)
|
Chris@16
|
105 ,perm)
|
Chris@16
|
106 {}
|
Chris@16
|
107
|
Chris@16
|
108 inline shm_named_semaphore::shm_named_semaphore
|
Chris@16
|
109 (open_only_t, const char *name)
|
Chris@16
|
110 : m_shmem (open_only
|
Chris@16
|
111 ,name
|
Chris@16
|
112 ,read_write
|
Chris@16
|
113 ,0
|
Chris@16
|
114 ,construct_func_t(DoOpen, 0))
|
Chris@16
|
115 {}
|
Chris@16
|
116
|
Chris@16
|
117 inline void shm_named_semaphore::post()
|
Chris@16
|
118 { semaphore()->post(); }
|
Chris@16
|
119
|
Chris@16
|
120 inline void shm_named_semaphore::wait()
|
Chris@16
|
121 { semaphore()->wait(); }
|
Chris@16
|
122
|
Chris@16
|
123 inline bool shm_named_semaphore::try_wait()
|
Chris@16
|
124 { return semaphore()->try_wait(); }
|
Chris@16
|
125
|
Chris@16
|
126 inline bool shm_named_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
|
Chris@101
|
127 { return semaphore()->timed_wait(abs_time); }
|
Chris@16
|
128
|
Chris@16
|
129 inline bool shm_named_semaphore::remove(const char *name)
|
Chris@16
|
130 { return shared_memory_object::remove(name); }
|
Chris@16
|
131
|
Chris@16
|
132 } //namespace ipcdetail {
|
Chris@16
|
133 } //namespace interprocess {
|
Chris@16
|
134 } //namespace boost {
|
Chris@16
|
135
|
Chris@16
|
136 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
137
|
Chris@16
|
138 #endif //BOOST_INTERPROCESS_SHM_NAMED_SEMAPHORE_HPP
|