Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 10 additions & 4 deletions Sources/ThreadLocal/ThreadLocal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import Darwin.C

/// Manages a thread-local defined via the ``ThreadLocal()`` macro.
///
/// You do not use this type directly. Instead, your code interacts with thread-local variables by simply directly accessing them via their getter and setter.
/// You do not use this type directly.
/// Instead, your code interacts with thread-local variables by simply directly accessing them via their getter and setter.
public final class ThreadLocal<Value>: Sendable {
nonisolated(unsafe) public private(set) var _key: pthread_key_t
public let _deallocator: Deallocator
Expand All @@ -38,15 +39,16 @@ public final class ThreadLocal<Value>: Sendable {
#if os(Linux)
let destroyFn: @convention(c) (UnsafeMutableRawPointer?) -> Void = { ptr in
if let ptr {
unsafeBitCast(ptr, to: Unmanaged<AnyObject>.self).release()
Unmanaged<AnyObject>.fromOpaque(ptr).release()
}
}
#else
let destroyFn: @convention(c) (UnsafeMutableRawPointer) -> Void = { ptr in
unsafeBitCast(ptr, to: Unmanaged<AnyObject>.self).release()
Unmanaged<AnyObject>.fromOpaque(ptr).release()
}
#endif
pthread_key_create(&_key, destroyFn)
let status = pthread_key_create(&_key, destroyFn)
precondition(status == 0, "pthread_key_create failed with status code \(status)")
}

@inlinable
Expand Down Expand Up @@ -85,6 +87,10 @@ public final class ThreadLocal<Value>: Sendable {
pthread_setspecific(_key, unmanaged.toOpaque())
}
}

deinit {
pthread_key_delete(_key)
}
}


Expand Down
6 changes: 3 additions & 3 deletions Sources/ThreadLocalMacros/ThreadLocalMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extension ThreadLocalMacro: PeerMacro {
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard let variableDeclaration = declaration.as(VariableDeclSyntax.self) else {
throw DiagnosticsError(syntax: declaration, message: "'@Property' can only be applied to a 'var' declaration", id: .invalidSyntax)
throw DiagnosticsError(syntax: declaration, message: "'@ThreadLocal' can only be applied to a 'var' declaration", id: .invalidSyntax)
}
guard variableDeclaration.isStatic else {
// not reporting an error here bc the other function above is already taking care of that.
Expand All @@ -82,14 +82,14 @@ extension ThreadLocalMacro: PeerMacro {
variableDeclaration.bindings.count == 1 else {
throw DiagnosticsError(
syntax: declaration,
message: "'@Property' can only be applied to a 'var' declaration with a single binding",
message: "'@ThreadLocal' can only be applied to a 'var' declaration with a single binding",
id: .invalidSyntax
)
}
guard let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier else {
throw DiagnosticsError(
syntax: declaration,
message: "'@Property' can only be applied to a 'var' declaration with a simple name",
message: "'@ThreadLocal' can only be applied to a 'var' declaration with a simple name",
id: .invalidSyntax
)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ThreadLocalMacros/ThreadLocalMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import SwiftSyntaxMacros


@main
struct SpeziSchedulerMacros: CompilerPlugin {
struct ThreadLocalMacros: CompilerPlugin {
var providingMacros: [any Macro.Type] = [ThreadLocalMacro.self]
}
Loading