File: Threads.k

package info (click to toggle)
kaya 0.4.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,448 kB
  • ctags: 1,694
  • sloc: cpp: 9,536; haskell: 7,461; sh: 3,013; yacc: 910; makefile: 816; perl: 90
file content (113 lines) | stat: -rw-r--r-- 4,085 bytes parent folder | download | duplicates (4)
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
/** -*-C-*-ish
    Kaya standard library
    Copyright (C) 2004, 2005 Edwin Brady

    This file is distributed under the terms of the GNU Lesser General
    Public Licence. See COPYING for licence.
*/
"<summary>POSIX thread threading support</summary>
<prose>This module supports threading using the \"pthread\" library.</prose>"
module Threads;

import Builtins;

%include "thread_glue.h";
%imported "thread_glue";
%link "pthread";

foreign "thread_glue.o" {
    "<summary>Terminate the current thread.</summary>
<prose>Terminate the current thread. Returning from the function used to create the thread also ends the thread.</prose>
<related><functionref>kill</functionref></related>
<related><functionref>waitFor</functionref></related>"
    public Void end() = endThread;

    Ptr docreateThread(fn func,arg fnarg) = createThread;
    Ptr docreateThreadNullary(fn func) = createThreadNullary;
    Void dokillThread(Ptr tid) = killThread;
    a dowaitForThread(Ptr tptr) = waitForThread;
    Ptr docreateMutex() = createMutex;
    Void dolock(Ptr m) = lock;
    Void dounlock(Ptr m) = unlock;
}

"<summary>A thread.</summary>
<prose>Threads are parameterised using their return type for type safety in <functionref>waitFor</functionref>.</prose>
<related><functionref>create</functionref></related>"
abstract data Thread<rettype> = Thread(Ptr tptr);

"<summary>A Mutex.</summary>
<prose>This data type represents a mutex.</prose>
<related><functionref>mutex</functionref></related>"
abstract data Mutex = Mutex(Ptr mptr);

"<argument name='fn'>The function to execute.</argument>
<argument name='arg'>The argument to pass to the function.</argument>
<summary>Create a thread.</summary>
<prose>Create a new thread that executes the specified function. <functionref>waitFor</functionref> may be used to find the return value of the function.</prose>
<related><functionref index='0'>create</functionref></related>"
public Thread<b> create(b(a) fn, a arg)
{
    id = docreateThread(fn,arg);
    return Thread(id);
}

"<argument name='fn'>The parameterless function to execute.</argument>
<summary>Create a thread.</summary>
<prose>Create a new thread that executes the specified function. <functionref>waitFor</functionref> may be used to find the return value of the function.</prose>
<related><functionref index='1'>create</functionref></related>"
public Thread<b> create(b() fn)
{
    id = docreateThreadNullary(@fn);
    return Thread(id);
}

"<argument name='id'>The thread to wait for</argument>
<summary>Wait for a thread to terminate.</summary>
<prose>Wait for a thread to terminate and return the thread's return value.</prose>
<related><functionref>end</functionref></related>
<related><functionref>kill</functionref></related>"
public rettype waitFor(Thread<rettype> id)
{
    return dowaitForThread(id.tptr);
}

"<argument name='id'>The thread to kill</argument>
<summary>Kill a thread immediately</summary>
<prose>Kill a thread immediately</prose>
<related><functionref>end</functionref></related>
<related><functionref>waitFor</functionref></related>"
public Void kill(Thread<rettype> id)
{
    dokillThread(id.tptr);
}

"<summary>Create a mutex.</summary>
<prose>Create a new mutex.</prose>
<related><functionref>lock</functionref></related>
<related><functionref>unlock</functionref></related>"
public Mutex mutex()
{
    m = docreateMutex();
    return Mutex(m);
}

"<argument name='m'>A mutex</argument>
<summary>Lock a mutex.</summary>
<prose>Lock a mutex. All other threads which attempt to lock this mutex will have to wait until it is unlocked. This prevents simultaneous access to shared resources.</prose>
<related><functionref>mutex</functionref></related>
<related><functionref>unlock</functionref></related>"
public Void lock(Mutex m)
{
    dolock(m.mptr);
}

"<argument name='m'>A mutex</argument>
<summary>Unlock a mutex.</summary>
<prose>Unlock a mutex, allowing other threads to lock it.</prose>
<related><functionref>lock</functionref></related>
<related><functionref>mutex</functionref></related>"
public Void unlock(Mutex m)
{
    dounlock(m.mptr);
}