monotone

monotone Mtn Source Tree

Root/botan/mutex.cpp

1/*************************************************
2* Mutex Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/mutex.h>
7#include <botan/libstate.h>
8
9namespace Botan {
10
11/*************************************************
12* Mutex_Holder Constructor *
13*************************************************/
14Mutex_Holder::Mutex_Holder(Mutex* m) : mux(m)
15 {
16 if(!mux)
17 throw Invalid_Argument("Mutex_Holder: Argument was NULL");
18 mux->lock();
19 }
20
21/*************************************************
22* Mutex_Holder Destructor *
23*************************************************/
24Mutex_Holder::~Mutex_Holder()
25 {
26 mux->unlock();
27 }
28
29/*************************************************
30* Named_Mutex_Holder Constructor *
31*************************************************/
32Named_Mutex_Holder::Named_Mutex_Holder(const std::string& name) :
33 mutex_name(name)
34 {
35 global_state().get_named_mutex(mutex_name)->lock();
36 }
37
38/*************************************************
39* Named_Mutex_Holder Destructor *
40*************************************************/
41Named_Mutex_Holder::~Named_Mutex_Holder()
42 {
43 global_state().get_named_mutex(mutex_name)->unlock();
44 }
45
46/*************************************************
47* Default Mutex Factory *
48*************************************************/
49Mutex* Default_Mutex_Factory::make()
50 {
51 class Default_Mutex : public Mutex
52 {
53 public:
54 class Mutex_State_Error : public Internal_Error
55 {
56 public:
57 Mutex_State_Error(const std::string& where) :
58 Internal_Error("Default_Mutex::" + where + ": " +
59 "Mutex is already " + where + "ed") {}
60 };
61
62 void lock()
63 {
64 if(locked)
65 throw Mutex_State_Error("lock");
66 locked = true;
67 }
68
69 void unlock()
70 {
71 if(!locked)
72 throw Mutex_State_Error("unlock");
73 locked = false;
74 }
75
76 Default_Mutex() { locked = false; }
77 private:
78 bool locked;
79 };
80
81 return new Default_Mutex;
82 }
83
84}

Archive Download this file

Branches

Tags

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