File: threads.txt

package info (click to toggle)
scsh 0.5.1-2
  • links: PTS
  • area: non-free
  • in suites: potato, slink
  • size: 6,540 kB
  • ctags: 8,656
  • sloc: lisp: 39,346; ansic: 13,466; sh: 1,669; makefile: 624
file content (113 lines) | stat: -rw-r--r-- 2,795 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

			       Threads


The following are exported by the THREADS structure.

(WITH-MULTITASKING thunk)
  Initializes for multitasking, then starts up a thread for the execution
  of <thunk>.  That thread and all others created will run in the dynamic
  context of the call to with-multitasking.  The call to with-multitasking
  finally returns only when the scheduler runs out of things to do.

(SPAWN thunk) => thread
  Create and schedule a new thread that will execute <thunk>.

(MAKE-LOCK) => lock
(WITH-LOCK lock thunk) => whatever <thunk> returns
(OBTAIN-LOCK lock)
(RELEASE-LOCK lock)
  Locks are semaphores.

(MAKE-CONDVAR) => condvar
(CONDVAR-REF condvar) => value of condvar
(CONDVAR-SET! condvar value)
  Condition variables.  Attempts to reference a condition variable before
  it has been set cause the referencing thread to block.  Setting a
  condition variable to two different values is an error.

(RELINQUISH-TIMESLICE)
  Let other threads run for a while.

ONE-SECOND
  The number of time units in one second.

(SLEEP time)
  Sleep for <time> time units.

(TIME) => integer
  The current time in time units.

(WITH-INTERRUPTS-INHIBITED thunk) => whatever <thunk> returns
(WITH-INTERRUPTS-ALLOWED thunk) => whatever <thunk> returns
  Execute the thunk with or without interrupts.  Interrupts are normally
  enabled.

(THREAD-READ-CHAR port) => character or EOF
(THREAD-PEEK-CHAR port) => character or EOF
(READ-CHAR-WITH-TIMEOUT port time) => character or EOF or 'TIMEOUT
  Read and peek for characters letting other threads run util a character
  is available.  READ-CHAR-WITH-TIMEOUT will return the symbol TIMEOUT if
  <time> time units go by without a character being available.

(THREAD? thing)
  #T if thing is a thread, #F otherwise.

-----

Debugging utilities:

(CURRENT-THREAD)
(TERMINATE-CURRENT-THREAD)
(KILL-THREAD thread)
(START-THREAD thread)
(STOP-THREAD thread)


(INTERRUPT-THREAD thread thunk)
  Interrupt <thread> and make it execute <thunk> before continuing.

(ACTIVE-THREADS)
  Returns a list containing all currently active threads.

(KILL-CONDVAR condvar)
  Kill all threads waiting for <condvar>.


-----

The following is exported by the MORE-THREADS structure.

(START-THREADS)
  Restart the command processor underneath a (WITH-MULTITASKING ...).

    > ,open threads more-threads
    [more-threads ...]
    > (start-threads)
    Multitasking started
    > (define (foo) (sleep one-second) (display "Hi") (newline) (foo))
    > (define th (spawn foo))
    > th
    Hi
    '#{Thread 1}
    > (let loop () (loop))
    Hi
    Hi
    Hi
    Hi

    Interrupt: keyboard
    1> 
    > th
    Hi
    '#{Thread 1}
    > (kill-thread th)
    #t
    > th
    '#{Thread 1}
    > 

-----

Original by RK, 18 Nov 1993.
Sample transcript added by JAR, 5 Dec 1993.