| 1 | /* |
| 2 | Copyright (C) 1999-2007 The Botan Project. All rights reserved. |
| 3 | |
| 4 | Redistribution and use in source and binary forms, for any use, with or without |
| 5 | modification, is permitted provided that the following conditions are met: |
| 6 | |
| 7 | 1. Redistributions of source code must retain the above copyright notice, this |
| 8 | list of conditions, and the following disclaimer. |
| 9 | |
| 10 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | this list of conditions, and the following disclaimer in the documentation |
| 12 | and/or other materials provided with the distribution. |
| 13 | |
| 14 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED |
| 15 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 16 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. |
| 17 | |
| 18 | IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, |
| 19 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 20 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 22 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 23 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 24 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | // LICENSEHEADER_END |
| 27 | namespace QCA { // WRAPNS_LINE |
| 28 | /************************************************* |
| 29 | * Mutex Source File * |
| 30 | * (C) 1999-2007 The Botan Project * |
| 31 | *************************************************/ |
| 32 | |
| 33 | } // WRAPNS_LINE |
| 34 | #include <cstdlib> |
| 35 | namespace QCA { // WRAPNS_LINE |
| 36 | } // WRAPNS_LINE |
| 37 | #include <botan/mutex.h> |
| 38 | namespace QCA { // WRAPNS_LINE |
| 39 | #ifndef BOTAN_NO_LIBSTATE |
| 40 | } // WRAPNS_LINE |
| 41 | #include <botan/libstate.h> |
| 42 | namespace QCA { // WRAPNS_LINE |
| 43 | #endif |
| 44 | |
| 45 | namespace Botan { |
| 46 | |
| 47 | /************************************************* |
| 48 | * Mutex_Holder Constructor * |
| 49 | *************************************************/ |
| 50 | Mutex_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 | *************************************************/ |
| 61 | Mutex_Holder::~Mutex_Holder() |
| 62 | { |
| 63 | mux->unlock(); |
| 64 | } |
| 65 | |
| 66 | #ifndef BOTAN_NO_LIBSTATE |
| 67 | /************************************************* |
| 68 | * Named_Mutex_Holder Constructor * |
| 69 | *************************************************/ |
| 70 | Named_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 | *************************************************/ |
| 79 | Named_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 |
| 89 | namespace { |
| 90 | #else |
| 91 | Mutex *Default_Mutex_Factory::make() |
| 92 | { |
| 93 | #endif |
| 94 | class Default_Mutex : public Mutex |
| 95 | { |
| 96 | public: |
| 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 | |
| 125 | private: |
| 126 | bool locked; |
| 127 | }; |
| 128 | |
| 129 | #ifdef BOTAN_FIX_GDB |
| 130 | } // end unnamed namespace |
| 131 | Mutex *Default_Mutex_Factory::make() |
| 132 | { |
| 133 | #endif |
| 134 | |
| 135 | return new Default_Mutex; |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | } // WRAPNS_LINE |
| 140 | |