comparison Core/MultiThreading/Mutex.cpp @ 760:b2a62f22fbe8

lock abstraction
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 Apr 2014 13:36:51 +0200
parents
children a811bdf8b8eb
comparison
equal deleted inserted replaced
757:40d09221077a 760:b2a62f22fbe8
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "Mutex.h"
34
35 #include "../OrthancException.h"
36
37 #if defined(_WIN32)
38 #include <windows.h>
39 #elif defined(__linux)
40 #include <pthread.h>
41 #else
42 #error Support your platform here
43 #endif
44
45 namespace Orthanc
46 {
47 #if defined (_WIN32)
48
49 struct Mutex::PImpl
50 {
51 CRITICAL_SECTION criticalSection_;
52 };
53
54 Mutex::Mutex()
55 {
56 pimpl_ = new PImpl;
57 ::InitializeCriticalSection(&pimpl_->criticalSection_);
58 }
59
60 Mutex::~Mutex()
61 {
62 ::DeleteCriticalSection(&pimpl_->criticalSection_);
63 delete pimpl_;
64 }
65
66 void Mutex::Lock()
67 {
68 ::EnterCriticalSection(&pimpl_->criticalSection_);
69 }
70
71 void Mutex::Unlock()
72 {
73 ::LeaveCriticalSection(&pimpl_->criticalSection_);
74 }
75
76
77 #elif defined(__linux)
78
79 struct Mutex::PImpl
80 {
81 pthread_mutex_t mutex_;
82 };
83
84 Mutex::Mutex()
85 {
86 pimpl_ = new PImpl;
87
88 if (pthread_mutex_init(&pimpl_->mutex_, NULL) != 0)
89 {
90 delete pimpl_;
91 throw OrthancException(ErrorCode_InternalError);
92 }
93 }
94
95 Mutex::~Mutex()
96 {
97 pthread_mutex_destroy(&pimpl_->mutex_);
98 delete pimpl_;
99 }
100
101 void Mutex::Lock()
102 {
103 if (pthread_mutex_lock(&pimpl_->mutex_) != 0)
104 {
105 throw OrthancException(ErrorCode_InternalError);
106 }
107 }
108
109 void Mutex::Unlock()
110 {
111 if (pthread_mutex_unlock(&pimpl_->mutex_) != 0)
112 {
113 throw OrthancException(ErrorCode_InternalError);
114 }
115 }
116
117 #else
118 #error Support your plateform here
119 #endif
120 }