]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // (C) Copyright Ion Gaztanaga 2011-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_WINDOWS_NAMED_SEMAPHORE_HPP | |
12 | #define BOOST_INTERPROCESS_WINDOWS_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/permissions.hpp> | |
26 | #include <boost/interprocess/detail/interprocess_tester.hpp> | |
27 | #include <boost/interprocess/detail/posix_time_types_wrk.hpp> | |
28 | #include <boost/interprocess/sync/windows/named_sync.hpp> | |
29 | #include <boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp> | |
30 | ||
31 | namespace boost { | |
32 | namespace interprocess { | |
33 | namespace ipcdetail { | |
34 | ||
35 | ||
36 | ||
37 | class windows_named_semaphore | |
38 | { | |
39 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
40 | ||
41 | //Non-copyable | |
42 | windows_named_semaphore(); | |
43 | windows_named_semaphore(const windows_named_semaphore &); | |
44 | windows_named_semaphore &operator=(const windows_named_semaphore &); | |
45 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
46 | ||
47 | public: | |
48 | windows_named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions()); | |
49 | ||
50 | windows_named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions()); | |
51 | ||
52 | windows_named_semaphore(open_only_t, const char *name); | |
53 | ||
54 | ~windows_named_semaphore(); | |
55 | ||
56 | void post(); | |
57 | void wait(); | |
58 | bool try_wait(); | |
59 | bool timed_wait(const boost::posix_time::ptime &abs_time); | |
60 | ||
61 | static bool remove(const char *name); | |
62 | ||
63 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
64 | private: | |
65 | friend class interprocess_tester; | |
66 | void dont_close_on_destruction(); | |
67 | winapi_semaphore_wrapper m_sem_wrapper; | |
68 | windows_named_sync m_named_sync; | |
69 | ||
70 | class named_sem_callbacks : public windows_named_sync_interface | |
71 | { | |
72 | public: | |
73 | typedef __int64 sem_count_t; | |
74 | named_sem_callbacks(winapi_semaphore_wrapper &sem_wrapper, sem_count_t sem_cnt) | |
75 | : m_sem_wrapper(sem_wrapper), m_sem_count(sem_cnt) | |
76 | {} | |
77 | ||
78 | virtual std::size_t get_data_size() const | |
79 | { return sizeof(sem_count_t); } | |
80 | ||
81 | virtual const void *buffer_with_final_data_to_file() | |
82 | { return &m_sem_count; } | |
83 | ||
84 | virtual const void *buffer_with_init_data_to_file() | |
85 | { return &m_sem_count; } | |
86 | ||
87 | virtual void *buffer_to_store_init_data_from_file() | |
88 | { return &m_sem_count; } | |
89 | ||
90 | virtual bool open(create_enum_t, const char *id_name) | |
91 | { | |
92 | std::string aux_str = "Global\\bipc.sem."; | |
93 | aux_str += id_name; | |
94 | // | |
95 | permissions sem_perm; | |
96 | sem_perm.set_unrestricted(); | |
97 | bool created; | |
98 | return m_sem_wrapper.open_or_create | |
99 | ( aux_str.c_str(), static_cast<long>(m_sem_count) | |
100 | , winapi_semaphore_wrapper::MaxCount, sem_perm, created); | |
101 | } | |
102 | ||
103 | virtual void close() | |
104 | { | |
105 | m_sem_wrapper.close(); | |
106 | } | |
107 | ||
108 | virtual ~named_sem_callbacks() | |
109 | {} | |
110 | ||
111 | private: | |
112 | sem_count_t m_sem_count; | |
113 | winapi_semaphore_wrapper& m_sem_wrapper; | |
114 | }; | |
115 | ||
116 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
117 | }; | |
118 | ||
119 | inline windows_named_semaphore::~windows_named_semaphore() | |
120 | { | |
121 | named_sem_callbacks callbacks(m_sem_wrapper, m_sem_wrapper.value()); | |
122 | m_named_sync.close(callbacks); | |
123 | } | |
124 | ||
125 | inline void windows_named_semaphore::dont_close_on_destruction() | |
126 | {} | |
127 | ||
128 | inline windows_named_semaphore::windows_named_semaphore | |
129 | (create_only_t, const char *name, unsigned int initial_count, const permissions &perm) | |
130 | : m_sem_wrapper() | |
131 | { | |
132 | named_sem_callbacks callbacks(m_sem_wrapper, initial_count); | |
133 | m_named_sync.open_or_create(DoCreate, name, perm, callbacks); | |
134 | } | |
135 | ||
136 | inline windows_named_semaphore::windows_named_semaphore | |
137 | (open_or_create_t, const char *name, unsigned int initial_count, const permissions &perm) | |
138 | : m_sem_wrapper() | |
139 | { | |
140 | named_sem_callbacks callbacks(m_sem_wrapper, initial_count); | |
141 | m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks); | |
142 | } | |
143 | ||
144 | inline windows_named_semaphore::windows_named_semaphore(open_only_t, const char *name) | |
145 | : m_sem_wrapper() | |
146 | { | |
147 | named_sem_callbacks callbacks(m_sem_wrapper, 0); | |
148 | m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks); | |
149 | } | |
150 | ||
151 | inline void windows_named_semaphore::post() | |
152 | { | |
153 | m_sem_wrapper.post(); | |
154 | } | |
155 | ||
156 | inline void windows_named_semaphore::wait() | |
157 | { | |
158 | m_sem_wrapper.wait(); | |
159 | } | |
160 | ||
161 | inline bool windows_named_semaphore::try_wait() | |
162 | { | |
163 | return m_sem_wrapper.try_wait(); | |
164 | } | |
165 | ||
166 | inline bool windows_named_semaphore::timed_wait(const boost::posix_time::ptime &abs_time) | |
167 | { | |
168 | return m_sem_wrapper.timed_wait(abs_time); | |
169 | } | |
170 | ||
171 | inline bool windows_named_semaphore::remove(const char *name) | |
172 | { | |
173 | return windows_named_sync::remove(name); | |
174 | } | |
175 | ||
176 | } //namespace ipcdetail { | |
177 | } //namespace interprocess { | |
178 | } //namespace boost { | |
179 | ||
180 | #include <boost/interprocess/detail/config_end.hpp> | |
181 | ||
182 | #endif //BOOST_INTERPROCESS_WINDOWS_NAMED_SEMAPHORE_HPP |