add generated_hello.pbc to examples-clean
[parrot.git] / src / pmc / parrotthread.pmc
blobaffdbb5785858d4ecc0d942a339a0911f2ef7b95
1 /*
2 Copyright (C) 2001-2010, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/parrotthread.pmc - Represents a Parrot Thread.
9 =head1 DESCRIPTION
11 This type represents a  parrot thread.
13 It provides the following methods:
14     - join
15     - detach
16     - kill
17     - pid
19 =head2 Methods
21 =over 4
23 =cut
27 #include "parrot/embed.h"
30 pmclass ParrotThread no_ro auto_attrs {
31     ATTR INTVAL tid; /* thread id */
33 /* HEADERIZER HFILE: none */
34 /* HEADERIZER BEGIN: static */
35 /* HEADERIZER END: static */
39 =item C<void init()>
41 Create a new, invalid handle to a running thread.
43 =cut
47     VTABLE void init() {
48         VTABLE_set_integer_native(INTERP, SELF, -1);
49     }
53 =item C<void init_pmc(PMC *notused)>
55 Create a new, invalid handle to a running thread.
57 =cut
61     VTABLE void init_pmc(PMC *notused) {
62         VTABLE_set_integer_native(INTERP, SELF, -1);
63     }
67 =item C<INTVAL get_integer()>
69 Return the thread ID of this thread.
71 =cut
75     VTABLE INTVAL get_integer() {
76         INTVAL ttid;
77         GETATTR_ParrotThread_tid(INTERP, SELF, ttid);
78         return ttid;
79     }
81     VTABLE void set_integer_native(INTVAL ttid) {
82         SETATTR_ParrotThread_tid(INTERP, SELF, ttid);
83     }
88 =item C<METHOD run(closure)>
90 Join the thread, returning whatever its main method returns.
92 =cut
95     METHOD run_clone(PMC *sub, PMC *args :slurpy) {
96         INTVAL ttid;
97         if (PMC_IS_NULL(sub)) {
98             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
99                     "Invalid thread sub");
100         }
102         ttid = pt_thread_create_run(INTERP,
103                                     enum_class_ThreadInterpreter, PARROT_CLONE_DEFAULT, sub, args);
104         VTABLE_set_integer_native(INTERP, SELF, ttid);
105     }
107     METHOD run(INTVAL clone_flags, PMC *sub, PMC *args :slurpy) {
108         INTVAL ttid;
109         if (PMC_IS_NULL(sub)) {
110             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
111                     "Invalid thread sub");
112         }
114         ttid = pt_thread_create_run(INTERP, enum_class_ThreadInterpreter, clone_flags, sub, args);
115         VTABLE_set_integer_native(INTERP, SELF, ttid);
116     }
119 =item C<METHOD join()>
121 Join the thread, returning whatever its main method returns.
123 =cut
126     METHOD join() {
127         PMC *ret;
128         INTVAL ttid = VTABLE_get_integer(INTERP, SELF);
130         ret = pt_thread_join(INTERP, ttid);
131         /* invalidate self */
132         VTABLE_set_integer_native(INTERP, SELF, -1);
134         RETURN(PMC *ret);
135     }
139 =item C<METHOD detach()>
141 Detach the thread so it cannot be joined and will free its resources
142 immediately when it exits.
144 =cut
148     METHOD detach() {
149         pt_thread_detach((UINTVAL)VTABLE_get_integer(INTERP, SELF));
150     }
154 =item C<METHOD kill()>
156 Terminate a running thread.
158 =cut
162     METHOD kill() {
163         pt_thread_kill((UINTVAL)VTABLE_get_integer(INTERP, SELF));
164     }
170 =back
172 =cut
177  * Local variables:
178  *   c-file-style: "parrot"
179  * End:
180  * vim: expandtab shiftwidth=4:
181  */