STXXL  1.4.1
 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 
22 
23 class semaphore : private noncopyable
24 {
25  //! value of the semaphore
26  int v;
27 
28  //! mutex for condition variable
30 
31  //! condition variable
33 
34 public:
35  //! construct semaphore
36  semaphore(int init_value = 1)
37  : v(init_value)
38  { }
39  //! function increments the semaphore and signals any threads that are
40  //! blocked waiting a change in the semaphore
41  int operator ++ (int)
42  {
43  scoped_mutex_lock lock(m_mutex);
44  int res = ++v;
45  lock.unlock();
46  m_cond.notify_one();
47  return res;
48  }
49  //! function decrements the semaphore and blocks if the semaphore is <= 0
50  //! until another thread signals a change
51  int operator -- (int)
52  {
53  scoped_mutex_lock lock(m_mutex);
54  while (v <= 0)
55  m_cond.wait(lock);
56 
57  return --v;
58  }
59  //! function does NOT block but simply decrements the semaphore should not
60  //! be used instead of down -- only for programs where multiple threads
61  //! must up on a semaphore before another thread can go down, i.e., allows
62  //! programmer to set the semaphore to a negative value prior to using it
63  //! for synchronization.
64  int decrement()
65  {
66  scoped_mutex_lock lock(m_mutex);
67  return --v;
68  }
69 #if 0
70  //! function returns the value of the semaphore at the time the
71  //! critical section is accessed. obviously the value is not guaranteed
72  //! after the function unlocks the critical section.
73  int get_value()
74  {
75  scoped_mutex_lock lock(m_mutex);
76  return v;
77  }
78 #endif
79 };
80 
82 
83 #endif // !STXXL_COMMON_SEMAPHORE_HEADER
int v
value of the semaphore
Definition: semaphore.h:26
void unlock()
unlock mutex hold prematurely
Definition: mutex.h:126
mutex m_mutex
mutex for condition variable
Definition: semaphore.h:29
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:163
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:105
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
uint_pair & operator--()
prefix decrement operator (directly manipulates the integer parts)
Definition: uint_types.h:173
int decrement()
function does NOT block but simply decrements the semaphore should not be used instead of down – only...
Definition: semaphore.h:64
semaphore(int init_value=1)
construct semaphore
Definition: semaphore.h:36
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
condition_variable m_cond
condition variable
Definition: semaphore.h:32