[svn:parrot] r46170 - in trunk: . src/pmc t/pmc

tewk at svn.parrot.org tewk at svn.parrot.org
Fri Apr 30 03:48:43 UTC 2010


Author: tewk
Date: Fri Apr 30 03:48:43 2010
New Revision: 46170
URL: https://trac.parrot.org/parrot/changeset/46170

Log:
Added new parrotthread pmcs

Added:
   trunk/src/pmc/parrotthread.pmc
   trunk/src/pmc/threadinterpreter.pmc
Modified:
   trunk/MANIFEST
   trunk/PBC_COMPAT
   trunk/src/pmc/parrotinterpreter.pmc
   trunk/t/pmc/parrotthread.t
   trunk/t/pmc/threads.t

Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST	Fri Apr 30 03:48:35 2010	(r46169)
+++ trunk/MANIFEST	Fri Apr 30 03:48:43 2010	(r46170)
@@ -1380,7 +1380,6 @@
 src/pmc/packfilesegment.pmc                                 []
 src/pmc/parrotinterpreter.pmc                               []
 src/pmc/parrotlibrary.pmc                                   []
-src/pmc/parrotrunningthread.pmc                             []
 src/pmc/parrotthread.pmc                                    []
 src/pmc/pmc.num                                             []
 src/pmc/pmcproxy.pmc                                        []
@@ -1401,6 +1400,7 @@
 src/pmc/stringiterator.pmc                                  []
 src/pmc/sub.pmc                                             []
 src/pmc/task.pmc                                            []
+src/pmc/parrotinterpreter.pmc                               []
 src/pmc/timer.pmc                                           []
 src/pmc/undef.pmc                                           []
 src/pmc/unmanagedstruct.pmc                                 []
@@ -1863,7 +1863,6 @@
 t/pmc/parrotio.t                                            [test]
 t/pmc/parrotlibrary.t                                       [test]
 t/pmc/parrotobject.t                                        [test]
-t/pmc/parrotrunningthread.t                                 [test]
 t/pmc/parrotthread.t                                        [test]
 t/pmc/pmc.t                                                 [test]
 t/pmc/pmcproxy.t                                            [test]

Modified: trunk/PBC_COMPAT
==============================================================================
--- trunk/PBC_COMPAT	Fri Apr 30 03:48:35 2010	(r46169)
+++ trunk/PBC_COMPAT	Fri Apr 30 03:48:43 2010	(r46170)
@@ -27,6 +27,7 @@
 
 # please insert tab separated entries at the top of the list
 
+6.11	2010.04.29	tewk	remove ParrotRunningThread
 6.10	2010.04.29	NotFound	packfiledebug.pmc
 6.9	2010.04.27	bacek	remove deprecated in-place string ops (bitwise, charset, case change)
 6.8	2010.04.27	bacek	remove deprecated in-place substr ops

Modified: trunk/src/pmc/parrotinterpreter.pmc
==============================================================================
--- trunk/src/pmc/parrotinterpreter.pmc	Fri Apr 30 03:48:35 2010	(r46169)
+++ trunk/src/pmc/parrotinterpreter.pmc	Fri Apr 30 03:48:43 2010	(r46170)
@@ -351,7 +351,9 @@
 
     VTABLE INTVAL get_integer() {
         const Parrot_Interp i = PMC_interp(SELF);
-        return (INTVAL)i->thread_data->tid;
+        if (i->thread_data)
+          return (INTVAL)i->thread_data->tid;
+        return 0;
     }
 
 /*
@@ -651,7 +653,7 @@
 
 */
 
