1 package frost.threads
2
3 ====================================================================================================
4 A lock (mutex) which allows multiple threads to coordinate access to a shared resource. Locks are
5 typically unnecessary in Frost, as only immutable data can be shared between threads without
6 resorting to unsafe code.
7 ====================================================================================================
8 class Lock : Immutable {
9 @private
10 def nativeLock:Int64
11
12 ================================================================================================
13 Creates a new, unlocked `Lock`.
14 ================================================================================================
15 init() {
16 create()
17 }
18
19 ================================================================================================
20 Blocks the current thread until the lock is available, and then locks it. While the lock is
21 held, any other threads which attempt to lock the `Lock` will block until the current thread
22 releases the `Lock`.
23
24 It is legal for a thread to lock the same `Lock` multiple times; the same number of [unlock()]
25 calls will be necessary to completely release the `Lock`.
26
27 @see unlock()
28 ================================================================================================
29 @external(frostLockLock)
30 method lock()
31
32 ================================================================================================
33 Releases a held `lock`. Unlocking a `Lock` which has not actually been locked by the current
34 thread is undefined behavior.
35
36 @see lock()
37 ================================================================================================
38 @external(frostLockUnlock)
39 method unlock()
40
41 @override
42 method cleanup() {
43 destroy()
44 }
45
46 @private
47 @external(frostLockCreate)
48 method create()
49
50 @private
51 @external(frostLockDestroy)
52 method destroy()
53 }