8000 Reset concurrency to the initial default by setting 0 or nil · libvips/ruby-vips@3e6cd18 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e6cd18

Browse files
committed
Reset concurrency to the initial default by setting 0 or nil
`Vips.concurrency_set(0)` suggests it'll set concurrency back to the default value, but it does nothing if concurrency is already set, which is always true because Vips.init has already configured thread pools. The result is that if you set concurrency to any value, setting it to zero will leave that value in place. This reads the default concurrency immediately after Vips.init so we can make good on having `concurrency_set 0` reset to default. Also allows setting `nil` to reset to default. Note that setting concurrency does not reconfigure existing thread pools. It affects thread pools spawned for future operations.
1 parent 53d279b commit 3e6cd18

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

lib/vips.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@ def to_s
626626
attach_function :vips_leak_set, [:int], :void
627627
attach_function :vips_vector_set_enabled, [:int], :void
628628
attach_function :vips_concurrency_set, [:int], :void
629+
attach_function :vips_concurrency_get, [], :int
630+
631+
# Track the original default concurrency so we can reset to it.
632+
DEFAULT_CONCURRENCY = vips_concurrency_get
629633

630634
# vips_foreign_get_suffixes was added in libvips 8.8
631635
begin
@@ -663,10 +667,23 @@ def self.cache_set_max_files size
663667
vips_cache_set_max_files size
664668
end
665669

666-
# Set the size of the libvips worker pool. This defaults to the number of
667-
# hardware threads on your computer. Set to 1 to disable threading.
670+
# Get the size of libvips worker pools. Defaults to the VIPS_CONCURRENCY env
671+
# var or the number of hardware threads on your computer.
672+
def self.concurrency
673+
vips_concurrency_get
674+
end
675+
676+
# Get the default size of libvips worker pools.
677+
def self.concurrency_default
678+
DEFAULT_CONCURRENCY
679+
end
680+
681+
# Set the size of each libvips worker pool. Max 1024 threads. Set to 1 to
682+
# disable threading. Set to 0 or nil to reset to default.
668683
def self.concurrency_set n
684+
n = DEFAULT_CONCURRENCY if n.to_i == 0
669685
vips_concurrency_set n
686+
concurrency
670687
end
671688

672689
# Enable or disable SIMD and the run-time compiler. This can give a nice

spec/spec_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Set default concurrency so we can check against it later. Must be set
2+
# before Vips.init sets concurrency to the default.
3+
DEFAULT_VIPS_CONCURRENCY = 5
4+
ENV["VIPS_CONCURRENCY"] = DEFAULT_VIPS_CONCURRENCY.to_s
5+
6+
# Disable stderr output since we purposefully trigger warn-able behavior.
7+
ENV["VIPS_WARNING"] = "1"
8+
19
require "vips"
210

311
require "tempfile"

spec/vips_spec.rb

Lines changed: 24 additions & 1 deletion
< 318D /tr>
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,31 @@
22

33
RSpec.describe Vips do
44
describe "Vips" do
5+
it "can get default concurrency" do
6+
expect(Vips.concurrency_default).to eq DEFAULT_VIPS_CONCURRENCY
7+
end
8+
9+
it "can get concurrency" do
10+
expect(Vips.concurrency).to eq Vips.concurrency_default
11+
end
12+
513
it "can set concurrency" do
6-
Vips.concurrency_set 12
14+
expect(Vips.concurrency_set(12)).to eq 12
15+
expect(Vips.concurrency).to eq 12
16+
end
17+
18+
it "clips concurrency to 1024" do
19+
expect(Vips.concurrency_set(1025)).to eq 1024
20+
end
21+
22+
it "can set concurrency to 0 to reset to default" do
23+
Vips.concurrency_set(rand(100))
24+
expect(Vips.concurrency_set(0)).to eq Vips.concurrency_default
25+
end
26+
27+
it "can set concurrency to nil to reset to default" do
28+
Vips.concurrency_set(rand(100))
29+
expect(Vips.concurrency_set(nil)).to eq Vips.concurrency_default
730
end
831

932
it "can set SIMD" do

0 commit comments

Comments
 (0)
0