summaryrefslogtreecommitdiff
path: root/libcult/cult/sched/mutex.cxx
blob: 9667a67f42b3735cd2855a6f530e86ec0315ed18 (plain)
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
// file      : cult/sched/mutex.cxx
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2005-2010 Boris Kolpackov
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <cult/sched/mutex.hxx>
#include <cult/sched/exception.hxx>

namespace Cult
{
  namespace Sched
  {
    Mutex::
    ~Mutex ()
    {
      if (Int e = pthread_mutex_destroy (&mutex_))
        throw Implementation (e);
    }

    Mutex::
    Mutex ()
    {
      if (Int e = pthread_mutex_init (&mutex_, 0))
        throw Implementation (e);
    }

    Void Mutex::
    lock ()
    {
      if (Int e = pthread_mutex_lock (&mutex_))
        throw Implementation (e);
    }

    Boolean Mutex::
    try_lock ()
    {
      Int e (pthread_mutex_trylock (&mutex_));

      switch (e)
      {
      case 0: return true;
      case EBUSY: return false;
      default: throw Implementation (e);
      }
    }

    Void Mutex::
    unlock ()
    {
      if (Int e = pthread_mutex_unlock (&mutex_))
        throw Implementation (e);
    }
  }
}