]> gitweb.factorcode.org Git - factor.git/blob - basis/threads/threads-docs.factor
change add-init-hook to add-startup-hook, new add-shutdown-hook word
[factor.git] / basis / threads / threads-docs.factor
1 USING: help.markup help.syntax kernel kernel.private io
2 threads.private continuations init quotations strings
3 assocs heaps boxes namespaces deques dlists ;
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 refied in one thread and then resumed 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" "Lightweight co-operative threads"
61 "Factor supports lightweight co-operative threads implemented on top of " { $link "continuations" } ". A thread will yield while waiting for input/output operations to complete, or when a yield has been explicitly requested."
62 $nl
63 "Factor threads are very lightweight. Each thread can take as little as 900 bytes of memory. This library has been tested running hundreds of thousands of simple threads."
64 $nl
65 "Words for working with threads are in the " { $vocab-link "threads" } " vocabulary."
66 { $subsections
67     "threads-start/stop"
68     "threads-yield"
69     "thread-state"
70     "thread-impl"
71 } ;
72
73 ABOUT: "threads"
74
75 HELP: thread
76 { $class-description "A thread. The slots are as follows:"
77     { $list
78         { { $snippet "id" } " - a unique identifier assigned to each thread." }
79         { { $snippet "name" } " - the name passed to " { $link spawn } "." }
80         { { $snippet "quot" } " - the initial quotation passed to " { $link spawn } "." }
81         { { $snippet "continuation" } " - a " { $link box } "; if the thread is ready to run, the box holds the continuation, otherwise it is empty." }
82     }
83 } ;
84
85 HELP: self
86 { $values { "thread" thread } }
87 { $description "Pushes the currently-running thread." } ;
88
89 HELP: <thread>
90 { $values { "quot" quotation } { "name" string } { "thread" thread } }
91 { $description "Low-level thread constructor. The thread runs the quotation when spawned."
92 $nl
93 "The name is used to identify the thread for debugging purposes; see " { $link "tools.threads" } "." }
94 { $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) } "." } ;
95
96 HELP: run-queue
97 { $values { "dlist" dlist } }
98 { $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."
99 $nl
100 "By convention, threads are queued with " { $link push-front } 
101 " and dequed with " { $link pop-back } "." } ;
102
103 HELP: resume
104 { $values { "thread" thread } }
105 { $description "Adds a thread to the end of the run queue. The thread must have previously been suspended by a call to " { $link suspend } "." } ;
106
107 HELP: resume-with
108 { $values { "obj" object } { "thread" thread } }
109 { $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." } ;
110
111 HELP: sleep-queue
112 { $values { "heap" min-heap } }
113 { $var-description "A " { $link min-heap } " storing the queue of sleeping threads." } ;
114
115 HELP: sleep-time
116 { $values { "us/f" "a non-negative integer or " { $link f } } }
117 { $description "Outputs 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, outputs " { $link f } "." } ;
118
119 HELP: stop
120 { $description "Stops the current thread. The thread may be started again from another thread using " { $link (spawn) } "." } ;
121
122 HELP: yield
123 { $description "Adds the current thread to the end of the run queue, and switches to the next runnable thread." } ;
124
125 HELP: sleep-until
126 { $values { "time/f" "a non-negative integer or " { $link f } } }
127 { $description "Suspends the current thread until the given time, or indefinitely if a value of " { $link f } " is passed in."
128 $nl
129 "Other threads may interrupt the sleep by calling " { $link interrupt } "." } ;
130
131 HELP: sleep
132 { $values { "dt" "a duration" } }
133 { $description "Suspends the current thread for the given duration."
134 $nl
135 "Other threads may interrupt the sleep by calling " { $link interrupt } "." }
136 { $examples
137     { $code "USING: threads calendar ;" "10 seconds sleep" }
138 } ;
139
140 HELP: interrupt
141 { $values { "thread" thread } }
142 { $description "Interrupts a sleeping thread." } ;
143
144 HELP: suspend
145 { $values { "quot" { $quotation "( thread -- )" } } { "state" string } { "obj" object } }
146 { $description "Suspends the current thread and passes it to the quotation."
147 $nl
148 "After the quotation returns, control yields to the next runnable thread and the current thread does not execute again until it is resumed, and so the quotation must arrange for another thread to later resume the suspended thread with a call to " { $link resume } " or " { $link resume-with } "."
149 $nl
150 "The status string is for debugging purposes; see " { $link "tools.threads" } "." } ;
151
152 HELP: spawn
153 { $values { "quot" quotation } { "name" string } { "thread" thread } }
154 { $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."
155 $nl
156 "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 } "." }
157 { $notes
158      "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 } "."
159 }
160 { $examples
161     { $code "1 2 [ + . ] 2curry \"Addition thread\" spawn" }
162 } ;
163
164 HELP: spawn-server
165 { $values { "quot" { $quotation "( -- ? )" } } { "name" string } { "thread" thread } }
166 { $description "Convenience wrapper around " { $link spawn } " which repeatedly calls the quotation in a new thread until it outputs " { $link f } "." }
167 { $examples
168     "A thread that runs forever:"
169     { $code "[ do-foo-bar t ] \"Foo bar server\" spawn-server" }
170 } ;
171
172 HELP: init-threads
173 { $description "Called during startup to initialize the threading system. This word should never be called directly." } ;
174
175 HELP: tnamespace
176 { $values { "assoc" assoc } }
177 { $description "Outputs the current thread's set of thread-local variables." } ;
178
179 HELP: tget
180 { $values { "key" object } { "value" object } }
181 { $description "Outputs the value of a thread-local variable." } ;
182
183 HELP: tset
184 { $values { "value" object } { "key" object } }
185 { $description "Sets the value of a thread-local variable." } ;
186
187 HELP: tchange
188 { $values { "key" object } { "quot" { $quotation "( value -- newvalue )" } } }
189 { $description "Applies the quotation to the current value of a thread-local variable, storing the result back to the same variable." } ;