Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cse550
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Winston Jodjana
cse550
Commits
206ff405
Commit
206ff405
authored
2 years ago
by
Mengqi Chen
Browse files
Options
Downloads
Patches
Plain Diff
add lockmanager
parent
d147632c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
assignment2/lockmanager.py
+51
-0
51 additions, 0 deletions
assignment2/lockmanager.py
with
51 additions
and
0 deletions
assignment2/lockmanager.py
0 → 100644
+
51
−
0
View file @
206ff405
from
assignment2.paxos
import
Address
from
typing
import
Dict
class
LockManager
:
def
__init__
(
self
)
->
None
:
self
.
locks
:
Dict
[
int
,
Address
]
=
dict
()
# returns true if lock is acquired or the same client tries to acquire
# a locks it already has, false otherwise
def
lock
(
self
,
value
:
int
,
client
:
Address
)
->
bool
:
# acquire lock
if
value
not
in
self
.
locks
:
self
.
locks
[
value
]
=
client
return
True
# client already acquired this lock before
if
self
.
locks
[
value
]
==
client
:
return
True
# lock is not available
return
False
# returns true if lock is unlocked, false otherwise
def
unlock
(
self
,
value
:
int
,
client
:
Address
)
->
bool
:
# lock does not exist
if
value
not
in
self
.
locks
:
return
False
# client is not the owner of the locok
if
self
.
locks
[
value
]
!=
client
:
return
False
# unlock
del
self
.
locks
[
value
]
return
True
# quick_test
#if __name__ == "__main__":
#lm = LockManager()
#ca = ("hello", 1234)
#cb = ("hello", 5678)
#assert lm.lock(1, ca) == True
#assert lm.lock(2, cb) == True
#assert lm.lock(1, ca) == True
#assert lm.lock(1, cb) == False
#assert lm.unlock(1, cb) == False
#assert lm.unlock(1, ca) == True
#assert lm.unlock(2, cb) == True
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment