File: pid_cache.rb

package info (click to toggle)
ruby-dalli 3.2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: ruby: 6,552; sh: 20; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 922 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

module Dalli
  ##
  # Dalli::PIDCache is a wrapper class for PID checking to avoid system calls when checking the PID.
  ##
  module PIDCache
    if !Process.respond_to?(:fork) # JRuby or TruffleRuby
      @pid = Process.pid
      singleton_class.attr_reader(:pid)
    elsif Process.respond_to?(:_fork) # Ruby 3.1+
      class << self
        attr_reader :pid

        def update!
          @pid = Process.pid
        end
      end
      update!

      ##
      # Dalli::PIDCache::CoreExt hooks into Process to be able to reset the PID cache after fork
      ##
      module CoreExt
        def _fork
          child_pid = super
          PIDCache.update! if child_pid.zero?
          child_pid
        end
      end
      Process.singleton_class.prepend(CoreExt)
    else # Ruby 3.0 or older
      class << self
        def pid
          Process.pid
        end
      end
    end
  end
end