Module: CrossThreadUsageDetector

Included in:
AsyncScheduler::Scheduler
Defined in:
lib/async_scheduler/cross_thread_usage_detector.rb

Defined Under Namespace

Classes: BelongingThreadAlreadySetError, CrossThreadUsageError

Instance Method Summary collapse

Instance Method Details

#set_belonging_thread!Object



5
6
7
8
9
10
11
# File 'lib/async_scheduler/cross_thread_usage_detector.rb', line 5

def set_belonging_thread!
  if defined? @belonging_thread_object_id
    raise BelongingThreadAlreadySetError.new("@belonging_thread_object_id is already set with #{@belonging_thread_object_id}, but it is attempted to set again with #{Thread.current.object_id}.")
  end

  @belonging_thread_object_id = Thread.current.object_id
end

#validate_used_in_original_thread!Object



13
14
15
16
17
# File 'lib/async_scheduler/cross_thread_usage_detector.rb', line 13

def validate_used_in_original_thread!
  return if @belonging_thread_object_id == Thread.current.object_id

  raise CrossThreadUsageError.new("Cross-thread usage detected. FiberScheduler was originally registered to a thread (#{@belonging_thread_object_id}), but it is attempted to be used in another thread (#{Thread.current.object_id}).")
end