Skip to content

Commit 0c1a433

Browse files
committed
Virtual thread testing
1 parent d6e63cf commit 0c1a433

File tree

7 files changed

+1171
-88
lines changed

7 files changed

+1171
-88
lines changed

core/src/com/biglybt/core/dht/control/impl/DHTControlImpl.java

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@
9797
private long router_start_time;
9898
private int router_count;
9999

100-
final ThreadPool internal_lookup_pool;
101-
final ThreadPool external_lookup_pool;
102-
final ThreadPool internal_put_pool;
103-
private final ThreadPool external_put_pool;
100+
private final ThreadPoolParent<DhtTask> internal_lookup_pool;
101+
private final ThreadPoolParent<DhtTask> external_lookup_pool;
102+
private final ThreadPoolParent<DhtTask> internal_put_pool;
103+
private final ThreadPoolParent<DhtTask> external_put_pool;
104104

105105
private final Map<HashWrapper, Object> imported_state = new HashMap<>();
106106

@@ -235,14 +235,27 @@
235235
transport.getProtocolVersion(),
236236
logger );
237237

238-
internal_lookup_pool = new ThreadPool("DHTControl:internallookups", lookup_concurrency );
239-
internal_put_pool = new ThreadPool("DHTControl:internalputs", lookup_concurrency );
240-
241-
// external pools queue when full ( as opposed to blocking )
242-
243-
external_lookup_pool = new ThreadPool("DHTControl:externallookups", EXTERNAL_LOOKUP_CONCURRENCY, true );
244-
external_put_pool = new ThreadPool("DHTControl:puts", EXTERNAL_PUT_CONCURRENCY, true );
238+
if ( AEThreadVirtual.areVirtualThreadsAvailable()){
239+
240+
internal_lookup_pool = new ThreadPoolVirtual<DhtTask>("DHTControl:internallookups", lookup_concurrency );
241+
internal_put_pool = new ThreadPoolVirtual<DhtTask>("DHTControl:internalputs", lookup_concurrency );
242+
243+
// external pools queue when full ( as opposed to blocking )
244+
245+
external_lookup_pool = new ThreadPoolVirtual<DhtTask>("DHTControl:externallookups", EXTERNAL_LOOKUP_CONCURRENCY, true );
246+
external_put_pool = new ThreadPoolVirtual<DhtTask>("DHTControl:puts", EXTERNAL_PUT_CONCURRENCY, true );
245247

248+
}else{
249+
250+
internal_lookup_pool = new ThreadPool<DhtTask>("DHTControl:internallookups", lookup_concurrency );
251+
internal_put_pool = new ThreadPool<DhtTask>("DHTControl:internalputs", lookup_concurrency );
252+
253+
// external pools queue when full ( as opposed to blocking )
254+
255+
external_lookup_pool = new ThreadPool<DhtTask>("DHTControl:externallookups", EXTERNAL_LOOKUP_CONCURRENCY, true );
256+
external_put_pool = new ThreadPool<DhtTask>("DHTControl:puts", EXTERNAL_PUT_CONCURRENCY, true );
257+
}
258+
246259
createRouter( transport.getLocalContact());
247260

248261
node_id_byte_count = router.getID().length;
@@ -962,7 +975,7 @@
962975

963976
// we don't want this to be blocking as it'll stuff the stats
964977

