]> gitweb.factorcode.org Git - factor.git/blob - basis/concurrency/locks/locks-docs.factor
factor: trim using lists
[factor.git] / basis / concurrency / locks / locks-docs.factor
1 USING: calendar help.markup help.syntax quotations ;
2 IN: concurrency.locks
3
4 HELP: lock
5 { $class-description "The class of mutual exclusion locks." } ;
6
7 HELP: <lock>
8 { $values { "lock" lock } }
9 { $description "Creates a non-reentrant lock." } ;
10
11 HELP: <reentrant-lock>
12 { $values { "lock" lock } }
13 { $description "Creates a reentrant lock." } ;
14
15 HELP: with-lock-timeout
16 { $values { "lock" lock } { "timeout" { $maybe duration } } { "quot" quotation } }
17 { $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." }
18 { $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." } ;
19
20 HELP: with-lock
21 { $values { "lock" lock } { "quot" quotation } }
22 { $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." } ;
23
24 ARTICLE: "concurrency.locks.mutex" "Mutual-exclusion locks"
25 "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."
26 $nl
27 "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."
28 { $subsections
29     lock
30     <lock>
31     <reentrant-lock>
32     with-lock
33     with-lock-timeout
34 } ;
35
36 HELP: rw-lock
37 { $class-description "The class of reader/writer locks." } ;
38
39 HELP: with-read-lock-timeout
40 { $values { "lock" lock } { "timeout" { $maybe duration } } { "quot" quotation } }
41 { $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." }
42 { $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." } ;
43
44 HELP: with-read-lock
45 { $values { "lock" lock } { "quot" quotation } }
46 { $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." } ;
47
48 HELP: with-write-lock-timeout
49 { $values { "lock" lock } { "timeout" { $maybe duration } } { "quot" quotation } }
50 { $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." }
51 { $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." } ;
52
53 HELP: with-write-lock
54 { $values { "lock" lock } { "quot" quotation } }
55 { $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." } ;
56
57 ARTICLE: "concurrency.locks.rw" "Read-write locks"
58 "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."
59 $nl
60 "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."
61 $nl
62 "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."
63 $nl
64 "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."
65 { $subsections
66     rw-lock
67     <rw-lock>
68     with-read-lock
69     with-write-lock
70 }
71 "Versions of the above that take a timeout duration:"
72 { $subsections
73     with-read-lock-timeout
74     with-write-lock-timeout
75 } ;
76
77 ARTICLE: "concurrency.locks" "Locks"
78 "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:"
79 { $subsections
80     "concurrency.locks.mutex"
81     "concurrency.locks.rw"
82 } ;
83
84 ABOUT: "concurrency.locks"