Mercurial > hg > orthanc-java
comparison Plugin/Mutex.cpp @ 0:3ecef5782f2c
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 18 Oct 2023 17:59:44 +0200 |
parents | |
children | 1c407ba1d311 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3ecef5782f2c |
---|---|
1 /** | |
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium | |
3 * SPDX-License-Identifier: GPL-3.0-or-later | |
4 */ | |
5 | |
6 /** | |
7 * Java plugin for Orthanc | |
8 * Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium | |
9 * | |
10 * This program is free software: you can redistribute it and/or | |
11 * modify it under the terms of the GNU General Public License as | |
12 * published by the Free Software Foundation, either version 3 of the | |
13 * License, or (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, but | |
16 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 * General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 **/ | |
23 | |
24 | |
25 #include "Mutex.h" | |
26 | |
27 #include <stdexcept> | |
28 | |
29 #if defined(_WIN32) | |
30 | |
31 # include <windows.h> | |
32 | |
33 struct Mutex::PImpl | |
34 { | |
35 CRITICAL_SECTION criticalSection_; | |
36 }; | |
37 | |
38 Mutex::Mutex() | |
39 { | |
40 pimpl_ = new PImpl; | |
41 ::InitializeCriticalSection(&pimpl_->criticalSection_); | |
42 } | |
43 | |
44 Mutex::~Mutex() | |
45 { | |
46 ::DeleteCriticalSection(&pimpl_->criticalSection_); | |
47 delete pimpl_; | |
48 } | |
49 | |
50 void Mutex::Lock() | |
51 { | |
52 ::EnterCriticalSection(&pimpl_->criticalSection_); | |
53 } | |
54 | |
55 void Mutex::Unlock() | |
56 { | |
57 ::LeaveCriticalSection(&pimpl_->criticalSection_); | |
58 } | |
59 | |
60 | |
61 #elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) | |
62 | |
63 # include <pthread.h> | |
64 | |
65 struct Mutex::PImpl | |
66 { | |
67 pthread_mutex_t mutex_; | |
68 }; | |
69 | |
70 Mutex::Mutex() | |
71 { | |
72 pimpl_ = new PImpl; | |
73 | |
74 if (pthread_mutex_init(&pimpl_->mutex_, NULL) != 0) | |
75 { | |
76 delete pimpl_; | |
77 throw std::runtime_error("Cannot create mutex"); | |
78 } | |
79 } | |
80 | |
81 Mutex::~Mutex() | |
82 { | |
83 pthread_mutex_destroy(&pimpl_->mutex_); | |
84 delete pimpl_; | |
85 } | |
86 | |
87 void Mutex::Lock() | |
88 { | |
89 if (pthread_mutex_lock(&pimpl_->mutex_) != 0) | |
90 { | |
91 throw std::runtime_error("Cannot lock mutex"); | |
92 } | |
93 } | |
94 | |
95 void Mutex::Unlock() | |
96 { | |
97 if (pthread_mutex_unlock(&pimpl_->mutex_) != 0) | |
98 { | |
99 throw std::runtime_error("Cannot unlock mutex"); | |
100 } | |
101 } | |
102 | |
103 #else | |
104 # error Support your plateform here | |
105 #endif |