diff --git a/CHANGELOG.md b/CHANGELOG.md index aa13362..b2cfbf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * raise Vips::Error if `copy_memory` fails [VitaliiLazebnyi] * `Source.new_from_memory` keeps a reference to the memory area [jeremy] * release the GVL during `copy_memory` and `write_to_memory` [andrewfader] +* release the GVL during `Region#fetch` [janko] ## Version 2.3.0 (2025-12-10) diff --git a/lib/vips/image.rb b/lib/vips/image.rb index 814621e..5f9a311 100644 --- a/lib/vips/image.rb +++ b/lib/vips/image.rb @@ -12,7 +12,7 @@ module Vips attach_function :vips_image_new_matrix_from_array, [:int, :int, :pointer, :int], :pointer - # these two evaluate the whole pipeline, so release the GVL while they run + # copy_memory evaluates the pipeline, so release the GVL while it runs attach_function :vips_image_copy_memory, [:pointer], :pointer, blocking: true @@ -32,6 +32,7 @@ module Vips attach_function :vips_foreign_find_save_target, [:string], :string end + # write_to_memory evaluates the pipeline, so release the GVL while it runs attach_function :vips_image_write_to_memory, [:pointer, SizeStruct.ptr], :pointer, blocking: true diff --git a/lib/vips/region.rb b/lib/vips/region.rb index 31479db..73aaa30 100644 --- a/lib/vips/region.rb +++ b/lib/vips/region.rb @@ -10,7 +10,9 @@ module Vips attach_function :vips_region_new, [:pointer], :pointer if Vips.at_least_libvips?(8, 8) - attach_function :vips_region_fetch, [:pointer, :int, :int, :int, :int, SizeStruct.ptr], :pointer + # fetch evaluates the pipeline, so release the GVL while it runs + attach_function :vips_region_fetch, [:pointer, :int, :int, :int, :int, SizeStruct.ptr], :pointer, + blocking: true attach_function :vips_region_width, [:pointer], :int attach_function :vips_region_height, [:pointer], :int end diff --git a/spec/image_spec.rb b/spec/image_spec.rb index 2cb0f4e..f1eeff7 100644 --- a/spec/image_spec.rb +++ b/spec/image_spec.rb @@ -94,36 +94,6 @@ expect { img.copy_memory }.to raise_exception(Vips::Error) end - # copy_memory and write_to_memory evaluate the whole pipeline, so they must - # release the GVL, or every other Ruby thread stalls until they return - def ticks_during - ticks = 0 - running = true - ticker = Thread.new do - while running - ticks += 1 - sleep 0.001 - end - end - sleep 0.01 until ticks > 0 - before = ticks - yield - during = ticks - before - running = false - ticker.join - during - end - - it "lets other threads run during copy_memory" do - image = Vips::Image.gaussnoise(2000, 2000).gaussblur(4) - expect(ticks_during { image.copy_memory }).to be > 0 - end - - it "lets other threads run during write_to_memory" do - image = Vips::Image.gaussnoise(2000, 2000).gaussblur(4) - expect(ticks_during { image.write_to_memory }).to be > 0 - end - it "throws an error when trying to load an image from memory with unknown size" do data = FFI::Pointer.new(1) expect { Vips::Image.new_from_memory(data, 16, 16, 1, :uchar) }.to raise_error(Vips::Error)