aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsamanagers/rwlock.cpp
blob: d654ce5fd4e1b06122d5aef54d183de1e5233796 (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
// Copyright Yahoo. 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.
 *
 */

#include <pthread.h>
#include <sched.h>
#include <cassert>

#include "rwlock.h"

namespace fsa {

// {{{ class RWLock::Impl

struct RWLock::Impl
{
  pthread_rwlock_t _rwlock;   /**< Lock.  */
};

// }}}

RWLock::RWLock(void) : _impl(new Impl)
{
  int rc;
  rc = pthread_rwlock_init(&(_impl->_rwlock),NULL);
  assert(rc == 0);
}

RWLock::~RWLock(void)
{
  pthread_rwlock_destroy(&(_impl->_rwlock));
  delete _impl;
}

bool RWLock::tryRdLock (void)
{
  return pthread_rwlock_tryrdlock(&(_impl->_rwlock)) == 0;
}

bool RWLock::tryWrLock (void)
{
  return pthread_rwlock_trywrlock(&(_impl->_rwlock)) == 0;
}

bool RWLock::rdLock (void)
{
  return pthread_rwlock_rdlock(&(_impl->_rwlock)) == 0;
}

bool RWLock::wrLock (void)
{
  return pthread_rwlock_wrlock(&(_impl->_rwlock)) == 0;
}

bool RWLock::unlock (void)
{
  return pthread_rwlock_unlock(&(_impl->_rwlock)) == 0;
}

} // namespace fsa