Use of nonisolated(unsafe) and bridging headers

I was reading the following threads SE-0412: Strict concurrency for global variables and Concurrency-safe global variables to prevent data races - SwiftLee, but now I stumble upon a similar situation where the swift compiler is still throwing the error:

error: reference to var 'my_c_variable' is not concurrency-safe because it involves shared mutable state

I have my_c_variable located in c source file and it's using a bridge header file in order to be compiled as part of my swift sources.

The problem is that I cannot use the nonisolated(unsafe) in the c source file because it's not valid.

This the actual error I get:

error: reference to var 'my_c_variable' is not concurrency-safe because it involves shared mutable state
        my_c_variable= 0xDEFEADED
        ^
__ObjC.my_c_variable:1:12: note: var declared here
public var my_c_variable: UInt32

Any ideas if there is a way to workaround this? or if if there is native identifier in c equivalent to nonisolated(unsafe)?

1 Like

The problem is that I cannot use the nonisolated(unsafe) in
the c source file because it's not valid.

The following works for me:

extern int my_c_variable __attribute__((swift_attr("nonisolated(unsafe)")));

I was using the Swift 6 in Xcode 16.0b1.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

4 Likes

Nice that worked. I completely forgot about attribute

I was working around it by re-declaring the variable in swift code like so

nonisolated(unsafe) public var my_c_variable: UInt32!

but wasn't sure if that was the correct way.

I appreciate the help. :smiley:

1 Like