STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
semaphore.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/semaphore.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2013 Timo Bingmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_COMMON_SEMAPHORE_HEADER
15 #define STXXL_COMMON_SEMAPHORE_HEADER
16 
17 #include <stxxl/bits/noncopyable.h>
20 
21 
23 
24 class semaphore : private noncopyable
25 {
26  //! value of the semaphore
27  int v;
28 
29  //! mutex for condition variable
31 
32  //! condition variable
34 
35 public:
36  //! construct semaphore
37  semaphore(int init_value = 1)
38  : v(init_value)
39  { }
40  //! function increments the semaphore and signals any threads that are
41  //! blocked waiting a change in the semaphore
42  int operator ++ (int)
43  {
44  scoped_mutex_lock lock(m_mutex);
45  int res = ++v;
46  lock.unlock();
47  m_cond.notify_one();
48  return res;
49  }
50  //! function decrements the semaphore and blocks if the semaphore is <= 0
51  //! until another thread signals a change
52  int operator -- (int)
53  {
54  scoped_mutex_lock lock(m_mutex);
55  while (v <= 0)
56  m_cond.wait(lock);
57 
58  return --v;
59  }
60  //! function does NOT block but simply decrements the semaphore should not
61  //! be used instead of down -- only for programs where multiple threads
62  //! must up on a semaphore before another thread can go down, i.e., allows
63  //! programmer to set the semaphore to a negative value prior to using it
64  //! for synchronization.
65  int decrement()
66  {
67  scoped_mutex_lock lock(m_mutex);
68  return --v;
69  }
70 #if 0
71  //! function returns the value of the semaphore at the time the
72  //! critical section is accessed. obviously the value is not guaranteed
73  //! after the function unlocks the critical section.
74  int get_value()
75  {
76  scoped_mutex_lock lock(m_mutex);
77  return v;
78  }
79 #endif
80 };
81 
83 
84 #endif // !STXXL_COMMON_SEMAPHORE_HEADER
int v
value of the semaphore
Definition: semaphore.h:27
void unlock()
unlock mutex hold prematurely
Definition: mutex.h:127
mutex m_mutex
mutex for condition variable
Definition: semaphore.h:30
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:166
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:106
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
uint_pair & operator--()
prefix decrement operator (directly manipulates the integer parts)
Definition: uint_types.h:176
int decrement()
function does NOT block but simply decrements the semaphore should not be used instead of down – only...
Definition: semaphore.h:65
semaphore(int init_value=1)
construct semaphore
Definition: semaphore.h:37
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
condition_variable m_cond
condition variable
Definition: semaphore.h:33