forked from duneroadrunner/SaferCPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmseasyncshared.h
3520 lines (3073 loc) · 190 KB
/
mseasyncshared.h
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015 Noah Lopez
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#ifndef MSEASYNCSHARED_H_
#define MSEASYNCSHARED_H_
#include "mseoptional.h"
#include "msemsearray.h"
#include "msemsevector.h"
#ifndef MSE_ASYNCSHARED_NO_XSCOPE_DEPENDENCE
#include "msescope.h"
#endif // !MSE_ASYNCSHARED_NO_XSCOPE_DEPENDENCE
#include <shared_mutex>
#include <thread>
#include <unordered_map>
#include <cassert>
#include <stdexcept>
#include <ctime>
#include <ratio>
#include <chrono>
#include <vector>
#include <future>
#include <map>
#if defined(MSE_SAFER_SUBSTITUTES_DISABLED) || defined(MSE_SAFERPTR_DISABLED)
#define MSE_ASYNCSHAREDPOINTER_DISABLED
#endif /*defined(MSE_SAFER_SUBSTITUTES_DISABLED) || defined(MSE_SAFERPTR_DISABLED)*/
#ifdef MSE_CUSTOM_THROW_DEFINITION
#include <iostream>
#define MSE_THROW(x) MSE_CUSTOM_THROW_DEFINITION(x)
#else // MSE_CUSTOM_THROW_DEFINITION
#define MSE_THROW(x) throw(x)
#endif // MSE_CUSTOM_THROW_DEFINITION
namespace mse {
#ifdef MSE_ASYNCSHAREDPOINTER_DISABLED
#else /*MSE_ASYNCSHAREDPOINTER_DISABLED*/
#endif /*MSE_ASYNCSHAREDPOINTER_DISABLED*/
#ifndef _STD
#define _STD std::
#endif /*_STD*/
#ifndef _NOEXCEPT
#define _NOEXCEPT
#endif /*_NOEXCEPT*/
#ifndef _THROW_NCEE
#define _THROW_NCEE(x, y) MSE_THROW(x(y))
#endif /*_THROW_NCEE*/
/* This macro roughly simulates constructor inheritance. */
#define MSE_ASYNC_USING(Derived, Base) \
template<typename ...Args, typename = typename std::enable_if<std::is_constructible<Base, Args...>::value>::type> \
Derived(Args &&...args) : Base(std::forward<Args>(args)...) {}
#ifdef MSEPOINTERBASICS_H
typedef StrongPointerTagBase AsyncSharedStrongPointerTagBase;
class AsyncSharedStrongPointerNotAsyncShareableTagBase : public AsyncSharedStrongPointerTagBase, public NotAsyncShareableTagBase {};
#else // MSEPOINTERBASICS_H
class AsyncSharedStrongPointerTagBase {};
class AsyncSharedStrongPointerNotAsyncShareableTagBase : public AsyncSharedStrongPointerTagBase {};
#endif // MSEPOINTERBASICS_H
class asyncshared_runtime_error : public std::runtime_error { public:
using std::runtime_error::runtime_error;
};
class asyncshared_use_of_invalid_pointer_error : public std::logic_error { public:
using std::logic_error::logic_error;
};
template <class _Ty>
class unlock_guard {
public:
unlock_guard(_Ty& mutex_ref) : m_mutex_ref(mutex_ref) {
m_mutex_ref.unlock();
}
~unlock_guard() {
m_mutex_ref.lock();
}
_Ty& m_mutex_ref;
};
/* todo: Detect the case where two threads each have a "read" lock and are (indefinitely) blocking on obtaining a "write"
lock (in addition to the "read" locks they already hold). This is a deadlock case. We should throw an exception when the
second thread requests a "write" lock (by calling "lock()"). */
/* Note that this "recursive_shared_timed_mutex" allows a thread to hold "read" (shared) locks and "write" locks at the
same time. It also provides "nonrecursive_lock()" member functions to obtain a lock that is exclusive within the thread
as well as between threads. */
class recursive_shared_timed_mutex : private std::shared_timed_mutex {
public:
typedef std::shared_timed_mutex base_class;
void lock()
{ // lock exclusive
std::lock_guard<std::mutex> lock1(m_mutex1);
if ((1 <= m_readlock_count) && (!m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock)) {
assert(0 == m_writelock_count);
const auto this_thread_id = std::this_thread::get_id();
const auto found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if (m_thread_id_readlock_count_map.end() != found_it) {
assert(1 <= (*found_it).second);
/* This thread currently holds a shared_lock on the underlying mutex. We'll release it (and note that
we did so) so as not to prevent the exclusive_lock from being acquired. */
base_class::unlock_shared();
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = true;
}
}
if ((1 <= m_writelock_count) && (std::this_thread::get_id() == m_writelock_thread_id)) {
if (m_writelock_is_nonrecursive) {
MSE_THROW(std::system_error(std::make_error_code(std::errc::resource_deadlock_would_occur)));
}
}
else {
assert((0 == m_writelock_count) || (std::this_thread::get_id() != m_writelock_thread_id));
{
unlock_guard<std::mutex> unlock1(m_mutex1);
base_class::lock();
}
m_writelock_thread_id = std::this_thread::get_id();
assert(0 == m_writelock_count);
}
m_writelock_count += 1;
}
bool try_lock()
{ // try to lock exclusive
bool retval = false;
std::lock_guard<std::mutex> lock1(m_mutex1);
if ((1 <= m_readlock_count) && (!m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock)) {
assert(0 == m_writelock_count);
const auto this_thread_id = std::this_thread::get_id();
const auto found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if (m_thread_id_readlock_count_map.end() != found_it) {
if ((*found_it).second == m_readlock_count) {
/* All of the (one or more) readlocks are owned by this thread. So we should be able to release
the shared_lock on the underlying mutex and immediately acquire an exclusive_lock. */
base_class::unlock_shared();
base_class::lock();
assert(0 == m_writelock_count);
m_writelock_thread_id = std::this_thread::get_id();
m_writelock_count += 1;
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = true;
retval = true;
}
}
}
if (!retval) {
if ((1 <= m_writelock_count) && (std::this_thread::get_id() == m_writelock_thread_id)) {
if (m_writelock_is_nonrecursive) {
retval = false;
}
else {
m_writelock_count += 1;
retval = true;
}
}
else {
retval = base_class::try_lock();
if (retval) {
assert(0 == m_writelock_count);
m_writelock_thread_id = std::this_thread::get_id();
m_writelock_count += 1;
}
}
}
return retval;
}
template<class _Rep, class _Period>
bool try_lock_for(const std::chrono::duration<_Rep, _Period>& _Rel_time)
{ // try to lock for duration
return (try_lock_until(std::chrono::steady_clock::now() + _Rel_time));
}
template<class _Clock, class _Duration>
bool try_lock_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time)
{ // try to lock until time point
bool retval = false;
std::lock_guard<std::mutex> lock1(m_mutex1);
if ((1 <= m_readlock_count) && (!m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock)) {
assert(0 == m_writelock_count);
const auto this_thread_id = std::this_thread::get_id();
const auto found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if (m_thread_id_readlock_count_map.end() != found_it) {
if ((*found_it).second == m_readlock_count) {
/* All of the (one or more) readlocks are owned by this thread. So we should be able to release
the shared_lock on the underlying mutex and immediately acquire an exclusive_lock. */
base_class::unlock_shared();
base_class::lock();
assert(0 == m_writelock_count);
m_writelock_thread_id = std::this_thread::get_id();
m_writelock_count += 1;
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = true;
retval = true;
}
}
}
if (!retval) {
if ((1 <= m_writelock_count) && (std::this_thread::get_id() == m_writelock_thread_id)) {
if (m_writelock_is_nonrecursive) {
retval = false;
}
else {
m_writelock_count += 1;
retval = true;
}
}
else {
{
unlock_guard<std::mutex> unlock1(m_mutex1);
retval = base_class::try_lock_until(_Abs_time);
}
if (retval) {
assert(0 == m_writelock_count);
m_writelock_thread_id = std::this_thread::get_id();
m_writelock_count += 1;
}
}
}
return retval;
}
void unlock()
{ // unlock exclusive
std::lock_guard<std::mutex> lock1(m_mutex1);
assert(std::this_thread::get_id() == m_writelock_thread_id);
if ((2 <= m_writelock_count) && (std::this_thread::get_id() == m_writelock_thread_id)) {
}
else {
assert(1 == m_writelock_count);
base_class::unlock();
m_writelock_is_nonrecursive = false;
if (m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock) {
/* We need to reacquire the shared_lock that was suspended to make way for the exclusive_lock we
just released. */
base_class::lock_shared();
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = false;
}
}
m_writelock_count -= 1;
}
void nonrecursive_lock()
{ // lock nonrecursive
std::lock_guard<std::mutex> lock1(m_mutex1);
if ((1 <= m_writelock_count) && (std::this_thread::get_id() == m_writelock_thread_id)) {
MSE_THROW(std::system_error(std::make_error_code(std::errc::resource_deadlock_would_occur)));
}
else {
if (1 <= m_readlock_count) {
const auto this_thread_id = std::this_thread::get_id();
const auto found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if (m_thread_id_readlock_count_map.end() != found_it) {
assert(1 <= (*found_it).second);
MSE_THROW(std::system_error(std::make_error_code(std::errc::resource_deadlock_would_occur)));
}
}
assert((std::this_thread::get_id() != m_writelock_thread_id) || (0 == m_writelock_count));
{
unlock_guard<std::mutex> unlock1(m_mutex1);
base_class::lock();
}
m_writelock_thread_id = std::this_thread::get_id();
assert(0 == m_writelock_count);
}
m_writelock_count += 1;
m_writelock_is_nonrecursive = true;
}
bool try_nonrecursive_lock()
{ // try to lock nonrecursive
bool retval = false;
std::lock_guard<std::mutex> lock1(m_mutex1);
if ((1 <= m_writelock_count) && (std::this_thread::get_id() == m_writelock_thread_id)) {
retval = false;
}
else {
if (1 <= m_readlock_count) {
const auto this_thread_id = std::this_thread::get_id();
const auto found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if (m_thread_id_readlock_count_map.end() != found_it) {
assert(1 <= (*found_it).second);
return false;
}
}
retval = base_class::try_lock();
if (retval) {
assert(0 == m_writelock_count);
m_writelock_thread_id = std::this_thread::get_id();
m_writelock_count += 1;
m_writelock_is_nonrecursive = true;
}
}
return retval;
}
template<class _Rep, class _Period>
bool try_nonrecursive_lock_for(const std::chrono::duration<_Rep, _Period>& _Rel_time)
{ // try to nonrecursive lock for duration
return (try_nonrecursive_lock_until(std::chrono::steady_clock::now() + _Rel_time));
}
template<class _Clock, class _Duration>
bool try_nonrecursive_lock_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time)
{ // try to nonrecursive lock until time point
bool retval = false;
std::lock_guard<std::mutex> lock1(m_mutex1);
if ((1 <= m_writelock_count) && (std::this_thread::get_id() == m_writelock_thread_id)) {
retval = false;
}
else {
if (1 <= m_readlock_count) {
const auto this_thread_id = std::this_thread::get_id();
const auto found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if (m_thread_id_readlock_count_map.end() != found_it) {
assert(1 <= (*found_it).second);
return false;
}
}
{
unlock_guard<std::mutex> unlock1(m_mutex1);
retval = base_class::try_lock_until(_Abs_time);
}
if (retval) {
assert(0 == m_writelock_count);
m_writelock_thread_id = std::this_thread::get_id();
m_writelock_count += 1;
m_writelock_is_nonrecursive = true;
}
}
return retval;
}
void nonrecursive_unlock()
{ // unlock nonrecursive
std::lock_guard<std::mutex> lock1(m_mutex1);
assert(std::this_thread::get_id() == m_writelock_thread_id);
assert((m_writelock_is_nonrecursive) && (1 == m_writelock_count));
assert(0 == m_readlock_count);
base_class::unlock();
m_writelock_is_nonrecursive = false;
m_writelock_count -= 1;
}
void lock_shared()
{ // lock non-exclusive
std::lock_guard<std::mutex> lock1(m_mutex1);
const auto this_thread_id = std::this_thread::get_id();
const auto found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if ((m_thread_id_readlock_count_map.end() != found_it) && (1 <= (*found_it).second)) {
(*found_it).second += 1;
}
else if ((1 <= m_writelock_count) && (this_thread_id == m_writelock_thread_id) && (!m_writelock_is_nonrecursive)) {
assert((m_thread_id_readlock_count_map.end() == found_it) || (0 == (*found_it).second));
assert(!m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock);
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = true;
try {
std::unordered_map<std::thread::id, int>::value_type item(this_thread_id, 1);
m_thread_id_readlock_count_map.insert(item);
}
catch (...) {
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = false;
MSE_THROW(asyncshared_runtime_error("std::unordered_map<>::insert() failed? - mse::recursive_shared_timed_mutex"));
}
}
else {
assert((m_thread_id_readlock_count_map.end() == found_it) || (0 == (*found_it).second));
{
unlock_guard<std::mutex> unlock1(m_mutex1);
base_class::lock_shared();
}
try {
/* Things could've changed so we have to check again. */
const auto l_found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if (m_thread_id_readlock_count_map.end() != l_found_it) {
assert(0 <= (*l_found_it).second);
(*l_found_it).second += 1;
}
else {
std::unordered_map<std::thread::id, int>::value_type item(this_thread_id, 1);
m_thread_id_readlock_count_map.insert(item);
}
}
catch (...) {
base_class::unlock_shared();
MSE_THROW(asyncshared_runtime_error("std::unordered_map<>::insert() failed? - mse::recursive_shared_timed_mutex"));
}
}
m_readlock_count += 1;
}
bool try_lock_shared()
{ // try to lock non-exclusive
bool retval = false;
std::lock_guard<std::mutex> lock1(m_mutex1);
const auto this_thread_id = std::this_thread::get_id();
const auto found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if ((m_thread_id_readlock_count_map.end() != found_it) && (1 <= (*found_it).second)) {
(*found_it).second += 1;
m_readlock_count += 1;
retval = true;
}
else if ((1 <= m_writelock_count) && (this_thread_id == m_writelock_thread_id) && (!m_writelock_is_nonrecursive)) {
assert((m_thread_id_readlock_count_map.end() == found_it) || (0 == (*found_it).second));
assert(!m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock);
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = true;
try {
std::unordered_map<std::thread::id, int>::value_type item(this_thread_id, 1);
m_thread_id_readlock_count_map.insert(item);
m_readlock_count += 1;
retval = true;
}
catch (...) {
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = false;
MSE_THROW(asyncshared_runtime_error("std::unordered_map<>::insert() failed? - mse::recursive_shared_timed_mutex"));
}
}
else {
retval = base_class::try_lock_shared();
if (retval) {
try {
if (m_thread_id_readlock_count_map.end() != found_it) {
assert(0 <= (*found_it).second);
(*found_it).second += 1;
}
else {
std::unordered_map<std::thread::id, int>::value_type item(this_thread_id, 1);
m_thread_id_readlock_count_map.insert(item);
}
m_readlock_count += 1;
}
catch (...) {
base_class::unlock_shared();
MSE_THROW(asyncshared_runtime_error("std::unordered_map<>::insert() failed? - mse::recursive_shared_timed_mutex"));
}
}
}
return retval;
}
template<class _Rep, class _Period>
bool try_lock_shared_for(const std::chrono::duration<_Rep, _Period>& _Rel_time)
{ // try to lock non-exclusive for relative time
return (try_lock_shared_until(_Rel_time + std::chrono::steady_clock::now()));
}
template<class _Clock, class _Duration>
bool try_lock_shared_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time)
{ // try to lock non-exclusive until absolute time
bool retval = false;
std::lock_guard<std::mutex> lock1(m_mutex1);
const auto this_thread_id = std::this_thread::get_id();
const auto found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if ((m_thread_id_readlock_count_map.end() != found_it) && (1 <= (*found_it).second)) {
(*found_it).second += 1;
m_readlock_count += 1;
retval = true;
}
else if ((1 <= m_writelock_count) && (this_thread_id == m_writelock_thread_id) && (!m_writelock_is_nonrecursive)) {
assert((m_thread_id_readlock_count_map.end() == found_it) || (0 == (*found_it).second));
assert(!m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock);
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = true;
try {
std::unordered_map<std::thread::id, int>::value_type item(this_thread_id, 1);
m_thread_id_readlock_count_map.insert(item);
m_readlock_count += 1;
retval = true;
}
catch (...) {
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = false;
MSE_THROW(asyncshared_runtime_error("std::unordered_map<>::insert() failed? - mse::recursive_shared_timed_mutex"));
}
}
else {
{
unlock_guard<std::mutex> unlock1(m_mutex1);
retval = base_class::try_lock_shared_until(_Abs_time);
}
if (retval) {
try {
if (m_thread_id_readlock_count_map.end() != found_it) {
assert(0 <= (*found_it).second);
(*found_it).second += 1;
}
else {
std::unordered_map<std::thread::id, int>::value_type item(this_thread_id, 1);
m_thread_id_readlock_count_map.insert(item);
}
m_readlock_count += 1;
}
catch (...) {
base_class::unlock_shared();
MSE_THROW(asyncshared_runtime_error("std::unordered_map<>::insert() failed? - mse::recursive_shared_timed_mutex"));
}
}
}
return retval;
}
void unlock_shared()
{ // unlock non-exclusive
std::lock_guard<std::mutex> lock1(m_mutex1);
const auto this_thread_id = std::this_thread::get_id();
const auto found_it = m_thread_id_readlock_count_map.find(this_thread_id);
if (m_thread_id_readlock_count_map.end() != found_it) {
if (2 <= (*found_it).second) {
(*found_it).second -= 1;
}
else {
assert(1 == (*found_it).second);
m_thread_id_readlock_count_map.erase(found_it);
if (m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock) {
m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = false;
}
else {
base_class::unlock_shared();
}
}
}
else {
assert(false);
MSE_THROW(asyncshared_runtime_error("unpaired unlock_shared() call? - mse::recursive_shared_timed_mutex"));
//base_class::unlock_shared();
}
m_readlock_count -= 1;
}
//std::mutex m_write_mutex;
//std::mutex m_read_mutex;
std::mutex m_mutex1;
std::thread::id m_writelock_thread_id;
int m_writelock_count = 0;
bool m_writelock_is_nonrecursive = false;
std::unordered_map<std::thread::id, int> m_thread_id_readlock_count_map;
int m_readlock_count = 0;
bool m_a_shared_lock_is_suspended_to_allow_an_exclusive_lock = false;
};
//typedef std::shared_timed_mutex async_shared_timed_mutex_type;
typedef recursive_shared_timed_mutex async_shared_timed_mutex_type;
template<typename _TAccessLease> class TAsyncSharedV2XWPReadWriteAccessRequesterBase;
template<typename _TAccessLease> class TAsyncSharedV2ReadWritePointerBase;
template<typename _TAccessLease> class TAsyncSharedV2ReadWriteConstPointerBase;
template<typename _TAccessLease> class TAsyncSharedV2ExclusiveReadWritePointerBase;
template<typename _TAccessLease> class TAsyncSharedV2XWPReadOnlyAccessRequesterBase;
template<typename _TAccessLease> class TAsyncSharedV2ReadOnlyConstPointerBase;
template<typename _TAccessLease> class TXScopeAsyncSharedV2XWPReadWriteAccessRequester;
template<typename _TAccessLease> class TXScopeAsyncSharedV2ReadWritePointer;
template<typename _TAccessLease> class TXScopeAsyncSharedV2ReadWriteConstPointer;
template<typename _TAccessLease> class TXScopeAsyncSharedV2ExclusiveReadWritePointer;
template<typename _TAccessLease> class TXScopeAsyncSharedV2XWPReadOnlyAccessRequester;
template<typename _TAccessLease> class TXScopeAsyncSharedV2ReadOnlyConstPointer;
template<typename _TAccessLease> class TAsyncSharedV2XWPReadWriteAccessRequester;
template<typename _TAccessLease> class TAsyncSharedV2ReadWritePointer;
template<typename _TAccessLease> class TAsyncSharedV2ReadWriteConstPointer;
template<typename _TAccessLease> class TAsyncSharedV2ExclusiveReadWritePointer;
template<typename _TAccessLease> class TAsyncSharedV2XWPReadOnlyAccessRequester;
template<typename _TAccessLease> class TAsyncSharedV2ReadOnlyConstPointer;
template <typename _TAccessLease>
class TAsyncSharedXWPAccessLeaseObj {
public:
TAsyncSharedXWPAccessLeaseObj(_TAccessLease&& access_lease)
: m_access_lease(std::forward<_TAccessLease>(access_lease)) {}
const _TAccessLease& cref() const {
return m_access_lease;
}
async_shared_timed_mutex_type& mutex_ref() const {
return m_mutex1;
}
private:
_TAccessLease m_access_lease;
mutable async_shared_timed_mutex_type m_mutex1;
friend class TAsyncSharedV2ReadWritePointerBase<_TAccessLease>;
friend class TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease>;
friend class TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease>;
friend class TAsyncSharedV2XWPReadOnlyAccessRequesterBase<_TAccessLease>;
friend class TAsyncSharedV2ReadOnlyConstPointerBase<_TAccessLease>;
};
template<typename _TAccessLease> class TAsyncSharedV2ReadWriteConstPointerBase;
template<typename _TAccessLease>
class TAsyncSharedV2ReadWritePointerBase : public AsyncSharedStrongPointerNotAsyncShareableTagBase {
public:
TAsyncSharedV2ReadWritePointerBase(const TAsyncSharedV2ReadWritePointerBase& src) : m_shptr(src.m_shptr), m_unique_lock(src.m_shptr->m_mutex1) {}
TAsyncSharedV2ReadWritePointerBase(TAsyncSharedV2ReadWritePointerBase&& src) = default;
virtual ~TAsyncSharedV2ReadWritePointerBase() {}
operator bool() const {
//assert(is_valid()); //{ MSE_THROW(asyncshared_use_of_invalid_pointer_error("attempt to use invalid pointer - mse::TAsyncSharedV2ReadWritePointerBase")); }
return m_shptr.operator bool();
}
typedef std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> m_shptr_t;
/* gcc5 crashes if you uncomment the explicit return type declaration (Nov 2017). */
auto& operator*() const/* -> typename std::add_lvalue_reference<decltype(*((*std::declval<m_shptr_t>()).cref()))>::type*/ {
assert(is_valid()); //{ MSE_THROW(asyncshared_use_of_invalid_pointer_error("attempt to use invalid pointer - mse::TAsyncSharedV2ReadWritePointerBase")); }
return (*((*m_shptr).cref()));
}
auto operator->() const/* -> decltype(std::addressof(*((*std::declval<m_shptr_t>()).cref())))*/ {
assert(is_valid()); //{ MSE_THROW(asyncshared_use_of_invalid_pointer_error("attempt to use invalid pointer - mse::TAsyncSharedV2ReadWritePointerBase")); }
return std::addressof(*((*m_shptr).cref()));
}
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
private:
TAsyncSharedV2ReadWritePointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr) : m_shptr(shptr), m_unique_lock(shptr->m_mutex1) {}
TAsyncSharedV2ReadWritePointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr, std::try_to_lock_t) : m_shptr(shptr), m_unique_lock(shptr->m_mutex1, std::defer_lock) {
if (!m_unique_lock.try_lock()) {
m_shptr = nullptr;
}
}
template<class _Rep, class _Period>
TAsyncSharedV2ReadWritePointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr, std::try_to_lock_t, const std::chrono::duration<_Rep, _Period>& _Rel_time) : m_shptr(shptr), m_unique_lock(shptr->m_mutex1, std::defer_lock) {
if (!m_unique_lock.try_lock_for(_Rel_time)) {
m_shptr = nullptr;
}
}
template<class _Clock, class _Duration>
TAsyncSharedV2ReadWritePointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr, std::try_to_lock_t, const std::chrono::time_point<_Clock, _Duration>& _Abs_time) : m_shptr(shptr), m_unique_lock(shptr->m_mutex1, std::defer_lock) {
if (!m_unique_lock.try_lock_until(_Abs_time)) {
m_shptr = nullptr;
}
}
TAsyncSharedV2ReadWritePointerBase<_TAccessLease>& operator=(const TAsyncSharedV2ReadWritePointerBase<_TAccessLease>& _Right_cref) = delete;
TAsyncSharedV2ReadWritePointerBase<_TAccessLease>& operator=(TAsyncSharedV2ReadWritePointerBase<_TAccessLease>&& _Right) = delete;
TAsyncSharedV2ReadWritePointerBase<_TAccessLease>* operator&() { return this; }
const TAsyncSharedV2ReadWritePointerBase<_TAccessLease>* operator&() const { return this; }
bool is_valid() const {
bool retval = m_shptr.operator bool();
return retval;
}
std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> m_shptr;
std::unique_lock<async_shared_timed_mutex_type> m_unique_lock;
friend class TAsyncSharedV2XWPReadWriteAccessRequesterBase<_TAccessLease>;
friend class TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>;
friend class TAsyncSharedV2ReadWritePointer<_TAccessLease>;
friend class TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease>;
};
template<typename _TAccessLease>
class TXScopeAsyncSharedV2ReadWritePointer : public TAsyncSharedV2ReadWritePointerBase<_TAccessLease>, public XScopeTagBase {
public:
typedef TAsyncSharedV2ReadWritePointerBase<_TAccessLease> base_class;
TXScopeAsyncSharedV2ReadWritePointer(const TXScopeAsyncSharedV2ReadWritePointer& src) = default;
TXScopeAsyncSharedV2ReadWritePointer(TXScopeAsyncSharedV2ReadWritePointer&& src) = default;
virtual ~TXScopeAsyncSharedV2ReadWritePointer() {}
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
private:
TXScopeAsyncSharedV2ReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr) : base_class(shptr) {}
TXScopeAsyncSharedV2ReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl) : base_class(shptr, ttl) {}
template<class _Rep, class _Period>
TXScopeAsyncSharedV2ReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::duration<_Rep, _Period>& _Rel_time) : base_class(shptr, ttl, _Rel_time) {}
template<class _Clock, class _Duration>
TXScopeAsyncSharedV2ReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::time_point<_Clock, _Duration>& _Abs_time) : base_class(shptr, ttl, _Abs_time) {}
TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>* operator&() { return this; }
const TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>* operator&() const { return this; }
friend class TAsyncSharedV2XWPReadWriteAccessRequesterBase<_TAccessLease>;
};
template<typename _TAccessLease>
class TAsyncSharedV2ReadWritePointer : public TAsyncSharedV2ReadWritePointerBase<_TAccessLease> {
public:
typedef TAsyncSharedV2ReadWritePointerBase<_TAccessLease> base_class;
TAsyncSharedV2ReadWritePointer(const TAsyncSharedV2ReadWritePointer& src) = default;
TAsyncSharedV2ReadWritePointer(TAsyncSharedV2ReadWritePointer&& src) = default;
virtual ~TAsyncSharedV2ReadWritePointer() {}
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
private:
TAsyncSharedV2ReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr) : base_class(shptr) {}
TAsyncSharedV2ReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl) : base_class(shptr, ttl) {}
template<class _Rep, class _Period>
TAsyncSharedV2ReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::duration<_Rep, _Period>& _Rel_time) : base_class(shptr, ttl, _Rel_time) {}
template<class _Clock, class _Duration>
TAsyncSharedV2ReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::time_point<_Clock, _Duration>& _Abs_time) : base_class(shptr, ttl, _Abs_time) {}
TAsyncSharedV2ReadWritePointer<_TAccessLease>* operator&() { return this; }
const TAsyncSharedV2ReadWritePointer<_TAccessLease>* operator&() const { return this; }
friend class TAsyncSharedV2XWPReadWriteAccessRequesterBase<_TAccessLease>;
};
template<typename _TAccessLease>
class TAsyncSharedV2ReadWriteConstPointerBase : public AsyncSharedStrongPointerNotAsyncShareableTagBase {
public:
TAsyncSharedV2ReadWriteConstPointerBase(const TAsyncSharedV2ReadWriteConstPointerBase& src) : m_shptr(src.m_shptr), m_shared_lock(src.m_shptr->m_mutex1) {}
TAsyncSharedV2ReadWriteConstPointerBase(TAsyncSharedV2ReadWriteConstPointerBase&& src) = default;
TAsyncSharedV2ReadWriteConstPointerBase(const TAsyncSharedV2ReadWritePointerBase<_TAccessLease>& src) : m_shptr(src.m_shptr), m_shared_lock(src.m_shptr->m_mutex1) {}
virtual ~TAsyncSharedV2ReadWriteConstPointerBase() {}
operator bool() const {
//assert(is_valid()); //{ MSE_THROW(asyncshared_use_of_invalid_pointer_error("attempt to use invalid pointer - mse::TAsyncSharedV2ReadWriteConstPointerBase")); }
return m_shptr.operator bool();
}
typedef std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> m_shptr_t;
const auto& operator*() const/* -> typename std::add_const<typename std::add_lvalue_reference<decltype(*((*std::declval<m_shptr_t>()).cref()))>::type>::type*/ {
assert(is_valid()); //{ MSE_THROW(asyncshared_use_of_invalid_pointer_error("attempt to use invalid pointer - mse::TAsyncSharedV2ReadWritePointerBase")); }
return (*((*m_shptr).cref()));
}
const auto operator->() const/* -> typename std::add_const<decltype(std::addressof(*((*std::declval<m_shptr_t>()).cref())))>::type*/ {
assert(is_valid()); //{ MSE_THROW(asyncshared_use_of_invalid_pointer_error("attempt to use invalid pointer - mse::TAsyncSharedV2ReadWritePointerBase")); }
return std::addressof(*((*m_shptr).cref()));
}
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
private:
TAsyncSharedV2ReadWriteConstPointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr) : m_shptr(shptr), m_shared_lock(shptr->m_mutex1) {}
TAsyncSharedV2ReadWriteConstPointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr, std::try_to_lock_t) : m_shptr(shptr), m_shared_lock(shptr->m_mutex1, std::defer_lock) {
if (!m_shared_lock.try_lock()) {
m_shptr = nullptr;
}
}
template<class _Rep, class _Period>
TAsyncSharedV2ReadWriteConstPointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr, std::try_to_lock_t, const std::chrono::duration<_Rep, _Period>& _Rel_time) : m_shptr(shptr), m_shared_lock(shptr->m_mutex1, std::defer_lock) {
if (!m_shared_lock.try_lock_for(_Rel_time)) {
m_shptr = nullptr;
}
}
template<class _Clock, class _Duration>
TAsyncSharedV2ReadWriteConstPointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr, std::try_to_lock_t, const std::chrono::time_point<_Clock, _Duration>& _Abs_time) : m_shptr(shptr), m_shared_lock(shptr->m_mutex1, std::defer_lock) {
if (!m_shared_lock.try_lock_until(_Abs_time)) {
m_shptr = nullptr;
}
}
TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease>& operator=(const TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease>& _Right_cref) = delete;
TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease>& operator=(TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease>&& _Right) = delete;
TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease>* operator&() { return this; }
const TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease>* operator&() const { return this; }
bool is_valid() const {
bool retval = m_shptr.operator bool();
return retval;
}
std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> m_shptr;
std::shared_lock<async_shared_timed_mutex_type> m_shared_lock;
friend class TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>;
friend class TAsyncSharedV2ReadWriteConstPointer<_TAccessLease>;
friend class TAsyncSharedV2XWPReadWriteAccessRequesterBase<_TAccessLease>;
};
template<typename _TAccessLease>
class TXScopeAsyncSharedV2ReadWriteConstPointer : public TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease>, public XScopeTagBase {
public:
typedef TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease> base_class;
TXScopeAsyncSharedV2ReadWriteConstPointer(const TXScopeAsyncSharedV2ReadWriteConstPointer& src) = default;
TXScopeAsyncSharedV2ReadWriteConstPointer(TXScopeAsyncSharedV2ReadWriteConstPointer&& src) = default;
virtual ~TXScopeAsyncSharedV2ReadWriteConstPointer() {}
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
private:
TXScopeAsyncSharedV2ReadWriteConstPointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr) : base_class(shptr) {}
TXScopeAsyncSharedV2ReadWriteConstPointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl) : base_class(shptr, ttl) {}
template<class _Rep, class _Period>
TXScopeAsyncSharedV2ReadWriteConstPointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::duration<_Rep, _Period>& _Rel_time) : base_class(shptr, ttl, _Rel_time) {}
template<class _Clock, class _Duration>
TXScopeAsyncSharedV2ReadWriteConstPointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::time_point<_Clock, _Duration>& _Abs_time) : base_class(shptr, ttl, _Abs_time) {}
TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>* operator&() { return this; }
const TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>* operator&() const { return this; }
friend class TAsyncSharedV2XWPReadWriteAccessRequesterBase<_TAccessLease>;
};
template<typename _TAccessLease>
class TAsyncSharedV2ReadWriteConstPointer : public TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease> {
public:
typedef TAsyncSharedV2ReadWriteConstPointerBase<_TAccessLease> base_class;
TAsyncSharedV2ReadWriteConstPointer(const TAsyncSharedV2ReadWriteConstPointer& src) = default;
TAsyncSharedV2ReadWriteConstPointer(TAsyncSharedV2ReadWriteConstPointer&& src) = default;
virtual ~TAsyncSharedV2ReadWriteConstPointer() {}
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
private:
TAsyncSharedV2ReadWriteConstPointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr) : base_class(shptr) {}
TAsyncSharedV2ReadWriteConstPointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl) : base_class(shptr, ttl) {}
template<class _Rep, class _Period>
TAsyncSharedV2ReadWriteConstPointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::duration<_Rep, _Period>& _Rel_time) : base_class(shptr, ttl, _Rel_time) {}
template<class _Clock, class _Duration>
TAsyncSharedV2ReadWriteConstPointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::time_point<_Clock, _Duration>& _Abs_time) : base_class(shptr, ttl, _Abs_time) {}
TAsyncSharedV2ReadWriteConstPointer<_TAccessLease>* operator&() { return this; }
const TAsyncSharedV2ReadWriteConstPointer<_TAccessLease>* operator&() const { return this; }
friend class TAsyncSharedV2XWPReadWriteAccessRequesterBase<_TAccessLease>;
};
template<typename _TAccessLease>
class TAsyncSharedV2ExclusiveReadWritePointerBase : public AsyncSharedStrongPointerNotAsyncShareableTagBase {
public:
TAsyncSharedV2ExclusiveReadWritePointerBase(const TAsyncSharedV2ExclusiveReadWritePointerBase& src) = delete;
TAsyncSharedV2ExclusiveReadWritePointerBase(TAsyncSharedV2ExclusiveReadWritePointerBase&& src) = default;
virtual ~TAsyncSharedV2ExclusiveReadWritePointerBase() {}
operator bool() const {
//assert(is_valid()); //{ MSE_THROW(asyncshared_use_of_invalid_pointer_error("attempt to use invalid pointer - mse::TAsyncSharedV2ExclusiveReadWritePointerBase")); }
return m_shptr.operator bool();
}
typedef std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> m_shptr_t;
auto& operator*() const/* -> typename std::add_lvalue_reference<decltype(*((*std::declval<m_shptr_t>()).cref()))>::type*/ {
assert(is_valid()); //{ MSE_THROW(asyncshared_use_of_invalid_pointer_error("attempt to use invalid pointer - mse::TAsyncSharedV2ReadWritePointerBase")); }
return (*((*m_shptr).cref()));
}
auto operator->() const/* -> decltype(std::addressof(*((*std::declval<m_shptr_t>()).cref())))*/ {
assert(is_valid()); //{ MSE_THROW(asyncshared_use_of_invalid_pointer_error("attempt to use invalid pointer - mse::TAsyncSharedV2ReadWritePointerBase")); }
return std::addressof(*((*m_shptr).cref()));
}
void async_shareable_tag() const {} /* Indication that this type is eligible to be shared between threads. */
private:
TAsyncSharedV2ExclusiveReadWritePointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr) : m_shptr(shptr), m_unique_lock(shptr->m_mutex1) {}
TAsyncSharedV2ExclusiveReadWritePointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr, std::try_to_lock_t) : m_shptr(shptr), m_unique_lock(shptr->m_mutex1, std::defer_lock) {
if (!m_unique_lock.try_lock()) {
m_shptr = nullptr;
}
}
template<class _Rep, class _Period>
TAsyncSharedV2ExclusiveReadWritePointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr, std::try_to_lock_t, const std::chrono::duration<_Rep, _Period>& _Rel_time) : m_shptr(shptr), m_unique_lock(shptr->m_mutex1, std::defer_lock) {
if (!m_unique_lock.try_lock_for(_Rel_time)) {
m_shptr = nullptr;
}
}
template<class _Clock, class _Duration>
TAsyncSharedV2ExclusiveReadWritePointerBase(std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> shptr, std::try_to_lock_t, const std::chrono::time_point<_Clock, _Duration>& _Abs_time) : m_shptr(shptr), m_unique_lock(shptr->m_mutex1, std::defer_lock) {
if (!m_unique_lock.try_lock_until(_Abs_time)) {
m_shptr = nullptr;
}
}
TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease>& operator=(const TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease>& _Right_cref) = delete;
TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease>& operator=(TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease>&& _Right) = delete;
TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease>* operator&() { return this; }
const TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease>* operator&() const { return this; }
bool is_valid() const {
bool retval = m_shptr.operator bool();
return retval;
}
std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>> m_shptr;
unique_nonrecursive_lock<async_shared_timed_mutex_type> m_unique_lock;
friend class TXScopeAsyncSharedV2ExclusiveReadWritePointer<_TAccessLease>;
friend class TAsyncSharedV2ExclusiveReadWritePointer<_TAccessLease>;
friend class TAsyncSharedV2XWPReadWriteAccessRequesterBase<_TAccessLease>;
};
template<typename _TAccessLease>
class TXScopeAsyncSharedV2ExclusiveReadWritePointer : public TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease>, public XScopeTagBase {
public:
typedef TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease> base_class;
TXScopeAsyncSharedV2ExclusiveReadWritePointer(const TXScopeAsyncSharedV2ExclusiveReadWritePointer& src) = delete;
TXScopeAsyncSharedV2ExclusiveReadWritePointer(TXScopeAsyncSharedV2ExclusiveReadWritePointer&& src) = default;
virtual ~TXScopeAsyncSharedV2ExclusiveReadWritePointer() {}
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
private:
TXScopeAsyncSharedV2ExclusiveReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr) : base_class(shptr) {}
TXScopeAsyncSharedV2ExclusiveReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl) : base_class(shptr, ttl) {}
template<class _Rep, class _Period>
TXScopeAsyncSharedV2ExclusiveReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::duration<_Rep, _Period>& _Rel_time) : base_class(shptr, ttl, _Rel_time) {}
template<class _Clock, class _Duration>
TXScopeAsyncSharedV2ExclusiveReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::time_point<_Clock, _Duration>& _Abs_time) : base_class(shptr, ttl, _Abs_time) {}
TXScopeAsyncSharedV2ExclusiveReadWritePointer<_TAccessLease>* operator&() { return this; }
const TXScopeAsyncSharedV2ExclusiveReadWritePointer<_TAccessLease>* operator&() const { return this; }
friend class TAsyncSharedV2XWPReadWriteAccessRequesterBase<_TAccessLease>;
};
template<typename _TAccessLease>
class TAsyncSharedV2ExclusiveReadWritePointer : public TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease> {
public:
typedef TAsyncSharedV2ExclusiveReadWritePointerBase<_TAccessLease> base_class;
TAsyncSharedV2ExclusiveReadWritePointer(const TAsyncSharedV2ExclusiveReadWritePointer& src) = delete;
TAsyncSharedV2ExclusiveReadWritePointer(TAsyncSharedV2ExclusiveReadWritePointer&& src) = default;
virtual ~TAsyncSharedV2ExclusiveReadWritePointer() {}
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
private:
TAsyncSharedV2ExclusiveReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr) : base_class(shptr) {}
TAsyncSharedV2ExclusiveReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl) : base_class(shptr, ttl) {}
template<class _Rep, class _Period>
TAsyncSharedV2ExclusiveReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::duration<_Rep, _Period>& _Rel_time) : base_class(shptr, ttl, _Rel_time) {}
template<class _Clock, class _Duration>
TAsyncSharedV2ExclusiveReadWritePointer(const std::shared_ptr<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>& shptr, const std::try_to_lock_t& ttl, const std::chrono::time_point<_Clock, _Duration>& _Abs_time) : base_class(shptr, ttl, _Abs_time) {}
TAsyncSharedV2ExclusiveReadWritePointer<_TAccessLease>* operator&() { return this; }
const TAsyncSharedV2ExclusiveReadWritePointer<_TAccessLease>* operator&() const { return this; }
friend class TAsyncSharedV2XWPReadWriteAccessRequesterBase<_TAccessLease>;
};
/* to do: verify that _TAccessLease is a (supported) safe, strong exclusive pointer type, or move the
TAsyncSharedV2XWPReadWriteAccessRequester classes to the "mse::us" namespace. */
template<typename _TAccessLease>
class TAsyncSharedV2XWPReadWriteAccessRequesterBase {
public:
TAsyncSharedV2XWPReadWriteAccessRequesterBase(const TAsyncSharedV2XWPReadWriteAccessRequesterBase& src_cref) = default;
TAsyncSharedV2XWPReadWriteAccessRequesterBase(_TAccessLease&& exclusive_write_pointer) {
m_shptr = std::make_shared<TAsyncSharedXWPAccessLeaseObj<_TAccessLease>>(std::forward<decltype(exclusive_write_pointer)>(exclusive_write_pointer));
}
TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease> xscope_writelock_ptr() {
return TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>(m_shptr);
}
mse::xscope_optional<TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>> xscope_try_writelock_ptr() {
mse::xscope_optional<TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>> retval(TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>(m_shptr, std::try_to_lock));
if (!((*retval).is_valid())) {
return{};
}
return retval;
}
template<class _Rep, class _Period>
mse::xscope_optional<TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>> xscope_try_writelock_ptr_for(const std::chrono::duration<_Rep, _Period>& _Rel_time) {
mse::xscope_optional<TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>> retval(TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>(m_shptr, std::try_to_lock, _Rel_time));
if (!((*retval).is_valid())) {
return{};
}
return retval;
}
template<class _Clock, class _Duration>
mse::xscope_optional<TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>> xscope_try_writelock_ptr_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time) {
mse::xscope_optional<TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>> retval(TXScopeAsyncSharedV2ReadWritePointer<_TAccessLease>(m_shptr, std::try_to_lock, _Abs_time));
if (!((*retval).is_valid())) {
return{};
}
return retval;
}
TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease> xscope_readlock_ptr() {
return TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>(m_shptr);
}
mse::xscope_optional<TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>> xscope_try_readlock_ptr() {
mse::xscope_optional<TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>> retval(TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>(m_shptr, std::try_to_lock));
if (!((*retval).is_valid())) {
return{};
}
return retval;
}
template<class _Rep, class _Period>
mse::xscope_optional<TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>> xscope_try_readlock_ptr_for(const std::chrono::duration<_Rep, _Period>& _Rel_time) {
mse::xscope_optional<TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>> retval(TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>(m_shptr, std::try_to_lock, _Rel_time));
if (!((*retval).is_valid())) {
return{};
}
return retval;
}
template<class _Clock, class _Duration>
mse::xscope_optional<TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>> xscope_try_readlock_ptr_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time) {
mse::xscope_optional<TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>> retval(TXScopeAsyncSharedV2ReadWriteConstPointer<_TAccessLease>(m_shptr, std::try_to_lock, _Abs_time));
if (!((*retval).is_valid())) {
return{};
}
return retval;
}
/* Note that an exclusive_writelock_ptr cannot coexist with any other lock_ptrs (targeting the same object), including ones in