1 module java.lang.Thread; 2 3 version(Tango){ 4 static import tango.core.Thread; 5 } else { // Phobos 6 static import core.thread; 7 static import core.time; 8 } 9 import java.lang.util; 10 import java.lang.Runnable; 11 12 class Thread { 13 14 version(Tango){ 15 alias tango.core.Thread.Thread TThread; 16 } else { // Phobos 17 alias core.thread.Thread TThread; 18 } 19 private TThread thread; 20 private Runnable runnable; 21 private bool interrupted_ = false; 22 version(Tango){ 23 private alias tango.core.Thread.ThreadLocal!(Thread) TTLS; 24 private static TTLS tls; 25 } else { // Phobos 26 private static Thread tls; //in tls 27 } 28 29 public static const int MAX_PRIORITY = 10; 30 public static const int MIN_PRIORITY = 1; 31 public static const int NORM_PRIORITY = 5; 32 33 version(Tango){ 34 private static TTLS getTls(){ 35 if( tls is null ){ 36 synchronized( Thread.classinfo ){ 37 if( tls is null ){ 38 tls = new TTLS(); 39 } 40 } 41 } 42 return tls; 43 } 44 } 45 46 public this(){ 47 thread = new TThread(&internalRun); 48 } 49 public this( void delegate() dg ){ 50 thread = new TThread(&internalRun); 51 runnable = dgRunnable( dg ); 52 } 53 public this(Runnable runnable){ 54 thread = new TThread(&internalRun); 55 this.runnable = runnable; 56 } 57 public this(Runnable runnable, String name){ 58 thread = new TThread(&internalRun); 59 this.runnable = runnable; 60 thread.name = name; 61 } 62 public this(String name){ 63 thread = new TThread(&internalRun); 64 thread.name = name; 65 } 66 67 public void start(){ 68 thread.start(); 69 } 70 71 public static Thread currentThread(){ 72 version(Tango){ 73 auto res = getTls().val(); 74 if( res is null ){ 75 // no synchronized needed 76 res = new Thread(); 77 res.thread = tango.core.Thread.Thread.getThis(); 78 getTls().val( res ); 79 } 80 assert( res ); 81 return res; 82 } else { // Phobos 83 auto res = tls; 84 if( res is null ){ 85 // no synchronized needed 86 res = new Thread(); 87 res.thread = TThread.getThis(); 88 tls = res; 89 } 90 assert( res ); 91 return res; 92 } 93 } 94 public int getPriority() { 95 return (thread.priority-TThread.PRIORITY_MIN) * (MAX_PRIORITY-MIN_PRIORITY) / (TThread.PRIORITY_MAX-TThread.PRIORITY_MIN) + MIN_PRIORITY; 96 } 97 public void setPriority( int newPriority ) { 98 // assert( MIN_PRIORITY < MAX_PRIORITY ); 99 // assert( tango.core.Thread.Thread.PRIORITY_MIN < tango.core.Thread.Thread.PRIORITY_MAX ); 100 auto scaledPrio = (newPriority-MIN_PRIORITY) * (TThread.PRIORITY_MAX-TThread.PRIORITY_MIN) / (MAX_PRIORITY-MIN_PRIORITY) +TThread.PRIORITY_MIN; 101 // getDwtLogger().trace( __FILE__, __LINE__, "Thread.setPriority: scale ({} {} {}) -> ({} {} {})", MIN_PRIORITY, newPriority, MAX_PRIORITY, TThread.PRIORITY_MIN, scaledPrio, TThread.PRIORITY_MAX); 102 // thread.priority( scaledPrio ); 103 } 104 105 private void internalRun(){ 106 version(Tango){ 107 // Store this thread object ref to the TLS 108 getTls().val( this ); 109 } else { // Phobos 110 tls = this; 111 } 112 if( runnable !is null ){ 113 runnable.run(); 114 } 115 else { 116 run(); 117 } 118 } 119 120 public bool isAlive(){ 121 return thread.isRunning(); 122 } 123 124 public bool isDaemon() { 125 return thread.isDaemon(); 126 } 127 128 public void join(){ 129 thread.join(); 130 } 131 132 public void setDaemon(bool on) { 133 thread.isDaemon(on); 134 } 135 136 public void setName(String name){ 137 thread.name = name; 138 } 139 public String getName(){ 140 return thread.name; 141 } 142 143 void interrupt() { 144 interrupted_ = true; 145 implMissing(__FILE__,__LINE__); 146 } 147 148 static bool interrupted() { 149 auto t = currentThread(); 150 synchronized(t){ 151 bool res = t.interrupted_; 152 t.interrupted_ = false; 153 return res; 154 } 155 } 156 157 public void run(){ 158 // default impl, do nothing 159 } 160 public static void sleep( int time ){ 161 version(Tango){ 162 TThread.sleep(time/1000.0); 163 } else { // Phobos 164 TThread.sleep(core.time.dur!("msecs")(time)); 165 } 166 } 167 public TThread nativeThread(){ 168 assert(thread); 169 return thread; 170 } 171 public override String toString(){ 172 return "Thread " ~ thread.name; 173 } 174 public static void yield(){ 175 TThread.yield(); 176 } 177 178 public static void joinAll(){ 179 version (Tango) 180 tango.core.Thread.thread_joinAll(); 181 else 182 core.thread.thread_joinAll(); 183 } 184 } 185