Skip to content

Commit 89ce45a

Browse files
authored
Merge pull request swiftlang#140 from allevato/fix-atomic-counter
Import _CSwiftSyntax using @_implementationOnly.
2 parents 1c2feb7 + efd4dc8 commit 89ce45a

File tree

5 files changed

+15
-37
lines changed

5 files changed

+15
-37
lines changed

Diff for: Sources/SwiftSyntax/AtomicCounter.swift

+2-26
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
// This is using the C function from '_CSwiftSyntax/src/atomic-counter.c' by
14-
// dynamically loading it using `dlsym`. The reason this mechanism is used
15-
// instead of importing a header is so that we preserve the property that
16-
// SwiftSyntax is a self-contained Swift module.
17-
// (also see '_CSwiftSyntax/include/README.md')
18-
19-
#if os(Linux)
20-
import Glibc
21-
#else
22-
import Darwin.C
23-
#endif
24-
25-
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
26-
fileprivate let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
27-
#elseif os(Linux)
28-
fileprivate let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: 0)
29-
#endif
30-
31-
fileprivate typealias get_unique_counter_ty = (@convention(c) ()->UInt64)
32-
fileprivate let get_unique_counter_str = "cswiftsyntax_get_unique_counter"
13+
@_implementationOnly import _CSwiftSyntax
3314

3415
/// Provides API to get an atomically increasing counter value.
3516
struct AtomicCounter {
3617
/// Get an atomically increasing counter value.
3718
static func next() -> UInt64 {
38-
return AtomicCounter.cswiftsyntax_get_unique_counter()
19+
return cswiftsyntax_get_unique_counter()
3920
}
40-
41-
static fileprivate let cswiftsyntax_get_unique_counter: get_unique_counter_ty = { ()->get_unique_counter_ty in
42-
let ptr = dlsym(RTLD_DEFAULT, get_unique_counter_str)!
43-
return unsafeBitCast(ptr, to: get_unique_counter_ty.self)
44-
}()
4521
}

Diff for: Sources/_CSwiftSyntax/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This module is an implementation detail of SwiftSyntax and does not export
2+
any public API. As such, any uses of it inside SwiftSyntax should import it
3+
with the `@_implementationOnly` attribute (available since Swift 5.1).
4+
5+
This preserves the property that SwiftSyntax is a self-contained Swift
6+
module, meaning that when distributing it, we do not also have to bundle
7+
these private headers or use any dynamic symbol loading tricks that require
8+
additional linker flags to be passed on some platforms (see
9+
[SR-11293](https://bugs.swift.org/browse/SR-11293), for example).

Diff for: Sources/_CSwiftSyntax/include/README.md

-10
This file was deleted.

Diff for: Sources/_CSwiftSyntax/include/atomic-counter.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <stdint.h>
2+
3+
uint64_t cswiftsyntax_get_unique_counter(void);

Diff for: Sources/_CSwiftSyntax/src/atomic-counter.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <stdint.h>
1+
#include "atomic-counter.h"
22

33
uint64_t cswiftsyntax_get_unique_counter(void) {
44
static _Atomic uint64_t counter = 0;

0 commit comments

Comments
 (0)