Skip to content

Commit ed6103c

Browse files
authored
Merge pull request #336 from jeremy/concurrency-default
Reset concurrency to the initial default
2 parents 3865cd6 + 3e6cd18 commit ed6103c

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
@@ -627,6 +627,10 @@ def to_s
627627
attach_function :vips_vector_set_enabled, [:int], :void
628628
attach_function :vips_vector_isenabled, [], :int
629629
attach_function :vips_concurrency_set, [:int], :void
630+
attach_function :vips_concurrency_get, [], :int
631+
632+
# Track the original default concurrency so we can reset to it.
633+
DEFAULT_CONCURRENCY = vips_concurrency_get
630634

631635
# vips_foreign_get_suffixes was added in libvips 8.8
632636
begin
@@ -721,10 +725,23 @@ def self.cache_drop_all # :nodoc:
721725
vips_cache_drop_all
722726
end
723727

724-
# Set the size of the libvips worker pool. This defaults to the number of
725-
# hardware threads on your computer. Set to 1 to disable threading.
728+
# Get the size of libvips worker pools. Defaults to the VIPS_CONCURRENCY env
729+
# var or the number of hardware threads on your computer.
730+
def self.concurrency
731+
vips_concurrency_get
732+
end
733+
734+
# Get the default size of libvips worker pools.
735+
def self.concurrency_default
736+
DEFAULT_CONCURRENCY
737+
end
738+
739+
# Set the size of each libvips worker pool. Max 1024 threads. Set to 1 to
740+
# disable threading. Set to 0 or nil to reset to default.
726741
def self.concurrency_set n
742+
n = DEFAULT_CONCURRENCY if n.to_i == 0
727743
vips_concurrency_set n
744+
concurrency
728745
end
729746

730747
# Whether SIMD and the run-time compiler are enabled. 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
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 "sets SIMD" do

0 commit comments

Comments
 (0)