1 // Copyright (C) 2016-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 
6 #ifndef VSOMEIP_V3_CRITICALSECTION_HPP
7 #define VSOMEIP_V3_CRITICALSECTION_HPP
8 
9 #include <memory>
10 #include <mutex>
11 
12 namespace vsomeip_v3 {
13 
14 #ifdef _WIN32
15 
16     // Windows: CriticalSection uses win32 CRITICAL_SECTION.
17     // Interface mimics std::mutex so we can use it in
18     // conjunction with std::unique_lock.
19     class CriticalSection final {
20     public:
21         CriticalSection();
22         ~CriticalSection();
23 
24         // prevent copying
25         CriticalSection(const CriticalSection&) = delete;
26         CriticalSection& operator=(const CriticalSection&) = delete;
27 
28         void lock();
29         void unlock();
30         bool try_lock();
31 
32     private:
33         struct Impl;
34         std::unique_ptr<Impl> m_impl;
35     };
36 
37 #else
38 
39     // Linux: CriticalSection is a type alias for std::mutex.
40     using CriticalSection = std::mutex;
41 
42 #endif
43 
44 } // namespace vsomeip_v3
45 
46 #endif //VSOMEIP_V3_CRITICALSECTION_HPP
47