-    VTABLE INTVAL is_equal(PMC *val) {
+    MULTI INTVAL is_equal(ParrotInterpreter val) {
         Parrot_Interp self  = PMC_interp(SELF);
         Parrot_Interp other = PMC_interp(val);
 
@@ -665,6 +667,22 @@
         return 0;
     }
 
+    MULTI INTVAL is_equal(ParrotThread value) {
+        Parrot_Interp self  = PMC_interp(SELF);
+
+        if (!self->thread_data)
+            return 0;
+
+        return self->thread_data->tid == (UINTVAL) VTABLE_get_integer(interp, value);
+    }
+
+    MULTI INTVAL is_equal(DEFAULT value) {
+        Parrot_ex_throw_from_c_args(INTERP, NULL,
+                EXCEPTION_INTERNAL_NOT_IMPLEMENTED,
+                "ParrotInterpreter: no multiple dispatch variant 'is_equal' for %Ss",
+                VTABLE_name(interp, value));
+    }
+
 /*
 
 =item C<void visit(PMC *info)>

Added: trunk/src/pmc/parrotthread.pmc
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/src/pmc/parrotthread.pmc	Fri Apr 30 03:48:43 2010	(r46170)
@@ -0,0 +1,206 @@
+/*
+Copyright (C) 2001-2010, Parrot Foundation.
+$Id$
+
+=head1 NAME
+
+src/pmc/parrotthread.pmc - Represents a Parrot Thread.
+
+=head1 DESCRIPTION
+
+This type represents a  parrot thread.
+
+It provides the following methods:
+    - join
+    - detach
+    - kill
+    - pid
+
+=head2 Methods
+
+=over 4
+
+=cut
+
+*/
+
+#include "parrot/embed.h"
+
+
+pmclass ParrotThread no_ro auto_attrs {
+    ATTR INTVAL tid; /* thread id */
+
+/* HEADERIZER HFILE: none */
+/* HEADERIZER BEGIN: static */
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
+
+static INTVAL do_thread_run(PARROT_INTERP,
+    ARGIN(PMC *thread),
+    INTVAL clone_flags,
+    ARGIN(PMC *sub),
+    ARGIN(PMC *args))
+        __attribute__nonnull__(1)
+        __attribute__nonnull__(2)
+        __attribute__nonnull__(4)
+        __attribute__nonnull__(5);
+
+static void stop_GC(ARGIN(Interp *parent), ARGIN(Interp *thread))
+        __attribute__nonnull__(1)
+        __attribute__nonnull__(2);
+
+#define ASSERT_ARGS_do_thread_run __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(interp) \
+    , PARROT_ASSERT_ARG(thread) \
+    , PARROT_ASSERT_ARG(sub) \
+    , PARROT_ASSERT_ARG(args))
+#define ASSERT_ARGS_stop_GC __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(parent) \
+    , PARROT_ASSERT_ARG(thread))
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
+/* HEADERIZER END: static */
+
+/*
+
+=item C<void init()>
+
+Create a new, invalid handle to a running thread.
+
+=cut
+
+*/
+
+    VTABLE void init() {
+        VTABLE_set_integer_native(interp, SELF, -1);
+    }
+
+/*
+
+=item C<void init_pmc(PMC *notused)>
+
+Create a new, invalid handle to a running thread.
+
+=cut
+
+*/
+
+    VTABLE void init_pmc(PMC *notused) {
+        VTABLE_set_integer_native(interp, SELF, -1);
+    }
+
+/*
+
+=item C<INTVAL get_integer()>
+
+Return the thread ID of this thread.
+
+=cut
+
+*/
+
+    VTABLE INTVAL get_integer() {
+        INTVAL ttid;
+        GETATTR_ParrotThread_tid(interp, SELF, ttid);
+        return ttid;
+    }
+
+    VTABLE INTVAL set_integer_native(INTVAL ttid) {
+        SETATTR_ParrotThread_tid(interp, SELF, ttid);
+        return ttid;
+    }
+
+
+/*
+
+=item C<METHOD run(closure)>
+
+Join the thread, returning whatever its main method returns.
+
+=cut
+
+*/
+    METHOD run_clone(PMC *sub, PMC *args :slurpy) {
+        INTVAL ttid;
+        if (PMC_IS_NULL(sub)) {
+            Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
+                    "Invalid thread sub");
+        }
+        
+        ttid = pt_thread_create_run(interp, enum_class_ThreadInterpreter, PARROT_CLONE_DEFAULT, sub, args);
+        VTABLE_set_integer_native(interp, SELF, ttid);
+    }
+
+    METHOD run(INTVAL clone_flags, PMC *sub, PMC *args :slurpy) {
+        INTVAL ttid;
+        if (PMC_IS_NULL(sub)) {
+            Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
+                    "Invalid thread sub");
+        }
+
+        ttid = pt_thread_create_run(interp, enum_class_ThreadInterpreter, clone_flags, sub, args);
+        VTABLE_set_integer_native(interp, SELF, ttid);
+    }
+/*
+
+=item C<METHOD join()>
+
+Join the thread, returning whatever its main method returns.
+
+=cut
+
+*/
+    METHOD join() {
+        PMC *ret;
+        INTVAL ttid = VTABLE_get_integer(INTERP, SELF);
+
+        ret = pt_thread_join(INTERP, ttid);
+        /* invalidate self */
+        VTABLE_set_integer_native(interp, SELF, -1);
+
+        RETURN(PMC *ret);
+    }
+
+/*
+
+=item C<METHOD detach()>
+
+Detach the thread so it cannot be joined and will free its resources
+immediately when it exits.
+
+=cut
+
+*/
+
+    METHOD detach() {
+        pt_thread_detach((UINTVAL)VTABLE_get_integer(INTERP, SELF));
+    }
+
+/*
+
+=item C<METHOD kill()>
+
+Terminate a running thread.
+
+=cut
+
+*/
+
+    METHOD kill() {
+        pt_thread_kill((UINTVAL)VTABLE_get_integer(INTERP, SELF));
+    }
+
+}
+
+/*
+
+=back
+
+=cut
+
+*/
+
+/*
+ * Local variables:
+ *   c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4:
+ */

