File tree Expand file tree Collapse file tree 3 files changed +59
-2
lines changed Expand file tree Collapse file tree 3 files changed +59
-2
lines changed Original file line number Diff line number Diff line change @@ -458,6 +458,13 @@ template <class ElemTy> struct ConcurrentReadableArray {
458
458
ReaderCount.fetch_sub (1 , std::memory_order_release);
459
459
}
460
460
461
+ void deallocateFreeList () {
462
+ for (Storage *storage : FreeList)
463
+ storage->deallocate ();
464
+ FreeList.clear ();
465
+ FreeList.shrink_to_fit ();
466
+ }
467
+
461
468
public:
462
469
struct Snapshot {
463
470
ConcurrentReadableArray *Array;
@@ -488,6 +495,12 @@ template <class ElemTy> struct ConcurrentReadableArray {
488
495
489
496
ConcurrentReadableArray () : Capacity(0 ), ReaderCount(0 ), Elements(nullptr ) {}
490
497
498
+ ~ConcurrentReadableArray () {
499
+ assert (ReaderCount.load (std::memory_order_acquire) == 0 &&
500
+ " deallocating ConcurrentReadableArray with outstanding snapshots" );
501
+ deallocateFreeList ();
502
+ }
503
+
491
504
void push_back (const ElemTy &elem) {
492
505
ScopedLock guard (WriterLock);
493
506
@@ -511,8 +524,7 @@ template <class ElemTy> struct ConcurrentReadableArray {
511
524
storage->Count .store (count + 1 , std::memory_order_release);
512
525
513
526
if (ReaderCount.load (std::memory_order_acquire) == 0 )
514
- for (Storage *storage : FreeList)
515
- storage->deallocate ();
527
+ deallocateFreeList ();
516
528
}
517
529
518
530
Snapshot snapshot () {
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
34
34
add_swift_unittest (SwiftRuntimeTests
35
35
Array.cpp
36
36
CompatibilityOverride.cpp
37
+ Concurrent.cpp
37
38
Exclusivity.cpp
38
39
Metadata.cpp
39
40
Mutex.cpp
Original file line number Diff line number Diff line change
1
+ // ===--- Concurrent.cpp - Concurrent data structure tests -----------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ // ===----------------------------------------------------------------------===//
12
+
13
+ #include " swift/Runtime/Concurrent.h"
14
+ #include " gtest/gtest.h"
15
+
16
+ using namespace swift ;
17
+
18
+ TEST (ConcurrentReadableArrayTest, SingleThreaded) {
19
+ ConcurrentReadableArray<size_t > array;
20
+
21
+ auto add = [&](size_t limit) {
22
+ for (size_t i = array.snapshot ().count (); i < limit; i++)
23
+ array.push_back (i);
24
+ };
25
+ auto check = [&]{
26
+ size_t i = 0 ;
27
+ for (auto element : array.snapshot ()) {
28
+ ASSERT_EQ (element, i);
29
+ i++;
30
+ }
31
+ };
32
+
33
+ check ();
34
+ add (1 );
35
+ check ();
36
+ add (16 );
37
+ check ();
38
+ add (100 );
39
+ check ();
40
+ add (1000 );
41
+ check ();
42
+ add (1000000 );
43
+ check ();
44
+ }
You can’t perform that action at this time.
0 commit comments