]> gitweb.factorcode.org Git - factor.git/blob - basis/threads/threads-docs.factor
minor cleanup to some docs.
[factor.git] / basis / threads / threads-docs.factor
1 USING: help.markup help.syntax kernel kernel.private io
2 threads.private init quotations strings assocs heaps boxes
3 namespaces deques dlists system ;
4 IN: threads
5
6 ARTICLE: "threads-start/stop" "Starting and stopping threads"
7 "Spawning new threads:"
8 { $subsections
9     spawn
10     spawn-server
11 }
12 "Creating and spawning a thread can be factored out into two separate steps:"
13 { $subsections
14     <thread>
15     (spawn)
16 }
17 "Threads stop either when the quotation given to " { $link spawn } " returns, or when the following word is called:"
18 { $subsections stop }
19 "If the image is saved and started again, all runnable threads are stopped. Vocabularies wishing to have a background thread always running should use " { $link add-startup-hook } "." ;
20
21 ARTICLE: "threads-yield" "Yielding and suspending threads"
22 "Yielding to other threads:"
23 { $subsections yield }
24 "Sleeping for a period of time:"
25 { $subsections sleep }
26 "Interrupting sleep:"
27 { $subsections interrupt }
28 "Threads can be suspended and woken up at some point in the future when a condition is satisfied:"
29 { $subsections
30     suspend
31     resume
32     resume-with
33 } ;
34
35 ARTICLE: "thread-state" "Thread-local state and variables"
36 "Threads form a class of objects:"
37 { $subsections thread }
38 "The current thread:"
39 { $subsections self }
40 "Thread-local variables:"
41 { $subsections
42     tnamespace
43     tget
44     tset
45     tchange
46 }
47 "Each thread has its own independent set of thread-local variables and newly-spawned threads begin with an empty set."
48 $nl
49 "Global hashtable of all threads, keyed by " { $snippet "id" } ":"
50 { $subsections threads }
51 "Threads have an identity independent of continuations. If a continuation is reified in one thread and then reflected in another thread, the code running in that continuation will observe a change in the value output by " { $link self } "." ;
52
53 ARTICLE: "thread-impl" "Thread implementation"
54 "Thread implementation:"
55 { $subsections
56     run-queue
57     sleep-queue
58 } ;
59
60 ARTICLE: "threads" "Co-operative threads"
61 "Factor supports co-operative threads. A thread will yield while waiting for input/output operations to complete, or when a yield has been explicitly requested."
62 $nl
63 "Words for working with threads are in the " { $vocab-link "threads" } " vocabulary."
64 { $subsections
65     "threads-start/stop"
66     "threads-yield"
67     "thread-state"
68     "thread-impl"
69 } ;
70
71 ABOUT: "threads"
72
73 HELP: thread
74 { $class-description "A thread. The slots are as follows:"
75     { $list
76         { { $snippet "id" } " - a unique identifier assigned to each thread." }
77         { { $snippet "name" } " - the name passed to " { $link spawn } "." }
78         { { $snippet "quot" } " - the initial quotation passed to " { $link spawn } "." }
79         { { $snippet "status" } " - a " { $link string } " indicating what the thread is waiting for, or " { $link f } ". This slot is intended to be used for debugging purposes." }
80     }
81 } ;
82
83 HELP: self
84 { $values { "thread" thread } }
85 { $description "Pushes the currently-running thread." } ;
86
87 HELP: <thread>
88 { $values { "quot" quotation } { "name" string } { "thread" thread } }
89 { $description "Low-level thread constructor. The thread runs the quotation when spawned."
90 $nl
91 "The name is used to identify the thread for debugging purposes; see " { $link "tools.threads" } "." }
92 { $notes "In most cases, user code should call " { $link spawn } " instead, however for control over the error handler quotation, threads can be created with " { $link <thread> } " then passed to " { $link (spawn) } "." } ;
93
94 HELP: run-queue
95 { $values { "dlist" dlist } }
96 { $var-description "Global variable holding the queue of runnable threads. Calls to " { $link yield } " switch to the thread which has been in the queue for the longest period of time."
97 $nl
98 "By convention, threads are queued with " { $link push-front }
99 " and dequed with " { $link pop-back } "." } ;
100
101 HELP: resume
102 { $values { "thread" thread } }
103 { $description "Adds a thread to the end of the run queue. The thread must have previously been suspended by a call to " { $link suspend } "." } ;
104
105 HELP: resume-with
106 { $values { "obj" object } { "thread" thread } }
107 { $description "Adds a thread to the end of the run queue together with an object to pass to the thread. The thread must have previously been suspended by a call to " { $link suspend } "; the object is returned from the " { $link suspend } " call." } ;
108
109 HELP: sleep-queue
110 { $values { "heap" min-heap } }
111 { $var-description "A " { $link min-heap } " storing the queue of sleeping threads." } ;
112
113 HELP: sleep-time
114 { $values { "nanos/f" { $maybe "a non-negative integer" } } }
115 { $description "Returns the time until the next sleeping thread is scheduled to wake up, which could be zero if there are threads in the run queue, or threads which need to wake up right now. If there are no runnable or sleeping threads, returns " { $link f } "." } ;
116
117 HELP: stop
118 { $description "Stops the current thread. The thread may be started again from another thread using " { $link (spawn) } "." } ;
119
120 HELP: yield
121 { $description "Adds the current thread to the end of the run queue, and switches to the next runnable thread." } ;
122
123 HELP: sleep-until
124 { $values { "n/f" { $maybe "a non-negative integer" } } }
125 { $description "Suspends the current thread until the given nanosecond count, returned by " { $link nano-count } ", is reached, or indefinitely if a value of " { $link f } " is passed in."
126 $nl
127 "Other threads may interrupt the sleep by calling " { $link interrupt } "." } ;
128
129 HELP: sleep
130 { $values { "dt" "a duration" } }
131 { $description "Suspends the current thread for the given duration."
132 $nl
133 "Other threads may interrupt the sleep by calling " { $link interrupt } "." }
134 { $examples
135     { $code "USING: threads calendar ;" "10 seconds sleep" }
136 } ;
137
138 HELP: interrupt
139 { $values { "thread" thread } }
140 { $description "Interrupts a sleeping thread." } ;
141
142 HELP: suspend
143 { $values { "state" string } { "obj" object } }
144 { $description "Suspends the current thread. Control yields to the next runnable thread and the current thread does not execute again until it is resumed, and so the caller of this word must arrange for another thread to later resume the suspended thread with a call to " { $link resume } " or " { $link resume-with } "."
145 $nl
146 "The status string is for debugging purposes; see " { $link "tools.threads" } "." } ;
147
148 HELP: spawn
149 { $values { "quot" quotation } { "name" string } { "thread" thread } }
150 { $description "Spawns a new thread. The thread begins executing the given quotation; the name is for debugging purposes. The new thread begins running immediately and the current thread is added to the end of the run queue."
151 $nl
152 "The new thread begins with an empty data stack, an empty retain stack, and an empty catch stack. The name stack is inherited from the parent thread but may be cleared with " { $link init-namespaces } "." }
153 { $notes
154      "The recommended way to pass data to the new thread is to explicitly construct a quotation containing the data, for example using " { $link curry } " or " { $link compose } "."
155 }
156 { $examples
157     "A simple thread that adds two numbers:"
158     { $code "1 2 [ + . ] 2curry \"Addition thread\" spawn" }
159     "A thread that counts to 10:"
160     { $code
161       "USING: math.parser threads ;"
162       "[ 10 iota [ number>string write nl yield ] each ] \"test\" spawn"
163       "10 [ yield ] times"
164       "0"
165       "1"
166       "2"
167       "3"
168       "4"
169       "5"
170       "6"
171       "7"
172       "8"
173       "9"
174     }
175 } ;
176
177 HELP: spawn-server
178 { $values { "quot" { $quotation ( -- ? ) } } { "name" string } { "thread" thread } }
179 { $description "Convenience wrapper around " { $link spawn } " which repeatedly calls the quotation in a new thread until it outputs " { $link f } "." }
180 { $examples
181     "A thread that runs forever:"
182     { $code "[ do-foo-bar t ] \"Foo bar server\" spawn-server" }
183 } ;
184
185 HELP: init-threads
186 { $description "Called during startup to initialize the threading system. This word should never be called directly." } ;
187
188 HELP: tnamespace
189 { $values { "assoc" assoc } }
190 { $description "Outputs the current thread's set of thread-local variables." } ;
191
192 HELP: tget
193 { $values { "key" object } { "value" object } }
194 { $description "Outputs the value of a thread-local variable." } ;
195
196 HELP: tset
197 { $values { "value" object } { "key" object } }
198 { $description "Sets the value of a thread-local variable." } ;
199
200 HELP: tchange
201 { $values { "key" object } { "quot" { $quotation ( ..a value -- ..b newvalue ) } } }
202 { $description "Applies the quotation to the current value of a thread-local variable, storing the result back to the same variable." } ;