Commit Graph

8 Commits

Author SHA1 Message Date
Yuta Saito
d64f83aeeb [test] disable a part of unittests/runtime/Concurrent.cpp for single-thread runtime 2021-09-21 22:55:35 +00:00
Mike Ash
ef8d20a0e6 [Runtime] Fix incorrect memory ordering in ConcurrentReadableArray/HashMap.
When reallocating storage, the storing the pointer to the new storage had insufficiently strong ordering. This could cause writers to check the reader count before storing the new storage pointer. If a reader then came in between that load and store, it would end up using the old storage pointer, while the writer could end up freeing it.

Also adjust the Concurrent.cpp tests to test with a variety of reader and writer counts. Counterintuitively, when freeing garbage is gated on there being zero readers, having a single reader will shake out problems that having lots of readers will not. We were testing with lots of readers in order to stress the code as much as possible, but this resulted in it being extremely rare for writers to ever see zero active readers.

rdar://69798617
2021-01-12 10:41:56 -05:00
Mike Ash
630aff7b19 [Runtime] Change SimpleGlobalCache to use ConcurrentReadableHashMap instead of ConcurrentMap.
This gives us faster lookups and a small advantage in memory usage. Most of these maps need stable addresses for their entries, so we add a level of indirection to ConcurrentReadableHashMap for these cases to accommodate that. This costs some extra memory, but it's still a net win.

A new StableAddressConcurrentReadableHashMap type handles this indirection and adds a convenience getOrInsert to take advantage of it.

ConcurrentReadableHashMap is tweaked to avoid any global constructors or destructors when using it as a global variable.

ForeignWitnessTables does not need stable addresses and it now uses ConcurrentReadableHashMap directly.

rdar://problem/70056398
2020-10-08 14:57:39 -04:00
Mike Ash
ecd6d4ddec Add a new ConcurrentReadableHashMap type. Switch the protocol conformance cache
to use it.

ConcurrentReadableHashMap is lock-free for readers, with writers using a lock to
ensure mutual exclusion amongst each other. The intent is to eventually replace
all uses ConcurrentMap with ConcurrentReadableHashMap.

ConcurrentReadableHashMap provides for relatively quick lookups by using a hash
table. Rearders perform an atomic increment/decrement in order to inform writers
that there are active readers. The design attempts to minimize wasted memory by
storing the actual elements out-of-line, and having the table store indices into
a separate array of elements.

The protocol conformance cache now uses ConcurrentReadableHashMap, which
provides faster lookups and less memory use than the previous ConcurrentMap
implementation. The previous implementation caches
ProtocolConformanceDescriptors and extracts the WitnessTable after the cache
lookup. The new implementation directly caches the WitnessTable, removing an
extra step (potentially a quite slow one) from the fast path.

The previous implementation used a generational scheme to detect when negative
cache entries became obsolete due to new dynamic libraries being loaded, and
update them in place. The new implementation just clears the entire cache when
libraries are loaded, greatly simplifying the code and saving the memory needed
to track the current generation in each negative cache entry. This means we need
to re-cache all requested conformances after loading a dynamic library, but
loading libraries at runtime is rare and slow anyway.

rdar://problem/67268325
2020-08-20 13:05:30 -04:00
Mike Ash
cbb1fd3f27 Add some comments to ConcurrentReadableArrayTest.MultiThreaded. Add a second test to stress the snapshot code. 2020-08-13 15:32:23 -04:00
Mike Ash
1ac565d6ef Add a multithreaded test for ConcurrentReadableArray. 2020-08-07 11:17:38 -04:00
Mike Ash
9134e65fca Move Mutex test's threading helpers to a separate header so other tests can use them. 2020-08-07 11:17:38 -04:00
Mike Ash
a4863c4dcd [Runtime] Fix ConcurrentReadableArray's handling of FreeList to not double-free items. Also implement a destructor so it can be used for non-globals.
rdar://problem/40484362
2018-05-23 11:29:26 -04:00