1 // semaphore class, win32 wrapper
2 // based on API proposed in N2043
4 // Copyright (C) 2008, 2009 Tim Blechmann
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
27 #include <boost/noncopyable.hpp>
28 #include <boost/static_assert.hpp>
35 /** semaphore class */
36 template <bool has_timed_wait
= false>
41 semaphore(unsigned int i
=0)
43 ghSemaphore
= CreateSemaphore(
44 NULL
, // default security attributes
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());
55 CloseHandle(ghSemaphore
);
58 /** signal semaphore */
61 int status
= ReleaseSemaphore(
62 ghSemaphore
, // handle to semaphore
63 1, // increase count by one
64 NULL
); // not interested in previous count
67 //printf("ReleaseSemaphore error: %d\n", GetLastError());
70 /** wait until this semaphore is signaled */
73 int status
= WaitForSingleObject(
75 INFINITE
); // wait forever
77 //TODO: report error status
80 /** try to wait for the semaphore
82 * \return true, if the value can be decremented
87 int status
= WaitForSingleObject(
89 0L); // zero-second time-out interval
93 /** try to wait for the semaphore until timeout
95 * \return true, if the value can be decremented
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(
104 milisecons
); // zero-second time-out interval
111 bool status
= ReleaseSemaphore( ghSemaphore
, 0, (long*)&ret
);
123 } // namespace nova_tt
126 #endif /* NOVA_TT_SEMAPHORE_WIN32_HPP */