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