1 // ----------------------------------------------------------------------------
2 //
3 // flxmlrpc Copyright (c) 2015 by W1HKJ, Dave Freese <iam_w1hkj@w1hkj.com>
4 //
5 // XmlRpc++ Copyright (c) 2002-2008 by Chris Morley
6 //
7 // This file is part of fldigi
8 //
9 // flxmlrpc is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU Lesser General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 // ----------------------------------------------------------------------------
17 
18 #if defined(XMLRPC_THREADS)
19 
20 #include <config.h>
21 
22 #include "XmlRpcMutex.h"
23 
24 #if defined(_WINDOWS)
25 # define WIN32_LEAN_AND_MEAN
26 # include <windows.h>
27 #else
28 # include <pthread.h>
29 #endif
30 
31 using namespace XmlRpc;
32 
33 
34 //! Destructor.
~XmlRpcMutex()35 XmlRpcMutex::~XmlRpcMutex()
36 {
37   if (_pMutex)
38   {
39 #if defined(_WINDOWS)
40     ::CloseHandle((HANDLE)_pMutex);
41 #else
42     ::pthread_mutex_destroy((pthread_mutex_t*)_pMutex);
43     delete _pMutex;
44 #endif
45     _pMutex = 0;
46   }
47 }
48 
49 //! Wait for the mutex to be available and then acquire the lock.
acquire()50 void XmlRpcMutex::acquire()
51 {
52 #if defined(_WINDOWS)
53   if ( ! _pMutex)
54     _pMutex = ::CreateMutex(0, TRUE, 0);
55   else
56     ::WaitForSingleObject(_pMutex, INFINITE);
57 #else
58   if ( ! _pMutex)
59   {
60     _pMutex = new pthread_mutex_t;
61     ::pthread_mutex_init((pthread_mutex_t*)_pMutex, 0);
62   }
63   ::pthread_mutex_lock((pthread_mutex_t*)_pMutex);
64 #endif
65 }
66 
67 //! Release the mutex.
release()68 void XmlRpcMutex::release()
69 {
70   if (_pMutex)
71 #if defined(_WINDOWS)
72     ::ReleaseMutex(_pMutex);
73 #else
74     ::pthread_mutex_unlock((pthread_mutex_t*)_pMutex);
75 #endif
76 }
77 
78 #endif // XMLRPC_THREADS
79 
80