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 · apple/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