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
526
527
528
529
530
531
534
535
536
537
538
539
540
543
544
545
546
547
548
551
552
553
554
555
556
557
560
561
562
563
564
565
568
569
570
571
572
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
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
629
630
631
632
633
634
635
636
637
640
641
643void QBasicMutex::lockInternal()
noexcept(FutexAlwaysAvailable)
645 if (futexAvailable()) {
648 while (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) !=
nullptr) {
654 Q_ASSERT(d_ptr.loadRelaxed());
656 lockInternal(QDeadlineTimer::Forever);
661
662
663#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
664bool QBasicMutex::lockInternal(
int timeout)
noexcept(FutexAlwaysAvailable)
669 return lockInternal(QDeadlineTimer(timeout));
674
675
677bool QBasicMutex::lockInternal(QDeadlineTimer deadlineTimer)
noexcept(FutexAlwaysAvailable)
679 if (deadlineTimer.hasExpired())
682 if (futexAvailable()) {
683 if (Q_UNLIKELY(deadlineTimer.isForever())) {
691 if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) ==
nullptr)
701 if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) ==
nullptr)
704 if (deadlineTimer.hasExpired())
709#if !defined(QT_ALWAYS_USE_FUTEX)
710 while (!fastTryLock()) {
711 QMutexPrivate *copy = d_ptr.loadAcquire();
715 if (copy == dummyLocked()) {
716 if (deadlineTimer.hasExpired())
720 QMutexPrivate *newD = QMutexPrivate::allocate();
721 if (!d_ptr.testAndSetOrdered(dummyLocked(), newD)) {
730 QMutexPrivate *d =
static_cast<QMutexPrivate *>(copy);
731 if (deadlineTimer.hasExpired() && !d->possiblyUnlocked.loadRelaxed())
744 if (d != d_ptr.loadAcquire()) {
755 old_waiters = d->waiters.loadAcquire();
756 if (old_waiters == -QMutexPrivate::BigNumber) {
759 if (d_ptr.testAndSetAcquire(d, dummyLocked())) {
764 Q_ASSERT(d != d_ptr.loadRelaxed());
767 old_waiters = QMutexPrivate::BigNumber;
771 }
while (!d->waiters.testAndSetRelaxed(old_waiters, old_waiters + 1));
773 if (d != d_ptr.loadAcquire()) {
775 if (old_waiters != QMutexPrivate::BigNumber) {
777 Q_ASSERT(d->waiters.loadRelaxed() >= 1);
784 if (d->wait(deadlineTimer)) {
786 if (d->possiblyUnlocked.loadRelaxed() && d->possiblyUnlocked.testAndSetRelaxed(
true,
false))
790 Q_ASSERT(d == d_ptr.loadRelaxed());
798 if (!d->possiblyUnlocked.testAndSetRelaxed(
false,
true)) {
806 Q_ASSERT(d_ptr.loadRelaxed() != 0);
813#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
816
817
818void QBasicMutex::unlockInternal()
noexcept
820 QMutexPrivate *copy = d_ptr.loadAcquire();
821 if (futexAvailable()) {
822 d_ptr.storeRelease(
nullptr);
823 unlockInternalFutex(copy);
825 unlockInternal(copy);
831
832
833
834
835
836
837
839void QBasicMutex::unlockInternalFutex(
void *copy)
noexcept
842 if (!futexAvailable())
850
851
852
854void QBasicMutex::unlockInternal(
void *copy)
noexcept
857 Q_ASSERT(copy != dummyLocked());
859# if defined(Q_OS_FREEBSD) || defined(Q_OS_LINUX) || defined(Q_OS_WIN)
865 if (futexAvailable()) {
866 d_ptr.storeRelease(
nullptr);
867 return unlockInternalFutex(copy);
870#if !defined(QT_ALWAYS_USE_FUTEX)
871 static_assert(!FutexAlwaysAvailable,
"mismatch with QT_ALWAYS_USE_FUTEX");
872 QMutexPrivate *d =
reinterpret_cast<QMutexPrivate *>(copy);
879 if (d->waiters.fetchAndAddRelease(-QMutexPrivate::BigNumber) == 0) {
881 if (d_ptr.testAndSetRelease(d, 0)) {
883 if (d->possiblyUnlocked.loadRelaxed() && d->possiblyUnlocked.testAndSetRelaxed(
true,
false))
894 static_assert(FutexAlwaysAvailable,
"mismatch with QT_ALWAYS_USE_FUTEX");
899#if !defined(QT_ALWAYS_USE_FUTEX)
902struct FreeListConstants : QFreeListDefaultConstants {
903 enum { BlockCount = 4, MaxIndex=0xffff };
904 static const int Sizes[BlockCount];
906Q_CONSTINIT
const int FreeListConstants::Sizes[FreeListConstants::BlockCount] = {
910 FreeListConstants::MaxIndex - (16 + 128 + 1024)
913typedef QFreeList<QMutexPrivate, FreeListConstants> FreeList;
915Q_CONSTINIT
static FreeList freeList_;
922QMutexPrivate *QMutexPrivate::allocate()
924 int i = freelist()->next();
925 QMutexPrivate *d = &(*freelist())[i];
927 Q_ASSERT(d->refCount.loadRelaxed() == 0);
928 Q_ASSERT(!d->possiblyUnlocked.loadRelaxed());
929 Q_ASSERT(d->waiters.loadRelaxed() == 0);
930 d->refCount.storeRelaxed(1);
934void QMutexPrivate::release()
936 Q_ASSERT(refCount.loadRelaxed() == 0);
937 Q_ASSERT(!possiblyUnlocked.loadRelaxed());
938 Q_ASSERT(waiters.loadRelaxed() == 0);
939 freelist()->release(id);
943void QMutexPrivate::derefWaiters(
int value)
noexcept
948 old_waiters = waiters.loadRelaxed();
949 new_waiters = old_waiters;
950 if (new_waiters < 0) {
951 new_waiters += QMutexPrivate::BigNumber;
953 new_waiters -= value;
954 }
while (!waiters.testAndSetRelaxed(old_waiters, new_waiters));
960#if defined(QT_ALWAYS_USE_FUTEX)
Combined button and popup list for selecting options.
static QMutexPrivate * dummyFutexValue()