965-
external_lookup_pool.run(
978+
external_lookup_pool.runTask(
966979
new DhtTask(external_lookup_pool)
967980
{
968981
private byte[] target = {};
@@ -1072,7 +1085,7 @@
10721085

10731086
protected void
10741087
put(
1075-
ThreadPool thread_pool,
1088+
ThreadPoolParent<DhtTask> thread_pool,
10761089
boolean high_priority,
10771090
byte[] initial_encoded_key,
10781091
String description,
@@ -1099,7 +1112,7 @@
10991112

11001113
protected void
11011114
put(
1102-
final ThreadPool thread_pool,
1115+
final ThreadPoolParent<DhtTask> thread_pool,
11031116
final boolean high_priority,
11041117
final byte[] initial_encoded_key,
11051118
final String description,
@@ -1413,7 +1426,7 @@
14131426

14141427
protected void
14151428
put(
1416-
final ThreadPool thread_pool,
1429+
final ThreadPoolParent<DhtTask> thread_pool,
14171430
final boolean high_priority,
14181431
byte[][] initial_encoded_keys,
14191432
final String description,
@@ -2371,7 +2384,7 @@
23712384

23722385
protected DhtTask
23732386
lookup(
2374-
final ThreadPool thread_pool,
2387+
ThreadPoolParent<DhtTask> thread_pool,
23752388
boolean high_priority,
23762389
final byte[] _lookup_id,
23772390
final String description,
@@ -2588,14 +2601,22 @@ synchronized void release()
25882601
{
25892602
//System.out.println("release-start");
25902603
runningState = 1;
2591-
new AEThread2("DHT lookup runner",true) {
2592-
@Override
2593-
public void run() {
2594-
thread_pool.registerThreadAsChild(worker);
2595-
lookupSteps();
2596-
thread_pool.deregisterThreadAsChild(worker);
2597-
}
2598-
}.start();
2604+
2605+
if ( AEThreadVirtual.areVirtualThreadsAvailable()){
2606+
2607+
AEThreadVirtual.run("DHT lookup runner", this::lookupSteps );
2608+
2609+
}else{
2610+
2611+
new AEThread2("DHT lookup runner",true) {
2612+
@Override
2613+
public void run() {
2614+
thread_pool.registerThreadAsChild(worker);
2615+
lookupSteps();
2616+
thread_pool.deregisterThreadAsChild(worker);
2617+
}
2618+
}.start();
2619+
}
25992620
}
26002621
}
26012622

@@ -3139,7 +3160,7 @@ public int compare(
31393160
}
31403161
};
31413162

3142-
thread_pool.run( task, high_priority, true);
3163+
thread_pool.runTask( task, high_priority, true);
31433164

31443165
return( task );
31453166
}
@@ -5623,7 +5644,7 @@ public int compare(
56235644

56245645
protected
56255646
DhtTask(
5626-
ThreadPool thread_pool )
5647+
ThreadPoolParent<DhtTask> thread_pool )
56275648
{
56285649
activity = new controlActivity( thread_pool, this );
56295650

@@ -5819,13 +5840,13 @@ public int compare(
58195840
controlActivity
58205841
implements DHTControlActivity
58215842
{
5822-
protected final ThreadPool tp;
5823-
protected final DhtTask task;
5824-
protected final int type;
5843+
protected final ThreadPoolParent<DhtTask> tp;
5844+
protected final DhtTask task;
5845+
protected final int type;
58255846

58265847
protected
58275848
controlActivity(
5828-
ThreadPool _tp,
5849+
ThreadPoolParent<DhtTask> _tp,
58295850
DhtTask _task )
58305851
{
58315852
tp = _tp;

core/src/com/biglybt/core/util/AEThreadVirtual.java

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public class
3939

4040
Class<?> ThreadBuilder = Class.forName( "java.lang.Thread$Builder" );
4141

42-
name = ThreadBuilder.getMethod( "name", String.class );
43-
start = ThreadBuilder.getMethod( "start", Runnable.class );
42+
name = ThreadBuilder.getMethod( "name", String.class );
43+
start = ThreadBuilder.getMethod( "start", Runnable.class );
4444

4545
Object thread = ofv.invoke( null );
4646

@@ -68,14 +68,14 @@ public class
6868

6969
}catch( Throwable e ){
7070

71-
ofv = null;
72-
name = null;
73-
start = null;
71+
ofv = null;
72+
name = null;
73+
start = null;
7474
}
7575

76-
ofVirtual = ofv;
77-
ThreadBuilder_name = name;
78-
ThreadBuilder_start = start;
76+
ofVirtual = ofv;
77+
ThreadBuilder_name = name;
78+
ThreadBuilder_start = start;
7979

8080
available = ofVirtual != null;
8181
}
@@ -86,7 +86,16 @@ public class
8686
return( available );
8787
}
8888

89-
private final String name;
89+
public static void
90+
run(
91+
String name,
92+
Runnable r )
93+
{
94+
new AEThreadVirtual(name).start(r);
95+
}
96+
97+
private String name;
98+
private volatile Object thread;
9099

91100
public
92101
AEThreadVirtual(
@@ -108,6 +117,31 @@ public class
108117
}
109118
}
110119

120+
public void
121+
setName(
122+
String _name )
123+
{
124+
name = _name;
125+
126+
if ( thread != null ){
127+
128+
if ( thread instanceof AEThread2 ){
129+
130+
((AEThread2)thread).setName(_name);
131+
132+
}else{
133+
134+
try{
135+
136+
ThreadBuilder_name.invoke( thread, _name );
137+
138+
}catch( Throwable e ){
139+
140+
}
141+
}
142+
}
143+
}
144+
111145
public void
112146
start(
113147
Runnable runnable )
@@ -116,7 +150,7 @@ public class
116150

117151
try{
118152

119-
Object thread = ofVirtual.invoke( null );
153+
thread = ofVirtual.invoke( null );
120154

121155
ThreadBuilder_name.invoke( thread, name );
122156

@@ -129,13 +163,34 @@ public class
129163
}
130164
}
131165

132-
new AEThread2( name, true )
166+
AEThread2 t =
167+
new AEThread2( name, true )
133168
{
134169
public void
135170
run()
136171
{
137172
runnable.run();
138173
}
139-
}.start();
174+
};
175+
176+
thread = t;
177+
178+
t.start();
179+
}
180+
181+
public void
182+
interrupt()
183+
{
184+
if ( thread != null ){
185+
186+
if ( thread instanceof AEThread2 ){
187+
188+
((AEThread2)thread).interrupt();
189+
190+
}else{
191+
192+
((Thread)thread).interrupt();
193+
}
194+
}
140195
}
141196
}

0 commit comments

Comments
 (0)