| 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Purpose: Implementation of pthreads based mutex. |
| 4 | * |
| 5 | ****************************************************************************** |
| 6 | * Copyright (c) 2009 |
| 7 | * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada. |
| 8 | * |
| 9 | * SPDX-License-Identifier: MIT |
| 10 | ****************************************************************************/ |
| 11 | |
| 12 | #include "pcidsk_mutex.h" |
| 13 | #include "pcidsk_exception.h" |
| 14 | |
| 15 | #include <windows.h> |
| 16 | |
| 17 | |
| 18 | /************************************************************************/ |
| 19 | /* Win32Mutex */ |
| 20 | /************************************************************************/ |
| 21 | |
| 22 | class Win32Mutex final : public PCIDSK::Mutex |
| 23 | |
| 24 | { |
| 25 | private: |
| 26 | HANDLE hMutex; |
| 27 | |
| 28 | public: |
| 29 | Win32Mutex(); |
| 30 | ~Win32Mutex(); |
| 31 | |
| 32 | int Acquire(void) override; |
| 33 | int Release(void) override; |
| 34 | }; |
| 35 | |
| 36 | /************************************************************************/ |
| 37 | /* Win32Mutex() */ |
| 38 | /************************************************************************/ |
| 39 | |
| 40 | Win32Mutex::Win32Mutex(): hMutex(CreateMutex( NULL, 1, NULL )) |
| 41 | |
| 42 | { |
| 43 | Win32Mutex::Release(); // it is created acquired, but we want it free. |
| 44 | } |
| 45 | |
| 46 | /************************************************************************/ |
| 47 | /* ~PThreadMutex() */ |
| 48 | /************************************************************************/ |
| 49 | |
| 50 | Win32Mutex::~Win32Mutex() |
| 51 | |
| 52 | { |
| 53 | if( hMutex ) |
| 54 | CloseHandle( hMutex ); |
| 55 | } |
| 56 | |
| 57 | /************************************************************************/ |
| 58 | /* Release() */ |
| 59 | /************************************************************************/ |
| 60 | |
| 61 | int Win32Mutex::Release() |
| 62 | |
| 63 | { |
| 64 | ReleaseMutex( hMutex ); |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | /************************************************************************/ |
| 69 | /* Acquire() */ |
| 70 | /************************************************************************/ |
| 71 | |
| 72 | int Win32Mutex::Acquire() |
| 73 | |
| 74 | { |
| 75 | DWORD hr; |
| 76 | |
| 77 | hr = WaitForSingleObject( hMutex, (int) (3600 * 1000) ); |
| 78 | |
| 79 | if( hr != 0 ) |
| 80 | PCIDSK::ThrowPCIDSKException( "Failed to acquire mutex in 3600s." ); |
| 81 | |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | /************************************************************************/ |
| 86 | /* DefaultCreateMutex() */ |
| 87 | /************************************************************************/ |
| 88 | |
| 89 | PCIDSK::Mutex *PCIDSK::DefaultCreateMutex(void) |
| 90 | |
| 91 | { |
| 92 | return new Win32Mutex(); |
| 93 | } |