Added: trunk/src/pmc/threadinterpreter.pmc
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/src/pmc/threadinterpreter.pmc	Fri Apr 30 03:48:43 2010	(r46170)
@@ -0,0 +1,81 @@
+/*
+Copyright (C) 2001-2007, Parrot Foundation.
+$Id$
+
+=head1 NAME
+
+src/pmc/threadinterpreter.pmc - INTERNAL Interpreter for OS level worker thread(pthread).
+
+=head1 DESCRIPTION
+
+ThreadInterpreter extends ParrotInterpreter to provide a interpreter
+to be used internally in a OS level worker thread(pthread).
+which supports:
+
+=head2 Methods
+
+=over 4
+
+=cut
+
+*/
+
+#include "parrot/embed.h"
+
+#define PMC_interp(x) ((Parrot_ParrotInterpreter_attributes *)PMC_data(x))->interp
+
+/*
+ * can't do multi-threaded GC yet
+ * XXX a quick hack to pass the few tests
+ */
+static void
+stop_GC(Interp *parent, Interp *thread)
+{
+    Parrot_block_GC_mark(parent);
+    Parrot_block_GC_mark(thread);
+    Parrot_block_GC_sweep(parent);
+    Parrot_block_GC_sweep(thread);
+}
+
+
+
+pmclass ThreadInterpreter extends ParrotInterpreter no_ro {
+
+/*
+
+=item C<void init()>
+
+Initializes the thread.
+
+=cut
+
+*/
+
+    VTABLE void init() {
+        /* protect interpreter creation and list handling */
+        LOCK(interpreter_array_mutex);
+
+        SUPER();
+        pt_add_to_interpreters(INTERP, PMC_interp(SELF));
+
+        UNLOCK(interpreter_array_mutex);
+
+        /* can't allow GC runs for now */
+        stop_GC(INTERP, PMC_interp(SELF));
+    }
+}
+
+/*
+
+=back
+
+=cut
+
+*/
+
+/*
+ * Local variables:
+ *   c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4:
+ */

Modified: trunk/t/pmc/parrotthread.t
==============================================================================
--- trunk/t/pmc/parrotthread.t	Fri Apr 30 03:48:35 2010	(r46169)
+++ trunk/t/pmc/parrotthread.t	Fri Apr 30 03:48:43 2010	(r46170)
@@ -16,13 +16,14 @@
 
 =cut
 
-.sub main :main
+.sub 'main' :main
     .include 'test_more.pir'
 
     plan(1)
 
     new $P0, ['ParrotThread']
     ok(1, 'Instantiated a ParrotThread PMC')
+
 .end
 
 # Local Variables:

Modified: trunk/t/pmc/threads.t
==============================================================================
--- trunk/t/pmc/threads.t	Fri Apr 30 03:48:35 2010	(r46169)
+++ trunk/t/pmc/threads.t	Fri Apr 30 03:48:43 2010	(r46170)
@@ -47,7 +47,7 @@
 ok1:
     print "ok 1\n"
     new P4, ['ParrotThread']
-    ne P4, P2, ok2
+    ne P2, P4, ok2
     print "not"
 ok2:
     print "ok 2\n"
@@ -182,7 +182,7 @@
 CODE
 ok 1
 hello from thread
-ParrotThread tid 1
+ThreadInterpreter tid 1
 from 10 interp
 OUTPUT
 
@@ -215,7 +215,7 @@
     branch lp
 .end
 CODE
-start 1
+start -1
 in thread
 done
 OUTPUT


More information about the parrot-commits mailing list