[go: up one dir, main page]

Menu

[408bb9]: / common / semaphore.h  Maximize  Restore  History

Download this file

129 lines (118 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef SEMAPHORE_HEADER
#define SEMAPHORE_HEADER
/***************************************************************************
* semaphore.h
*
* Sat Aug 24 23:53:49 2002
* Copyright 2002 Roman Dementiev
* dementiev@mpi-sb.mpg.de
****************************************************************************/
#ifdef STXXL_BOOST_THREADS
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#else
#include <pthread.h>
#endif
#include "utils.h"
namespace stxxl
{
class semaphore
{
int v;
#ifdef STXXL_BOOST_THREADS
boost::mutex mutex;
boost::condition cond;
#else
pthread_mutex_t mutex;
pthread_cond_t cond;
#endif
semaphore(const semaphore &); // forbidden
semaphore & operator = (const semaphore &); // forbidden
public:
semaphore (int init_value = 1):v (init_value)
{
#ifndef STXXL_BOOST_THREADS
stxxl_nassert (pthread_mutex_init (&mutex, NULL));
stxxl_nassert (pthread_cond_init (&cond, NULL));
#endif
};
~semaphore ()
{
#ifndef STXXL_BOOST_THREADS
int res = pthread_mutex_trylock (&mutex);
if (res == 0 || res == EBUSY)
stxxl_nassert (pthread_mutex_unlock
(&mutex))
else
stxxl_function_error
stxxl_nassert (pthread_mutex_destroy
(&mutex));
stxxl_nassert (pthread_cond_destroy (&cond));
#endif
}
// function increments the semaphore and signals any threads that
// are blocked waiting a change in the semaphore
int operator ++ (int)
{
#ifdef STXXL_BOOST_THREADS
boost::mutex::scoped_lock Lock(mutex);
int res = ++v;
Lock.unlock();
cond.notify_one();
#else
stxxl_nassert (pthread_mutex_lock (&mutex));
int res = ++v;
stxxl_nassert (pthread_mutex_unlock (&mutex));
stxxl_nassert (pthread_cond_signal (&cond));
#endif
return res;
};
// function decrements the semaphore and blocks if the semaphore is
// <= 0 until another thread signals a change
int operator -- (int)
{
#ifdef STXXL_BOOST_THREADS
boost::mutex::scoped_lock Lock(mutex);
while (v <= 0)
cond.wait(Lock);
int res = --v;
#else
stxxl_nassert (pthread_mutex_lock (&mutex));
while (v <= 0)
stxxl_nassert (pthread_cond_wait
(&cond, &mutex));
int res = --v;
stxxl_nassert (pthread_mutex_unlock (&mutex));
#endif
return res;
};
// function does NOT block but simply decrements the semaphore
// should not be used instead of down -- only for programs where
// multiple threads must up on a semaphore before another thread
// can go down, i.e., allows programmer to set the semaphore to
// a negative value prior to using it for synchronization.
int decrement ()
{
#ifdef STXXL_BOOST_THREADS
boost::mutex::scoped_lock Lock(mutex);
return (--v);
#else
stxxl_nassert (pthread_mutex_lock (&mutex));
int res = --v;
stxxl_nassert (pthread_mutex_unlock (&mutex));
return res;
#endif
};
// function returns the value of the semaphore at the time the
// critical section is accessed. obviously the value is not guarenteed
// after the function unlocks the critical section.
//int operator()
//{
// stxxl_nassert(pthread_mutex_lock(&mutex));
// int res = v;
// stxxl_nassert(pthread_mutex_unlock(&mutex));
// return res;
//};
};
}
#endif