swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
66.86k stars 10.3k forks source link

@ThreadLocal wanted #71556

Open endeavour42 opened 4 months ago

endeavour42 commented 4 months ago

Motivation

We have @TaskLocal, a similar @ThreadLocal would be useful at times we need to work with thread local variables. It would make the code both safer, shorter and nicer.

Proposed solution

Implement @ThreadLocal similar to how @TaskLocal is implemented

Alternatives considered

Continue to rely on unsafe pthread APIs (pthread_key_create, pthread_setspecific, pthread_getspecific) to work with thread locals.

Additional information

No response

ktoso commented 4 months ago

I'm tempted to say this can just be a third party mini library. 🤔

Swift concurrency is all about tasks so this is doomed to not work "in Swift concurrency"...

As a specialist tool for low level things perhaps one could stuff it into Synchronization module where we have all the locks and low level things.

ktoso commented 4 months ago

I would suggest starting it as a mini library and once we have it and we like it we can discuss if Swift should be shipping such a thing or not.

I would suggest writing a macro to try out out the concept