|
15 | 15 | require_relative "../version"
|
16 | 16 | require_relative "../util"
|
17 | 17 | require_relative "../runtime"
|
18 |
| -require_relative "../promise" |
19 | 18 | require_relative "western_theory"
|
20 | 19 |
|
21 | 20 | ## TODO: create _* equivalents of all fns - for silent (i.e computation) versions
|
@@ -2297,130 +2296,22 @@ def live_loop(name=nil, *args, &block)
|
2297 | 2296 | raise ArgumentError, "Live loop block must only accept 0 or 1 args"
|
2298 | 2297 | end
|
2299 | 2298 |
|
2300 |
| - start_live_loop = Promise.new |
2301 |
| - parent_thread = Thread.current |
2302 |
| - mk_live_loop = lambda do |
2303 |
| - in_thread(name: ll_name, delay: delay, sync: sync_sym, sync_bpm: sync_bpm_sym) do |
2304 |
| - |
2305 |
| - start_live_loop.get |
2306 |
| - __system_thread_locals.set_local :sonic_pi_local_live_loop_auto_cue, auto_cue |
2307 |
| - if args_h.has_key?(:init) |
2308 |
| - res = args_h[:init] |
2309 |
| - else |
2310 |
| - res = 0 |
2311 |
| - end |
2312 |
| - use_random_seed args_h[:seed] if args_h[:seed] |
2313 |
| - |
2314 |
| - loop do |
2315 |
| - new_bus = nil |
2316 |
| - new_group = nil |
2317 |
| - new_parent_t = nil |
2318 |
| - completed_prom = nil |
2319 |
| - new_thread_moved_prom = nil |
2320 |
| - new_thread_moved_ack_prom = nil |
2321 |
| - __system_thread_locals.get(:sonic_pi_local_spider_thread_move_mut).synchronize do |
2322 |
| - new_bus, new_group, new_parent_t, completed_prom, new_thread_moved_prom, new_thread_moved_ack_prom = __system_thread_locals.get :sonic_pi_local_live_loop_move_to_bus_and_parent_t |
2323 |
| - |
2324 |
| - # immediately reset for the next move |
2325 |
| - __system_thread_locals.set_local :sonic_pi_local_live_loop_move_to_bus_and_parent_t, nil |
2326 |
| - end |
2327 |
| - if new_bus |
2328 |
| - moved_prom = __system_thread_locals.get :sonic_pi_local_spider_thread_moved |
2329 |
| - ack_prom = __system_thread_locals.get :sonic_pi_local_spider_thread_moved_ack |
2330 |
| - # update the context |
2331 |
| - # reset tracker and send old tracker with ack |
2332 |
| - tracker = __current_tracker |
2333 |
| - __system_thread_locals.set_local(:sonic_pi_local_tracker, nil) |
2334 |
| - __system_thread_locals.set(:sonic_pi_mod_sound_synth_out_bus, new_bus) |
2335 |
| - __system_thread_locals.set(:sonic_pi_mod_sound_job_group, new_group) |
2336 |
| - moved_prom.deliver! tracker |
2337 |
| - ack_prom.get |
2338 |
| - __system_thread_locals.set_local :sonic_pi_local_spider_thread_moved, new_thread_moved_prom |
2339 |
| - __system_thread_locals.set_local :sonic_pi_local_spider_thread_moved_ack, new_thread_moved_ack_prom |
2340 |
| - __system_thread_locals.set_local :sonic_pi_local_live_loop_move_to_bus_and_parent_t, nil |
2341 |
| - new_job_id = __system_thread_locals(new_parent_t).get(:sonic_pi_spider_job_id) |
2342 |
| - job_subthread_move_named(Thread.current, new_job_id, ll_name) |
2343 |
| - __remove_thread_from_parent_subthreads!(Thread.current) |
2344 |
| - __move_thread_to_new_parent!(Thread.current, new_parent_t) |
2345 |
| - completed_prom.deliver! :moved |
2346 |
| - end |
2347 |
| - __live_loop_cue name if __system_thread_locals.get :sonic_pi_local_live_loop_auto_cue |
2348 |
| - res = send(ll_name, res) |
2349 |
| - end |
| 2299 | + in_thread(name: ll_name, delay: delay, sync: sync_sym, sync_bpm: sync_bpm_sym) do |
| 2300 | + __system_thread_locals.set_local :sonic_pi_local_live_loop_auto_cue, auto_cue |
| 2301 | + if args_h.has_key?(:init) |
| 2302 | + res = args_h[:init] |
| 2303 | + else |
| 2304 | + res = 0 |
| 2305 | + end |
| 2306 | + use_random_seed args_h[:seed] if args_h[:seed] |
| 2307 | + loop do |
| 2308 | + __live_loop_cue name if __system_thread_locals.get :sonic_pi_local_live_loop_auto_cue |
| 2309 | + res = send(ll_name, res) |
2350 | 2310 | end
|
2351 | 2311 | end
|
2352 | 2312 |
|
2353 |
| - live_loop_thread = mk_live_loop.call |
2354 | 2313 | st = sthread(ll_name)
|
2355 |
| - if live_loop_thread.alive? |
2356 |
| - |
2357 |
| - # live loop was created - this was the first time |
2358 |
| - # let it start normally |
2359 |
| - start_live_loop.deliver! :go |
2360 |
| - else |
2361 |
| - new_out_bus = current_out_bus |
2362 |
| - new_synth_group = current_group |
2363 |
| - in_thread do |
2364 |
| - new_thread_moved_prom = __system_thread_locals.get(:sonic_pi_local_spider_thread_moved) |
2365 |
| - new_thread_moved_ack_prom = __system_thread_locals.get(:sonic_pi_local_spider_thread_moved_ack) |
2366 |
| - move_holding_thread_prom = Promise.new |
2367 |
| - Thread.new do |
2368 |
| - __system_thread_locals.set_local :sonic_pi_local_thread_group, "ll swapper and holding thread for #{name} #{st}" |
2369 |
| - completed_prom = Promise.new |
2370 |
| - |
2371 |
| - |
2372 |
| - ct1 = Thread.new do |
2373 |
| - __system_thread_locals.set_local :sonic_pi_local_thread_group, "ll ct1 mv waiter for #{name} #{st.object_id} #{__system_thread_locals(st).get(:sonic_pi_local_thread_group)}" |
2374 |
| - __system_thread_locals(st).get(:sonic_pi_local_spider_thread_move_mut).synchronize do |
2375 |
| - |
2376 |
| - _b, _g, t, p = __system_thread_locals(st).get(:sonic_pi_local_live_loop_move_to_bus_and_parent_t) |
2377 |
| - if p |
2378 |
| - ## another live loop already registered a move, but didn't manage to swap in time - so we're going to clobber it |
2379 |
| - p.deliver! :clobbered, false |
2380 |
| - end |
2381 |
| - __system_thread_locals(st).set_local :sonic_pi_local_live_loop_auto_cue, auto_cue if st |
2382 |
| - ## register our move |
2383 |
| - |
2384 |
| - __system_thread_locals(st).set_local :sonic_pi_local_live_loop_move_to_bus_and_parent_t, [new_out_bus, new_synth_group, parent_thread, completed_prom, new_thread_moved_prom, new_thread_moved_ack_prom] |
2385 |
| - end |
2386 |
| - end |
2387 |
| - |
2388 |
| - ct2 = Thread.new do |
2389 |
| - __system_thread_locals.set_local :sonic_pi_local_thread_group, "ll ct2 join waiter for #{name} #{st.object_id} #{__system_thread_locals(st).get(:sonic_pi_local_thread_group)}" |
2390 |
| - st.join |
2391 |
| - completed_prom.deliver! :joined, false |
2392 |
| - end |
2393 |
| - |
2394 |
| - moved_or_clobbered = completed_prom.get |
2395 |
| - ct1.kill |
2396 |
| - ct2.kill |
2397 |
| - |
2398 |
| - if moved_or_clobbered == :moved |
2399 |
| - st_joined_or_moved_again = Promise.new |
2400 |
| - mt1 = Thread.new do |
2401 |
| - __system_thread_locals.set_local :sonic_pi_local_thread_group, "ll join waiter for #{name} #{st.object_id} #{__system_thread_locals(st).get(:sonic_pi_local_thread_group)}" |
2402 |
| - st.join |
2403 |
| - st_joined_or_moved_again.deliver! true, false |
2404 |
| - end |
2405 |
| - |
2406 |
| - mt2 = Thread.new do |
2407 |
| - __system_thread_locals.set_local :sonic_pi_local_thread_group, "ll move waiter for #{name} #{st.object_id} #{__system_thread_locals(st).get(:sonic_pi_local_thread_group)}" |
2408 |
| - |
2409 |
| - ack_p = __system_thread_locals(st).get(:sonic_pi_local_spider_thread_moved_ack) |
2410 |
| - ack_p.get |
2411 |
| - st_joined_or_moved_again.deliver! true, false |
2412 |
| - end |
2413 |
| - st_joined_or_moved_again.get |
2414 |
| - mt1.kill |
2415 |
| - mt2.kill |
2416 |
| - end |
2417 |
| - move_holding_thread_prom.deliver! true |
2418 |
| - end |
2419 |
| - tracker = new_thread_moved_prom.get |
2420 |
| - tracker.get |
2421 |
| - move_holding_thread_prom.get |
2422 |
| - end |
2423 |
| - end |
| 2314 | + __system_thread_locals(st).set_local :sonic_pi_local_live_loop_auto_cue, auto_cue if st |
2424 | 2315 | st
|
2425 | 2316 | end
|
2426 | 2317 | doc name: :live_loop,
|
|
0 commit comments