18
18
import org .truffleruby .RubyLanguage ;
19
19
import org .truffleruby .SuppressFBWarnings ;
20
20
import org .truffleruby .cext .ValueWrapperManagerFactory .AllocateHandleNodeGen ;
21
- import org .truffleruby .cext .ValueWrapperManagerFactory .GetHandleBlockHolderNodeGen ;
22
21
import org .truffleruby .extra .ffi .Pointer ;
23
22
import org .truffleruby .language .ImmutableRubyObject ;
24
23
import org .truffleruby .language .NotProvided ;
@@ -60,35 +59,8 @@ public class ValueWrapperManager {
60
59
61
60
private volatile HandleBlockWeakReference [] blockMap = new HandleBlockWeakReference [0 ];
62
61
63
- private final ThreadLocal <HandleThreadData > threadBlocks ;
64
-
65
- private final RubyContext context ;
66
-
67
- public ValueWrapperManager (RubyContext context ) {
68
- this .context = context ;
69
- this .threadBlocks = ThreadLocal .withInitial (this ::makeThreadData );
70
- }
71
-
72
- public HandleThreadData makeThreadData () {
73
- HandleThreadData threadData = new HandleThreadData ();
74
- HandleBlockHolder holder = threadData .holder ;
75
- context .getFinalizationService ().addFinalizer (
76
- context ,
77
- threadData ,
78
- ValueWrapperManager .class ,
79
- () -> context .getMarkingService ().queueForMarking (holder .handleBlock ),
80
- null );
81
- return threadData ;
82
- }
83
-
84
- @ TruffleBoundary
85
- public HandleThreadData getBlockHolder () {
86
- return threadBlocks .get ();
87
- }
88
-
89
- @ TruffleBoundary
90
- public void cleanupBlockHolder () {
91
- threadBlocks .remove ();
62
+ public static HandleBlockHolder getBlockHolder (RubyContext context , RubyLanguage language ) {
63
+ return language .getCurrentThread ().getCurrentFiber ().handleData ;
92
64
}
93
65
94
66
/* We keep a map of long wrappers that have been generated because various C extensions assume that any given fixnum
@@ -102,7 +74,7 @@ public ValueWrapper doubleWrapper(double value) {
102
74
}
103
75
104
76
@ TruffleBoundary
105
- public synchronized void addToBlockMap (HandleBlock block , RubyLanguage language ) {
77
+ public synchronized void addToBlockMap (HandleBlock block , RubyContext context , RubyLanguage language ) {
106
78
int blockIndex = block .getIndex ();
107
79
long blockBase = block .getBase ();
108
80
HandleBlockAllocator allocator = language .handleBlockAllocator ;
@@ -117,7 +89,7 @@ public synchronized void addToBlockMap(HandleBlock block, RubyLanguage language)
117
89
}
118
90
119
91
@ TruffleBoundary
120
- public void addToSharedBlockMap (HandleBlock block , RubyLanguage language ) {
92
+ public void addToSharedBlockMap (HandleBlock block , RubyContext context , RubyLanguage language ) {
121
93
synchronized (language ) {
122
94
int blockIndex = block .getIndex ();
123
95
long blockBase = block .getBase ();
@@ -186,6 +158,11 @@ public void freeAllBlocksInMap(RubyLanguage language) {
186
158
}
187
159
}
188
160
161
+ public void cleanup (RubyContext context , HandleBlockHolder holder ) {
162
+ context .getMarkingService ().queueForMarking (holder .handleBlock );
163
+ holder .handleBlock = null ;
164
+ }
165
+
189
166
protected static class FreeHandleBlock {
190
167
public final long start ;
191
168
public final FreeHandleBlock next ;
@@ -299,86 +276,43 @@ public static final class HandleBlockWeakReference extends WeakReference<HandleB
299
276
}
300
277
}
301
278
302
- protected static class HandleBlockHolder {
279
+ public static class HandleBlockHolder {
303
280
protected HandleBlock handleBlock = null ;
304
281
protected HandleBlock sharedHandleBlock = null ;
305
282
}
306
283
307
- protected static class HandleThreadData {
308
-
309
- private final HandleBlockHolder holder = new HandleBlockHolder ();
310
-
311
- public HandleBlock currentBlock () {
312
- return holder .handleBlock ;
313
- }
314
-
315
- public HandleBlock currentSharedBlock () {
316
- return holder .sharedHandleBlock ;
317
- }
318
-
319
- public HandleBlock makeNewBlock (RubyContext context , HandleBlockAllocator allocator ) {
320
- return (holder .handleBlock = new HandleBlock (context , allocator ));
321
- }
322
-
323
- public HandleBlock makeNewSharedBlock (RubyContext context , HandleBlockAllocator allocator ) {
324
- return (holder .sharedHandleBlock = new HandleBlock (context , allocator ));
325
- }
326
- }
327
-
328
- @ GenerateUncached
329
- public abstract static class GetHandleBlockHolderNode extends RubyBaseNode {
330
-
331
- public abstract HandleThreadData execute (ValueWrapper wrapper );
332
-
333
- @ Specialization (guards = "cachedThread == currentJavaThread(wrapper)" , limit = "getCacheLimit()" )
334
- protected HandleThreadData getHolderOnKnownThread (ValueWrapper wrapper ,
335
- @ Cached ("currentJavaThread(wrapper)" ) Thread cachedThread ,
336
- @ Cached ("getBlockHolder(wrapper)" ) HandleThreadData threadData ) {
337
- return threadData ;
338
- }
339
-
340
- @ Specialization (replaces = "getHolderOnKnownThread" )
341
- protected HandleThreadData getBlockHolder (ValueWrapper wrapper ) {
342
- return getContext ().getValueWrapperManager ().getBlockHolder ();
343
- }
344
-
345
- protected static Thread currentJavaThread (ValueWrapper wrapper ) {
346
- return Thread .currentThread ();
347
- }
348
-
349
- public int getCacheLimit () {
350
- return getLanguage ().options .THREAD_CACHE ;
351
- }
352
-
353
- public static GetHandleBlockHolderNode create () {
354
- return GetHandleBlockHolderNodeGen .create ();
355
- }
356
- }
357
-
358
284
@ GenerateUncached
359
285
public abstract static class AllocateHandleNode extends RubyBaseNode {
360
286
361
287
public abstract long execute (ValueWrapper wrapper );
362
288
363
289
@ Specialization (guards = "!isSharedObject(wrapper)" )
364
- protected long allocateHandleOnKnownThread (ValueWrapper wrapper ,
365
- @ Cached GetHandleBlockHolderNode getBlockHolderNode ) {
366
- return allocateHandle (wrapper , getContext (), getLanguage (), getBlockHolderNode .execute (wrapper ), false );
290
+ protected long allocateHandleOnKnownThread (ValueWrapper wrapper ) {
291
+ return allocateHandle (
292
+ wrapper ,
293
+ getContext (),
294
+ getLanguage (),
295
+ getBlockHolder (getContext (), getLanguage ()),
296
+ false );
367
297
}
368
298
369
299
@ Specialization (guards = "isSharedObject(wrapper)" )
370
- protected long allocateSharedHandleOnKnownThread (ValueWrapper wrapper ,
371
- @ Cached GetHandleBlockHolderNode getBlockHolderNode ) {
372
- return allocateHandle (wrapper , getContext (), getLanguage (), getBlockHolderNode .execute (wrapper ), true );
300
+ protected long allocateSharedHandleOnKnownThread (ValueWrapper wrapper ) {
301
+ return allocateHandle (
302
+ wrapper ,
303
+ getContext (),
304
+ getLanguage (),
305
+ getBlockHolder (getContext (), getLanguage ()),
306
+ true );
373
307
}
374
308
375
309
protected static long allocateHandle (ValueWrapper wrapper , RubyContext context , RubyLanguage language ,
376
- HandleThreadData threadData , boolean shared ) {
310
+ HandleBlockHolder holder , boolean shared ) {
377
311
HandleBlock block ;
378
312
if (shared ) {
379
- block = threadData . holder .sharedHandleBlock ;
313
+ block = holder .sharedHandleBlock ;
380
314
} else {
381
- block = threadData . holder .handleBlock ;
315
+ block = holder .handleBlock ;
382
316
}
383
317
384
318
if (context .getOptions ().CEXTS_TO_NATIVE_STATS ) {
@@ -390,13 +324,11 @@ protected static long allocateHandle(ValueWrapper wrapper, RubyContext context,
390
324
context .getMarkingService ().queueForMarking (block );
391
325
}
392
326
if (shared ) {
393
- block = threadData
394
- .makeNewSharedBlock (context , language .handleBlockAllocator );
395
- context .getValueWrapperManager ().addToSharedBlockMap (block , language );
327
+ block = (holder .sharedHandleBlock = new HandleBlock (context , language .handleBlockAllocator ));
328
+ context .getValueWrapperManager ().addToSharedBlockMap (block , context , language );
396
329
} else {
397
- block = threadData
398
- .makeNewBlock (context , language .handleBlockAllocator );
399
- context .getValueWrapperManager ().addToBlockMap (block , language );
330
+ block = (holder .handleBlock = new HandleBlock (context , language .handleBlockAllocator ));
331
+ context .getValueWrapperManager ().addToBlockMap (block , context , language );
400
332
}
401
333
402
334
}
0 commit comments