Warning, file /libraries/qca/src/botantools/botan/mutex.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002 Copyright (C) 1999-2007 The Botan Project. All rights reserved.
0003 
0004 Redistribution and use in source and binary forms, for any use, with or without
0005 modification, is permitted provided that the following conditions are met:
0006 
0007 1. Redistributions of source code must retain the above copyright notice, this
0008 list of conditions, and the following disclaimer.
0009 
0010 2. Redistributions in binary form must reproduce the above copyright notice,
0011 this list of conditions, and the following disclaimer in the documentation
0012 and/or other materials provided with the distribution.
0013 
0014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
0015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
0017 
0018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
0019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
0023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
0024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 */
0026 // LICENSEHEADER_END
0027 namespace QCA { // WRAPNS_LINE
0028 /*************************************************
0029  * Mutex Source File                              *
0030  * (C) 1999-2007 The Botan Project                *
0031  *************************************************/
0032 
0033 } // WRAPNS_LINE
0034 #include <cstdlib>
0035 namespace QCA { // WRAPNS_LINE
0036 } // WRAPNS_LINE
0037 #include <botan/mutex.h>
0038 namespace QCA { // WRAPNS_LINE
0039 #ifndef BOTAN_NO_LIBSTATE
0040 } // WRAPNS_LINE
0041 #include <botan/libstate.h>
0042 namespace QCA { // WRAPNS_LINE
0043 #endif
0044 
0045 namespace Botan {
0046 
0047 /*************************************************
0048  * Mutex_Holder Constructor                       *
0049  *************************************************/
0050 Mutex_Holder::Mutex_Holder(Mutex *m)
0051     : mux(m)
0052 {
0053     if (!mux)
0054         throw Invalid_Argument("Mutex_Holder: Argument was NULL");
0055     mux->lock();
0056 }
0057 
0058 /*************************************************
0059  * Mutex_Holder Destructor                        *
0060  *************************************************/
0061 Mutex_Holder::~Mutex_Holder()
0062 {
0063     mux->unlock();
0064 }
0065 
0066 #ifndef BOTAN_NO_LIBSTATE
0067 /*************************************************
0068  * Named_Mutex_Holder Constructor                 *
0069  *************************************************/
0070 Named_Mutex_Holder::Named_Mutex_Holder(const std::string &name)
0071     : mutex_name(name)
0072 {
0073     global_state().get_named_mutex(mutex_name)->lock();
0074 }
0075 
0076 /*************************************************
0077  * Named_Mutex_Holder Destructor                  *
0078  *************************************************/
0079 Named_Mutex_Holder::~Named_Mutex_Holder()
0080 {
0081     global_state().get_named_mutex(mutex_name)->unlock();
0082 }
0083 #endif
0084 
0085 /*************************************************
0086  * Default Mutex Factory                          *
0087  *************************************************/
0088 #ifdef BOTAN_FIX_GDB
0089 namespace {
0090 #else
0091 Mutex *Default_Mutex_Factory::make()
0092 {
0093 #endif
0094 class Default_Mutex : public Mutex
0095 {
0096 public:
0097     class Mutex_State_Error : public Internal_Error
0098     {
0099     public:
0100         Mutex_State_Error(const std::string &where)
0101             : Internal_Error("Default_Mutex::" + where + ": " + "Mutex is already " + where + "ed")
0102         {
0103         }
0104     };
0105 
0106     void lock() override
0107     {
0108         if (locked)
0109             throw Mutex_State_Error("lock");
0110         locked = true;
0111     }
0112 
0113     void unlock() override
0114     {
0115         if (!locked)
0116             throw Mutex_State_Error("unlock");
0117         locked = false;
0118     }
0119 
0120     Default_Mutex()
0121     {
0122         locked = false;
0123     }
0124 
0125 private:
0126     bool locked;
0127 };
0128 
0129 #ifdef BOTAN_FIX_GDB
0130 } // end unnamed namespace
0131 Mutex *Default_Mutex_Factory::make()
0132 {
0133 #endif
0134 
0135     return new Default_Mutex;
0136 }
0137 
0138 }
0139 } // WRAPNS_LINE