~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/doc/discussion/oz-thread-api.txt

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Some rough notes about the Oz threading model
 
2
=============================================
 
3
 
 
4
(almost verbatim from CTM)
 
5
 
 
6
Scheduling
 
7
----------
 
8
 
 
9
Fair scheduling through round-robin.
 
10
 
 
11
With priority levels : three queues exist, which manage high, medium,
 
12
low priority threads. The time slice ratio for these is
 
13
100:10:1. Threads inherit the priority of their parent.
 
14
 
 
15
Mozart uses an external timer approach to implement thread preemption.
 
16
 
 
17
Thread ops
 
18
----------
 
19
 
 
20
All these ops are defined in a Thread namespace/module.
 
21
 
 
22
this()               -> current thread's name (*not* another thread's name)
 
23
state(t)             -> return state of t in {runnable, blocked, terminated}
 
24
suspend(t)            : suspend t
 
25
resume(t)             : resume execution of t
 
26
preempt(t)            : preempt t
 
27
terminate(t)          : terminate t immediately
 
28
injectException(t, e) : raise exception e in t
 
29
setPriority(t, p)     : set t's priority to p
 
30
 
 
31
Interestingly, coroutines can be build upon this thread
 
32
API. Coroutines have two ops : spawn and resume.
 
33
 
 
34
spawn(p)             -> creates a coroutine with procedure p, returns pid
 
35
resume(c)             : transfers control from current coroutine to c
 
36
 
 
37
The implementation of these ops in terms of the threads API is as
 
38
follows :
 
39
 
 
40
def spawn(p):
 
41
    in_thread:
 
42
        pid = Thread.this()
 
43
        Thread.suspend(pid)
 
44
        p()
 
45
 
 
46
def resume(cid):
 
47
    Thread.resume cid
 
48
    Thread.suspend(Thread.this())
 
49