]> gitweb.factorcode.org Git - factor.git/blob - basis/concurrency/locks/locks-docs.factor
77bed82f76e224786dc1a54d94eb573b4126bcbc
[factor.git] / basis / concurrency / locks / locks-docs.factor
1 USING: help.markup help.syntax sequences kernel quotations
2 calendar ;
3 IN: concurrency.locks
4
5 HELP: lock
6 { $class-description "The class of mutual exclusion locks." } ;
7
8 HELP: <lock>
9 { $values { "lock" lock } }
10 { $description "Creates a non-reentrant lock." } ;
11
12 HELP: <reentrant-lock>
13 { $values { "lock" lock } }
14 { $description "Creates a reentrant lock." } ;
15
16 HELP: with-lock-timeout
17 { $values { "lock" lock } { "timeout" { $maybe duration } } { "quot" quotation } }
18 { $description "Calls the quotation, ensuring that only one thread executes with the lock held at a time. If another thread is holding the lock, blocks until the thread releases the lock." }
19 { $errors "Throws an error if the lock could not be acquired before the timeout expires. A timeout value of " { $link f } " means the thread is willing to wait indefinitely." } ;
20
21 HELP: with-lock
22 { $values { "lock" lock } { "quot" quotation } }
23 { $description "Calls the quotation, ensuring that only one thread executes with the lock held at a time. If another thread is holding the lock, blocks until the thread releases the lock." } ;
24
25 ARTICLE: "concurrency.locks.mutex" "Mutual-exclusion locks"
26 "A mutual-exclusion lock ensures that only one thread executes with the lock held at a time. They are used to protect critical sections so that certain operations appear to be atomic to other threads."
27 $nl
28 "There are two varieties of locks: non-reentrant and reentrant. The latter may be acquired recursively by the same thread. Attempting to do so with the former will deadlock."
29 { $subsections
30     lock
31     <lock>
32     <reentrant-lock>
33     with-lock
34     with-lock-timeout
35 } ;
36
37 HELP: rw-lock
38 { $class-description "The class of reader/writer locks." } ;
39
40 HELP: with-read-lock-timeout
41 { $values { "lock" lock } { "timeout" { $maybe duration } } { "quot" quotation } }
42 { $description "Calls the quotation, ensuring that no other thread is holding a write lock at the same time. If another thread is holding a write lock, blocks until the thread releases the lock." }
43 { $errors "Throws an error if the lock could not be acquired before the timeout expires. A timeout value of " { $link f } " means the thread is willing to wait indefinitely." } ;
44
45 HELP: with-read-lock
46 { $values { "lock" lock } { "quot" quotation } }
47 { $description "Calls the quotation, ensuring that no other thread is holding a write lock at the same time. If another thread is holding a write lock, blocks until the thread releases the lock." } ;
48
49 HELP: with-write-lock-timeout
50 { $values { "lock" lock } { "timeout" { $maybe duration } } { "quot" quotation } }
51 { $description "Calls the quotation, ensuring that no other thread is holding a read or write lock at the same time. If another thread is holding a read or write lock, blocks until the thread releases the lock." }
52 { $errors "Throws an error if the lock could not be acquired before the timeout expires. A timeout value of " { $link f } " means the thread is willing to wait indefinitely." } ;
53
54 HELP: with-write-lock
55 { $values { "lock" lock } { "quot" quotation } }
56 { $description "Calls the quotation, ensuring that no other thread is holding a read or write lock at the same time. If another thread is holding a read or write lock, blocks until the thread releases the lock." } ;
57
58 ARTICLE: "concurrency.locks.rw" "Read-write locks"
59 "A read-write lock encapsulates a common pattern in the implementation of concurrent data structures, where one wishes to ensure that a thread is able to see a consistent view of the structure for a period of time, during which no other thread modifies the structure."
60 $nl
61 "While this can be achieved with a simple " { $link "concurrency.locks.mutex" } ", performance will suffer, since in fact multiple threads can view the structure at the same time; serialization must only be enforced for writes."
62 $nl
63 "Read/write locks allow any number of threads to hold the read lock simultaneously, however attempting to acquire a write lock blocks until all other threads release read locks and write locks."
64 $nl
65 "Read/write locks are reentrant. A thread holding a write lock may acquire a read lock or a write lock without blocking. However a thread holding a read lock may not acquire a write lock recursively since that could break invariants assumed by the code executing with the read lock held."
66 { $subsections
67     rw-lock
68     <rw-lock>
69     with-read-lock
70     with-write-lock
71 }
72 "Versions of the above that take a timeout duration:"
73 { $subsections
74     with-read-lock-timeout
75     with-write-lock-timeout
76 } ;
77
78 ARTICLE: "concurrency.locks" "Locks"
79 "A " { $emphasis "lock" } " is an object protecting a critical region of code, enforcing a particular mutual-exclusion policy. The " { $vocab-link "concurrency.locks" } " vocabulary implements two types of locks:"
80 { $subsections
81     "concurrency.locks.mutex"
82     "concurrency.locks.rw"
83 } ;
84
85 ABOUT: "concurrency.locks"