monotone

monotone Mtn Source Tree

Root/botan/mutex.cpp

1/*************************************************
2* Mutex Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/mutex.h>
7#include <botan/exceptn.h>
8#include <botan/init.h>
9
10namespace Botan {
11
12namespace {
13
14/*************************************************
15* Global Mutex Variables *
16*************************************************/
17Mutex* mutex_factory = 0;
18Mutex* mutex_init_lock = 0;
19
20/*************************************************
21* Default Mutex *
22*************************************************/
23class Default_Mutex : public Mutex
24 {
25 public:
26 void lock();
27 void unlock();
28 Mutex* clone() const { return new Default_Mutex; }
29 Default_Mutex() { locked = false; }
30 private:
31 bool locked;
32 };
33
34/*************************************************
35* Lock the mutex *
36*************************************************/
37void Default_Mutex::lock()
38 {
39 if(locked)
40 throw Internal_Error("Default_Mutex::lock: Mutex is already locked");
41 locked = true;
42 }
43
44/*************************************************
45* Unlock the mutex *
46*************************************************/
47void Default_Mutex::unlock()
48 {
49 if(!locked)
50 throw Internal_Error("Default_Mutex::unlock: Mutex is already unlocked");
51 locked = false;
52 }
53
54}
55
56/*************************************************
57* Get a mew mutex *
58*************************************************/
59Mutex* get_mutex()
60 {
61 if(mutex_factory == 0)
62 return new Default_Mutex;
63 return mutex_factory->clone();
64 }
65
66/*************************************************
67* Initialize a mutex atomically *
68*************************************************/
69void initialize_mutex(Mutex*& mutex)
70 {
71 if(mutex) return;
72
73 if(mutex_init_lock)
74 {
75 Mutex_Holder lock(mutex_init_lock);
76 if(mutex == 0)
77 mutex = get_mutex();
78 }
79 else
80 mutex = get_mutex();
81 }
82
83namespace Init {
84
85/*************************************************
86* Set the Mutex type *
87*************************************************/
88void set_mutex_type(Mutex* mutex)
89 {
90 delete mutex_factory;
91 delete mutex_init_lock;
92
93 mutex_factory = mutex;
94
95 if(mutex) mutex_init_lock = get_mutex();
96 else mutex_init_lock = 0;
97 }
98
99}
100
101}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status