67 lines
2.8 KiB
Swift
67 lines
2.8 KiB
Swift
// RTThreadIdentityTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
import Darwin
|
|
import Dispatch
|
|
@testable import LyrebirdSynth
|
|
|
|
/// Verifies that the pthread-TLS marker used by ``RTThreadIdentity`` is
|
|
/// thread-local: marking one thread does not bleed into another. The
|
|
/// DEBUG-only `_lyrebirdAssertNotOnRTThread` trap is not exercised here
|
|
/// (`preconditionFailure` aborts the test process); the contract that
|
|
/// underpins it is fully covered by these read/write tests.
|
|
@Suite("RTThreadIdentity")
|
|
struct RTThreadIdentityTests {
|
|
|
|
@Test func warmUpDoesNotMarkCallerAsRT() {
|
|
RTThreadIdentity.warmUp()
|
|
#expect(RTThreadIdentity.isCurrentThreadRT == false)
|
|
}
|
|
|
|
@Test func markingIsThreadLocalNotProcessGlobal() async {
|
|
// Pre-condition: this (test) thread is not marked.
|
|
#expect(RTThreadIdentity.isCurrentThreadRT == false)
|
|
|
|
// Mark a *different* pthread, confirm via that thread's own read.
|
|
// Use a one-shot detached `Thread` (NOT a `DispatchQueue`): a GCD worker pthread is
|
|
// recycled into the shared pool still carrying the RT marker, which can then
|
|
// false-trip `_lyrebirdAssertNotOnRTThread` in a later `.compute()` test the pool
|
|
// hands the same thread. A detached `Thread` terminates its pthread when the block
|
|
// returns, so the marker cannot leak; we also clear it defensively.
|
|
let markerSawRT: Bool = await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
|
|
let thread: Thread = Thread {
|
|
RTThreadIdentity.markCurrentThreadAsRT()
|
|
let observed: Bool = RTThreadIdentity.isCurrentThreadRT
|
|
lyrebirdClearRTMarkAfterTestRender()
|
|
continuation.resume(returning: observed)
|
|
}
|
|
thread.start()
|
|
}
|
|
#expect(markerSawRT == true)
|
|
|
|
// This (test) thread must still be unmarked — the other thread's
|
|
// pthread_setspecific must not leak into our TLS slot.
|
|
#expect(RTThreadIdentity.isCurrentThreadRT == false)
|
|
}
|
|
|
|
@Test func markIsIdempotent() async {
|
|
let observedMarked: Bool = await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
|
|
let thread: Thread = Thread {
|
|
RTThreadIdentity.markCurrentThreadAsRT()
|
|
RTThreadIdentity.markCurrentThreadAsRT()
|
|
RTThreadIdentity.markCurrentThreadAsRT()
|
|
let observed: Bool = RTThreadIdentity.isCurrentThreadRT
|
|
lyrebirdClearRTMarkAfterTestRender()
|
|
continuation.resume(returning: observed)
|
|
}
|
|
thread.start()
|
|
}
|
|
#expect(observedMarked == true)
|
|
}
|
|
}
|