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
475
476
477
478
481
482
483
484
485
486
487
488
491
492
493
494
495
496
497
498
499
502
503
504
505
506
507
508
509
510
511
512
515
516
517
518
519
520
521
522
525
526
527
528
529
530
531
534
535
536
537
538
539
542
543
544
545
546
547
548
551
552
553
554
555
556
559
560
561
562
563
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
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
625
626
627
628
631
632
634void QBasicMutex::lockInternal()
noexcept(FutexAlwaysAvailable)
636 if (futexAvailable()) {
639 while (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) !=
nullptr) {
645 Q_ASSERT(d_ptr.loadRelaxed());
647 lockInternal(QDeadlineTimer::Forever);
652
653
654#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
655bool QBasicMutex::lockInternal(
int timeout)
noexcept(FutexAlwaysAvailable)
660 return lockInternal(QDeadlineTimer(timeout));
665
666
668bool QBasicMutex::lockInternal(QDeadlineTimer deadlineTimer)
noexcept(FutexAlwaysAvailable)
670 if (deadlineTimer.hasExpired())
673 if (futexAvailable()) {
674 if (Q_UNLIKELY(deadlineTimer.isForever())) {
682 if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) ==
nullptr)
692 if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) ==
nullptr)
695 if (deadlineTimer.hasExpired())
700#if !defined(QT_ALWAYS_USE_FUTEX)
701 while (!fastTryLock()) {
702 QMutexPrivate *copy = d_ptr.loadAcquire();
706 if (copy == dummyLocked()) {
707 if (deadlineTimer.hasExpired())
711 QMutexPrivate *newD = QMutexPrivate::allocate();
712 if (!d_ptr.testAndSetOrdered(dummyLocked(), newD)) {
721 QMutexPrivate *d =
static_cast<QMutexPrivate *>(copy);
722 if (deadlineTimer.hasExpired() && !d->possiblyUnlocked.loadRelaxed())
735 if (d != d_ptr.loadAcquire()) {
746 old_waiters = d->waiters.loadAcquire();
747 if (old_waiters == -QMutexPrivate::BigNumber) {
750 if (d_ptr.testAndSetAcquire(d, dummyLocked())) {
755 Q_ASSERT(d != d_ptr.loadRelaxed());
758 old_waiters = QMutexPrivate::BigNumber;
762 }
while (!d->waiters.testAndSetRelaxed(old_waiters, old_waiters + 1));
764 if (d != d_ptr.loadAcquire()) {
766 if (old_waiters != QMutexPrivate::BigNumber) {
768 Q_ASSERT(d->waiters.loadRelaxed() >= 1);
775 if (d->wait(deadlineTimer)) {
777 if (d->possiblyUnlocked.loadRelaxed() && d->possiblyUnlocked.testAndSetRelaxed(
true,
false))
781 Q_ASSERT(d == d_ptr.loadRelaxed());
789 if (!d->possiblyUnlocked.testAndSetRelaxed(
false,
true)) {
797 Q_ASSERT(d_ptr.loadRelaxed() != 0);
804#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
807
808
809void QBasicMutex::unlockInternal()
noexcept
811 QMutexPrivate *copy = d_ptr.loadAcquire();
812 if (futexAvailable()) {
813 d_ptr.storeRelease(
nullptr);
814 unlockInternalFutex(copy);
816 unlockInternal(copy);
822
823
824
825
826
827
828
830void QBasicMutex::unlockInternalFutex(
void *copy)
noexcept
833 if (!futexAvailable())
841
842
843
845void QBasicMutex::unlockInternal(
void *copy)
noexcept
848 Q_ASSERT(copy != dummyLocked());
850# if defined(Q_OS_FREEBSD) || defined(Q_OS_LINUX) || defined(Q_OS_WIN)
856 if (futexAvailable()) {
857 d_ptr.storeRelease(
nullptr);
858 return unlockInternalFutex(copy);
861#if !defined(QT_ALWAYS_USE_FUTEX)
862 static_assert(!FutexAlwaysAvailable,
"mismatch with QT_ALWAYS_USE_FUTEX");
863 QMutexPrivate *d =
reinterpret_cast<QMutexPrivate *>(copy);
870 if (d->waiters.fetchAndAddRelease(-QMutexPrivate::BigNumber) == 0) {
872 if (d_ptr.testAndSetRelease(d, 0)) {
874 if (d->possiblyUnlocked.loadRelaxed() && d->possiblyUnlocked.testAndSetRelaxed(
true,
false))
885 static_assert(FutexAlwaysAvailable,
"mismatch with QT_ALWAYS_USE_FUTEX");
890#if !defined(QT_ALWAYS_USE_FUTEX)
893struct FreeListConstants : QFreeListDefaultConstants {
894 enum { BlockCount = 4, MaxIndex=0xffff };
895 static const int Sizes[BlockCount];
897Q_CONSTINIT
const int FreeListConstants::Sizes[FreeListConstants::BlockCount] = {
901 FreeListConstants::MaxIndex - (16 + 128 + 1024)
904typedef QFreeList<QMutexPrivate, FreeListConstants> FreeList;
906Q_CONSTINIT
static FreeList freeList_;
913QMutexPrivate *QMutexPrivate::allocate()
915 int i = freelist()->next();
916 QMutexPrivate *d = &(*freelist())[i];
918 Q_ASSERT(d->refCount.loadRelaxed() == 0);
919 Q_ASSERT(!d->possiblyUnlocked.loadRelaxed());
920 Q_ASSERT(d->waiters.loadRelaxed() == 0);
921 d->refCount.storeRelaxed(1);
925void QMutexPrivate::release()
927 Q_ASSERT(refCount.loadRelaxed() == 0);
928 Q_ASSERT(!possiblyUnlocked.loadRelaxed());
929 Q_ASSERT(waiters.loadRelaxed() == 0);
930 freelist()->release(id);
934void QMutexPrivate::derefWaiters(
int value)
noexcept
939 old_waiters = waiters.loadRelaxed();
940 new_waiters = old_waiters;
941 if (new_waiters < 0) {
942 new_waiters += QMutexPrivate::BigNumber;
944 new_waiters -= value;
945 }
while (!waiters.testAndSetRelaxed(old_waiters, new_waiters));
951#if defined(QT_ALWAYS_USE_FUTEX)
953#elif defined(Q_OS_DARWIN)
954# include "qmutex_mac.cpp"
Combined button and popup list for selecting options.
static QMutexPrivate * dummyFutexValue()