How to correctly refer to global C variables?

Hi,
I'm trying to do some stuff with ncurses (for now, import Darwin.ncurses, but that will have to change if I do other platforms). I'm using Swift 6, and I'm getting some errors when I try and get the dimensions of the terminal:

class Console {
    var size: Point { return Point(Int(getmaxx(stdscr)), Int(getmaxy(stdscr))) }
}
Reference to var 'stdscr' is not concurrency-safe because it involves shared mutable state

Which is true. I've not sat down and fully comes to terms with the new concurrency stuff. But this is a single threaded affair, and I'm quite happy to restrict access to the console to a single thread.

The problem I'm running into is that it seems like I'd have to annotate the global variable itself, which I can't do. I can make the error go away by storing the pointer to stdscr that initscr returns, and then mark the Console class as @MainActor to ensure everything is above board (if my understanding is accurate).

Is there a way to annotate global variables from another package that I've missed, or is make-a-copy-and-annotate-that the way to go?

1 Like

Does @preconcurrency import Darwin.ncurses get you anywhere?

3 Likes

That works. Good to know, thanks.

I am still curious to know what the 'correct' fix for global non-Sendable variables like stdscr would be, on the binding side or on the client side.

The correct fix would either be to declare it as a constant instead (although that may not work for non Sendable types like pointers) or mark it as nonisolated(unsafe) (but I’m not sure if there’s a C macro for that)

1 Like