File: threadutils.cc

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 (311 lines) | stat: -rw-r--r-- 8,465 bytes parent folder | download | duplicates (2)
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
300
301
302
303
304
305
306
307
308
309
310
311
/*
 *  This file is part of RawTherapee.
 *
 *  Copyright (c) 2016 Adam Reichold <adam.reichold@t-online.de>
 *
 *  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/>.
 */
#include "threadutils.h"

#include <csignal>
#include <iostream>

#ifdef _WIN32
#include <windows.h>
#endif

#if STRICT_MUTEX && !NDEBUG

MyMutex::MyMutex() : locked(false) {}

bool MyMutex::checkLock (bool noError)
{
    if (locked) {
        if (noError) {
            return false;
        }

        std::cerr << "MyMutex already locked!" << std::endl;

#ifdef _WIN32
        DebugBreak ();
#else
        raise (SIGTRAP);
#endif
    }

    locked = true;
    return true;
}

void MyMutex::checkUnlock ()
{
    if (!locked) {
        std::cerr << "MyMutex already unlocked!" << std::endl;

#ifdef _WIN32
        DebugBreak ();
#else
        raise (SIGTRAP);
#endif
    }

    locked = false;
}

#endif

#if !TRACE_MYRWMUTEX

MyRWMutex::MyRWMutex() :
    writerCount(0),
    readerCount(0)
{}

void MyReaderLock::acquire ()
{
    if (locked) {
        return;
    }

    std::unique_lock<std::mutex> lock (mutex.mutex);

    if (mutex.writerCount == 0) {
        // There's no writer operating, we can increment the writer count which will lock writers.
        ++mutex.writerCount;
    } else if (mutex.readerCount == 0) {
        // The writer count is non null, but a reader can be the owner of the writer lock,
        // which will be the case if the reader count is not zero too.
        while (mutex.writerCount != 0) {
            mutex.cond.wait (lock);
        }

        // Then, we can increment the writer count.
        ++mutex.writerCount;
    }

    // Finally, we can increment the reader count as well.
    ++mutex.readerCount;

    locked = true;
}

void MyReaderLock::release ()
{
    if (!locked) {
        return;
    }

    std::unique_lock<std::mutex> lock (mutex.mutex);

    // decrement the writer number first...
    --mutex.readerCount;

    if (mutex.readerCount == 0) {
        // ...if no more reader, we decrement the writer count as well...
        --mutex.writerCount;

        // ...and signal the next waiting reader/writer that it's free
        mutex.cond.notify_all ();
    }

    locked = false;
}

void MyWriterLock::acquire ()
{
    if (locked) {
        return;
    }

    std::unique_lock<std::mutex> lock (mutex.mutex);

    // The writer count is not zero, so we have to wait for it to be zero again...
    while (mutex.writerCount != 0) {
        mutex.cond.wait (lock);
    }

    // ...then we can increment the writer count.
    ++mutex.writerCount;

    locked = true;
}

void MyWriterLock::release ()
{
    if (!locked) {
        return;
    }

    std::unique_lock<std::mutex> lock (mutex.mutex);

    // Decrement the writer number first...
    if (--mutex.writerCount == 0) {
        // ...and if the writer count is zero again, we wake up all of the waiting writer or reader.
        mutex.cond.notify_all ();
    }

    locked = false;
}

#else

namespace
{

std::ostream& trace (const char* file, int line)
{
    const auto currentThread = std::this_thread::get_id();

    return std::cout << currentThread << ":" << file << ":" << line << ": ";
}

}

MyRWMutex::MyRWMutex() :
    lastWriterFile(nullptr),
    lastWriterLine(0),
    writerCount(0),
    readerCount(0)
{}

