Potential issue for NS_INLINE+ dispatch_once on swift-corelibs-foundation

Background

  1. static variables in the static function are shared by the same compile Unit, but not by different compile Units.
  2. static variables in normal functions share one copy.

The NS_INLINE is defined as follows

#if !defined(NS_INLINE)
    #if defined(__GNUC__)
        #define NS_INLINE static __inline__ __attribute__((always_inline))
    #elif defined(__MWERKS__) || defined(__cplusplus)
        #define NS_INLINE static inline
    #elif defined(_MSC_VER)
        #define NS_INLINE static __inline
    #endif
#endif

Issue

So for the following code snippet in this repo, if we include and use them in diff compile units(eg. different .m files) the block in dispatch_once will be executed once for each compile units.

eg.

Example project

CDemoKit.zip

Solution

  1. Use __inline__ __attribute__((always_inline)) directly instead of using NS_INLINE
  2. To be discussed ...

See GitHub issue here static function + dispatch_once in header file will cause the once logic executable more than once · Issue #4904 · swiftlang/swift-corelibs-foundation · GitHub

1 Like

I think the best solution is simply to not inline these at all but instead be regular functions.

I’m working on a patch in this area anyway - we can take care of it then, unless this it really urgent.

1 Like

I’m working on a patch in this area anyway - we can take care of it then, unless this it really urgent.

Hey @Tony_Parker.

Just wanted to check in and see if there’s been any update or movement on this issue. No rush at all — just curious since I’ve been digging into some related low-level behavior recently.

By the way, I’ve also found a similar header include issue(included by multi compile unit) in dyld (filed as FB20820097. See detail in this article).

Appreciate all the work you’ve been doing on corelibs Foundation!

Hi there @Kyle-Ye – the time zone, locale, and calendar code was removed in commit 3b4b7b4658 when we ditched the C implementations of these APIs and moved them to using the Swift ones that are part of swift-foundation.

Are there other inline functions that have the same issue?

That’s great! I’ve updated the issue status to close it.

No further issues on the corelibs side from my end.