aboutsummaryrefslogtreecommitdiffstats
path: root/modules/mux_pthr/mux_pthr.cpp
blob: ec2308faea477723a43d4681d642e7f05cb87281 (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
55
56
57
58
59
60
61
62
/*************************************************
* Pthread Mutex Source File                      *
* (C) 1999-2006 The Botan Project                *
*************************************************/

#include <botan/mux_pthr.h>
#include <botan/exceptn.h>

#ifndef _POSIX_C_SOURCE
  #define _POSIX_C_SOURCE 199506
#endif

#include <pthread.h>

namespace Botan {

namespace {

/*************************************************
* Pthread Mutex                                  *
*************************************************/
class Pthread_Mutex : public Mutex
   {
   public:
      void lock()
         {
         if(pthread_mutex_lock(&mutex) != 0)
            throw Exception("Pthread_Mutex::lock: Error occured");
         }

      void unlock()
         {
         if(pthread_mutex_unlock(&mutex) != 0)
            throw Exception("Pthread_Mutex::unlock: Error occured");
         }

      Pthread_Mutex()
         {
         if(pthread_mutex_init(&mutex, 0) != 0)
            throw Exception("Pthread_Mutex: initialization failed");
         }

      ~Pthread_Mutex()
         {
         if(pthread_mutex_destroy(&mutex) != 0)
            throw Invalid_State("~Pthread_Mutex: mutex is still locked");
         }
   private:
      pthread_mutex_t mutex;
   };

}

/*************************************************
* Pthread Mutex Factory                          *
*************************************************/
Mutex* Pthread_Mutex_Factory::make()
   {
   return new Pthread_Mutex();
   }

}