* t/pmc/complex.t:
[parrot.git] / src / pmc / parrotthread.pmc
blob146c42a1e1210feb95099d3a839559da2915443f
1 /*
2 Copyright (C) 2001-2007, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/parrotthread.pmc - Threaded Interpreter
9 =head1 DESCRIPTION
11 ParrotThread extends ParrotInterpreter to provide a threaded interpreter
12 which supports:
14     new P2, "ParrotThread"        # create new threaded interp
15     find_method P0, P2, "thread3" # thread-run function
16     find_global P5, "_th1"        # locate thread function
17     invoke                        # run thread
19     set I0, P2                    # get thread id
21 and these methods:
23     thread1
24     thread2
25     thread3                   # start thread of type 1..3
26     join
27     detach
28     yield
29     kill
31 =head2 Methods
33 =over 4
35 =cut
39 #include "parrot/parrot.h"
40 #include "parrot/embed.h"
43  * can't do multi-threaded DOD/GC yet
44  * XXX a quick hack to pass the few tests
45  */
46 static void
47 stop_GC(Interp *parent, Interp *thread)
49 #if 0
50     Parrot_block_DOD(parent);
51     Parrot_block_DOD(thread);
52     Parrot_block_GC(parent);
53     Parrot_block_GC(thread);
54 #endif
57 /* XXX FIXME probably not the best interface [see also list post of
58    coke's]
59  */
61 static INTVAL do_thread_run(PARROT_INTERP, PMC *thread,
62                             INTVAL clone_flags, PMC *sub, PMC *args) {
63     INTVAL tid = VTABLE_get_integer(interp, thread);
65     clone_interpreter(PMC_data_typed(thread, Parrot_Interp),
66                       interp, clone_flags);
68     interp->flags &= ~PARROT_THR_COPY_INTERP; /* XXX */
69     pt_thread_run(interp, thread, sub, args);
71     pmc_reuse(interp, thread, enum_class_ParrotRunningThread, 0);
73     PObj_active_destroy_CLEAR(thread);
74     PObj_custom_mark_CLEAR(thread);
75     PMC_int_val(thread) = tid;
77     return tid;
80 static INTVAL do_thread_run_clone_default(PARROT_INTERP,
81                                           PMC *thread, PMC *sub, PMC *args) {
82     return do_thread_run(interp, thread, PARROT_CLONE_DEFAULT, sub, args);
86 pmclass ParrotThread extends ParrotInterpreter need_ext no_ro {
90 =item C<thread_id = thread.'run'(CLONE_FLAGS, sub, args...)>
92 Run the thread. This object is morphed into an appropriate
93 ParrotRunningThread PMC.  The CLONE_FLAGS are or'd together values
94 taken from C<cloneflags.pasm>.
96 =item C<thread_id = thread.'run_clone'(sub, args...)>
98 Equivalent to calling run with PARROT_CLONE_DEFAULT.
100 =cut
104     void class_init() {
105         const int typ = enum_class_ParrotThread;
107         if (pass) {
108             register_nci_method(INTERP, typ,
109                     F2DPTR(do_thread_run), "run", "IJOIP@");
111             /* XXX appropriate name given that this won't clone globals? */
112             register_nci_method(INTERP, typ,
113                     F2DPTR(do_thread_run_clone_default), "run_clone", "IJOP@");
114         }
115     }
119 =item C<void init()>
121 Initializes the thread.
123 =cut
127     VTABLE void init() {
128         /* protect interpreter creation and list handling */
129         LOCK(interpreter_array_mutex);
131         SUPER();
132         pt_add_to_interpreters(INTERP, PMC_data_typed(SELF, Parrot_Interp));
134         UNLOCK(interpreter_array_mutex);
136         /* can't allow DOD runs for now */
137         stop_GC(INTERP, PMC_data_typed(SELF, Parrot_Interp));
138     }
142 =item C<void init_pmc(PMC *parent)>
144 Create a new thread by cloning the passed interpreter.
146 =cut
150     VTABLE void init_pmc(PMC *parent) {
151         LOCK(interpreter_array_mutex);
152         SUPER(parent);
154         pt_add_to_interpreters(PMC_data_typed(parent, Parrot_Interp),
155                                PMC_data_typed(SELF,   Parrot_Interp));
157         UNLOCK(interpreter_array_mutex);
159         /* can't allow DOD runs for now */
160         stop_GC(INTERP, PMC_data_typed(SELF, Parrot_Interp));
161     }
166 =back
168 =head1 HISTORY
170 2003.12.18 leo initial review.
172 =cut
177  * Local variables:
178  *   c-file-style: "parrot"
179  * End:
180  * vim: expandtab shiftwidth=4:
181  */