1/* Copyright 2006-2023 Joaquin M Lopez Munoz.
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
5 *
6 * See http://www.boost.org/libs/flyweight for library home page.
7 */
8
9#ifndef BOOST_FLYWEIGHT_DETAIL_RECURSIVE_LW_MUTEX_HPP
10#define BOOST_FLYWEIGHT_DETAIL_RECURSIVE_LW_MUTEX_HPP
11
12#if defined(_MSC_VER)
13#pragma once
14#endif
15
16/* Recursive lightweight mutex.
17 * - If <mutex> is provided, it uses std::recursive_mutex.
18 * - Else if Pthreads is available, it uses a Pthreads mutex with
19 * PTHREAD_MUTEX_RECURSIVE attribute.
20 * - Else if on Windows/Cygwin, it relies on boost::detail::lightweight_mutex
21 * for an implementation based on (recursive) Win32 CRITICAL_SECTION.
22 * - Else, it provides a dummy implementation.
23 */
24
25#include <boost/config.hpp>
26
27#if !defined(BOOST_NO_CXX11_HDR_MUTEX)
28#include <boost/noncopyable.hpp>
29#include <mutex>
30
31namespace boost{
32
33namespace flyweights{
34
35namespace detail{
36
37struct recursive_lightweight_mutex:noncopyable
38{
39 recursive_lightweight_mutex(){}
40
41 struct scoped_lock;
42 friend struct scoped_lock;
43 struct scoped_lock:noncopyable
44 {
45 public:
46 scoped_lock(recursive_lightweight_mutex& m):m_(m.m_){m_.lock();}
47 ~scoped_lock(){m_.unlock();}
48
49 private:
50 std::recursive_mutex& m_;
51 };
52
53private:
54 std::recursive_mutex m_;
55};
56
57} /* namespace flyweights::detail */
58
59} /* namespace flyweights */
60
61} /* namespace boost */
62#elif defined(BOOST_HAS_PTHREADS)
63/* code shamelessly ripped from <boost/detail/lwm_pthreads.hpp> */
64
65#include <boost/assert.hpp>
66#include <boost/noncopyable.hpp>
67#include <pthread.h>
68
69namespace boost{
70
71namespace flyweights{
72
73namespace detail{
74
75struct recursive_lightweight_mutex:noncopyable
76{
77 recursive_lightweight_mutex()
78 {
79 pthread_mutexattr_t attr;
80 BOOST_VERIFY(pthread_mutexattr_init(&attr)==0);
81 BOOST_VERIFY(pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE)==0);
82 BOOST_VERIFY(pthread_mutex_init(&m_,&attr)==0);
83 BOOST_VERIFY(pthread_mutexattr_destroy(&attr)==0);
84 }
85
86 ~recursive_lightweight_mutex(){pthread_mutex_destroy(&m_);}
87
88 struct scoped_lock;
89 friend struct scoped_lock;
90 struct scoped_lock:noncopyable
91 {
92 public:
93 scoped_lock(recursive_lightweight_mutex& m):m_(m.m_)
94 {
95 BOOST_VERIFY(pthread_mutex_lock(&m_)==0);
96 }
97
98 ~scoped_lock(){BOOST_VERIFY(pthread_mutex_unlock(&m_)==0);}
99
100 private:
101 pthread_mutex_t& m_;
102 };
103
104private:
105 pthread_mutex_t m_;
106};
107
108} /* namespace flyweights::detail */
109
110} /* namespace flyweights */
111
112} /* namespace boost */
113#elif defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
114#include <boost/smart_ptr/detail/lightweight_mutex.hpp>
115
116namespace boost{
117
118namespace flyweights{
119
120namespace detail{
121
122typedef boost::detail::lightweight_mutex recursive_lightweight_mutex;
123
124} /* namespace flyweights::detail */
125
126} /* namespace flyweights */
127
128} /* namespace boost */
129#else
130namespace boost{
131
132namespace flyweights{
133
134namespace detail{
135
136struct recursive_lightweight_mutex
137{
138 typedef recursive_lightweight_mutex scoped_lock;
139};
140
141} /* namespace flyweights::detail */
142
143} /* namespace flyweights */
144
145} /* namespace boost */
146#endif
147
148#endif
149

source code of boost/libs/flyweight/include/boost/flyweight/detail/recursive_lw_mutex.hpp