7#include "global/qglobal.h"
8#include "qplatformdefs.h"
16#ifndef QT_ALWAYS_USE_FUTEX
17#include "private/qfreelist_p.h"
22using namespace QtFutex;
25 return reinterpret_cast<QMutexPrivate *>(quintptr(3));
29
30
31
32
33
34
35
36
37
38
39
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
94
95
96
97
100
101
102
103
104
107 auto d =
static_cast<QMutexPrivate *>(ptr);
110 if (!futexAvailable()) {
111 if (d != dummyLocked() && d->possiblyUnlocked.loadRelaxed() && tryLock()) {
116 qWarning(
"QMutex: destroying locked mutex");
120
121
122
123
124
125
126
127
128
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
148
149
150
151
152
153
154
155
156
157
158
159
162
163
164
165
166
167
168
169
170
171
174
175
176
177
178
179
180
181
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
218
219
220
221
222
223
224
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
258
259
260
261
262
265
266
267
268
274
275
276
277
278
279
280
281
282
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321bool QRecursiveMutex::tryLock(QDeadlineTimer timeout)
noexcept(LockIsNoexcept)
323 unsigned tsanFlags = QtTsan::MutexWriteReentrant | QtTsan::TryLock;
324 QtTsan::mutexPreLock(
this, tsanFlags);
326 Qt::HANDLE self = QThread::currentThreadId();
327 if (owner.loadRelaxed() == self) {
329 Q_ASSERT_X(count != 0,
"QMutex::lock",
"Overflow in recursion counter");
330 QtTsan::mutexPostLock(
this, tsanFlags, 0);
334 if (timeout.isForever()) {
337 success = mutex.tryLock(timeout);
341 owner.storeRelaxed(self);
343 tsanFlags |= QtTsan::TryLockFailed;
345 QtTsan::mutexPostLock(
this, tsanFlags, 0);
351
352
353
354
355
356
357
358
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
401
402
403
404
405
406
409 Q_ASSERT(owner.loadRelaxed() == QThread::currentThreadId());
410 QtTsan::mutexPreUnlock(
this, 0u);
415 owner.storeRelaxed(
nullptr);
419 QtTsan::mutexPostUnlock(
this, 0u);
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
477
478
479
480
481
482
483
484
487
488
489
490
491
492
493
494
495
498
499
500
501
502
503
504
505
506
507
508
511
512
513
514
515
516
517
518
521
522
523
524
525
526
527
530
531
532
533
534
535
538
539
540
541
542
543
544
547
548
549
550
551
552
555
556
557
558
559
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
627
628
630void QBasicMutex::lockInternal()
noexcept(FutexAlwaysAvailable)
632 if (futexAvailable()) {
635 while (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) !=
nullptr) {
637 futexWait(d_ptr, dummyFutexValue());
641 Q_ASSERT(d_ptr.loadRelaxed());
643 lockInternal(QDeadlineTimer::Forever);
648
649
650#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
651bool QBasicMutex::lockInternal(
int timeout)
noexcept(FutexAlwaysAvailable)
656 return lockInternal(QDeadlineTimer(timeout));
661
662
664bool QBasicMutex::lockInternal(QDeadlineTimer deadlineTimer)
noexcept(FutexAlwaysAvailable)
666 if (deadlineTimer.hasExpired())
669 if (futexAvailable()) {
670 if (Q_UNLIKELY(deadlineTimer.isForever())) {
678 if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) ==
nullptr)
682 if (!futexWait(d_ptr, dummyFutexValue(), deadlineTimer))
688 if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) ==
nullptr)
691 if (deadlineTimer.hasExpired())
696#if !defined(QT_ALWAYS_USE_FUTEX)
697 while (!fastTryLock()) {
698 QMutexPrivate *copy = d_ptr.loadAcquire();
702 if (copy == dummyLocked()) {
703 if (deadlineTimer.hasExpired())
707 QMutexPrivate *newD = QMutexPrivate::allocate();
708 if (!d_ptr.testAndSetOrdered(dummyLocked(), newD)) {
717 QMutexPrivate *d =
static_cast<QMutexPrivate *>(copy);
718 if (deadlineTimer.hasExpired() && !d->possiblyUnlocked.loadRelaxed())
731 if (d != d_ptr.loadAcquire()) {
742 old_waiters = d->waiters.loadAcquire();
743 if (old_waiters == -QMutexPrivate::BigNumber) {
746 if (d_ptr.testAndSetAcquire(d, dummyLocked())) {
751 Q_ASSERT(d != d_ptr.loadRelaxed());
754 old_waiters = QMutexPrivate::BigNumber;
758 }
while (!d->waiters.testAndSetRelaxed(old_waiters, old_waiters + 1));
760 if (d != d_ptr.loadAcquire()) {
762 if (old_waiters != QMutexPrivate::BigNumber) {
764 Q_ASSERT(d->waiters.loadRelaxed() >= 1);
771 if (d->wait(deadlineTimer)) {
773 if (d->possiblyUnlocked.loadRelaxed() && d->possiblyUnlocked.testAndSetRelaxed(
true,
false))
777 Q_ASSERT(d == d_ptr.loadRelaxed());
785 if (!d->possiblyUnlocked.testAndSetRelaxed(
false,
true)) {
793 Q_ASSERT(d_ptr.loadRelaxed() != 0);
800#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
803
804
805void QBasicMutex::unlockInternal()
noexcept
807 QMutexPrivate *copy = d_ptr.loadAcquire();
808 if (futexAvailable()) {
809 d_ptr.storeRelease(
nullptr);
810 unlockInternalFutex(copy);
812 unlockInternal(copy);
818
819
820
821
822
823
824
826void QBasicMutex::unlockInternalFutex(
void *copy)
noexcept
829 if (!futexAvailable())
837
838
839
841void QBasicMutex::unlockInternal(
void *copy)
noexcept
844 Q_ASSERT(copy != dummyLocked());
846# if defined(Q_OS_FREEBSD) || defined(Q_OS_LINUX) || defined(Q_OS_WIN)
852 if (futexAvailable()) {
853 d_ptr.storeRelease(
nullptr);
854 return unlockInternalFutex(copy);
857#if !defined(QT_ALWAYS_USE_FUTEX)
858 static_assert(!FutexAlwaysAvailable,
"mismatch with QT_ALWAYS_USE_FUTEX");
859 QMutexPrivate *d =
reinterpret_cast<QMutexPrivate *>(copy);
866 if (d->waiters.fetchAndAddRelease(-QMutexPrivate::BigNumber) == 0) {
868 if (d_ptr.testAndSetRelease(d, 0)) {
870 if (d->possiblyUnlocked.loadRelaxed() && d->possiblyUnlocked.testAndSetRelaxed(
true,
false))
881 static_assert(FutexAlwaysAvailable,
"mismatch with QT_ALWAYS_USE_FUTEX");
886#if !defined(QT_ALWAYS_USE_FUTEX)
889struct FreeListConstants : QFreeListDefaultConstants {
890 enum { BlockCount = 4, MaxIndex=0xffff };
891 static const int Sizes[BlockCount];
893Q_CONSTINIT
const int FreeListConstants::Sizes[FreeListConstants::BlockCount] = {
897 FreeListConstants::MaxIndex - (16 + 128 + 1024)
900typedef QFreeList<QMutexPrivate, FreeListConstants> FreeList;
902Q_CONSTINIT
static FreeList freeList_;
909QMutexPrivate *QMutexPrivate::allocate()
911 int i = freelist()->next();
912 QMutexPrivate *d = &(*freelist())[i];
914 Q_ASSERT(d->refCount.loadRelaxed() == 0);
915 Q_ASSERT(!d->possiblyUnlocked.loadRelaxed());
916 Q_ASSERT(d->waiters.loadRelaxed() == 0);
917 d->refCount.storeRelaxed(1);
921void QMutexPrivate::release()
923 Q_ASSERT(refCount.loadRelaxed() == 0);
924 Q_ASSERT(!possiblyUnlocked.loadRelaxed());
925 Q_ASSERT(waiters.loadRelaxed() == 0);
926 freelist()->release(id);
930void QMutexPrivate::derefWaiters(
int value)
noexcept
935 old_waiters = waiters.loadRelaxed();
936 new_waiters = old_waiters;
937 if (new_waiters < 0) {
938 new_waiters += QMutexPrivate::BigNumber;
940 new_waiters -= value;
941 }
while (!waiters.testAndSetRelaxed(old_waiters, new_waiters));
947#if defined(QT_ALWAYS_USE_FUTEX)
949#elif defined(Q_OS_DARWIN)
950# include "qmutex_mac.cpp"
static QMutexPrivate * dummyFutexValue()