Skip to content

Commit

Permalink
Add lock (#185)
Browse files Browse the repository at this point in the history
* Add a semaphore for serialize document update

* Open isApplyingChangePack variable

* Revert add isApplyingChagePack variable

* Add defer closure to signal semaphore.
  • Loading branch information
humdrum authored Jun 28, 2024
1 parent f8b9233 commit 023f1d7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
18 changes: 18 additions & 0 deletions Examples/Example.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
"version" : "1.1.0"
}
},
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247",
"version" : "1.3.0"
}
},
{
"identity" : "swift-docc-symbolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-symbolkit",
"state" : {
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"version" : "1.0.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
Expand Down
21 changes: 18 additions & 3 deletions Sources/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import Combine
import Foundation
import Semaphore

/**
* `DocumentOptions` are the options to create a new document.
Expand Down Expand Up @@ -99,6 +100,8 @@ public actor Document {
*/
private var presences: [ActorID: StringValueTypeDictionary]

private let workSemaphore = AsyncSemaphore(value: 1)

public init(key: String) {
self.init(key: key, opts: DocumentOptions(disableGC: false))
}
Expand All @@ -120,7 +123,13 @@ public actor Document {
/**
* `update` executes the given updater to update this document.
*/
public func update(_ updater: (_ root: JSONObject, _ presence: inout Presence) throws -> Void, _ message: String? = nil) throws {
public func update(_ updater: (_ root: JSONObject, _ presence: inout Presence) async throws -> Void, _ message: String? = nil) async throws {
await self.workSemaphore.wait()

defer {
self.workSemaphore.signal()
}

guard self.status != .removed else {
throw YorkieError.documentRemoved(message: "\(self) is removed.")
}
Expand All @@ -141,7 +150,7 @@ public actor Document {

var presence = Presence(changeContext: context, presence: self.clone?.presences[actorID] ?? [:])

try updater(proxy, &presence)
try await updater(proxy, &presence)

self.clone?.presences[actorID] = presence.presence

Expand Down Expand Up @@ -250,7 +259,13 @@ public actor Document {
*
* - Parameter pack: change pack
*/
func applyChangePack(_ pack: ChangePack) throws {
func applyChangePack(_ pack: ChangePack) async throws {
await self.workSemaphore.wait()

defer {
self.workSemaphore.signal()
}

if let snapshot = pack.getSnapshot() {
try self.applySnapshot(pack.getCheckpoint().getServerSeq(), snapshot)
} else if pack.hasChanges() {
Expand Down

0 comments on commit 023f1d7

Please sign in to comment.