mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Introduces a _ThreadLocalStorage struct to hold thread-local, but global resources. Set it up to host a UBreakIterator and a cache key for resetting text. UBreakIterators are extremely expensive to create on the fly, so we store one for each thread. UBreakIterators are also expensive to bind to new text, so we cache the text it's currently bound to in order to help avoid it. The struct can be expanded with more functionality in the future, but the standard library should only ever use a single key, and thus everything should go on this struct. The _ThreadLocalStorage struct is not meant to be copyable, creatable (by anyone else except the once-per-thread initialize routine), and should accessed through the pointers it provides. Future immediate directions could include cashing multiple UBreakIterators (e.g. avoid a text reset for mutual character iteration patterns, etc). Test added in test/stdlib/ThreadLocalStorage.swift.
41 lines
930 B
Swift
41 lines
930 B
Swift
// RUN: %target-run-stdlib-swift
|
|
// REQUIRES: executable_test
|
|
// REQUIRES: OS=macosx
|
|
|
|
import StdlibUnittest
|
|
import Swift
|
|
import SwiftPrivate
|
|
|
|
import Darwin
|
|
|
|
var str = "Lorem ipsum dolor sit amet\r\n 🌎 🇺🇸🇨🇦🍁"
|
|
|
|
func checkString(_unused: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
|
|
expectEqual(str.count, str.reversed().count)
|
|
return nil
|
|
}
|
|
|
|
var PThreadTests = TestSuite("Thread Local Storage")
|
|
|
|
// Test that the destructor for our TLS is invoked
|
|
PThreadTests.test("destructor") {
|
|
#if INTERNAL_CHECKS_ENABLED
|
|
let numIters = 100
|
|
for _ in 0..<numIters {
|
|
var thread : pthread_t? = nil
|
|
guard pthread_create(&thread, nil, checkString, &thread) == 0 else {
|
|
fatalError("pthread_create failed")
|
|
}
|
|
guard pthread_join(thread!, nil) == 0 else {
|
|
fatalError("pthread_join failed")
|
|
}
|
|
}
|
|
expectEqual(numIters, _destroyTLSCounter.load())
|
|
#endif
|
|
var x = 1
|
|
checkString(_unused: &x)
|
|
}
|
|
|
|
runAllTests()
|
|
|