Mercurial > hg > orthanc
annotate OrthancFramework/Resources/Graveyard/Multithreading/Mutex.cpp @ 5159:f5907aecbaed
conditional usage of malloc_trim
author | Alain Mazy <am@osimis.io> |
---|---|
date | Thu, 02 Feb 2023 11:28:35 +0100 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
rev | line source |
---|---|
760 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
937
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
760 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
760 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
760 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
760 | 21 **/ |
22 | |
23 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
24 #include "../PrecompiledHeaders.h" |
760 | 25 #include "Mutex.h" |
26 | |
27 #include "../OrthancException.h" | |
28 | |
29 #if defined(_WIN32) | |
30 #include <windows.h> | |
2364 | 31 #elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) |
760 | 32 #include <pthread.h> |
33 #else | |
34 #error Support your platform here | |
35 #endif | |
36 | |
37 namespace Orthanc | |
38 { | |
39 #if defined (_WIN32) | |
40 | |
41 struct Mutex::PImpl | |
42 { | |
43 CRITICAL_SECTION criticalSection_; | |
44 }; | |
45 | |
46 Mutex::Mutex() | |
47 { | |
48 pimpl_ = new PImpl; | |
49 ::InitializeCriticalSection(&pimpl_->criticalSection_); | |
50 } | |
51 | |
52 Mutex::~Mutex() | |
53 { | |
54 ::DeleteCriticalSection(&pimpl_->criticalSection_); | |
55 delete pimpl_; | |
56 } | |
57 | |
58 void Mutex::Lock() | |
59 { | |
60 ::EnterCriticalSection(&pimpl_->criticalSection_); | |
61 } | |
62 | |
63 void Mutex::Unlock() | |
64 { | |
65 ::LeaveCriticalSection(&pimpl_->criticalSection_); | |
66 } | |
67 | |
68 | |
2364 | 69 #elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) |
760 | 70 |
71 struct Mutex::PImpl | |
72 { | |
73 pthread_mutex_t mutex_; | |
74 }; | |
75 | |
76 Mutex::Mutex() | |
77 { | |
78 pimpl_ = new PImpl; | |
79 | |
80 if (pthread_mutex_init(&pimpl_->mutex_, NULL) != 0) | |
81 { | |
82 delete pimpl_; | |
83 throw OrthancException(ErrorCode_InternalError); | |
84 } | |
85 } | |
86 | |
87 Mutex::~Mutex() | |
88 { | |
89 pthread_mutex_destroy(&pimpl_->mutex_); | |
90 delete pimpl_; | |
91 } | |
92 | |
93 void Mutex::Lock() | |
94 { | |
95 if (pthread_mutex_lock(&pimpl_->mutex_) != 0) | |
96 { | |
97 throw OrthancException(ErrorCode_InternalError); | |
98 } | |
99 } | |
100 | |
101 void Mutex::Unlock() | |
102 { | |
103 if (pthread_mutex_unlock(&pimpl_->mutex_) != 0) | |
104 { | |
105 throw OrthancException(ErrorCode_InternalError); | |
106 } | |
107 } | |
108 | |
109 #else | |
110 #error Support your plateform here | |
111 #endif | |
112 } |