2 Copyright (C) 2001-2010, Parrot Foundation.
7 src/pmc/parrotthread.pmc - Represents a Parrot Thread.
11 This type represents a parrot thread.
13 It provides the following methods:
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 */
41 Create a new, invalid handle to a running thread.
48 VTABLE_set_integer_native(INTERP, SELF, -1);
53 =item C<void init_pmc(PMC *notused)>
55 Create a new, invalid handle to a running thread.
61 VTABLE void init_pmc(PMC *notused) {
62 VTABLE_set_integer_native(INTERP, SELF, -1);
67 =item C<INTVAL get_integer()>
69 Return the thread ID of this thread.
75 VTABLE INTVAL get_integer() {
77 GETATTR_ParrotThread_tid(INTERP, SELF, ttid);
81 VTABLE void set_integer_native(INTVAL ttid) {
82 SETATTR_ParrotThread_tid(INTERP, SELF, ttid);
88 =item C<METHOD run(closure)>
90 Join the thread, returning whatever its main method returns.
95 METHOD run_clone(PMC *sub, PMC *args :slurpy) {
97 if (PMC_IS_NULL(sub)) {
98 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
99 "Invalid thread sub");
102 ttid = pt_thread_create_run(INTERP,
103 enum_class_ThreadInterpreter, PARROT_CLONE_DEFAULT, sub, args);
104 VTABLE_set_integer_native(INTERP, SELF, ttid);
107 METHOD run(INTVAL clone_flags, PMC *sub, PMC *args :slurpy) {
109 if (PMC_IS_NULL(sub)) {
110 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
111 "Invalid thread sub");
114 ttid = pt_thread_create_run(INTERP, enum_class_ThreadInterpreter, clone_flags, sub, args);
115 VTABLE_set_integer_native(INTERP, SELF, ttid);
119 =item C<METHOD join()>
121 Join the thread, returning whatever its main method returns.
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);
139 =item C<METHOD detach()>
141 Detach the thread so it cannot be joined and will free its resources
142 immediately when it exits.
149 pt_thread_detach((UINTVAL)VTABLE_get_integer(INTERP, SELF));
154 =item C<METHOD kill()>
156 Terminate a running thread.
163 pt_thread_kill((UINTVAL)VTABLE_get_integer(INTERP, SELF));
178 * c-file-style: "parrot"
180 * vim: expandtab shiftwidth=4: