blob: 9c296dfe9805c853e003af568827cbcde828d940 (
plain) (
blame)
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
* @author Peter Boros
* @date 2004/09/07
* @version $Id$
* @file rwlock.cpp
* @brief Read-write lock.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef DISABLE_THREADS
#include <pthread.h>
#include <sched.h>
#include <assert.h>
#endif
#include "rwlock.h"
namespace fsa {
// {{{ class RWLock::Impl
struct RWLock::Impl
{
#ifndef DISABLE_THREADS
pthread_rwlock_t _rwlock; /**< Lock. */
#else
int _rwlock;
#endif
};
// }}}
RWLock::RWLock(void) : _impl(new Impl)
{
#ifndef DISABLE_THREADS
int rc;
rc = pthread_rwlock_init(&(_impl->_rwlock),NULL);
assert(rc == 0);
#endif
}
RWLock::~RWLock(void)
{
#ifndef DISABLE_THREADS
pthread_rwlock_destroy(&(_impl->_rwlock));
#endif
}
bool RWLock::tryRdLock (void)
{
#ifndef DISABLE_THREADS
return pthread_rwlock_tryrdlock(&(_impl->_rwlock)) == 0;
#else
return true;
#endif
}
bool RWLock::tryWrLock (void)
{
#ifndef DISABLE_THREADS
return pthread_rwlock_trywrlock(&(_impl->_rwlock)) == 0;
#else
return true;
#endif
}
bool RWLock::rdLock (void)
{
#ifndef DISABLE_THREADS
return pthread_rwlock_rdlock(&(_impl->_rwlock)) == 0;
#else
return true;
#endif
}
bool RWLock::wrLock (void)
{
#ifndef DISABLE_THREADS
return pthread_rwlock_wrlock(&(_impl->_rwlock)) == 0;
#else
return true;
#endif
}
bool RWLock::unlock (void)
{
#ifndef DISABLE_THREADS
return pthread_rwlock_unlock(&(_impl->_rwlock)) == 0;
#else
return true;
#endif
}
} // namespace fsa
|