Mercurial > projects > dwt2
diff base/src/java/lang/Thread.d @ 27:1bf55a6eb092
Renamed java tree to base
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 21 Mar 2009 11:33:57 +0100 |
parents | java/src/java/lang/Thread.d@9b96950f2c3c |
children | af948d4bbf8c |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base/src/java/lang/Thread.d Sat Mar 21 11:33:57 2009 +0100 @@ -0,0 +1,172 @@ +module java.lang.Thread; + +version(Tango){ + static import tango.core.Thread; +} else { // Phobos + static import core.thread; +} +import java.lang.util; +import java.lang.Runnable; + +class Thread { + + version(Tango){ + alias tango.core.Thread.Thread TThread; + } else { // Phobos + alias core.thread.Thread TThread; + } + private TThread thread; + private Runnable runnable; + + 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; + + version(Tango){ + private static TTLS getTls(){ + if( tls is null ){ + synchronized( Thread.classinfo ){ + if( tls is null ){ + tls = new TTLS(); + } + } + } + return tls; + } + } + + public this(){ + thread = new TThread(&internalRun); + } + public this( void delegate() dg ){ + thread = new TThread(&internalRun); + runnable = dgRunnable( dg ); + } + public this(Runnable runnable){ + thread = new TThread(&internalRun); + this.runnable = runnable; + } + public this(Runnable runnable, String name){ + thread = new TThread(&internalRun); + this.runnable = runnable; + thread.name = cast(char[])name; + } + public this(String name){ + thread = new TThread(&internalRun); + thread.name = cast(char[])name; + } + + public void start(){ + thread.start(); + } + + public static Thread currentThread(){ + 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; + } + } + public int getPriority() { + 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) * (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(){ + version(Tango){ + // Store this thread object ref to the TLS + getTls().val( this ); + } else { // Phobos + tls = this; + } + if( runnable !is null ){ + runnable.run(); + } + else { + run(); + } + } + + public bool isAlive(){ + return thread.isRunning(); + } + + public bool isDaemon() { + return thread.isDaemon(); + } + + public void join(){ + thread.join(); + } + + public void setDaemon(bool on) { + thread.isDaemon(on); + } + + public void setName(String name){ + thread.name = cast(char[])name; + } + public String getName(){ + return cast(String)thread.name; + } + + void interrupt() { + implMissing(__FILE__,__LINE__); + } + + static bool interrupted() { + implMissing(__FILE__,__LINE__); + return false; + } + + public void run(){ + // default impl, do nothing + } + public static void sleep( int time ){ + version(Tango){ + TThread.sleep(time/1000.0); + } else { // Phobos + TThread.sleep(time*10_000); + } + } + public TThread nativeThread(){ + assert(thread); + return thread; + } + public override String toString(){ + return cast(String) "Thread "~cast(String)thread.name; + } + public static void yield(){ + TThread.yield(); + } +} +