forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spinlock.mpl
51 lines (40 loc) · 1.25 KB
/
Spinlock.mpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright (C) Matway Burkow
#
# This repository and all its contents belong to Matway Burkow (referred here and below as "the owner").
# The content is for demonstration purposes only.
# It is forbidden to use the content or any part of it for any purpose without explicit permission from the owner.
# By contributing to the repository, contributors acknowledge that ownership of their work transfers to the owner.
"atomic.ACQUIRE" use
"atomic.RELEASE" use
"atomic.atomicExchange" use
"atomic.atomicStore" use
"control.assert" use
Spinlock: [{
SCHEMA_NAME: "Spinlock" virtual;
LOCKED: [1n8];
UNLOCKED: [0n8];
MAXIMUM_SPIN_COUNT: [2000000];
state: UNLOCKED;
DIE: [
[state UNLOCKED =] "Attempted to destroy a locked [Spinlock]" assert
];
INIT: [
UNLOCKED !state
];
lock: [
DEBUG [counter: 0;] [] uif
[
[counter MAXIMUM_SPIN_COUNT = ~] "The maximum [Spinlock] spin count has been reached" assert
LOCKED @state ACQUIRE atomicExchange LOCKED = [
DEBUG [counter 1 + !counter] [] if
TRUE
] [
FALSE
] if
] loop
];
unlock: [
[state LOCKED =] "[unlock] was called in an invalid [Spinlock] state" assert
UNLOCKED @state RELEASE atomicStore
];
}];