-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing concurrency crash with translation of index based pagination
- Loading branch information
1 parent
03c0d96
commit 7f522a6
Showing
2 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#if os(Linux) | ||
import Glibc | ||
#else | ||
import Darwin.C | ||
#endif | ||
|
||
final class Lock { | ||
private var mutex = pthread_mutex_t() | ||
|
||
init() { | ||
let result = pthread_mutex_init(&mutex, nil) | ||
|
||
guard result == 0 else { | ||
fatalError("Failed to initialize Lock: \(result)") | ||
} | ||
} | ||
|
||
deinit { | ||
pthread_mutex_destroy(&mutex) | ||
} | ||
|
||
func withLock<T>(_ closure: () throws -> (T)) rethrows -> T { | ||
acquire() | ||
defer { release() } | ||
return try closure() | ||
} | ||
|
||
func acquire() { | ||
let result = pthread_mutex_lock(&mutex) | ||
|
||
guard result == 0 else { | ||
fatalError("Failed to aquire Lock: \(result)") | ||
} | ||
} | ||
|
||
func release() { | ||
pthread_mutex_unlock(&mutex) | ||
} | ||
} |