Skip to content

[TLS] Add a uText to our TLS #19458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions stdlib/public/core/ThreadLocalStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ internal struct _ThreadLocalStorage {
//
// private
internal var uBreakIterator: OpaquePointer
internal var uText: OpaquePointer

// TODO: Consider saving two, e.g. for character-by-character comparison

Expand All @@ -55,8 +56,9 @@ internal struct _ThreadLocalStorage {
// TODO: unowned reference to string owner, base address, and _countAndFlags

// private: Should only be called by _initializeThreadLocalStorage
internal init(_uBreakIterator: OpaquePointer) {
internal init(_uBreakIterator: OpaquePointer, _uText: OpaquePointer) {
self.uBreakIterator = _uBreakIterator
self.uText = _uText
}

// Get the current thread's TLS pointer. On first call for a given thread,
Expand Down Expand Up @@ -104,19 +106,26 @@ internal func _destroyTLS(_ ptr: UnsafeMutableRawPointer?) {
internal func _createThreadLocalStorage()
-> UnsafeMutablePointer<_ThreadLocalStorage>
{
// Create and initialize one.
// Allocate and initialize a UBreakIterator and UText.
var err = __swift_stdlib_U_ZERO_ERROR
let newUBreakIterator = __swift_stdlib_ubrk_open(
/*type:*/ __swift_stdlib_UBRK_CHARACTER, /*locale:*/ nil,
/*text:*/ nil, /*textLength:*/ 0, /*status:*/ &err)
_precondition(err.isSuccess, "Unexpected ubrk_open failure")

// utext_openUTF8 needs a valid pointer, even though we won't read from it
var a: Int8 = 0x41
let newUText = __swift_stdlib_utext_openUTF8(
/*ut:*/ nil, /*s:*/ &a, /*len:*/ 1, /*status:*/ &err)

_precondition(err.isSuccess, "Unexpected utext_openUTF8 failure")

let tlsPtr: UnsafeMutablePointer<_ThreadLocalStorage>
= UnsafeMutablePointer<_ThreadLocalStorage>.allocate(
capacity: 1
)
tlsPtr.initialize(
to: _ThreadLocalStorage(_uBreakIterator: newUBreakIterator)
)
tlsPtr.initialize(to: _ThreadLocalStorage(
_uBreakIterator: newUBreakIterator, _uText: newUText))

return tlsPtr
}