Mercurial > projects > dwt-addons
annotate dwtx/dwtxhelper/JThread.d @ 185:987b95661bb9
Added impl for Timer/TimerTask
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 18 Oct 2008 15:02:49 +0200 |
parents | ed80c5c2b550 |
children |
rev | line source |
---|---|
167 | 1 module dwtx.dwtxhelper.JThread; |
2 | |
3 import tango.core.Thread; | |
4 import dwt.dwthelper.utils; | |
185
987b95661bb9
Added impl for Timer/TimerTask
Frank Benoit <benoit@tionex.de>
parents:
176
diff
changeset
|
5 import tango.util.log.Trace; |
167 | 6 |
7 class JThread { | |
8 | |
9 private Thread thread; | |
10 private Runnable runnable; | |
11 | |
12 private alias ThreadLocal!(JThread) TTLS; | |
13 private static TTLS tls; | |
14 | |
15 public static const int MAX_PRIORITY = 10; | |
16 public static const int MIN_PRIORITY = 1; | |
17 public static const int NORM_PRIORITY = 5; | |
18 | |
19 private static TTLS getTls(){ | |
20 if( tls is null ){ | |
21 synchronized( JThread.classinfo ){ | |
22 if( tls is null ){ | |
23 tls = new TTLS(); | |
24 } | |
25 } | |
26 } | |
27 return tls; | |
28 } | |
29 | |
30 public this(){ | |
31 thread = new Thread(&internalRun); | |
32 } | |
33 public this( void delegate() dg ){ | |
34 thread = new Thread(&internalRun); | |
35 runnable = dgRunnable( dg ); | |
36 } | |
37 public this(Runnable runnable){ | |
38 thread = new Thread(&internalRun); | |
39 this.runnable = runnable; | |
40 } | |
41 public this(Runnable runnable, String name){ | |
42 thread = new Thread(&internalRun); | |
43 this.runnable = runnable; | |
44 thread.name = name; | |
45 } | |
46 public this(String name){ | |
47 thread = new Thread(&internalRun); | |
48 thread.name = name; | |
49 } | |
50 | |
51 public void start(){ | |
52 thread.start(); | |
53 } | |
54 | |
55 public static JThread currentThread(){ | |
175
9e7e1a8bc813
get a JThread for a thread that was just a Thread.
Frank Benoit <benoit@tionex.de>
parents:
167
diff
changeset
|
56 auto res = getTls().val(); |
9e7e1a8bc813
get a JThread for a thread that was just a Thread.
Frank Benoit <benoit@tionex.de>
parents:
167
diff
changeset
|
57 if( res is null ){ |
176 | 58 // no synchronized needed |
59 res = new JThread(); | |
60 res.thread = Thread.getThis(); | |
61 getTls().val( res ); | |
175
9e7e1a8bc813
get a JThread for a thread that was just a Thread.
Frank Benoit <benoit@tionex.de>
parents:
167
diff
changeset
|
62 } |
176 | 63 assert( res ); |
175
9e7e1a8bc813
get a JThread for a thread that was just a Thread.
Frank Benoit <benoit@tionex.de>
parents:
167
diff
changeset
|
64 return res; |
167 | 65 } |
66 public int getPriority() { | |
67 return (thread.priority-Thread.PRIORITY_MIN) * (MAX_PRIORITY-MIN_PRIORITY) / (Thread.PRIORITY_MAX-Thread.PRIORITY_MIN) + MIN_PRIORITY; | |
68 } | |
69 public void setPriority( int newPriority ) { | |
185
987b95661bb9
Added impl for Timer/TimerTask
Frank Benoit <benoit@tionex.de>
parents:
176
diff
changeset
|
70 // assert( MIN_PRIORITY < MAX_PRIORITY ); |
987b95661bb9
Added impl for Timer/TimerTask
Frank Benoit <benoit@tionex.de>
parents:
176
diff
changeset
|
71 // assert( Thread.PRIORITY_MIN < Thread.PRIORITY_MAX ); |
987b95661bb9
Added impl for Timer/TimerTask
Frank Benoit <benoit@tionex.de>
parents:
176
diff
changeset
|
72 auto scaledPrio = (newPriority-MIN_PRIORITY) * (Thread.PRIORITY_MAX-Thread.PRIORITY_MIN) / (MAX_PRIORITY-MIN_PRIORITY) +Thread.PRIORITY_MIN; |
987b95661bb9
Added impl for Timer/TimerTask
Frank Benoit <benoit@tionex.de>
parents:
176
diff
changeset
|
73 Trace.formatln( "JThread.setPriority: scale ({} {} {}) -> ({} {} {})", MIN_PRIORITY, newPriority, MAX_PRIORITY, Thread.PRIORITY_MIN, scaledPrio, Thread.PRIORITY_MAX); |
987b95661bb9
Added impl for Timer/TimerTask
Frank Benoit <benoit@tionex.de>
parents:
176
diff
changeset
|
74 // thread.priority( scaledPrio ); |
167 | 75 } |
76 | |
77 private void internalRun(){ | |
78 getTls().val( this ); | |
79 if( runnable !is null ){ | |
80 runnable.run(); | |
81 } | |
82 else { | |
83 run(); | |
84 } | |
85 } | |
86 | |
87 public bool isAlive(){ | |
88 return thread.isRunning(); | |
89 } | |
90 | |
91 public bool isDaemon() { | |
92 return thread.isDaemon(); | |
93 } | |
94 | |
95 public void join(){ | |
96 thread.join(); | |
97 } | |
98 | |
99 public void setDaemon(bool on) { | |
100 thread.isDaemon(on); | |
101 } | |
102 | |
103 public void setName(String name){ | |
104 thread.name = name; | |
105 } | |
106 public String getName(){ | |
107 return thread.name; | |
108 } | |
109 | |
110 void interrupt() { | |
111 implMissing(__FILE__,__LINE__); | |
112 } | |
113 | |
114 static bool interrupted() { | |
115 implMissing(__FILE__,__LINE__); | |
116 return false; | |
117 } | |
118 | |
119 public void run(){ | |
120 // default impl, do nothing | |
121 } | |
122 public static void sleep( int time ){ | |
123 Thread.sleep(time/1000.0); | |
124 } | |
125 public Thread nativeThread(){ | |
176 | 126 assert(thread); |
167 | 127 return thread; |
128 } | |
129 public override char[] toString(){ | |
130 return "JThread "~thread.name; | |
131 } | |
132 public static void yield(){ | |
133 Thread.yield(); | |
134 } | |
135 } | |
136 |