Mercurial > projects > dwt-addons
annotate dwtx/dwtxhelper/JThread.d @ 176:ed80c5c2b550
rm unneeded sync
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 12 Sep 2008 11:25:56 +0200 |
parents | 9e7e1a8bc813 |
children | 987b95661bb9 |
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 ){ |
176 | 57 // no synchronized needed |
58 res = new JThread(); | |
59 res.thread = Thread.getThis(); | |
60 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
|
61 } |
176 | 62 assert( res ); |
175
9e7e1a8bc813
get a JThread for a thread that was just a Thread.
Frank Benoit <benoit@tionex.de>
parents:
167
diff
changeset
|
63 return res; |
167 | 64 } |
65 public int getPriority() { | |
66 return (thread.priority-Thread.PRIORITY_MIN) * (MAX_PRIORITY-MIN_PRIORITY) / (Thread.PRIORITY_MAX-Thread.PRIORITY_MIN) + MIN_PRIORITY; | |
67 } | |
68 public void setPriority( int newPriority ) { | |
69 thread.priority( (newPriority-MIN_PRIORITY) * (Thread.PRIORITY_MAX-Thread.PRIORITY_MIN) / (MAX_PRIORITY-MIN_PRIORITY) +Thread.PRIORITY_MIN ); | |
70 } | |
71 | |
72 private void internalRun(){ | |
73 getTls().val( this ); | |
74 if( runnable !is null ){ | |
75 runnable.run(); | |
76 } | |
77 else { | |
78 run(); | |
79 } | |
80 } | |
81 | |
82 public bool isAlive(){ | |
83 return thread.isRunning(); | |
84 } | |
85 | |
86 public bool isDaemon() { | |
87 return thread.isDaemon(); | |
88 } | |
89 | |
90 public void join(){ | |
91 thread.join(); | |
92 } | |
93 | |
94 public void setDaemon(bool on) { | |
95 thread.isDaemon(on); | |
96 } | |
97 | |
98 public void setName(String name){ | |
99 thread.name = name; | |
100 } | |
101 public String getName(){ | |
102 return thread.name; | |
103 } | |
104 | |
105 void interrupt() { | |
106 implMissing(__FILE__,__LINE__); | |
107 } | |
108 | |
109 static bool interrupted() { | |
110 implMissing(__FILE__,__LINE__); | |
111 return false; | |
112 } | |
113 | |
114 public void run(){ | |
115 // default impl, do nothing | |
116 } | |
117 public static void sleep( int time ){ | |
118 Thread.sleep(time/1000.0); | |
119 } | |
120 public Thread nativeThread(){ | |
176 | 121 assert(thread); |
167 | 122 return thread; |
123 } | |
124 public override char[] toString(){ | |
125 return "JThread "~thread.name; | |
126 } | |
127 public static void yield(){ | |
128 Thread.yield(); | |
129 } | |
130 } | |
131 |