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