File: threadutils.h

package info (click to toggle)
rawtherapee 5.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 124,328 kB
  • sloc: cpp: 271,715; ansic: 27,904; sh: 1,109; python: 521; cs: 155; xml: 57; makefile: 15
file content (299 lines) | stat: -rw-r--r-- 6,270 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 *  This file is part of RawTherapee.
 *
 *  Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
 *
 *  RawTherapee is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  RawTherapee is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with RawTherapee.  If not, see <https://www.gnu.org/licenses/>.
 */
#pragma once

// Uncomment this if you want to bypass the CMakeList options and force the values, but do not commit!
//#undef TRACE_MYRWMUTEX
//#define TRACE_MYRWMUTEX 1
//#undef STRICT_MUTEX
//#define STRICT_MUTEX 1

#include <mutex>
#include <thread>
#include <condition_variable>
#include "rtengine/noncopyable.h"

#if STRICT_MUTEX && NDEBUG
using MyMutexBase = std::mutex;
#else
using MyMutexBase = std::recursive_mutex;
#endif

/**
 * @brief Custom implementation to replace Glib::Threads::Mutex.
 *
 * Glib::Threads::Mutex shows different behaviour on Windows (recursive) and Linux (non-recursive).
 * We therefore use a custom implementation that is optionally recursive and instrumented.
 * It will behave like Glib::Threads::RecMutex (STRICT_MUTEX=0) or Glib::Threads::Mutex (STRICT_MUTEX=1).
 * Debug builds with strict mutexes, will emit a message and crash immediately upon recursive locking.
 */
class MyMutex :
    public rtengine::NonCopyable,
    private MyMutexBase
{
public:
    class MyLock;

    void lock ();
    bool trylock ();
    void unlock ();

#if STRICT_MUTEX && !NDEBUG
    MyMutex();

private:
    bool locked;
    bool checkLock (bool noError = false);
    void checkUnlock ();
#endif
};

class MyMutex::MyLock :
    public rtengine::NonCopyable
{
public:
    explicit MyLock (MyMutex& mutex);

    ~MyLock ();

    void acquire ();
    bool try_acquire ();
    void release ();

private:
    MyMutex& mutex;
    bool locked;
};

/**
 * @brief Custom implementation to replace Glib::Threads::RWLock
 */
class MyRWMutex :
    public rtengine::NonCopyable
{
public:
    friend class MyReaderLock;
    friend class MyWriterLock;

    MyRWMutex();

private:
#if TRACE_MYRWMUTEX
    std::thread::id ownerThread;
    const char* lastWriterFile;
    int lastWriterLine;
#endif

    std::mutex mutex;
    std::condition_variable cond;

    std::size_t writerCount;
    std::size_t readerCount;
};

/**
 * @brief Custom implementation to replace Glib::Threads::RWLock::ReaderLock
 */
class MyReaderLock :
    public rtengine::NonCopyable
{
public:
    ~MyReaderLock ();

#if !TRACE_MYRWMUTEX
    explicit MyReaderLock (MyRWMutex& mutex);

    void acquire ();
    void release ();
#else
    explicit MyReaderLock (MyRWMutex& mutex, const char* file, int line);

    void acquire (const char* file, int line);
    void release (const char* file, int line);
#endif

private:
    MyRWMutex& mutex;
    bool locked;
};

/**
 * @brief Custom implementation to replace Glib::Threads::RWLock::WriterLock
 */
class MyWriterLock :
    public rtengine::NonCopyable
{
public:
    ~MyWriterLock ();

#if !TRACE_MYRWMUTEX
    explicit MyWriterLock (MyRWMutex& mutex);

    void acquire ();
    void release ();
#else
    MyWriterLock (MyRWMutex& mutex, const char* file, int line);

    void acquire (const char* file, int line);
    void release (const char* file, int line);
#endif

private:
    MyRWMutex& mutex;
    bool locked;
};

inline void MyMutex::lock ()
{
    MyMutexBase::lock ();

#if STRICT_MUTEX && !NDEBUG
    checkLock ();
#endif
}

inline bool MyMutex::trylock ()
{
    if (MyMutexBase::try_lock ()) {
#if STRICT_MUTEX && !NDEBUG
        return checkLock(true);
#else
        return true;
#endif
    }

    return false;
}

inline void MyMutex::unlock ()
{
#if STRICT_MUTEX && !NDEBUG
    checkUnlock ();
#endif

    MyMutexBase::unlock ();
}

inline MyMutex::MyLock::MyLock (MyMutex& mutex)
    : mutex (mutex)
    , locked (true)
{
    mutex.lock();
}

inline MyMutex::MyLock::~MyLock ()
{
    if (locked) {
        mutex.unlock ();
    }
}

inline void MyMutex::MyLock::acquire ()
{
    mutex.lock ();
    locked = true;
}
inline bool MyMutex::MyLock::try_acquire ()
{
    return locked = mutex.trylock ();
}

inline void MyMutex::MyLock::release ()
{
    mutex.unlock ();
    locked = false;
}

#if !TRACE_MYRWMUTEX

inline MyReaderLock::MyReaderLock (MyRWMutex& mutex)
    : mutex (mutex)
    , locked (false)
{
    acquire ();
}

inline MyWriterLock::MyWriterLock (MyRWMutex& mutex)
    : mutex (mutex)
    , locked (false)
{
    acquire ();
}

inline MyReaderLock::~MyReaderLock ()
{
    if (locked) {
        release ();
    }
}

inline MyWriterLock::~MyWriterLock ()
{
    if (locked) {
        release ();
    }
}

#else

inline MyReaderLock::MyReaderLock (MyRWMutex& mutex, const char* file, int line)
    : mutex (mutex)
    , locked (false)
{
    acquire (file, line);
}

inline MyWriterLock::MyWriterLock (MyRWMutex& mutex, const char* file, int line)
    : mutex (mutex)
    , locked (false)
{
    acquire (file, line);
}

inline MyReaderLock::~MyReaderLock ()
{
    if (locked) {
        release (__FILE__, __LINE__);
    }
}

inline MyWriterLock::~MyWriterLock ()
{
    if (locked) {
        release (__FILE__, __LINE__);
    }
}

#endif

#if TRACE_MYRWMUTEX
#define MYREADERLOCK(ln, e) MyReaderLock ln(e, __FILE__, __LINE__);
#define MYWRITERLOCK(ln, e) MyWriterLock ln(e, __FILE__, __LINE__);
#define MYREADERLOCK_ACQUIRE(ln) ln.acquire(__FILE__, __LINE__);
#define MYWRITERLOCK_ACQUIRE(ln) ln.acquire(__FILE__, __LINE__);
#define MYREADERLOCK_RELEASE(ln) ln.release(__FILE__, __LINE__);
#define MYWRITERLOCK_RELEASE(ln) ln.release(__FILE__, __LINE__);
#else
#define MYREADERLOCK(ln, e) MyReaderLock ln(e);
#define MYWRITERLOCK(ln, e) MyWriterLock ln(e);
#define MYREADERLOCK_ACQUIRE(ln) ln.acquire();
#define MYWRITERLOCK_ACQUIRE(ln) ln.acquire();
#define MYREADERLOCK_RELEASE(ln) ln.release();
#define MYWRITERLOCK_RELEASE(ln) ln.release();
#endif