rmagick 7.2.0 → 7.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +124 -0
- data/README.md +2 -1
- data/ext/RMagick/extconf.rb +5 -2
- data/ext/RMagick/rmagick.cpp +55 -58
- data/ext/RMagick/rmagick.h +97 -55
- data/ext/RMagick/rmagick_gvl.cpp +553 -375
- data/ext/RMagick/rmagick_gvl.hpp +15 -10
- data/ext/RMagick/rmdraw.cpp +654 -277
- data/ext/RMagick/rmenum.cpp +59 -19
- data/ext/RMagick/rmfill.cpp +131 -40
- data/ext/RMagick/rmilist.cpp +316 -166
- data/ext/RMagick/rmimage.cpp +2519 -1051
- data/ext/RMagick/rminfo.cpp +250 -107
- data/ext/RMagick/rmkinfo.cpp +87 -29
- data/ext/RMagick/rmmain.cpp +742 -571
- data/ext/RMagick/rmmontage.cpp +121 -46
- data/ext/RMagick/rmpixel.cpp +108 -108
- data/ext/RMagick/rmstruct.cpp +147 -98
- data/ext/RMagick/rmutil.cpp +386 -95
- data/lib/rmagick/version.rb +1 -1
- data/lib/rmagick_internal.rb +69 -19
- data/sig/rmagick/_image_common_methods.rbs +1 -1
- data/sig/rmagick/enum.rbs +2 -0
- data/sig/rmagick/image.rbs +1 -0
- data/sig/rmagick/image_list.rbs +1 -0
- data/sig/rmagick/optional_method_arguments.rbs +1 -1
- metadata +2 -2
data/ext/RMagick/rmagick_gvl.cpp
CHANGED
|
@@ -8,12 +8,17 @@
|
|
|
8
8
|
|
|
9
9
|
#include "rmagick.h"
|
|
10
10
|
#include "ruby/ractor.h"
|
|
11
|
-
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
12
11
|
#include <atomic>
|
|
12
|
+
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
13
13
|
#include <cstddef>
|
|
14
|
-
#if defined(HAVE_WORKING_FORK)
|
|
15
|
-
#include <pthread.h>
|
|
16
14
|
#endif
|
|
15
|
+
// TruffleRuby cannot fork, and has no ruby_thread_has_gvl_p
|
|
16
|
+
#if defined(HAVE_WORKING_FORK) && !defined(TRUFFLERUBY)
|
|
17
|
+
#define RMAGICK_ATFORK 1
|
|
18
|
+
#include <pthread.h>
|
|
19
|
+
|
|
20
|
+
// ruby/thread.h declares it from Ruby 4.0
|
|
21
|
+
extern "C" int ruby_thread_has_gvl_p(void);
|
|
17
22
|
#endif
|
|
18
23
|
|
|
19
24
|
|
|
@@ -42,37 +47,54 @@
|
|
|
42
47
|
* 2. Other fibers on the same thread run while the call is in flight. While a
|
|
43
48
|
* call reads an object, changing or destroying it raises. While a call
|
|
44
49
|
* changes an image, any use of it raises. Nothing waits, so no fiber is
|
|
45
|
-
* suspended while it holds a pointer that another fiber could free.
|
|
50
|
+
* suspended while it holds a pointer that another fiber could free. An
|
|
51
|
+
* Image, Info, KernelInfo, Draw or Montage keeps this state itself, so a
|
|
52
|
+
* call in another Ractor that shares a frozen object sees it too.
|
|
46
53
|
*
|
|
47
54
|
* ImageMagick reads some images through a working area of the pixel cache
|
|
48
55
|
* that every thread outside OpenMP shares, so while an offloaded call uses an
|
|
49
|
-
* image, no other call can use the image
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
56
|
+
* image, no other call can use the image. No two Image objects share a pixel
|
|
57
|
+
* cache: an Image object takes one of its own, and a copy that RMagick makes
|
|
58
|
+
* for one call shares it only with the image that the call marks. A call that
|
|
59
|
+
* stays on its thread does not take the image for itself, so an image must
|
|
60
|
+
* not be used by two threads without a scheduler at once. RMagick itself
|
|
61
|
+
* reads and writes pixels through a cache view of its own.
|
|
53
62
|
*
|
|
54
63
|
* The child of a fork keeps none of the workers of the parent, so it drops
|
|
55
64
|
* the marks of every fiber but the one that forked, whose calls go on in the
|
|
56
65
|
* child, and changes back what the dropped calls changed for themselves, such
|
|
57
66
|
* as a channel mask or the links of a list. A dropped call that is resumed
|
|
58
67
|
* there raises instead of running. An image that a call was changing at the
|
|
59
|
-
* fork is in an undefined state in the child.
|
|
68
|
+
* fork is in an undefined state in the child. The state of an object holds
|
|
69
|
+
* the fork generation it was taken in, so the child ignores the marks of the
|
|
70
|
+
* calls in other Ractors without finding them. Those calls change only
|
|
71
|
+
* objects that are not shareable, which the child cannot reach.
|
|
60
72
|
*
|
|
61
73
|
* Some cases are not handled. A scheduler must resume a fiber that waits in a
|
|
62
74
|
* call: a fiber collected while it waits leaves its marks behind, and the next
|
|
63
75
|
* fork changes back objects that may be gone. A worker that holds a lock of
|
|
64
76
|
* ImageMagick at the fork can leave the cleanup of a dropped call in the child
|
|
65
|
-
* waiting for good.
|
|
66
|
-
* rm_gvl_call, so they are changed back only when the call returns.
|
|
77
|
+
* waiting for good.
|
|
67
78
|
*
|
|
68
79
|
* Without a scheduler that offloads, or on Ruby before 4.0, rm_gvl_call runs
|
|
69
80
|
* the function with rb_thread_call_without_gvl. An interrupt that arrives
|
|
70
81
|
* during the function, such as Thread#raise or Timeout, is raised after the
|
|
71
82
|
* function returns, so what the caller registered is released then too.
|
|
72
|
-
* Interrupts are deferred until then because
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
83
|
+
* Interrupts are deferred until then because, where ImageMagick allocates
|
|
84
|
+
* with xmalloc (Magick::MANAGED_MEMORY without malloc_usable_size or a similar
|
|
85
|
+
* function), a GC that xmalloc starts in the function takes the GVL back and
|
|
86
|
+
* would raise them through ImageMagick. An exception raised by a trap handler
|
|
87
|
+
* there is not deferred.
|
|
88
|
+
*
|
|
89
|
+
* Ruby code still runs while such a call is in flight: a trap handler or a
|
|
90
|
+
* finalizer before and after the function and during a GC that it starts, and
|
|
91
|
+
* other threads while it runs. The call is linked in its Ractor until it
|
|
92
|
+
* returns, and like a call that is offloaded, it keeps that code from changing
|
|
93
|
+
* or destroying the objects it uses, and from using the objects it changes.
|
|
94
|
+
* Other Ractors share only frozen objects, which they cannot change. The
|
|
95
|
+
* child of a fork drops the calls of the other threads. The link is on the
|
|
96
|
+
* stack of the call, so that code must not leave the fiber of the call
|
|
97
|
+
* suspended for good.
|
|
76
98
|
*/
|
|
77
99
|
|
|
78
100
|
typedef enum
|
|
@@ -147,24 +169,117 @@ call_deferring_interrupts(VALUE arg)
|
|
|
147
169
|
return rb_block_call(rb_cThread, id_handle_interrupt, 1, &interrupt_mask, call_without_gvl_block, arg);
|
|
148
170
|
}
|
|
149
171
|
|
|
150
|
-
|
|
172
|
+
static void raise_in_use(void) ATTRIBUTE_NORETURN;
|
|
151
173
|
|
|
152
|
-
//
|
|
153
|
-
|
|
174
|
+
// A call that stays on its thread, linked in its Ractor while it is in flight
|
|
175
|
+
typedef struct pinned_call
|
|
176
|
+
{
|
|
177
|
+
const rm_gvl_call *call;
|
|
178
|
+
VALUE thread;
|
|
179
|
+
struct pinned_call *next;
|
|
180
|
+
} pinned_call_t;
|
|
154
181
|
|
|
155
|
-
|
|
182
|
+
// The calls in flight in a Ractor
|
|
183
|
+
typedef struct offloaded
|
|
184
|
+
{
|
|
185
|
+
struct offload_frame *frames;
|
|
186
|
+
pinned_call_t *pinned;
|
|
187
|
+
} offloaded_t;
|
|
156
188
|
|
|
157
189
|
static rb_ractor_local_key_t offloaded_key;
|
|
158
190
|
|
|
159
|
-
//
|
|
191
|
+
// The entry of the main Ractor, the only one that can fork
|
|
192
|
+
static offloaded_t *main_entry;
|
|
193
|
+
|
|
194
|
+
// Calls that stay on their thread in all Ractors, so that checking an object
|
|
195
|
+
// looks for them only when there are some
|
|
196
|
+
static std::atomic<unsigned int> pinned_in_flight(0);
|
|
197
|
+
|
|
198
|
+
static void
|
|
199
|
+
offloaded_free(void *ptr)
|
|
200
|
+
{
|
|
201
|
+
xfree(ptr);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
static const struct rb_ractor_local_storage_type offloaded_type = { NULL, offloaded_free };
|
|
205
|
+
|
|
206
|
+
static offloaded_t *
|
|
207
|
+
offloaded_entry(void)
|
|
208
|
+
{
|
|
209
|
+
offloaded_t *entry = (offloaded_t *)rb_ractor_local_storage_ptr(offloaded_key);
|
|
210
|
+
|
|
211
|
+
if (!entry)
|
|
212
|
+
{
|
|
213
|
+
entry = ALLOC(offloaded_t);
|
|
214
|
+
entry->frames = NULL;
|
|
215
|
+
entry->pinned = NULL;
|
|
216
|
+
rb_ractor_local_storage_ptr_set(offloaded_key, entry);
|
|
217
|
+
}
|
|
218
|
+
return entry;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Link a call that stays on its thread
|
|
222
|
+
static void
|
|
223
|
+
pin_call(offloaded_t *entry, pinned_call_t *pin)
|
|
224
|
+
{
|
|
225
|
+
pin->next = entry->pinned;
|
|
226
|
+
entry->pinned = pin;
|
|
227
|
+
pinned_in_flight.fetch_add(1, std::memory_order_relaxed);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static void
|
|
231
|
+
unpin_call(offloaded_t *entry, pinned_call_t *pin)
|
|
232
|
+
{
|
|
233
|
+
for (pinned_call_t **link = &entry->pinned; *link; link = &(*link)->next)
|
|
234
|
+
{
|
|
235
|
+
if (*link == pin)
|
|
236
|
+
{
|
|
237
|
+
*link = pin->next;
|
|
238
|
+
pinned_in_flight.fetch_sub(1, std::memory_order_relaxed);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Whether a call in this Ractor that stays on its thread uses obj, or changes
|
|
245
|
+
// it if update is true
|
|
246
|
+
static bool
|
|
247
|
+
pinned(VALUE obj, bool update)
|
|
248
|
+
{
|
|
249
|
+
offloaded_t *entry;
|
|
250
|
+
|
|
251
|
+
if (pinned_in_flight.load(std::memory_order_relaxed) == 0)
|
|
252
|
+
{
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
entry = (offloaded_t *)rb_ractor_local_storage_ptr(offloaded_key);
|
|
256
|
+
for (pinned_call_t *pin = entry ? entry->pinned : NULL; pin; pin = pin->next)
|
|
257
|
+
{
|
|
258
|
+
if (pin->call->uses(obj, update))
|
|
259
|
+
{
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
267
|
+
|
|
268
|
+
// Calls in flight in all Ractors, so that a call that stays on its thread
|
|
269
|
+
// checks its images only when there are some
|
|
160
270
|
static std::atomic<unsigned int> offloads_in_flight(0);
|
|
161
271
|
|
|
162
|
-
// Forks seen by this process; a
|
|
272
|
+
// Forks seen by this process; a state from an earlier fork is free
|
|
163
273
|
static unsigned int fork_generation;
|
|
164
274
|
|
|
275
|
+
// The state of an object holds the fork generation above the number of calls
|
|
276
|
+
// that read it, or STATE_UPDATING
|
|
277
|
+
#define STATE_UPDATING UINT_MAX
|
|
278
|
+
|
|
279
|
+
// A mark on the offload state of an object
|
|
165
280
|
typedef struct
|
|
166
281
|
{
|
|
167
|
-
|
|
282
|
+
rm_offload_state_t *state;
|
|
168
283
|
OffloadMode mode;
|
|
169
284
|
bool image;
|
|
170
285
|
} offload_mark_t;
|
|
@@ -201,88 +316,56 @@ typedef struct offload_frame
|
|
|
201
316
|
struct offload_frame *next;
|
|
202
317
|
} offload_frame_t;
|
|
203
318
|
|
|
204
|
-
//
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
typedef struct
|
|
319
|
+
// The number of calls that read an object, or STATE_UPDATING
|
|
320
|
+
static unsigned int
|
|
321
|
+
state_count(uint64_t value)
|
|
208
322
|
{
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// The entry of the main Ractor, the only one that can fork
|
|
215
|
-
static offloaded_t *main_entry;
|
|
216
|
-
|
|
217
|
-
static void
|
|
218
|
-
offloaded_free(void *ptr)
|
|
219
|
-
{
|
|
220
|
-
offloaded_t *entry = (offloaded_t *)ptr;
|
|
221
|
-
|
|
222
|
-
st_free_table(entry->table);
|
|
223
|
-
xfree(entry);
|
|
323
|
+
if ((unsigned int)(value >> 32) != fork_generation)
|
|
324
|
+
{
|
|
325
|
+
return 0;
|
|
326
|
+
}
|
|
327
|
+
return (unsigned int)value;
|
|
224
328
|
}
|
|
225
329
|
|
|
226
|
-
static
|
|
227
|
-
|
|
228
|
-
static void
|
|
229
|
-
mark_insert(st_table *table, const void *ptr, OffloadMode mode)
|
|
330
|
+
static uint64_t
|
|
331
|
+
state_value(unsigned int count)
|
|
230
332
|
{
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
st_lookup(table, (st_data_t)ptr, &state);
|
|
234
|
-
st_insert(table, (st_data_t)ptr, mode == OffloadUpdate ? OFFLOAD_UPDATING : state + 1);
|
|
333
|
+
return ((uint64_t)fork_generation << 32) | count;
|
|
235
334
|
}
|
|
236
335
|
|
|
237
|
-
|
|
238
|
-
|
|
336
|
+
// Take a state for a call: a call reads an object that other calls only read,
|
|
337
|
+
// and changes one that no call uses. A call that uses an image is the only one.
|
|
338
|
+
static bool
|
|
339
|
+
state_acquire(rm_offload_state_t *state, OffloadMode mode, bool image)
|
|
239
340
|
{
|
|
240
|
-
|
|
241
|
-
st_data_t state = 0;
|
|
341
|
+
uint64_t value = state->load(std::memory_order_relaxed);
|
|
242
342
|
|
|
243
|
-
|
|
244
|
-
if (state == OFFLOAD_UPDATING || state <= 1)
|
|
245
|
-
{
|
|
246
|
-
st_delete(table, &key, NULL);
|
|
247
|
-
}
|
|
248
|
-
else
|
|
343
|
+
while (true)
|
|
249
344
|
{
|
|
250
|
-
|
|
345
|
+
unsigned int count = state_count(value);
|
|
346
|
+
|
|
347
|
+
if (count == STATE_UPDATING || ((mode == OffloadUpdate || image) && count != 0))
|
|
348
|
+
{
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
if (state->compare_exchange_weak(value, state_value(mode == OffloadUpdate ? STATE_UPDATING : count + 1), std::memory_order_acq_rel))
|
|
352
|
+
{
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
251
355
|
}
|
|
252
356
|
}
|
|
253
357
|
|
|
254
|
-
static
|
|
255
|
-
|
|
358
|
+
static void
|
|
359
|
+
state_release(rm_offload_state_t *state, OffloadMode mode)
|
|
256
360
|
{
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
if (!entry)
|
|
361
|
+
if (mode == OffloadUpdate)
|
|
260
362
|
{
|
|
261
|
-
|
|
262
|
-
entry->table = st_init_numtable();
|
|
263
|
-
entry->generation = fork_generation;
|
|
264
|
-
entry->frames = NULL;
|
|
265
|
-
rb_ractor_local_storage_ptr_set(offloaded_key, entry);
|
|
363
|
+
state->store(0, std::memory_order_release);
|
|
266
364
|
}
|
|
267
|
-
else
|
|
365
|
+
else
|
|
268
366
|
{
|
|
269
|
-
|
|
270
|
-
entry->generation = fork_generation;
|
|
271
|
-
for (offload_frame_t *frame = entry->frames; frame; frame = frame->next)
|
|
272
|
-
{
|
|
273
|
-
for (long i = 0; i < frame->nmarks; i++)
|
|
274
|
-
{
|
|
275
|
-
mark_insert(entry->table, frame->marks[i].key, frame->marks[i].mode);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
367
|
+
state->fetch_sub(1, std::memory_order_release);
|
|
278
368
|
}
|
|
279
|
-
return entry;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
static st_table *
|
|
283
|
-
offloaded(void)
|
|
284
|
-
{
|
|
285
|
-
return offloaded_entry()->table;
|
|
286
369
|
}
|
|
287
370
|
|
|
288
371
|
static void
|
|
@@ -305,7 +388,7 @@ frame_dropped(const offload_frame_t *frame)
|
|
|
305
388
|
}
|
|
306
389
|
|
|
307
390
|
static void
|
|
308
|
-
|
|
391
|
+
frame_remove(offloaded_t *entry, offload_frame_t *frame)
|
|
309
392
|
{
|
|
310
393
|
if (frame->prev)
|
|
311
394
|
{
|
|
@@ -321,10 +404,15 @@ frame_unlink(offloaded_t *entry, offload_frame_t *frame)
|
|
|
321
404
|
}
|
|
322
405
|
frame->linked = false;
|
|
323
406
|
}
|
|
407
|
+
#endif
|
|
324
408
|
|
|
325
|
-
#if defined(
|
|
326
|
-
// The fiber that
|
|
409
|
+
#if defined(RMAGICK_ATFORK)
|
|
410
|
+
// The thread and the fiber that fork, noted before the fork, and whether the
|
|
411
|
+
// thread holds the GVL
|
|
412
|
+
static thread_local VALUE fork_thread;
|
|
413
|
+
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
327
414
|
static thread_local VALUE fork_fiber;
|
|
415
|
+
#endif
|
|
328
416
|
static thread_local bool fork_with_gvl;
|
|
329
417
|
|
|
330
418
|
// rb_fiber_current allocates the object of a root fiber on its first call,
|
|
@@ -332,73 +420,83 @@ static thread_local bool fork_with_gvl;
|
|
|
332
420
|
static void
|
|
333
421
|
atfork_prepare(void)
|
|
334
422
|
{
|
|
335
|
-
VALUE thread;
|
|
336
|
-
|
|
337
|
-
fork_fiber = 0;
|
|
338
423
|
fork_with_gvl = ruby_thread_has_gvl_p() && rb_ractor_local_storage_ptr(offloaded_key) == main_entry;
|
|
339
424
|
if (!fork_with_gvl)
|
|
340
425
|
{
|
|
341
426
|
return;
|
|
342
427
|
}
|
|
343
|
-
|
|
428
|
+
fork_thread = rb_thread_current();
|
|
429
|
+
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
430
|
+
fork_fiber = 0;
|
|
344
431
|
for (offload_frame_t *frame = main_entry->frames; frame; frame = frame->next)
|
|
345
432
|
{
|
|
346
|
-
if (frame->thread ==
|
|
433
|
+
if (frame->thread == fork_thread)
|
|
347
434
|
{
|
|
348
435
|
fork_fiber = rb_fiber_current();
|
|
349
436
|
break;
|
|
350
437
|
}
|
|
351
438
|
}
|
|
439
|
+
#endif
|
|
352
440
|
}
|
|
353
441
|
|
|
354
|
-
// Runs in the child before Ruby does
|
|
355
|
-
//
|
|
356
|
-
//
|
|
357
|
-
//
|
|
358
|
-
//
|
|
442
|
+
// Runs in the child before Ruby does. A thread without the GVL, such as a
|
|
443
|
+
// delegate of ImageMagick, forks to exec, and the list may be changing under
|
|
444
|
+
// it. The calls of the other fibers never run in the child, so the objects
|
|
445
|
+
// they changed for the call are changed back while nothing in the child can
|
|
446
|
+
// have destroyed them yet. The new generation frees every state, and the
|
|
447
|
+
// calls that go on take theirs again. The calls of the other threads that stay
|
|
448
|
+
// on their thread never return in the child either, and are changed back the
|
|
449
|
+
// same way.
|
|
359
450
|
static void
|
|
360
451
|
atfork_child(void)
|
|
361
452
|
{
|
|
362
|
-
unsigned int
|
|
363
|
-
offload_frame_t *next;
|
|
453
|
+
unsigned int pins = 0;
|
|
364
454
|
|
|
365
455
|
if (!fork_with_gvl)
|
|
366
456
|
{
|
|
367
457
|
return;
|
|
368
458
|
}
|
|
459
|
+
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
460
|
+
unsigned int count = 0;
|
|
461
|
+
offload_frame_t *next;
|
|
462
|
+
|
|
463
|
+
fork_generation++;
|
|
369
464
|
for (offload_frame_t *frame = main_entry->frames; frame; frame = next)
|
|
370
465
|
{
|
|
371
466
|
next = frame->next;
|
|
372
467
|
if (frame->fiber == fork_fiber)
|
|
373
468
|
{
|
|
374
|
-
|
|
375
|
-
}
|
|
376
|
-
else
|
|
377
|
-
{
|
|
378
|
-
for (int i = 0; i < frame->nrestores; i++)
|
|
469
|
+
for (long i = 0; i < frame->nmarks; i++)
|
|
379
470
|
{
|
|
380
|
-
|
|
471
|
+
state_acquire(frame->marks[i].state, frame->marks[i].mode, false);
|
|
381
472
|
}
|
|
382
|
-
|
|
473
|
+
count++;
|
|
474
|
+
continue;
|
|
383
475
|
}
|
|
476
|
+
for (int i = 0; i < frame->nrestores; i++)
|
|
477
|
+
{
|
|
478
|
+
frame->restores[i].release(frame->restores[i].ptr, frame->restores[i].arg);
|
|
479
|
+
}
|
|
480
|
+
frame_remove(main_entry, frame);
|
|
384
481
|
}
|
|
385
482
|
offloads_in_flight.store(count, std::memory_order_relaxed);
|
|
386
|
-
fork_generation++;
|
|
387
|
-
}
|
|
388
483
|
#endif
|
|
389
|
-
|
|
390
|
-
static st_data_t
|
|
391
|
-
offload_state(const void *ptr)
|
|
392
|
-
{
|
|
393
|
-
st_data_t state = 0;
|
|
394
|
-
|
|
395
|
-
if (offloads_in_flight.load(std::memory_order_relaxed) == 0)
|
|
484
|
+
for (pinned_call_t **link = &main_entry->pinned; *link;)
|
|
396
485
|
{
|
|
397
|
-
|
|
486
|
+
if ((*link)->thread == fork_thread)
|
|
487
|
+
{
|
|
488
|
+
pins++;
|
|
489
|
+
link = &(*link)->next;
|
|
490
|
+
}
|
|
491
|
+
else
|
|
492
|
+
{
|
|
493
|
+
(*link)->call->restore();
|
|
494
|
+
*link = (*link)->next;
|
|
495
|
+
}
|
|
398
496
|
}
|
|
399
|
-
|
|
400
|
-
return state;
|
|
497
|
+
pinned_in_flight.store(pins, std::memory_order_relaxed);
|
|
401
498
|
}
|
|
499
|
+
#endif
|
|
402
500
|
|
|
403
501
|
static void
|
|
404
502
|
raise_in_use(void)
|
|
@@ -406,16 +504,50 @@ raise_in_use(void)
|
|
|
406
504
|
rb_raise(rb_eRuntimeError, "object is in use by another fiber");
|
|
407
505
|
}
|
|
408
506
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
507
|
+
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
508
|
+
|
|
509
|
+
// The offload state of an Image, Info, KernelInfo, Draw or Montage, or NULL for
|
|
510
|
+
// another object, which is not tracked
|
|
511
|
+
static rm_offload_state_t *
|
|
512
|
+
object_state(VALUE obj)
|
|
413
513
|
{
|
|
414
|
-
|
|
514
|
+
const rb_data_type_t *type;
|
|
515
|
+
|
|
516
|
+
if (RB_SPECIAL_CONST_P(obj) || !RB_TYPE_P(obj, T_DATA) || !RTYPEDDATA_P(obj))
|
|
415
517
|
{
|
|
416
518
|
return NULL;
|
|
417
519
|
}
|
|
418
|
-
|
|
520
|
+
type = RTYPEDDATA_TYPE(obj);
|
|
521
|
+
if (type == &rm_image_data_type)
|
|
522
|
+
{
|
|
523
|
+
return &((MagickImage *)RTYPEDDATA_DATA(obj))->offload;
|
|
524
|
+
}
|
|
525
|
+
if (type == &rm_info_data_type)
|
|
526
|
+
{
|
|
527
|
+
return &((MagickImageInfo *)RTYPEDDATA_DATA(obj))->offload;
|
|
528
|
+
}
|
|
529
|
+
if (type == &rm_kernel_info_data_type)
|
|
530
|
+
{
|
|
531
|
+
return &((MagickKernelInfo *)RTYPEDDATA_DATA(obj))->offload;
|
|
532
|
+
}
|
|
533
|
+
if (type == &rm_draw_data_type)
|
|
534
|
+
{
|
|
535
|
+
return &((MagickDraw *)RTYPEDDATA_DATA(obj))->offload;
|
|
536
|
+
}
|
|
537
|
+
if (type == &rm_montage_data_type)
|
|
538
|
+
{
|
|
539
|
+
return &((MagickMontage *)RTYPEDDATA_DATA(obj))->offload;
|
|
540
|
+
}
|
|
541
|
+
return NULL;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// Number of calls in flight that read obj, or STATE_UPDATING
|
|
545
|
+
static unsigned int
|
|
546
|
+
object_offload_state(VALUE obj)
|
|
547
|
+
{
|
|
548
|
+
rm_offload_state_t *state = object_state(obj);
|
|
549
|
+
|
|
550
|
+
return state ? state_count(state->load(std::memory_order_acquire)) : 0;
|
|
419
551
|
}
|
|
420
552
|
|
|
421
553
|
static VALUE
|
|
@@ -456,13 +588,14 @@ rm_gvl_init(void)
|
|
|
456
588
|
interrupt_mask = rb_hash_new();
|
|
457
589
|
rb_funcall(interrupt_mask, rb_intern("compare_by_identity"), 0);
|
|
458
590
|
rb_hash_aset(interrupt_mask, rb_cObject, ID2SYM(rb_intern("never")));
|
|
591
|
+
#if defined(HAVE_RB_RACTOR_MAKE_SHAREABLE)
|
|
459
592
|
rb_ractor_make_shareable(interrupt_mask);
|
|
593
|
+
#endif
|
|
460
594
|
id_handle_interrupt = rb_intern("handle_interrupt");
|
|
461
595
|
|
|
462
|
-
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
463
596
|
offloaded_key = rb_ractor_local_storage_ptr_newkey(&offloaded_type);
|
|
464
597
|
main_entry = offloaded_entry();
|
|
465
|
-
#if defined(
|
|
598
|
+
#if defined(RMAGICK_ATFORK)
|
|
466
599
|
int err = pthread_atfork(atfork_prepare, NULL, atfork_child);
|
|
467
600
|
|
|
468
601
|
if (err)
|
|
@@ -470,120 +603,137 @@ rm_gvl_init(void)
|
|
|
470
603
|
rb_syserr_fail(err, "pthread_atfork");
|
|
471
604
|
}
|
|
472
605
|
#endif
|
|
473
|
-
#endif
|
|
474
606
|
}
|
|
475
607
|
|
|
476
608
|
|
|
477
609
|
/**
|
|
478
|
-
* Raise if
|
|
610
|
+
* Raise if a call in flight is changing the object.
|
|
479
611
|
*
|
|
480
612
|
* No Ruby usage (internal function)
|
|
481
613
|
*
|
|
482
|
-
* @param
|
|
614
|
+
* @param obj an Image, Info, KernelInfo, Draw or Montage
|
|
483
615
|
*/
|
|
484
616
|
void
|
|
485
|
-
rm_gvl_check_readable(
|
|
617
|
+
rm_gvl_check_readable(VALUE obj)
|
|
486
618
|
{
|
|
487
619
|
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
488
|
-
if (
|
|
620
|
+
if (object_offload_state(obj) == STATE_UPDATING)
|
|
489
621
|
{
|
|
490
622
|
raise_in_use();
|
|
491
623
|
}
|
|
492
624
|
#endif
|
|
625
|
+
if (pinned(obj, true))
|
|
626
|
+
{
|
|
627
|
+
raise_in_use();
|
|
628
|
+
}
|
|
493
629
|
}
|
|
494
630
|
|
|
495
631
|
|
|
496
632
|
/**
|
|
497
|
-
* Whether
|
|
633
|
+
* Whether a call in flight is using the object.
|
|
498
634
|
*
|
|
499
635
|
* No Ruby usage (internal function)
|
|
500
636
|
*
|
|
501
|
-
* @param
|
|
637
|
+
* @param obj an Image, Info, KernelInfo, Draw or Montage
|
|
502
638
|
* @return true if a call reads or changes the object
|
|
503
639
|
*/
|
|
504
640
|
bool
|
|
505
|
-
rm_gvl_in_use(
|
|
641
|
+
rm_gvl_in_use(VALUE obj)
|
|
506
642
|
{
|
|
507
643
|
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
644
|
+
if (object_offload_state(obj) != 0)
|
|
645
|
+
{
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
511
648
|
#endif
|
|
649
|
+
return pinned(obj, false);
|
|
512
650
|
}
|
|
513
651
|
|
|
514
652
|
|
|
515
653
|
/**
|
|
516
|
-
* Raise if
|
|
654
|
+
* Raise if a call in flight is using the object.
|
|
517
655
|
*
|
|
518
656
|
* No Ruby usage (internal function)
|
|
519
657
|
*
|
|
520
|
-
* @param
|
|
658
|
+
* @param obj an Image, Info, KernelInfo, Draw or Montage
|
|
521
659
|
*/
|
|
522
660
|
void
|
|
523
|
-
rm_gvl_check_writable(
|
|
661
|
+
rm_gvl_check_writable(VALUE obj)
|
|
524
662
|
{
|
|
525
|
-
|
|
526
|
-
if (rm_gvl_in_use(ptr))
|
|
663
|
+
if (rm_gvl_in_use(obj))
|
|
527
664
|
{
|
|
528
665
|
raise_in_use();
|
|
529
666
|
}
|
|
530
|
-
#endif
|
|
531
667
|
}
|
|
532
668
|
|
|
533
669
|
|
|
534
670
|
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
535
|
-
|
|
536
|
-
|
|
671
|
+
// Mark an object registered with read() or update(), once for each call. An
|
|
672
|
+
// object that the call both reads and changes is marked as changed.
|
|
673
|
+
static void
|
|
674
|
+
add_object_mark(offload_mark_t *marks, long *nmarks, VALUE obj, OffloadMode mode)
|
|
537
675
|
{
|
|
538
|
-
|
|
676
|
+
rm_offload_state_t *state = object_state(obj);
|
|
677
|
+
|
|
678
|
+
if (!state)
|
|
539
679
|
{
|
|
540
|
-
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
for (long i = 0; i < *nmarks; i++)
|
|
683
|
+
{
|
|
684
|
+
if (marks[i].state == state)
|
|
541
685
|
{
|
|
542
|
-
|
|
686
|
+
if (mode == OffloadUpdate)
|
|
687
|
+
{
|
|
688
|
+
marks[i].mode = OffloadUpdate;
|
|
689
|
+
}
|
|
690
|
+
return;
|
|
543
691
|
}
|
|
544
692
|
}
|
|
545
|
-
|
|
693
|
+
marks[*nmarks].state = state;
|
|
694
|
+
marks[*nmarks].mode = mode;
|
|
695
|
+
marks[*nmarks].image = RTYPEDDATA_TYPE(obj) == &rm_image_data_type;
|
|
696
|
+
(*nmarks)++;
|
|
546
697
|
}
|
|
547
698
|
|
|
548
|
-
//
|
|
549
|
-
//
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
object_key(VALUE obj, const void *ptr, bool each, long j, bool *image)
|
|
699
|
+
// Take the marks of a frame and link it, or take none and return false if
|
|
700
|
+
// another call holds an object
|
|
701
|
+
static bool
|
|
702
|
+
frame_acquire(offloaded_t *entry, offload_frame_t *frame)
|
|
553
703
|
{
|
|
554
|
-
|
|
704
|
+
offloads_in_flight.fetch_add(1, std::memory_order_relaxed);
|
|
705
|
+
for (long i = 0; i < frame->nmarks; i++)
|
|
555
706
|
{
|
|
556
|
-
|
|
557
|
-
|
|
707
|
+
if (!state_acquire(frame->marks[i].state, frame->marks[i].mode, frame->marks[i].image))
|
|
708
|
+
{
|
|
709
|
+
while (i-- > 0)
|
|
710
|
+
{
|
|
711
|
+
state_release(frame->marks[i].state, frame->marks[i].mode);
|
|
712
|
+
}
|
|
713
|
+
offloads_in_flight.fetch_sub(1, std::memory_order_relaxed);
|
|
714
|
+
return false;
|
|
715
|
+
}
|
|
558
716
|
}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
return offload_key(obj);
|
|
717
|
+
frame_push(entry, frame);
|
|
718
|
+
return true;
|
|
562
719
|
}
|
|
563
720
|
|
|
564
|
-
|
|
565
|
-
|
|
721
|
+
// Release the marks of a frame and unlink it, or return false if a fork
|
|
722
|
+
// dropped it in the child
|
|
723
|
+
static bool
|
|
724
|
+
frame_release(offloaded_t *entry, offload_frame_t *frame)
|
|
566
725
|
{
|
|
567
|
-
if (!
|
|
726
|
+
if (!frame->linked)
|
|
568
727
|
{
|
|
569
|
-
return;
|
|
728
|
+
return false;
|
|
570
729
|
}
|
|
571
|
-
|
|
572
|
-
marks[*nmarks].mode = mode;
|
|
573
|
-
marks[*nmarks].image = image;
|
|
574
|
-
(*nmarks)++;
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
static bool
|
|
578
|
-
mark_in_use(const offload_mark_t *mark)
|
|
579
|
-
{
|
|
580
|
-
st_data_t state = offload_state(mark->key);
|
|
581
|
-
|
|
582
|
-
if (mark->image)
|
|
730
|
+
for (long i = 0; i < frame->nmarks; i++)
|
|
583
731
|
{
|
|
584
|
-
|
|
732
|
+
state_release(frame->marks[i].state, frame->marks[i].mode);
|
|
585
733
|
}
|
|
586
|
-
|
|
734
|
+
frame_remove(entry, frame);
|
|
735
|
+
offloads_in_flight.fetch_sub(1, std::memory_order_relaxed);
|
|
736
|
+
return true;
|
|
587
737
|
}
|
|
588
738
|
#endif
|
|
589
739
|
|
|
@@ -659,14 +809,13 @@ rm_gvl_call::rm_gvl_call(gvl_function_t *fp, void *args)
|
|
|
659
809
|
|
|
660
810
|
|
|
661
811
|
rm_gvl_call &
|
|
662
|
-
rm_gvl_call::add_object(VALUE obj,
|
|
812
|
+
rm_gvl_call::add_object(VALUE obj, bool update, bool each)
|
|
663
813
|
{
|
|
664
814
|
if (nobjects == MaxObjects)
|
|
665
815
|
{
|
|
666
816
|
rb_bug("too many objects for an offloaded call");
|
|
667
817
|
}
|
|
668
818
|
objects[nobjects].obj = obj;
|
|
669
|
-
objects[nobjects].ptr = ptr;
|
|
670
819
|
objects[nobjects].update = update;
|
|
671
820
|
objects[nobjects].each = each;
|
|
672
821
|
nobjects++;
|
|
@@ -686,30 +835,12 @@ rm_gvl_call::add_object(VALUE obj, const void *ptr, bool update, bool each)
|
|
|
686
835
|
rm_gvl_call &
|
|
687
836
|
rm_gvl_call::cleanup(void (*release)(void *, intptr_t), void *ptr, intptr_t arg)
|
|
688
837
|
{
|
|
689
|
-
return add_cleanup(release, ptr, arg,
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
/**
|
|
694
|
-
* Like cleanup, for a release that changes an object the caller passed in
|
|
695
|
-
* back, rather than freeing what the call made.
|
|
696
|
-
*
|
|
697
|
-
* @param release the function
|
|
698
|
-
* @param ptr its first argument
|
|
699
|
-
* @param arg its second argument
|
|
700
|
-
* @param size if not 0, arg points to a value of this size, which an
|
|
701
|
-
* offloaded call keeps a copy of
|
|
702
|
-
* @return self
|
|
703
|
-
*/
|
|
704
|
-
rm_gvl_call &
|
|
705
|
-
rm_gvl_call::restore(void (*release)(void *, intptr_t), void *ptr, intptr_t arg, size_t size)
|
|
706
|
-
{
|
|
707
|
-
return add_cleanup(release, ptr, arg, size, true);
|
|
838
|
+
return add_cleanup(release, ptr, arg, false);
|
|
708
839
|
}
|
|
709
840
|
|
|
710
841
|
|
|
711
842
|
rm_gvl_call &
|
|
712
|
-
rm_gvl_call::add_cleanup(void (*release)(void *, intptr_t), void *ptr, intptr_t arg,
|
|
843
|
+
rm_gvl_call::add_cleanup(void (*release)(void *, intptr_t), void *ptr, intptr_t arg, bool restore)
|
|
713
844
|
{
|
|
714
845
|
if (!ptr)
|
|
715
846
|
{
|
|
@@ -722,7 +853,6 @@ rm_gvl_call::add_cleanup(void (*release)(void *, intptr_t), void *ptr, intptr_t
|
|
|
722
853
|
cleanups[ncleanups].release = release;
|
|
723
854
|
cleanups[ncleanups].ptr = ptr;
|
|
724
855
|
cleanups[ncleanups].arg = arg;
|
|
725
|
-
cleanups[ncleanups].size = size;
|
|
726
856
|
cleanups[ncleanups].restore = restore;
|
|
727
857
|
ncleanups++;
|
|
728
858
|
return *this;
|
|
@@ -734,27 +864,13 @@ rm_gvl_call::add_cleanup(void (*release)(void *, intptr_t), void *ptr, intptr_t
|
|
|
734
864
|
* change or destroy it while the call is in flight. Another call cannot use
|
|
735
865
|
* an image that the call reads.
|
|
736
866
|
*
|
|
737
|
-
* @param obj an Image, Info or
|
|
867
|
+
* @param obj an Image, Info, KernelInfo, Draw or Montage
|
|
738
868
|
* @return self
|
|
739
869
|
*/
|
|
740
870
|
rm_gvl_call &
|
|
741
871
|
rm_gvl_call::read(VALUE obj)
|
|
742
872
|
{
|
|
743
|
-
return add_object(obj,
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
/**
|
|
748
|
-
* Like read(VALUE), for the data pointer of an Image that the caller has
|
|
749
|
-
* fetched.
|
|
750
|
-
*
|
|
751
|
-
* @param ptr the data pointer
|
|
752
|
-
* @return self
|
|
753
|
-
*/
|
|
754
|
-
rm_gvl_call &
|
|
755
|
-
rm_gvl_call::read(const void *ptr)
|
|
756
|
-
{
|
|
757
|
-
return add_object(Qundef, ptr, false, false);
|
|
873
|
+
return add_object(obj, false, false);
|
|
758
874
|
}
|
|
759
875
|
|
|
760
876
|
|
|
@@ -762,55 +878,66 @@ rm_gvl_call::read(const void *ptr)
|
|
|
762
878
|
* The call changes the data of obj, or the caller replaces it with the result.
|
|
763
879
|
* Other fibers cannot use it while the call is in flight.
|
|
764
880
|
*
|
|
765
|
-
* @param obj an Image, Info or
|
|
881
|
+
* @param obj an Image, Info, KernelInfo, Draw or Montage
|
|
766
882
|
* @return self
|
|
767
883
|
*/
|
|
768
884
|
rm_gvl_call &
|
|
769
885
|
rm_gvl_call::update(VALUE obj)
|
|
770
886
|
{
|
|
771
|
-
return add_object(obj,
|
|
887
|
+
return add_object(obj, true, false);
|
|
772
888
|
}
|
|
773
889
|
|
|
774
890
|
|
|
775
891
|
/**
|
|
776
|
-
*
|
|
892
|
+
* The call reads every object in the array, such as the images of an ImageList.
|
|
893
|
+
* A copy of the array keeps the objects alive while the call is in flight,
|
|
894
|
+
* even if another fiber removes them from the array.
|
|
777
895
|
*
|
|
778
|
-
* @param
|
|
896
|
+
* @param ary the array
|
|
779
897
|
* @return self
|
|
780
898
|
*/
|
|
781
899
|
rm_gvl_call &
|
|
782
|
-
rm_gvl_call::
|
|
900
|
+
rm_gvl_call::read_each(VALUE ary)
|
|
783
901
|
{
|
|
784
|
-
return add_object(
|
|
902
|
+
return add_object(rb_ary_dup(ary), false, true);
|
|
785
903
|
}
|
|
786
904
|
|
|
787
905
|
|
|
788
906
|
/**
|
|
789
|
-
* The call
|
|
790
|
-
*
|
|
791
|
-
* even if another fiber removes them from the array.
|
|
907
|
+
* The call changes every object in the array, such as the images of an
|
|
908
|
+
* ImageList. Like read_each(), a copy of the array keeps the objects alive.
|
|
792
909
|
*
|
|
793
910
|
* @param ary the array
|
|
794
911
|
* @return self
|
|
795
912
|
*/
|
|
796
913
|
rm_gvl_call &
|
|
797
|
-
rm_gvl_call::
|
|
914
|
+
rm_gvl_call::update_each(VALUE ary)
|
|
798
915
|
{
|
|
799
|
-
return add_object(rb_ary_dup(ary),
|
|
916
|
+
return add_object(rb_ary_dup(ary), true, true);
|
|
800
917
|
}
|
|
801
918
|
|
|
802
919
|
|
|
803
920
|
/**
|
|
804
|
-
* The call changes
|
|
805
|
-
*
|
|
921
|
+
* The call reads obj, or changes it if changes is true, and the caller uses
|
|
922
|
+
* its data after the call. obj may be an Array, whose objects the call uses
|
|
923
|
+
* like read_each() and update_each(), or nil, which registers nothing.
|
|
806
924
|
*
|
|
807
|
-
* @param
|
|
925
|
+
* @param obj an Image, an Array of Images, or nil
|
|
926
|
+
* @param changes whether the call changes the objects
|
|
808
927
|
* @return self
|
|
809
928
|
*/
|
|
810
929
|
rm_gvl_call &
|
|
811
|
-
rm_gvl_call::
|
|
930
|
+
rm_gvl_call::hold(VALUE obj, bool changes)
|
|
812
931
|
{
|
|
813
|
-
|
|
932
|
+
if (NIL_P(obj))
|
|
933
|
+
{
|
|
934
|
+
return *this;
|
|
935
|
+
}
|
|
936
|
+
if (RB_TYPE_P(obj, T_ARRAY))
|
|
937
|
+
{
|
|
938
|
+
return changes ? update_each(obj) : read_each(obj);
|
|
939
|
+
}
|
|
940
|
+
return changes ? update(obj) : read(obj);
|
|
814
941
|
}
|
|
815
942
|
|
|
816
943
|
|
|
@@ -914,7 +1041,7 @@ rm_gvl_call::destroy(Image *image)
|
|
|
914
1041
|
rm_gvl_call &
|
|
915
1042
|
rm_gvl_call::split(Image *images)
|
|
916
1043
|
{
|
|
917
|
-
return
|
|
1044
|
+
return add_cleanup(split_images, images, 0, true);
|
|
918
1045
|
}
|
|
919
1046
|
|
|
920
1047
|
|
|
@@ -929,7 +1056,7 @@ rm_gvl_call::split(Image *images)
|
|
|
929
1056
|
rm_gvl_call &
|
|
930
1057
|
rm_gvl_call::restore_mask(Image *image, ChannelType channel_mask)
|
|
931
1058
|
{
|
|
932
|
-
return
|
|
1059
|
+
return add_cleanup(restore_channel_mask, image, (intptr_t)channel_mask, true);
|
|
933
1060
|
}
|
|
934
1061
|
#endif
|
|
935
1062
|
|
|
@@ -998,172 +1125,218 @@ rm_gvl_call::unwind(ResultType type, void *result, bool abandoned)
|
|
|
998
1125
|
}
|
|
999
1126
|
|
|
1000
1127
|
|
|
1128
|
+
/**
|
|
1129
|
+
* Change back the objects that the caller passed in, for a call that the
|
|
1130
|
+
* child of a fork dropped.
|
|
1131
|
+
*
|
|
1132
|
+
* No Ruby usage (internal function)
|
|
1133
|
+
*/
|
|
1134
|
+
void
|
|
1135
|
+
rm_gvl_call::restore() const
|
|
1136
|
+
{
|
|
1137
|
+
for (int i = 0; i < ncleanups; i++)
|
|
1138
|
+
{
|
|
1139
|
+
if (cleanups[i].restore)
|
|
1140
|
+
{
|
|
1141
|
+
cleanups[i].release(cleanups[i].ptr, cleanups[i].arg);
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
|
|
1001
1147
|
void *
|
|
1002
1148
|
rm_gvl_call::call(ResultType type)
|
|
1003
1149
|
{
|
|
1004
|
-
|
|
1005
|
-
bool offload = !keep && offload_p();
|
|
1150
|
+
void *result;
|
|
1006
1151
|
|
|
1007
|
-
|
|
1152
|
+
check_frozen_images(type);
|
|
1153
|
+
result = call_body(type);
|
|
1154
|
+
rm_gc_continue();
|
|
1155
|
+
return result;
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
// Raise if the call changes an image that is frozen. An ImageList registers
|
|
1160
|
+
// its frozen images with update_each but writes copies of them, so only the
|
|
1161
|
+
// images registered one by one are checked.
|
|
1162
|
+
void
|
|
1163
|
+
rm_gvl_call::check_frozen_images(ResultType type)
|
|
1164
|
+
{
|
|
1165
|
+
for (int i = 0; i < nobjects; i++)
|
|
1008
1166
|
{
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
offloaded_t *entry;
|
|
1013
|
-
offload_mark_t *marks;
|
|
1014
|
-
VALUE marks_buffer = 0;
|
|
1015
|
-
long count = 0, nmarks = 0;
|
|
1016
|
-
size_t marks_size, values_size = 0;
|
|
1017
|
-
bool dropped;
|
|
1018
|
-
int tag;
|
|
1019
|
-
|
|
1020
|
-
for (int i = 0; i < nobjects; i++)
|
|
1021
|
-
{
|
|
1022
|
-
count += objects[i].each ? RARRAY_LEN(objects[i].obj) : 1;
|
|
1023
|
-
}
|
|
1024
|
-
marks_size = frame_align(2 * count * sizeof(offload_mark_t));
|
|
1025
|
-
if (offload)
|
|
1026
|
-
{
|
|
1027
|
-
for (int i = 0; i < ncleanups; i++)
|
|
1028
|
-
{
|
|
1029
|
-
values_size += frame_align(cleanups[i].size);
|
|
1030
|
-
}
|
|
1031
|
-
frame = (offload_frame_t *)xmalloc(frame_align(sizeof(offload_frame_t)) + marks_size + values_size);
|
|
1032
|
-
marks = (offload_mark_t *)((char *)frame + frame_align(sizeof(offload_frame_t)));
|
|
1033
|
-
}
|
|
1034
|
-
else
|
|
1167
|
+
VALUE obj = objects[i].obj;
|
|
1168
|
+
|
|
1169
|
+
if (objects[i].update && !objects[i].each && OBJ_FROZEN(obj) && rb_typeddata_is_kind_of(obj, &rm_image_data_type))
|
|
1035
1170
|
{
|
|
1036
|
-
|
|
1171
|
+
unwind(type, NULL);
|
|
1172
|
+
rb_check_frozen(obj);
|
|
1037
1173
|
}
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1038
1176
|
|
|
1039
|
-
|
|
1040
|
-
|
|
1177
|
+
void *
|
|
1178
|
+
rm_gvl_call::call_body(ResultType type)
|
|
1179
|
+
{
|
|
1180
|
+
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
1181
|
+
if (!keep && offload_p())
|
|
1182
|
+
{
|
|
1183
|
+
return offload(type);
|
|
1184
|
+
}
|
|
1185
|
+
if (offloads_in_flight.load(std::memory_order_relaxed) != 0)
|
|
1186
|
+
{
|
|
1187
|
+
check_images_idle(type);
|
|
1188
|
+
}
|
|
1189
|
+
#endif
|
|
1190
|
+
|
|
1191
|
+
return call_here(type);
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
// Whether f(obj, update) is true for an object that the call registered, or for
|
|
1196
|
+
// an object in an array that it registered with each. update_only skips the
|
|
1197
|
+
// objects that the call does not change.
|
|
1198
|
+
template <typename F>
|
|
1199
|
+
bool
|
|
1200
|
+
rm_gvl_call::any_object(bool update_only, F f) const
|
|
1201
|
+
{
|
|
1202
|
+
for (int i = 0; i < nobjects; i++)
|
|
1203
|
+
{
|
|
1204
|
+
long len = objects[i].each ? RARRAY_LEN(objects[i].obj) : 1;
|
|
1205
|
+
|
|
1206
|
+
if (update_only && !objects[i].update)
|
|
1041
1207
|
{
|
|
1042
|
-
|
|
1043
|
-
{
|
|
1044
|
-
long len = objects[i].each ? RARRAY_LEN(objects[i].obj) : 1;
|
|
1045
|
-
OffloadMode mode = objects[i].update ? OffloadUpdate : OffloadRead;
|
|
1046
|
-
|
|
1047
|
-
if (objects[i].update != (pass == 0))
|
|
1048
|
-
{
|
|
1049
|
-
continue;
|
|
1050
|
-
}
|
|
1051
|
-
for (long j = 0; j < len; j++)
|
|
1052
|
-
{
|
|
1053
|
-
bool image;
|
|
1054
|
-
void *key = object_key(objects[i].obj, objects[i].ptr, objects[i].each, j, &image);
|
|
1055
|
-
|
|
1056
|
-
add_mark(marks, &nmarks, key, mode, image);
|
|
1057
|
-
if (key && image)
|
|
1058
|
-
{
|
|
1059
|
-
add_mark(marks, &nmarks, ((Image *)key)->cache, mode, true);
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1208
|
+
continue;
|
|
1063
1209
|
}
|
|
1064
|
-
|
|
1065
|
-
for (long i = 0; i < nmarks; i++)
|
|
1210
|
+
for (long j = 0; j < len; j++)
|
|
1066
1211
|
{
|
|
1067
|
-
if ((
|
|
1212
|
+
if (f(objects[i].each ? rb_ary_entry(objects[i].obj, j) : objects[i].obj, objects[i].update))
|
|
1068
1213
|
{
|
|
1069
|
-
|
|
1070
|
-
{
|
|
1071
|
-
xfree(frame);
|
|
1072
|
-
}
|
|
1073
|
-
else
|
|
1074
|
-
{
|
|
1075
|
-
ALLOCV_END(marks_buffer);
|
|
1076
|
-
}
|
|
1077
|
-
unwind(type, NULL);
|
|
1078
|
-
raise_in_use();
|
|
1214
|
+
return true;
|
|
1079
1215
|
}
|
|
1080
1216
|
}
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
return call_here(type);
|
|
1085
|
-
}
|
|
1217
|
+
}
|
|
1218
|
+
return false;
|
|
1219
|
+
}
|
|
1086
1220
|
|
|
1087
|
-
// The frame keeps a copy of the values to change back, which can be on
|
|
1088
|
-
// the stack of a fiber that is gone by the next fork.
|
|
1089
|
-
static_assert(MaxCleanups <= OFFLOAD_MAX_RESTORES, "a frame holds every cleanup that restores");
|
|
1090
|
-
char *values = (char *)marks + marks_size;
|
|
1091
1221
|
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
offload_restore_t restore = { cleanups[i].release, cleanups[i].ptr, cleanups[i].arg };
|
|
1100
|
-
|
|
1101
|
-
if (cleanups[i].size)
|
|
1102
|
-
{
|
|
1103
|
-
memcpy(values, (const void *)cleanups[i].arg, cleanups[i].size);
|
|
1104
|
-
restore.arg = (intptr_t)values;
|
|
1105
|
-
values += frame_align(cleanups[i].size);
|
|
1106
|
-
}
|
|
1107
|
-
frame->restores[frame->nrestores++] = restore;
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
frame->thread = rb_thread_current();
|
|
1111
|
-
frame->fiber = fiber;
|
|
1112
|
-
call.frame = frame;
|
|
1222
|
+
#if defined(RMAGICK_OFFLOAD_SAFE)
|
|
1223
|
+
// A frame for an offloaded call, with a mark for each object it registered
|
|
1224
|
+
offload_frame_t *
|
|
1225
|
+
rm_gvl_call::new_frame()
|
|
1226
|
+
{
|
|
1227
|
+
offload_frame_t *frame;
|
|
1228
|
+
long count = 0;
|
|
1113
1229
|
|
|
1114
|
-
|
|
1115
|
-
|
|
1230
|
+
for (int i = 0; i < nobjects; i++)
|
|
1231
|
+
{
|
|
1232
|
+
count += objects[i].each ? RARRAY_LEN(objects[i].obj) : 1;
|
|
1233
|
+
}
|
|
1234
|
+
frame = (offload_frame_t *)xmalloc(frame_align(sizeof(offload_frame_t)) + count * sizeof(offload_mark_t));
|
|
1235
|
+
frame->marks = (offload_mark_t *)((char *)frame + frame_align(sizeof(offload_frame_t)));
|
|
1236
|
+
frame->nmarks = 0;
|
|
1237
|
+
any_object(false, [frame](VALUE obj, bool update) {
|
|
1238
|
+
add_object_mark(frame->marks, &frame->nmarks, obj, update ? OffloadUpdate : OffloadRead);
|
|
1239
|
+
return false;
|
|
1240
|
+
});
|
|
1241
|
+
|
|
1242
|
+
static_assert(MaxCleanups <= OFFLOAD_MAX_RESTORES, "a frame holds every cleanup that restores");
|
|
1243
|
+
frame->nrestores = 0;
|
|
1244
|
+
for (int i = 0; i < ncleanups; i++)
|
|
1245
|
+
{
|
|
1246
|
+
if (cleanups[i].restore)
|
|
1116
1247
|
{
|
|
1117
|
-
|
|
1118
|
-
}
|
|
1119
|
-
offloads_in_flight.fetch_add((unsigned int)nmarks, std::memory_order_relaxed);
|
|
1120
|
-
frame_push(entry, frame);
|
|
1248
|
+
offload_restore_t restore = { cleanups[i].release, cleanups[i].ptr, cleanups[i].arg };
|
|
1121
1249
|
|
|
1122
|
-
|
|
1123
|
-
if (!tag && !call.started && frame->linked)
|
|
1124
|
-
{
|
|
1125
|
-
rb_protect(offload_call, (VALUE)&call, &tag);
|
|
1250
|
+
frame->restores[frame->nrestores++] = restore;
|
|
1126
1251
|
}
|
|
1252
|
+
}
|
|
1253
|
+
frame->thread = rb_thread_current();
|
|
1254
|
+
frame->fiber = rb_fiber_current();
|
|
1255
|
+
return frame;
|
|
1256
|
+
}
|
|
1127
1257
|
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
}
|
|
1258
|
+
|
|
1259
|
+
void *
|
|
1260
|
+
rm_gvl_call::offload(ResultType type)
|
|
1261
|
+
{
|
|
1262
|
+
offload_frame_t *frame = new_frame();
|
|
1263
|
+
offload_call_t call = { fp, args, NULL, frame, false, false };
|
|
1264
|
+
bool dropped;
|
|
1265
|
+
int tag;
|
|
1266
|
+
|
|
1267
|
+
if (!frame_acquire(offloaded_entry(), frame))
|
|
1268
|
+
{
|
|
1140
1269
|
xfree(frame);
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1270
|
+
unwind(type, NULL);
|
|
1271
|
+
raise_in_use();
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
rb_protect(offload_call, (VALUE)&call, &tag);
|
|
1275
|
+
if (!tag && !call.started && frame->linked)
|
|
1276
|
+
{
|
|
1277
|
+
rb_protect(offload_call, (VALUE)&call, &tag);
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
dropped = !frame_release(offloaded_entry(), frame);
|
|
1281
|
+
xfree(frame);
|
|
1282
|
+
if (dropped)
|
|
1283
|
+
{
|
|
1284
|
+
// The worker may have finished before the fork
|
|
1285
|
+
unwind(type, call.done ? call.result : NULL, true);
|
|
1151
1286
|
if (tag)
|
|
1152
1287
|
{
|
|
1153
|
-
// The scheduler raised into this fiber after the worker finished.
|
|
1154
|
-
unwind(type, call.result);
|
|
1155
1288
|
rb_jump_tag(tag);
|
|
1156
1289
|
}
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1290
|
+
rb_raise(rb_eRuntimeError, "call abandoned in the child of a fork");
|
|
1291
|
+
}
|
|
1292
|
+
if (tag)
|
|
1293
|
+
{
|
|
1294
|
+
// The scheduler raised into this fiber after the worker finished.
|
|
1295
|
+
unwind(type, call.result);
|
|
1296
|
+
rb_jump_tag(tag);
|
|
1297
|
+
}
|
|
1298
|
+
if (!call.started)
|
|
1299
|
+
{
|
|
1300
|
+
return call_here(type);
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
return call.result;
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1161
1306
|
|
|
1162
|
-
|
|
1307
|
+
// Raise if an offloaded call uses an image that this call, which stays on its
|
|
1308
|
+
// thread, uses too
|
|
1309
|
+
void
|
|
1310
|
+
rm_gvl_call::check_images_idle(ResultType type)
|
|
1311
|
+
{
|
|
1312
|
+
bool busy = any_object(false, [](VALUE obj, bool) {
|
|
1313
|
+
rm_offload_state_t *state = object_state(obj);
|
|
1314
|
+
|
|
1315
|
+
return state && RTYPEDDATA_TYPE(obj) == &rm_image_data_type && state_count(state->load(std::memory_order_acquire)) != 0;
|
|
1316
|
+
});
|
|
1317
|
+
|
|
1318
|
+
if (busy)
|
|
1319
|
+
{
|
|
1320
|
+
unwind(type, NULL);
|
|
1321
|
+
raise_in_use();
|
|
1163
1322
|
}
|
|
1323
|
+
}
|
|
1164
1324
|
#endif
|
|
1165
1325
|
|
|
1166
|
-
|
|
1326
|
+
|
|
1327
|
+
/**
|
|
1328
|
+
* Whether the call uses obj, or changes it if update is true.
|
|
1329
|
+
*
|
|
1330
|
+
* No Ruby usage (internal function)
|
|
1331
|
+
*
|
|
1332
|
+
* @param obj the object
|
|
1333
|
+
* @param update whether to look only at the objects that the call changes
|
|
1334
|
+
* @return true if the call registered obj
|
|
1335
|
+
*/
|
|
1336
|
+
bool
|
|
1337
|
+
rm_gvl_call::uses(VALUE obj, bool update) const
|
|
1338
|
+
{
|
|
1339
|
+
return any_object(update, [obj](VALUE registered, bool) { return registered == obj; });
|
|
1167
1340
|
}
|
|
1168
1341
|
|
|
1169
1342
|
|
|
@@ -1171,19 +1344,24 @@ void *
|
|
|
1171
1344
|
rm_gvl_call::call_here(ResultType type)
|
|
1172
1345
|
{
|
|
1173
1346
|
offload_call_t call = { fp, args, NULL, NULL, false, false };
|
|
1347
|
+
offloaded_t *entry = offloaded_entry();
|
|
1348
|
+
pinned_call_t pin = { this, rb_thread_current(), NULL };
|
|
1174
1349
|
int tag;
|
|
1175
1350
|
|
|
1351
|
+
pin_call(entry, &pin);
|
|
1176
1352
|
rb_protect(check_interrupts, Qnil, &tag);
|
|
1177
1353
|
if (tag)
|
|
1178
1354
|
{
|
|
1355
|
+
unpin_call(entry, &pin);
|
|
1179
1356
|
unwind(type, NULL);
|
|
1180
1357
|
rb_jump_tag(tag);
|
|
1181
1358
|
}
|
|
1182
1359
|
|
|
1183
1360
|
rb_protect(call_deferring_interrupts, (VALUE)&call, &tag);
|
|
1361
|
+
unpin_call(entry, &pin);
|
|
1184
1362
|
if (tag)
|
|
1185
1363
|
{
|
|
1186
|
-
if (call.done)
|
|
1364
|
+
if (call.done || !call.started)
|
|
1187
1365
|
{
|
|
1188
1366
|
unwind(type, call.result);
|
|
1189
1367
|
}
|