-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d2d84e
commit 13a6f5c
Showing
11 changed files
with
131 additions
and
67 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
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,37 @@ | ||
# Advisory reentrant lock | ||
type ReentrantLock | ||
locked_by::Nullable{Task} | ||
cond_wait::Condition | ||
reentrancy_cnt::Int | ||
|
||
ReentrantLock() = new(nothing, Condition(), 0) | ||
end | ||
|
||
function lock(rl::ReentrantLock) | ||
t = current_task() | ||
while true | ||
if rl.reentrancy_cnt == 0 | ||
rl.locked_by = t | ||
rl.reentrancy_cnt = 1 | ||
return | ||
elseif t == get(rl.locked_by) | ||
rl.reentrancy_cnt += 1 | ||
return | ||
end | ||
wait(rl.cond_wait) | ||
end | ||
end | ||
|
||
unlock(o::Any) = unlock(o.lock) | ||
|
||
function unlock(rl::ReentrantLock) | ||
rl.reentrancy_cnt = rl.reentrancy_cnt - 1 | ||
if rl.reentrancy_cnt == 0 | ||
rl.locked_by = nothing | ||
notify(rl.cond_wait) | ||
elseif rl.reentrancy_cnt < 0 | ||
AssertionError("unlock count must match lock count") | ||
end | ||
rl | ||
end | ||
|
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
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
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
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