Skip to content

Commit f270db9

Browse files
committed
C++ mutex
1 parent cd9009f commit f270db9

File tree

10 files changed

+125
-137
lines changed

10 files changed

+125
-137
lines changed

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ add_swift_syntax_library(SwiftSyntax
1818
Identifier.swift
1919
MemoryLayout.swift
2020
MissingNodeInitializers.swift
21-
PlatformMutex.swift
2221
SourceEdit.swift
2322
SourceLength.swift
2423
SourceLocation.swift

Sources/SwiftSyntax/PlatformMutex.swift

Lines changed: 0 additions & 125 deletions
This file was deleted.

Sources/SwiftSyntax/Syntax.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if compiler(>=6)
14+
@_implementationOnly private import _SwiftSyntaxCShims
15+
#else
16+
@_implementationOnly import _SwiftSyntaxCShims
17+
#endif
18+
1319
// `Syntax` is a user facing "red" tree. A value is a pair of strong reference to
1420
// the _arena_ and a pointer to the `SyntaxData` allocated in the arena.
1521
// The _arena_ is shared between the all node in the tree and only in the tree.
@@ -358,12 +364,16 @@ final class SyntaxDataArena: @unchecked Sendable {
358364
precondition(rawNodeArena == raw.arenaReference)
359365
assert(MemoryLayout<SyntaxData>.alignment >= MemoryLayout<UnsafePointer<SyntaxData>?>.alignment)
360366

361-
self.mutex = PlatformMutex()
367+
self.mutex = PlatformMutex.create()
362368
self.allocator = BumpPtrAllocator(initialSlabSize: 4096)
363369
self.rawArena = rawNodeArena
364370
self.root = Self.createDataImpl(allocator: allocator, parent: nil, raw: raw, absoluteInfo: .forRoot(raw))
365371
}
366372

373+
deinit {
374+
self.mutex.destroy()
375+
}
376+
367377
/// Return the childen data of the given node.
368378
///
369379
/// The layout buffer is created and cached when it's first accssed.
@@ -395,15 +405,16 @@ final class SyntaxDataArena: @unchecked Sendable {
395405
return SyntaxDataReferenceBuffer(buffer)
396406
}
397407

398-
return mutex.withGuard {
399-
// Recheck before populating, maybe some other thread has populated the buffer
400-
// during acquiring the lock.
401-
if !isPopulated() {
402-
populateDataLayoutImpl(parent)
403-
}
408+
mutex.lock()
409+
defer { mutex.unlock() }
404410

405-
return SyntaxDataReferenceBuffer(buffer)
411+
// Recheck before populating, maybe some other thread has populated the buffer
412+
// during acquiring the lock.
413+
if !isPopulated() {
414+
populateDataLayoutImpl(parent)
406415
}
416+
417+
return SyntaxDataReferenceBuffer(buffer)
407418
}
408419

409420
/// Fill the layout buffer of the node.

Sources/SwiftSyntax/SyntaxProtocol.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ extension SyntaxProtocol {
156156
/// Return this subtree with this node as the root, ie. detach this node
157157
/// from its parent.
158158
public var detached: Self {
159+
if !self.hasParent {
160+
return self
161+
}
159162
// Make sure `self` (and thus the arena of `self.raw`) can’t get deallocated
160163
// before the detached node can be created.
161164
return withExtendedLifetime(self) {
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
set(target ${SWIFTSYNTAX_TARGET_NAMESPACE}_SwiftSyntaxCShims)
2-
add_library(${target} INTERFACE)
3-
target_include_directories(${target} INTERFACE "include")
2+
add_library(${target} STATIC
3+
PlatformMutex.cpp
4+
)
5+
target_include_directories(${target} PUBLIC "include")
46
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${target})
57
install(TARGETS ${target} EXPORT SwiftSyntaxTargets)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "PlatformMutex.h"
14+
15+
#ifdef __APPLE__
16+
#include <os/lock.h>
17+
PlatformMutex swiftsyntax_platform_mutex_create() {
18+
return {new os_unfair_lock(OS_UNFAIR_LOCK_INIT)};
19+
}
20+
21+
__attribute__((swift_name("PlatformMutex.lock(self:)")))
22+
void swiftsyntax_platform_mutex_lock(PlatformMutex m) {
23+
os_unfair_lock_lock(static_cast<os_unfair_lock_t>(m.opaque));
24+
}
25+
26+
__attribute__((swift_name("PlatformMutex.unlock(self:)")))
27+
void swiftsyntax_platform_mutex_unlock(PlatformMutex m) {
28+
os_unfair_lock_unlock(static_cast<os_unfair_lock_t>(m.opaque));
29+
}
30+
31+
__attribute__((swift_name("PlatformMutex.destroy(self:)")))
32+
void swiftsyntax_platform_mutex_destroy(PlatformMutex m) {
33+
delete static_cast<os_unfair_lock_t>(m.opaque);
34+
}
35+
36+
#else
37+
#include <mutex>
38+
39+
PlatformMutex swiftsyntax_platform_mutex_create() {
40+
return {new std::mutex()};
41+
}
42+
43+
__attribute__((swift_name("PlatformMutex.lock(self:)")))
44+
void swiftsyntax_platform_mutex_lock(PlatformMutex m) {
45+
return static_cast<std::mutex *>(m.opaque)->lock();
46+
}
47+
48+
__attribute__((swift_name("PlatformMutex.unlock(self:)")))
49+
void swiftsyntax_platform_mutex_unlock(PlatformMutex m) {
50+
return static_cast<std::mutex *>(m.opaque)->unlock();
51+
}
52+
53+
__attribute__((swift_name("PlatformMutex.destroy(self:)")))
54+
void swiftsyntax_platform_mutex_destroy(PlatformMutex m) {
55+
delete static_cast<std::mutex *>(m.opaque);
56+
}
57+
#endif

Sources/_SwiftSyntaxCShims/dummy.c

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFTSYNTAX_PLATFORMMUTEX_H
14+
#define SWIFTSYNTAX_PLATFORMMUTEX_H
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
typedef struct PlatformMutex {
21+
void *opaque;
22+
} PlatformMutex;
23+
24+
__attribute__((swift_name("PlatformMutex.create()")))
25+
PlatformMutex swiftsyntax_platform_mutex_create(void);
26+
27+
__attribute__((swift_name("PlatformMutex.lock(self:)")))
28+
void swiftsyntax_platform_mutex_lock(PlatformMutex m);
29+
30+
__attribute__((swift_name("PlatformMutex.unlock(self:)")))
31+
void swiftsyntax_platform_mutex_unlock(PlatformMutex m);
32+
33+
__attribute__((swift_name("PlatformMutex.destroy(self:)")))
34+
void swiftsyntax_platform_mutex_destroy(PlatformMutex m);
35+
36+
#ifdef __cplusplus
37+
} // extern "C"
38+
#endif
39+
40+
#endif // SWIFTSYNTAX_PLATFORMMUTEX_H

Sources/_SwiftSyntaxCShims/include/SwiftSyntaxCShims.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212

1313
#include "_includes.h"
1414
#include "AtomicBool.h"
15+
#include "PlatformMutex.h"
1516
#include "swiftsyntax_errno.h"
1617
#include "swiftsyntax_stdio.h"

Sources/_SwiftSyntaxCShims/include/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module _SwiftSyntaxCShims {
22
header "_includes.h"
33
header "AtomicBool.h"
4+
header "PlatformMutex.h"
45
header "swiftsyntax_errno.h"
56
header "swiftsyntax_stdio.h"
67
export *

0 commit comments

Comments
 (0)