Background
- static variables in the static function are shared by the same compile Unit, but not by different compile Units.
- 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
Solution
- Use
__inline__ __attribute__((always_inline))
directly instead of usingNS_INLINE
- 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