Merge branch 'master' into scmaster
[nova-tt.git] / nova-tt / semaphore_win32.hpp
blob57234e408eec2da31d52c94a97d71cc4cd7d15d7
1 // semaphore class, win32 wrapper
2 // based on API proposed in N2043
3 //
4 // Copyright (C) 2008, 2009 Tim Blechmann
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; see the file COPYING. If not, write to
18 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, USA.
21 #ifndef NOVA_TT_SEMAPHORE_WIN32_HPP
22 #define NOVA_TT_SEMAPHORE_WIN32_HPP
24 #include <cassert>
25 #include <limits>
27 #include <boost/noncopyable.hpp>
28 #include <boost/static_assert.hpp>
30 #include <windows.h>
32 namespace nova {
33 namespace nova_tt {
35 /** semaphore class */
36 template <bool has_timed_wait = false>
37 class semaphore:
38 boost::noncopyable
40 public:
41 semaphore(unsigned int i=0)
43 ghSemaphore = CreateSemaphore(
44 NULL, // default security attributes
45 i, // initial count
46 (std::numeric_limits<LONG>::numeric_limits::max)(), // maximum count
47 NULL); // unnamed semaphore
48 assert(ghSemaphore != NULL);
49 //TODO: take care of NULL return -> exception
50 //printf("CreateSemaphore error: %d\n", GetLastError());
53 ~semaphore(void)
55 CloseHandle(ghSemaphore);
58 /** signal semaphore */
59 void post(void)
61 int status = ReleaseSemaphore(
62 ghSemaphore, // handle to semaphore
63 1, // increase count by one
64 NULL); // not interested in previous count
65 assert(status != 0);
66 //TODO report:
67 //printf("ReleaseSemaphore error: %d\n", GetLastError());
70 /** wait until this semaphore is signaled */
71 void wait(void)
73 int status = WaitForSingleObject(
74 ghSemaphore,
75 INFINITE); // wait forever
76 assert(status == 0);
77 //TODO: report error status
80 /** try to wait for the semaphore
82 * \return true, if the value can be decremented
83 * false, otherweise
85 bool try_wait(void)
87 int status = WaitForSingleObject(
88 ghSemaphore,
89 0L); // zero-second time-out interval
90 return status == 0;
93 /** try to wait for the semaphore until timeout
95 * \return true, if the value can be decremented
96 * false, otherweise
98 bool timed_wait(struct timespec const & absolute_timeout)
100 long milisecons = 0 ;//TODO absolute_timeout - now
101 BOOST_STATIC_ASSERT(has_timed_wait);
102 int status = WaitForSingleObject(
103 ghSemaphore,
104 milisecons); // zero-second time-out interval
105 return status == 0;
108 int value(void)
110 int ret;
111 bool status = ReleaseSemaphore( ghSemaphore, 0, (long*)&ret );
112 assert(status == 0);
113 if (ret < 0)
114 return 0;
115 else
116 return ret;
119 private:
120 HANDLE ghSemaphore;
123 } // namespace nova_tt
124 } // namespace nova
126 #endif /* NOVA_TT_SEMAPHORE_WIN32_HPP */