Imported Upstream version 1.49.0
[platform/upstream/boost.git] / boost / interprocess / sync / windows / semaphore.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2011. 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_DETAIL_WINDOWS_SEMAPHORE_HPP
12 #define BOOST_INTERPROCESS_DETAIL_WINDOWS_SEMAPHORE_HPP
13
14 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
15 #  pragma once
16 #endif
17
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
21 #include <boost/interprocess/detail/win32_api.hpp>
22 #include <boost/interprocess/detail/intermodule_singleton.hpp>
23 #include <boost/interprocess/sync/windows/sync_utils.hpp>
24 #include <boost/interprocess/exceptions.hpp>
25
26
27 namespace boost {
28 namespace interprocess {
29 namespace ipcdetail {
30
31 class windows_semaphore
32 {
33    windows_semaphore(const windows_semaphore &);
34    windows_semaphore &operator=(const windows_semaphore &);
35    public:
36
37    windows_semaphore(unsigned int initialCount);
38    ~windows_semaphore();
39
40    void post(long release_count = 1);
41    void wait();
42    bool try_wait();
43    bool timed_wait(const boost::posix_time::ptime &abs_time);
44
45    private:
46    const sync_id id_;
47 };
48
49 inline windows_semaphore::windows_semaphore(unsigned int initialCount) 
50    : id_()
51 {
52    sync_handles &handles =
53       intermodule_singleton<sync_handles>::get();
54    //Force smeaphore creation with the initial count
55    bool open_or_created;
56    handles.obtain_semaphore(this->id_, initialCount, &open_or_created);
57    //The semaphore must be created, never opened
58    assert(open_or_created);
59    assert(open_or_created && winapi::get_last_error() != winapi::error_already_exists);
60    (void)open_or_created;
61 }
62
63 inline windows_semaphore::~windows_semaphore() 
64 {
65    sync_handles &handles =
66       intermodule_singleton<sync_handles>::get();
67    handles.destroy_handle(this->id_);
68 }
69
70 inline void windows_semaphore::wait(void)
71 {
72    sync_handles &handles =
73       intermodule_singleton<sync_handles>::get();
74    //This can throw
75    void *hnd = handles.obtain_semaphore(this->id_, 0);
76    unsigned long ret = winapi::wait_for_single_object(hnd, winapi::infinite_time);
77    if(ret == winapi::wait_failed){
78       error_info err(winapi::get_last_error());
79       throw interprocess_exception(err);
80    }
81 }
82
83 inline bool windows_semaphore::try_wait(void)
84 {
85    sync_handles &handles =
86       intermodule_singleton<sync_handles>::get();
87    //This can throw
88    void *hnd = handles.obtain_semaphore(this->id_, 0);
89    unsigned long ret = winapi::wait_for_single_object(hnd, 0);
90    if(ret == winapi::wait_failed){
91       error_info err(winapi::get_last_error());
92       throw interprocess_exception(err);
93    }
94    return ret != winapi::wait_timeout;
95 }
96
97 inline bool windows_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
98 {
99    if(abs_time == boost::posix_time::pos_infin){
100       this->wait();
101       return true;
102    }
103    boost::posix_time::ptime now
104       = boost::posix_time::microsec_clock::universal_time();
105
106    unsigned long ms = (unsigned long)(abs_time-now).total_milliseconds();
107    sync_handles &handles =
108       intermodule_singleton<sync_handles>::get();
109    //This can throw
110    void *hnd = handles.obtain_semaphore(this->id_, 0);
111    unsigned long ret = winapi::wait_for_single_object(hnd, ms);
112    if(ret == winapi::wait_failed){
113       error_info err(winapi::get_last_error());
114       throw interprocess_exception(err);
115    }
116    return ret != winapi::wait_timeout;
117 }
118
119 inline void windows_semaphore::post(long release_count)
120 {
121    sync_handles &handles =
122       intermodule_singleton<sync_handles>::get();
123    //This can throw
124    void *hnd = handles.obtain_semaphore(this->id_, 0);
125    long prev_count;
126    int ret = winapi::release_semaphore(hnd, release_count, &prev_count);
127    (void)ret;
128    assert(ret);
129 }
130
131 }  //namespace ipcdetail {
132 }  //namespace interprocess {
133 }  //namespace boost {
134
135 #include <boost/interprocess/detail/config_end.hpp>
136
137 #endif   //BOOST_INTERPROCESS_DETAIL_WINDOWS_SEMAPHORE_HPP