Skip to content

Commit

Permalink
Prefer Process.pid over $$
Browse files Browse the repository at this point in the history
It's way more readable, but also it allows `Process.pid` to be cached
by things like https://github.com/Shopify/pid_cache

Hopefully in Ruby 3.3 it would matter anymore though.
  • Loading branch information
byroot authored and eregon committed Feb 21, 2023
1 parent ebf63ce commit 993a252
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/concurrent-ruby/concurrent/async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def initialize(delegate)
@delegate = delegate
@queue = []
@executor = Concurrent.global_io_executor
@ruby_pid = $$
@ruby_pid = Process.pid
end

# Delegates method calls to the wrapped object.
Expand Down Expand Up @@ -346,9 +346,9 @@ def perform
end

def reset_if_forked
if $$ != @ruby_pid
if Process.pid != @ruby_pid
@queue.clear
@ruby_pid = $$
@ruby_pid = Process.pid
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def ns_initialize(opts)
@completed_task_count = 0
@largest_length = 0
@workers_counter = 0
@ruby_pid = $$ # detects if Ruby has forked
@ruby_pid = Process.pid # detects if Ruby has forked

@gc_interval = opts.fetch(:gc_interval, @idletime / 2.0).to_i # undocumented
@next_gc_time = Concurrent.monotonic_time + @gc_interval
Expand Down Expand Up @@ -287,15 +287,15 @@ def ns_prune_pool
end

def ns_reset_if_forked
if $$ != @ruby_pid
if Process.pid != @ruby_pid
@queue.clear
@ready.clear
@pool.clear
@scheduled_task_count = 0
@completed_task_count = 0
@largest_length = 0
@workers_counter = 0
@ruby_pid = $$
@ruby_pid = Process.pid
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/concurrent-ruby/concurrent/executor/timer_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def ns_initialize(opts)
@task_executor = Options.executor_from_options(opts) || Concurrent.global_io_executor
@timer_executor = SingleThreadExecutor.new
@condition = Event.new
@ruby_pid = $$ # detects if Ruby has forked
@ruby_pid = Process.pid # detects if Ruby has forked
end

# Post the task to the internal queue.
Expand Down Expand Up @@ -127,10 +127,10 @@ def ns_shutdown_execution
end

def ns_reset_if_forked
if $$ != @ruby_pid
if Process.pid != @ruby_pid
@queue.clear
@condition.reset
@ruby_pid = $$
@ruby_pid = Process.pid
end
end

Expand Down

0 comments on commit 993a252

Please sign in to comment.