void MyReaderLock::acquire (const char* file, int line)
{
    if (locked) {
        trace (file, line) << "MyReaderLock is already locked." << std::endl;
        return;
    }

    trace (file, line) << "Acquiring MyReaderLock..." << std::endl;

    std::unique_lock<std::mutex> lock (mutex.mutex);

    if (mutex.writerCount == 0) {
        // There's no writer operating, we can increment the writer count which will lock writers.
        ++mutex.writerCount;
    } else if (mutex.readerCount == 0) {
        // The writer count is non null, but a reader can be the owner of the writer lock,
        // which will be the case if the reader count is not zero too.
        while (mutex.writerCount != 0) {
            trace (file, line) << "Waiting for current owner of MyWriterLock..." << std::endl
                               << "\tOwner thread: " << mutex.ownerThread << std::endl
                               << "\tLast writer file: " << mutex.lastWriterFile << std::endl
                               << "\tLast writer line: " << mutex.lastWriterLine << std::endl;

            mutex.cond.wait (lock);
        }

        // Then, we can increment the writer count.
        ++mutex.writerCount;

        mutex.ownerThread = std::this_thread::get_id ();
        mutex.lastWriterFile = file;
        mutex.lastWriterLine = line;
    }

    // Finally, we can increment the reader count as well.
    ++mutex.readerCount;

    trace (file, line) << "MyReaderLock is now locked, reader count is " << mutex.readerCount << ", writer count is " << mutex.writerCount << "." << std::endl;
    locked = true;
}

void MyReaderLock::release (const char* file, int line)
{
    if (!locked) {
        trace (file, line) << "MyReaderLock is already unlocked." << std::endl;
        return;
    }

    trace (file, line) << "Releasing MyReaderLock..." << std::endl;

    std::unique_lock<std::mutex> lock (mutex.mutex);

    // decrement the writer number first...
    --mutex.readerCount;

    if (mutex.readerCount == 0) {
        // ...if no more reader, we decrement the writer count as well...
        --mutex.writerCount;

        // ...and signal the next waiting reader/writer that it's free
        mutex.cond.notify_all ();

        mutex.ownerThread = std::thread::id();
        mutex.lastWriterFile = "";
        mutex.lastWriterLine = 0;
    }

    trace (file, line) << "MyReaderLock is now unlocked, reader count is " << mutex.readerCount << ", writer count is " << mutex.writerCount << "." << std::endl;
    locked = false;
}

void MyWriterLock::acquire (const char* file, int line)
{
    if (locked) {
        trace (file, line) << "MyWriterLock is already locked." << std::endl;
        return;
    }

    trace (file, line) << "Acquiring MyWriterLock..." << std::endl;

    std::unique_lock<std::mutex> lock (mutex.mutex);

    // The writer count is not zero, so we have to wait for it to be zero again...
    while (mutex.writerCount != 0) {
        trace (file, line) << "Waiting for current owner of MyWriterLock..." << std::endl
                           << "\tOwner thread: " << mutex.ownerThread << std::endl
                           << "\tLast writer file: " << mutex.lastWriterFile << std::endl
                           << "\tLast writer line: " << mutex.lastWriterLine << std::endl;

        mutex.cond.wait (lock);
    }

    // ...then we can increment the writer count.
    ++mutex.writerCount;

    mutex.ownerThread = std::this_thread::get_id ();
    mutex.lastWriterFile = file;
    mutex.lastWriterLine = line;

    trace (file, line) << "MyWriterLock is now locked, reader count is " << mutex.readerCount << ", writer count is " << mutex.writerCount << "." << std::endl;
    locked = true;
}

void MyWriterLock::release (const char* file, int line)
{
    if (!locked) {
        trace (file, line) << "MyWriterLock is already unlocked." << std::endl;
        return;
    }

    trace (file, line) << "Releasing MyWriterLock..." << std::endl;

    std::unique_lock<std::mutex> lock (mutex.mutex);

    // Decrement the writer number first...
    if (--mutex.writerCount == 0) {
        // ...and if the writer count is zero again, we wake up all of the waiting writer or reader.
        mutex.cond.notify_all ();

        mutex.ownerThread = std::thread::id();
        mutex.lastWriterFile = "";
        mutex.lastWriterLine = 0;
    }

    trace (file, line) << "MyWriterLock is now unlocked, reader count is " << mutex.readerCount << ", writer count is " << mutex.writerCount << "." << std::endl;
    locked = false;
}

#endif