diff java/src/java/lang/Thread.d @ 21:9b96950f2c3c

the 'java' tree compiles on both D1-Tango and D2-Phobos
author Frank Benoit <benoit@tionex.de>
date Thu, 19 Mar 2009 20:38:55 +0100
parents 52184e4b815c
children
line wrap: on
line diff
--- a/java/src/java/lang/Thread.d	Wed Mar 18 12:10:17 2009 +0100
+++ b/java/src/java/lang/Thread.d	Thu Mar 19 20:38:55 2009 +0100
@@ -1,51 +1,66 @@
 module java.lang.Thread;
 
-static import tango.core.Thread;
+version(Tango){
+    static import tango.core.Thread;
+} else { // Phobos
+    static import core.thread;
+}
 import java.lang.util;
 import java.lang.Runnable;
 
 class Thread {
 
-    private tango.core.Thread.Thread thread;
+    version(Tango){
+        alias tango.core.Thread.Thread TThread;
+    } else { // Phobos
+        alias core.thread.Thread TThread;
+    }
+    private TThread thread;
     private Runnable runnable;
 
-    private alias tango.core.Thread.ThreadLocal!(Thread) TTLS;
-    private static TTLS tls;
+    version(Tango){
+        private alias tango.core.Thread.ThreadLocal!(Thread) TTLS;
+        private static TTLS tls;
+    } else { // Phobos
+        mixin( "static __thread Thread tls;" );
+    }
 
     public static const int MAX_PRIORITY  = 10;
     public static const int MIN_PRIORITY  =  1;
     public static const int NORM_PRIORITY =  5;
 
-    private static TTLS getTls(){
-        if( tls is null ){
-            synchronized( Thread.classinfo ){
-                if( tls is null ){
-                    tls = new TTLS();
+    version(Tango){
+        private static TTLS getTls(){
+            if( tls is null ){
+                synchronized( Thread.classinfo ){
+                    if( tls is null ){
+                        tls = new TTLS();
+                    }
                 }
             }
+            return tls;
         }
-        return tls;
     }
 
     public this(){
-        thread = new tango.core.Thread.Thread(&internalRun);
+        thread = new TThread(&internalRun);
     }
     public this( void delegate() dg ){
-        thread = new tango.core.Thread.Thread(&internalRun);
+        thread = new TThread(&internalRun);
         runnable = dgRunnable( dg );
     }
     public this(Runnable runnable){
-        thread = new tango.core.Thread.Thread(&internalRun);
+        thread = new TThread(&internalRun);
         this.runnable = runnable;
     }
     public this(Runnable runnable, String name){
-        thread = new tango.core.Thread.Thread(&internalRun);
+        thread = new TThread(&internalRun);
         this.runnable = runnable;
-        thread.name = name;
+        thread.name = cast(char[])name;
     }
     public this(String name){
-        thread = new tango.core.Thread.Thread(&internalRun);
-        thread.name = name;
+        thread = new TThread(&internalRun);
+        thread.name = cast(char[])name;
     }
 
     public void start(){
@@ -53,29 +68,46 @@
     }
 
     public static Thread currentThread(){
-        auto res = getTls().val();
-        if( res is null ){
-            // no synchronized needed
-            res = new Thread();
-            res.thread = tango.core.Thread.Thread.getThis();
-            getTls().val( res );
+        version(Tango){
+            auto res = getTls().val();
+            if( res is null ){
+                // no synchronized needed
+                res = new Thread();
+                res.thread = tango.core.Thread.Thread.getThis();
+                getTls().val( res );
+            }
+            assert( res );
+            return res;
+        } else { // Phobos
+            auto res = tls;
+            if( res is null ){
+                // no synchronized needed
+                res = new Thread();
+                res.thread = TThread.getThis();
+                tls = res;
+            }
+            assert( res );
+            return res;
         }
-        assert( res );
-        return res;
     }
     public int getPriority() {
-        return (thread.priority-tango.core.Thread.Thread.PRIORITY_MIN) * (MAX_PRIORITY-MIN_PRIORITY) / (tango.core.Thread.Thread.PRIORITY_MAX-tango.core.Thread.Thread.PRIORITY_MIN) + MIN_PRIORITY;
+        return (thread.priority-TThread.PRIORITY_MIN) * (MAX_PRIORITY-MIN_PRIORITY) / (TThread.PRIORITY_MAX-TThread.PRIORITY_MIN) + MIN_PRIORITY;
     }
     public void setPriority( int newPriority ) {
 //         assert( MIN_PRIORITY < MAX_PRIORITY );
 //         assert( tango.core.Thread.Thread.PRIORITY_MIN < tango.core.Thread.Thread.PRIORITY_MAX );
-        auto scaledPrio = (newPriority-MIN_PRIORITY) * (tango.core.Thread.Thread.PRIORITY_MAX-tango.core.Thread.Thread.PRIORITY_MIN) / (MAX_PRIORITY-MIN_PRIORITY) +tango.core.Thread.Thread.PRIORITY_MIN;
-        getDwtLogger().trace( __FILE__, __LINE__, "Thread.setPriority: scale ({} {} {}) -> ({} {} {})", MIN_PRIORITY, newPriority, MAX_PRIORITY, tango.core.Thread.Thread.PRIORITY_MIN, scaledPrio, tango.core.Thread.Thread.PRIORITY_MAX);
+        auto scaledPrio = (newPriority-MIN_PRIORITY) * (TThread.PRIORITY_MAX-TThread.PRIORITY_MIN) / (MAX_PRIORITY-MIN_PRIORITY) +TThread.PRIORITY_MIN;
+        getDwtLogger().trace( __FILE__, __LINE__, "Thread.setPriority: scale ({} {} {}) -> ({} {} {})", MIN_PRIORITY, newPriority, MAX_PRIORITY, TThread.PRIORITY_MIN, scaledPrio, TThread.PRIORITY_MAX);
 //         thread.priority( scaledPrio );
     }
 
     private void internalRun(){
-        getTls().val( this );
+        version(Tango){
+            // Store this thread object ref to the TLS
+            getTls().val( this );
+        } else { // Phobos
+            tls = this;
+        }
         if( runnable !is null ){
             runnable.run();
         }
@@ -101,10 +133,10 @@
     }
 
     public void setName(String name){
-        thread.name = name;
+        thread.name = cast(char[])name;
     }
     public String getName(){
-        return thread.name;
+        return cast(String)thread.name;
     }
 
     void interrupt() {
@@ -120,17 +152,21 @@
         // default impl, do nothing
     }
     public static void sleep( int time ){
-        tango.core.Thread.Thread.sleep(time/1000.0);
+        version(Tango){
+            TThread.sleep(time/1000.0);
+        } else { // Phobos
+            TThread.sleep(time*10_000);
+        }
     }
-    public tango.core.Thread.Thread nativeThread(){
+    public TThread nativeThread(){
         assert(thread);
         return thread;
     }
-    public override char[] toString(){
-        return "Thread "~thread.name;
+    public override String toString(){
+        return cast(String) "Thread "~cast(String)thread.name;
     }
     public static void yield(){
-        tango.core.Thread.Thread.yield();
+        TThread.yield();
     }
 }