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