1/*
2Copyright (C) 1999-2007 The Botan Project. All rights reserved.
3
4Redistribution and use in source and binary forms, for any use, with or without
5modification, is permitted provided that the following conditions are met:
6
71. Redistributions of source code must retain the above copyright notice, this
8list of conditions, and the following disclaimer.
9
102. Redistributions in binary form must reproduce the above copyright notice,
11this list of conditions, and the following disclaimer in the documentation
12and/or other materials provided with the distribution.
13
14THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
15WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
17
18IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
19INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*/
26// LICENSEHEADER_END
27namespace QCA { // WRAPNS_LINE
28/*************************************************
29 * Mutex Source File *
30 * (C) 1999-2007 The Botan Project *
31 *************************************************/
32
33} // WRAPNS_LINE
34#include <cstdlib>
35namespace QCA { // WRAPNS_LINE
36} // WRAPNS_LINE
37#include <botan/mutex.h>
38namespace QCA { // WRAPNS_LINE
39#ifndef BOTAN_NO_LIBSTATE
40} // WRAPNS_LINE
41#include <botan/libstate.h>
42namespace QCA { // WRAPNS_LINE
43#endif
44
45namespace Botan {
46
47/*************************************************
48 * Mutex_Holder Constructor *
49 *************************************************/
50Mutex_Holder::Mutex_Holder(Mutex *m)
51 : mux(m)
52{
53 if (!mux)
54 throw Invalid_Argument("Mutex_Holder: Argument was NULL");
55 mux->lock();
56}
57
58/*************************************************
59 * Mutex_Holder Destructor *
60 *************************************************/
61Mutex_Holder::~Mutex_Holder()
62{
63 mux->unlock();
64}
65
66#ifndef BOTAN_NO_LIBSTATE
67/*************************************************
68 * Named_Mutex_Holder Constructor *
69 *************************************************/
70Named_Mutex_Holder::Named_Mutex_Holder(const std::string &name)
71 : mutex_name(name)
72{
73 global_state().get_named_mutex(mutex_name)->lock();
74}
75
76/*************************************************
77 * Named_Mutex_Holder Destructor *
78 *************************************************/
79Named_Mutex_Holder::~Named_Mutex_Holder()
80{
81 global_state().get_named_mutex(mutex_name)->unlock();
82}
83#endif
84
85/*************************************************
86 * Default Mutex Factory *
87 *************************************************/
88#ifdef BOTAN_FIX_GDB
89namespace {
90#else
91Mutex *Default_Mutex_Factory::make()
92{
93#endif
94class Default_Mutex : public Mutex
95{
96public:
97 class Mutex_State_Error : public Internal_Error
98 {
99 public:
100 Mutex_State_Error(const std::string &where)
101 : Internal_Error("Default_Mutex::" + where + ": " + "Mutex is already " + where + "ed")
102 {
103 }
104 };
105
106 void lock() override
107 {
108 if (locked)
109 throw Mutex_State_Error("lock");
110 locked = true;
111 }
112
113 void unlock() override
114 {
115 if (!locked)
116 throw Mutex_State_Error("unlock");
117 locked = false;
118 }
119
120 Default_Mutex()
121 {
122 locked = false;
123 }
124
125private:
126 bool locked;
127};
128
129#ifdef BOTAN_FIX_GDB
130} // end unnamed namespace
131Mutex *Default_Mutex_Factory::make()
132{
133#endif
134
135 return new Default_Mutex;
136}
137
138}
139} // WRAPNS_LINE
140

source code of qca/src/botantools/botan/mutex.cpp