# Potential issue for NS\_INLINE+ dispatch\_once on swift-corelibs-foundation

**URL:** <https://forums.swift.org/t/potential-issue-for-ns-inline-dispatch-once-on-swift-corelibs-foundation/70439>\
**Category:** Core Libraries\
**Created:** [March 5, 2024, 1:13pm UTC](https://forums.swift.org/t/potential-issue-for-ns-inline-dispatch-once-on-swift-corelibs-foundation/70439 "2024-03-05T13:13:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [March 5, 2024, 1:13pm UTC](https://forums.swift.org/t/potential-issue-for-ns-inline-dispatch-once-on-swift-corelibs-foundation/70439/1 "2024-03-05T13:13:13Z")

</div>

## 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

```c
#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.

> <https://github.com/swiftlang/swift-corelibs-foundation/blob/778dde90ff7cf63f05634c3df6b0788c89249770/Darwin/shims/NSCalendarShims.h#L17-L26>

> <https://github.com/swiftlang/swift-corelibs-foundation/blob/778dde90ff7cf63f05634c3df6b0788c89249770/Darwin/shims/NSLocaleShims.h#L17-L24>

> <https://github.com/swiftlang/swift-corelibs-foundation/blob/778dde90ff7cf63f05634c3df6b0788c89249770/Darwin/shims/NSTimeZoneShims.h#L25-L32>

## Example project

 ![image](https://global.discourse-cdn.com/swift/original/3X/5/3/5373f53ce4498238e4cde82f129f0b9f9640bf6e.jpeg)

[CDemoKit.zip](https://github.com/apple/swift-corelibs-foundation/files/14495176/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](https://github.com/apple/swift-corelibs-foundation/issues/4904)

---

<div class="post-metadata">

**Author:** ![Tony\_Parker](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tony_parker/32/273_2.png) [@Tony\_Parker](https://forums.swift.org/u/Tony_Parker)\
**Post date:** [March 5, 2024, 3:51pm UTC](https://forums.swift.org/t/potential-issue-for-ns-inline-dispatch-once-on-swift-corelibs-foundation/70439/2 "2024-03-05T15:51:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tony\_Parker](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tony_parker/32/273_2.png) [@Tony\_Parker](https://forums.swift.org/u/Tony_Parker)\
**Post date:** [March 5, 2024, 3:52pm UTC](https://forums.swift.org/t/potential-issue-for-ns-inline-dispatch-once-on-swift-corelibs-foundation/70439/3 "2024-03-05T15:52:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [October 25, 2025, 6:19pm UTC](https://forums.swift.org/t/potential-issue-for-ns-inline-dispatch-once-on-swift-corelibs-foundation/70439/4 "2025-10-25T18:19:18Z")

</div>

> 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](https://kyleye.top/posts/dyld-redundant-data-copy/)).

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

---

<div class="post-metadata">

**Author:** ![Tony\_Parker](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tony_parker/32/273_2.png) [@Tony\_Parker](https://forums.swift.org/u/Tony_Parker)\
**Post date:** [October 27, 2025, 3:21pm UTC](https://forums.swift.org/t/potential-issue-for-ns-inline-dispatch-once-on-swift-corelibs-foundation/70439/5 "2025-10-27T15:21:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [October 27, 2025, 4:51pm UTC](https://forums.swift.org/t/potential-issue-for-ns-inline-dispatch-once-on-swift-corelibs-foundation/70439/6 "2025-10-27T16:51:04Z")

</div>

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

No further issues on the corelibs side from my end.
