-
Notifications
You must be signed in to change notification settings - Fork 441
SyntaxArena: thread safety #807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,60 @@ | |
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if canImport(Darwin) | ||
import Darwin | ||
|
||
class ScopeGuard { | ||
private var lock: os_unfair_lock | ||
init() { | ||
self.lock = os_unfair_lock() | ||
} | ||
func withGuard<T>(body: () throws -> T) rethrows -> T { | ||
os_unfair_lock_lock(&lock) | ||
defer { os_unfair_lock_unlock(&lock)} | ||
return try body() | ||
} | ||
} | ||
|
||
#elseif canImport(Glibc) | ||
import Glibc | ||
|
||
class ScopeGuard { | ||
var lock: pthread_mutex_t | ||
init() { | ||
self.lock = pthread_mutex_t() | ||
pthread_mutex_init(&self.lock, nil) | ||
} | ||
deinit { | ||
pthread_mutex_destroy(&self.lock) | ||
} | ||
func withGuard<T>(body: () throws -> T) rethrows -> T { | ||
pthread_mutex_lock(&lock) | ||
defer { pthread_mutex_unlock(&lock) } | ||
return try body() | ||
} | ||
} | ||
#else | ||
// FIXME: Support other platforms. | ||
|
||
/// Dummy mutex that doesn't actually guard at all. | ||
class ScopeGuard { | ||
init() {} | ||
func withGuard<T>(body: () throws -> T) rethrows -> T { | ||
return try body() | ||
} | ||
} | ||
#endif | ||
|
||
public class SyntaxArena { | ||
|
||
@_spi(RawSyntax) | ||
public typealias ParseTriviaFunction = (_ source: SyntaxText, _ position: TriviaPosition) -> [RawTriviaPiece] | ||
|
||
/// Thread safe guard. | ||
private let lock: ScopeGuard | ||
private var singleThreadMode: Bool | ||
|
||
/// Bump-pointer allocator for all "intern" methods. | ||
private let allocator: BumpPtrAllocator | ||
/// Source file buffer the Syntax tree represents. | ||
|
@@ -30,6 +79,8 @@ public class SyntaxArena { | |
|
||
@_spi(RawSyntax) | ||
public init(parseTriviaFunction: @escaping ParseTriviaFunction) { | ||
lock = ScopeGuard() | ||
self.singleThreadMode = false | ||
allocator = BumpPtrAllocator() | ||
children = [] | ||
sourceBuffer = .init(start: nil, count: 0) | ||
|
@@ -41,15 +92,32 @@ public class SyntaxArena { | |
self.init(parseTriviaFunction: _defaultParseTriviaFunction(_:_:)) | ||
} | ||
|
||
private func withGuard<R>(body: () throws -> R) rethrows -> R { | ||
if self.singleThreadMode { | ||
return try body() | ||
} else { | ||
return try self.lock.withGuard(body: body) | ||
} | ||
} | ||
|
||
public func assumingSingleThread<R>(body: () throws -> R) rethrows -> R { | ||
let oldValue = self.singleThreadMode | ||
defer { self.singleThreadMode = oldValue } | ||
self.singleThreadMode = true | ||
return try body() | ||
} | ||
|
||
/// Copies a source buffer in to the memory this arena manages, and returns | ||
/// the interned buffer. | ||
/// | ||
/// The interned buffer is guaranteed to be null-terminated. | ||
/// `contains(address _:)` is faster if the address is inside the memory | ||
/// range this function returned. | ||
public func internSourceBuffer(_ buffer: UnsafeBufferPointer<UInt8>) -> UnsafeBufferPointer<UInt8> { | ||
let allocated = lock.withGuard { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be |
||
allocator.allocate(UInt8.self, count: buffer.count + /* for NULL */1) | ||
} | ||
precondition(sourceBuffer.baseAddress == nil, "SourceBuffer should only be set once.") | ||
let allocated = allocator.allocate(UInt8.self, count: buffer.count + /* for NULL */1) | ||
_ = allocated.initialize(from: buffer) | ||
|
||
// NULL terminate. | ||
|
@@ -69,20 +137,27 @@ public class SyntaxArena { | |
/// Allocates a buffer of `RawSyntax?` with the given count, then returns the | ||
/// uninitlialized memory range as a `UnsafeMutableBufferPointer<RawSyntax?>`. | ||
func allocateRawSyntaxBuffer(count: Int) -> UnsafeMutableBufferPointer<RawSyntax?> { | ||
return allocator.allocate(RawSyntax?.self, count: count) | ||
return self.withGuard { | ||
allocator.allocate(RawSyntax?.self, count: count) | ||
} | ||
} | ||
|
||
/// Allcates a buffer of `RawTriviaPiece` with the given count, then returns | ||
/// the uninitialized memory range as a `UnsafeMutableBufferPointer<RawTriviaPiece>`. | ||
func allocateRawTriviaPieceBuffer( | ||
count: Int) -> UnsafeMutableBufferPointer<RawTriviaPiece> { | ||
return allocator.allocate(RawTriviaPiece.self, count: count) | ||
count: Int | ||
) -> UnsafeMutableBufferPointer<RawTriviaPiece> { | ||
return self.withGuard { | ||
allocator.allocate(RawTriviaPiece.self, count: count) | ||
} | ||
} | ||
|
||
/// Allcates a buffer of `UInt8` with the given count, then returns the | ||
/// uninitialized memory range as a `UnsafeMutableBufferPointer<UInt8>`. | ||
func allocateTextBuffer(count: Int) -> UnsafeMutableBufferPointer<UInt8> { | ||
return allocator.allocate(UInt8.self, count: count) | ||
return self.withGuard { | ||
allocator.allocate(UInt8.self, count: count) | ||
} | ||
} | ||
|
||
/// Copies the contents of a `SyntaxText` to the memory this arena manages, | ||
|
@@ -114,7 +189,9 @@ public class SyntaxArena { | |
/// Copies a `RawSyntaxData` to the memory this arena manages, and retuns the | ||
/// pointer to the destination. | ||
func intern(_ value: RawSyntaxData) -> UnsafePointer<RawSyntaxData> { | ||
let allocated = allocator.allocate(RawSyntaxData.self, count: 1).baseAddress! | ||
let allocated = lock.withGuard { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, should this be |
||
allocator.allocate(RawSyntaxData.self, count: 1).baseAddress! | ||
} | ||
allocated.initialize(to: value) | ||
return UnsafePointer(allocated) | ||
} | ||
|
@@ -128,21 +205,26 @@ public class SyntaxArena { | |
/// See also `RawSyntax.layout()`. | ||
func addChild(_ arenaRef: SyntaxArenaRef) { | ||
if SyntaxArenaRef(self) == arenaRef { return } | ||
|
||
let other = arenaRef.value | ||
|
||
precondition( | ||
!self.hasParent, | ||
"an arena can't have a new child once it's owned by other arenas") | ||
|
||
other.hasParent = true | ||
children.insert(other) | ||
other.withGuard { | ||
self.withGuard { | ||
precondition( | ||
!self.hasParent, | ||
"an arena can't have a new child once it's owned by other arenas") | ||
|
||
other.hasParent = true | ||
children.insert(other) | ||
} | ||
} | ||
} | ||
|
||
/// Recursively checks if this arena contains given `arena` as a descendant. | ||
func contains(arena: SyntaxArena) -> Bool { | ||
return children.contains { child in | ||
child === arena || child.contains(arena: arena) | ||
self.withGuard { | ||
children.contains { child in | ||
child === arena || child.contains(arena: arena) | ||
} | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.