Skip to content

Instantly share code, notes, and snippets.

@kn1kn1
Created February 3, 2016 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kn1kn1/564202800684e362be36 to your computer and use it in GitHub Desktop.
Save kn1kn1/564202800684e362be36 to your computer and use it in GitHub Desktop.
# Stops all threads in the current buffer.
# @example
# load "~/sp-stop-util.rb"
# stop_current_buffer
# @note This method is using the Sonic Pi's internal methods and variables.
# SonicPi::RuntimeMethods#__current_job_info, __stop_job, __current_job_id
# SonicPi::Runtime#user_jobs, job_subthreads
def stop_current_buffer
current_job_info = __current_job_info
current_workspace_name = current_job_info[:workspace] || ''
puts "current workspace name: #{current_workspace_name}"
# find sub threads job id
current_buffer_job_ids = Set.new
@user_jobs.each_id do |job_id|
puts "job_id: #{job_id}"
subthreads = @job_subthreads[job_id]
next unless subthreads
subthreads.each do |st|
info = st.thread_variable_get :sonic_pi_spider_job_info || {}
puts "info: #{info}"
workspace = info[:workspace]
puts "workspace name: #{workspace}"
current_buffer_job_ids.add job_id if workspace == current_workspace_name
end
end
# kill sub threads
current_buffer_job_ids.each do |job_id|
__stop_job job_id
end
# kill current thread at last
__stop_job __current_job_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment