Skip to content

Commit 9ad9a33

Browse files
committed
feat: Add shim to allow wrapped access to CLOCK_REALTIME struct from WASILibc.
1 parent c8bf8bb commit 9ad9a33

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

Package.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ let package = Package(
3535
dependencies: [
3636
.product(name: "ServiceContextModule", package: "swift-service-context"),
3737
.target(name: "Instrumentation"),
38+
.target(name: "_CWASI", condition: .when(platforms: [.wasi])),
3839
]
3940
),
4041
.testTarget(
@@ -43,6 +44,16 @@ let package = Package(
4344
.target(name: "Tracing")
4445
]
4546
),
47+
48+
// ==== --------------------------------------------------------------------------------------------------------
49+
// MARK: Wasm Support
50+
51+
// Provides C shims for compiling to wasm
52+
.target(
53+
name: "_CWASI",
54+
dependencies: []
55+
),
56+
4657
]
4758
)
4859

Sources/Tracing/TracingTime.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ public struct DefaultTracerClock {
9090

9191
public var now: Self.Instant {
9292
var ts = timespec()
93-
clock_gettime(CLOCK_REALTIME, &ts)
93+
#if os(WASI)
94+
CWASI_clock_gettime_realtime(&ts)
95+
#else
96+
clock_gettime(CLOCK_REALTIME, &ts)
97+
#endif
9498
/// We use unsafe arithmetic here because `UInt64.max` nanoseconds is more than 580 years,
9599
/// and the odds that this code will still be running 530 years from now is very, very low,
96100
/// so as a practical matter this will never overflow.

Sources/_CWASI/_CWASI.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Distributed Tracing open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift Distributed Tracing project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift Distributed Tracing project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//

Sources/_CWASI/include/_CWASI.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Distributed Tracing open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift Distributed Tracing project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift Distributed Tracing project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#pragma once
16+
17+
#if __wasi__
18+
19+
#include <fcntl.h>
20+
#include <time.h>
21+
22+
static inline void CWASI_clock_gettime_realtime(struct timespec *tv) {
23+
// ClangImporter doesn't support `CLOCK_REALTIME` declaration in WASILibc, thus we have to define a bridge manually
24+
clock_gettime(CLOCK_REALTIME, tv);
25+
}
26+
27+
#endif // __wasi__

0 commit comments

Comments
 (0)