Debian Codegraph @

src/core/chuck_vm.h

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: chuck_vm.h
28 // desc: chuck virtual machine
29 //
30 // author: Ge Wang (https://ccrma.stanford.edu/~ge/)
31 // date: Autumn 2002
32 //-----------------------------------------------------------------------------
33 #ifndef __CHUCK_VM_H__
34 #define __CHUCK_VM_H__
35
36 #include <string>
37 #include <map>
38 #include <vector>
39 #include <list>
40
41 #include "chuck_oo.h"
42 #include "chuck_ugen.h"
43 #include "chuck_type.h"
44 #include "chuck_carrier.h"
45
46 // tracking
47 #ifdef __CHUCK_STAT_TRACK__
48 #include "chuck_stats.h"
49 #endif
50
51
52
53
54 //-----------------------------------------------------------------------------
55 // vm defines
56 //-----------------------------------------------------------------------------
57 #define CKVM_MEM_STACK_SIZE (0x1 << 16)
58 #define CKVM_REG_STACK_SIZE (0x1 << 14)
59
60
61 // forward references
62 struct Chuck_Instr;
63 struct Chuck_VM;
64 struct Chuck_VM_Func;
65 struct Chuck_VM_FTable;
66 struct Chuck_Msg;
67 struct Chuck_Globals_Manager; // added 1.4.1.0 (jack)
68 struct Chuck_Instr_Reg_Push_Imm; // 1.5.1.5 (ge)
69 class CBufferSimple;
70 #ifndef __DISABLE_SERIAL__
71 // hack: spencer?
72 struct Chuck_IO_Serial;
73 #endif
74
75
76
77
78 //-----------------------------------------------------------------------------
79 // name: struct Chuck_VM_Stack
80 // desc: a VM stack; each shred has at least two (mem and reg)
81 //-----------------------------------------------------------------------------
82 struct Chuck_VM_Stack
83 {
84 //-----------------------------------------------------------------------------
85 // functions
86 //-----------------------------------------------------------------------------
87 public:
88 // constructor
89 Chuck_VM_Stack();
90 // destructor
91 ~Chuck_VM_Stack();
92
93 public:
94 // initialize stack of at least 'size' bytes
95 t_CKBOOL initialize( t_CKUINT size );
96 // shutdown and cleanup stack
97 t_CKBOOL shutdown();
98
99 //-----------------------------------------------------------------------------
100 // data
101 //-----------------------------------------------------------------------------
102 public: // machine
103 t_CKBYTE * stack;
104 t_CKBYTE * sp;
105 t_CKBYTE * sp_max;
106
107 public: // linked list
108 Chuck_VM_Stack * prev;
109 Chuck_VM_Stack * next;
110
111 public: // state
112 t_CKBOOL m_is_init;
113 t_CKUINT m_size; // 1.5.1.5
114 };
115
116
117
118
119 //-----------------------------------------------------------------------------
120 // name: struct Chuck_VM_Code
121 // desc: ...
122 //-----------------------------------------------------------------------------
123 struct Chuck_VM_Code : public Chuck_Object
124 {
125 public:
126 // constructor
127 Chuck_VM_Code();
128 // destructor
129 virtual ~Chuck_VM_Code();
130
131 public:
132 // array of Chuck_Instr *, should always end with Chuck_Instr_EOF
133 Chuck_Instr ** instr;
134 // size of the array
135 t_CKUINT num_instr;
136
137 // name of this code
138 std::string name;
139 // the depth of any function arguments (in bytes)
140 t_CKUINT stack_depth;
141 // whether the function needs 'this' pointer or not
142 t_CKBOOL need_this;
143 // whether the function is a static function inside class
144 t_CKBOOL is_static; // 1.4.1.0
145 // native
146 t_CKUINT native_func;
147 // what kind of native func?
148 ae_FuncPointerKind native_func_kind;
149
150 // filename this code came from (added 1.3.0.0)
151 std::string filename;
152 };
153
154
155
156
157 //-----------------------------------------------------------------------------
158 // name: struct Chuck_VM_Shred
159 // desc: ...
160 //-----------------------------------------------------------------------------
161 struct Chuck_VM_Shred : public Chuck_Object
162 {
163 //-----------------------------------------------------------------------------
164 // functions
165 //-----------------------------------------------------------------------------
166 public:
167 // constructor
168 Chuck_VM_Shred( );
169 // destructor
170 virtual ~Chuck_VM_Shred( );
171
172 // initialize shred
173 t_CKBOOL initialize( Chuck_VM_Code * c,
174 t_CKUINT mem_st_size = 0,
175 t_CKUINT reg_st_size = 0 );
176 // shutdown shred
177 t_CKBOOL shutdown();
178 // run the shred on vm
179 t_CKBOOL run( Chuck_VM * vm );
180 // yield the shred in vm (without advancing time, politely yield to run
181 // all other shreds waiting to run at the current (i.e., 0::second +=> now;)
182 t_CKBOOL yield(); // 1.5.0.5 (ge) made this a function from scattered code
183 // add parent object reference (added 1.3.1.2)
184 t_CKVOID add_parent_ref( Chuck_Object * obj );
185 // add get shred id | 1.5.0.8 (ge)
186 t_CKUINT get_id() const { return this->xid; }
187
188 public:
189 // associate ugen with shred
190 t_CKBOOL add( Chuck_UGen * ugen );
191 // unassociate ugen with shred
192 t_CKBOOL remove( Chuck_UGen * ugen );
193 // detach all associate ugens | 1.5.1.5 (ge) added
194 void detach_ugens();
195 // remove all attached ugen, releasing ones without external reference
196 void prune_ugens();
197
198 public:
199 // manually trigger a per-shred garbage collection pass | 1.5.2.0 (ge) added
200 void gc();
201 // acrue towards a GC pass
202 void gc_inc( t_CKDUR inc );
203 // affects children shreds sporked from this one
204 // mem is memory / call stack (for local vars)
205 t_CKINT childSetMemSize( t_CKINT sizeInBytes );
206 t_CKINT childGetMemSize( );
207 // reg is rester / operand stack (for evaluating expressions)
208 t_CKINT childSetRegSize( t_CKINT sizeInBytes );
209 t_CKINT childGetRegSize( );
210
211 #ifndef __DISABLE_SERIAL__
212 // HACK - spencer (added 1.3.2.0)
213 // add/remove SerialIO devices to close on shred exit
214 // REFACTOR-2017: TODO -- remove
215 t_CKVOID add_serialio( Chuck_IO_Serial * serial );
216 t_CKVOID remove_serialio( Chuck_IO_Serial * serial );
217 #endif
218
219 //-----------------------------------------------------------------------------
220 // data
221 //-----------------------------------------------------------------------------
222 public: // machine components
223 // stacks; mem for "memory" and reg for "register"
224 Chuck_VM_Stack * mem; // call stack
225 Chuck_VM_Stack * reg; // operand stack
226
227 // ref to base stack - if this is the root, then base is mem
228 Chuck_VM_Stack * base_ref;
229
230 // current vm code being run (this could change, e.g., across func calls)
231 Chuck_VM_Code * code;
232 // the original vm code attached to this shred (also: the one to release)
233 Chuck_VM_Code * code_orig;
234 // instructions
235 Chuck_Instr ** instr;
236
237 // parent shred
238 Chuck_VM_Shred * parent;
239 // children shreds
240 std::map<t_CKUINT, Chuck_VM_Shred *> children;
241
242 // child stack size hints | 1.5.1.5
243 t_CKINT memStackSize;
244 t_CKINT regStackSize;
245
246 // program counter
247 t_CKUINT pc;
248 // time
249 t_CKTIME now;
250 // when started, in chuck time, relative to start of VM
251 t_CKTIME start;
252 // vm reference
253 Chuck_VM * vm_ref;
254
255 public:
256 // shred state
257 t_CKTIME wake_time;
258 t_CKUINT next_pc;
259 t_CKBOOL is_done;
260 t_CKBOOL is_running;
261 t_CKBOOL is_abort;
262 t_CKBOOL is_dumped;
263
264 // event shred is waiting on
265 Chuck_Event * event;
266
267 public: // id
268 t_CKUINT xid;
269 std::string name;
270 std::vector<std::string> args;
271
272 public:
273 // linked list
274 Chuck_VM_Shred * prev;
275 Chuck_VM_Shred * next;
276
277 public:
278 // tracking
279 CK_TRACK( Shred_Stat * stat );
280
281 public:
282 // map of ugens for the shred
283 std::map<Chuck_UGen *, Chuck_UGen *> m_ugen_map;
284 // references kept by the shred itself (e.g., when sporking member functions)
285 // to be released when shred is done -- added 1.3.1.2
286 std::vector<Chuck_Object *> m_parent_objects;
287
288 public: // ge: 1.3.5.3
289 // make and push new loop counter
290 t_CKUINT * pushLoopCounter();
291 // get top loop counter
292 t_CKUINT * currentLoopCounter();
293 // pop and clean up loop counter
294 bool popLoopCounter();
295 // loop counter pointer stack
296 std::vector<t_CKUINT *> m_loopCounters;
297
298 public: // immediate mode temporal restriction | 1.5.1.5 (ge)
299 // while in this mode, exception will be thrown on any time ops:
300 // 1) if shred advances time (even by 0 duration)
301 // 2) if shred waits on event
302 // 3) if shred yields, or calls Machine.eval()
303 // which implicits yields to runs code on a new shred
304 // this is typically set in specific cases where the programmer
305 // is expected to provide a callback function that must return
306 // immediately without any shreduling; for example Chugen.tick( float in )
307 // or GGen.update( float dt );
308
309 // toggle immediate mode
310 void setImmediateMode( t_CKBOOL onOff ) { is_immediate_mode = onOff; }
311 // check if shred is in immediate mode
312 t_CKBOOL immediateMode() const { return is_immediate_mode; }
313 // check if shred is in immediate mode
314 t_CKBOOL immediateModeVioation() const { return is_immediate_mode_violation; }
315 // test and report for immediate mode violations
316 t_CKBOOL checkImmediatModeException( t_CKUINT linepos = 0 );
317
318 protected:
319 t_CKBOOL is_immediate_mode;
320 t_CKBOOL is_immediate_mode_violation;
321
322 protected:
323 t_CKDUR m_gc_inc; // current GC increment (in samps)
324 t_CKDUR m_gc_threshold; // threshold (in samps) beyond which will trigger a gc()
325
326 #ifndef __DISABLE_SERIAL__
327 private:
328 // serial IO list for event synchronization
329 std::list<Chuck_IO_Serial *> * m_serials;
330 #endif
331 };
332
333
334
335
336 //-----------------------------------------------------------------------------
337 // name: struct Chuck_VM_Shreds_Watcher
338 // desc: ChucK virtual machine
339 //-----------------------------------------------------------------------------
340 struct Chuck_VM_Shreds_Watcher
341 {
342 // function pointer to call
343 f_shreds_watcher cb;
344 // user data
345 void * userdata;
346
347 // constructor
348 Chuck_VM_Shreds_Watcher( f_shreds_watcher f = NULL, void * data = NULL ) : cb(f), userdata(data) { }
349 // copy constructor
350 Chuck_VM_Shreds_Watcher( const Chuck_VM_Shreds_Watcher & other )
351 : cb(other.cb), userdata(other.userdata) { }
352 // ==
353 bool operator ==( const Chuck_VM_Shreds_Watcher & other )
354 { return this->cb == other.cb; }
355 };
356
357
358
359
360 //-----------------------------------------------------------------------------
361 // name: struct Chuck_VM_Shred_Status
362 // desc: status pertaining to a single shred
363 //-----------------------------------------------------------------------------
364 struct Chuck_VM_Shred_Status : public Chuck_Object
365 {
366 public:
367 t_CKUINT xid;
368 std::string name;
369 t_CKTIME start;
370 t_CKBOOL has_event;
371
372 public:
373 // constructor
374 Chuck_VM_Shred_Status( t_CKUINT _id, const std::string & n, t_CKTIME _start, t_CKBOOL e )
375 {
376 xid = _id;
377 name = n;
378 start = _start;
379 has_event = e;
380 }
381
382 // destructor
383 virtual ~Chuck_VM_Shred_Status() { }
384 };
385
386
387
388
389 //-----------------------------------------------------------------------------
390 // name: struct Chuck_VM_Status
391 // desc: vm status
392 //-----------------------------------------------------------------------------
393 struct Chuck_VM_Status : public Chuck_Object
394 {
395 public:
396 Chuck_VM_Status();
397 virtual ~Chuck_VM_Status();
398 void clear();
399
400 public:
401 // sample rate
402 t_CKUINT srate;
403 // now
404 t_CKTIME now_system;
405 // for display
406 t_CKUINT t_second;
407 t_CKUINT t_minute;
408 t_CKUINT t_hour;
409 // list of shred status
410 std::vector<Chuck_VM_Shred_Status *> list;
411 };
412
413
414
415
416 //-----------------------------------------------------------------------------
417 // name: struct Chuck_VM_Callback_On_SampleRate_Update | 1.5.4.2 (ge) added
418 // desc: an entry for a callback to be called when system sample rate changes
419 //-----------------------------------------------------------------------------
420 struct Chuck_VM_Callback_On_SampleRate_Update
421 {
422 // function pointer to call
423 f_callback_on_srate_update cb;
424 // user data
425 void * userdata;
426
427 // constructor
428 Chuck_VM_Callback_On_SampleRate_Update( f_callback_on_srate_update f = NULL, void * data = NULL )
429 : cb(f), userdata(data) { }
430
431 // copy constructor
432 Chuck_VM_Callback_On_SampleRate_Update( const Chuck_VM_Callback_On_SampleRate_Update & other )
433 : cb(other.cb), userdata(other.userdata) { }
434
435 // ==
436 bool operator ==( const Chuck_VM_Callback_On_SampleRate_Update & other )
437 { return this->cb == other.cb && this->userdata == other.userdata; }
438 };
439
440
441
442
443 //-----------------------------------------------------------------------------
444 // name: struct Chuck_VM_Callback_On_Shutdown
445 // desc: an entry for a callback to be called on VM shutdown
446 //-----------------------------------------------------------------------------
447 struct Chuck_VM_Callback_On_Shutdown
448 {
449 // function pointer to call
450 f_callback_on_shutdown cb;
451 // user data
452 void * userdata;
453
454 // constructor
455 Chuck_VM_Callback_On_Shutdown( f_callback_on_shutdown f = NULL, void * data = NULL )
456 : cb(f), userdata(data) { }
457
458 // copy constructor
459 Chuck_VM_Callback_On_Shutdown( const Chuck_VM_Callback_On_Shutdown & other )
460 : cb(other.cb), userdata(other.userdata) { }
461
462 // ==
463 bool operator ==( const Chuck_VM_Callback_On_Shutdown & other )
464 { return this->cb == other.cb && this->userdata == other.userdata; }
465 };
466
467
468
469
470 //-----------------------------------------------------------------------------
471 // name: struct Chuck_VM_Shreduler
472 // desc: a ChucK shreduler shredules shreds
473 //-----------------------------------------------------------------------------
474 struct Chuck_VM_Shreduler : public Chuck_Object
475 {
476 //-----------------------------------------------------------------------------
477 // functions
478 //-----------------------------------------------------------------------------
479 public:
480 // constructor
481 Chuck_VM_Shreduler();
482 // destructor
483 virtual ~Chuck_VM_Shreduler();
484
485 public: // initialization and shutdown
486 // initialize shreduler
487 t_CKBOOL initialize();
488 // shutdown shreduler
489 t_CKBOOL shutdown();
490
491 public: // shreduling (shred interface part 1)
492 // shredule a shred in the shreduler (equivalent to ADD)
493 t_CKBOOL shredule( Chuck_VM_Shred * shred );
494 // shredule a shred in the shreduler with a specific wake time
495 t_CKBOOL shredule( Chuck_VM_Shred * shred, t_CKTIME wake_time );
496 // get next shred to run
497 Chuck_VM_Shred * get();
498 // advance shreduler
499 void advance( t_CKINT N );
500 // advance shreduler vectorized edition
501 void advance_v( t_CKINT & num_left, t_CKINT & offset );
502 // set adaptive mode and adaptive max block size
503 void set_adaptive( t_CKUINT max_block_size );
504
505 public: // remove, replace, status (shred interface part 2)
506 // remove a shred from the shreduler
507 t_CKBOOL remove( Chuck_VM_Shred * shred );
508 // replace a shred with another shred
509 t_CKBOOL replace( Chuck_VM_Shred * out, Chuck_VM_Shred * in );
510 // print status
511 void status();
512 // get status
513 void status( Chuck_VM_Status * status );
514
515 public: // shred lookup and retrieval (shred interface part 3)
516 // look a shred by ID
517 Chuck_VM_Shred * lookup( t_CKUINT xid ) const;
518 // get ID of shred currently with the highest ID
519 t_CKUINT highest() const;
520
521 // retrieve the ready list of shreds in the shreduler
522 // NOTE shreds on the shreduler's ready list are sorted by
523 // wake-up time (e.g., as specifiedy by `second => now`)
524 // NOTE the ready list DO NOT include shreds waiting on Events
525 // waiting shreds are on the shreduler's blocked list
526 // NOTE the ready list does not include the shred currently
527 // executing in the VM
528 void get_ready_shreds( std::vector<Chuck_VM_Shred *> & shreds,
529 t_CKBOOL clearVector = TRUE ) const;
530 // retrieve list of active shred IDs
531 void get_ready_shred_ids( std::vector<t_CKUINT> & shredIDs,
532 t_CKBOOL clearVector = TRUE ) const;
533
534 // retrieve the list of waiting shreds in the shreduler
535 // NOTE a waiting shred is on the shreduler's blocked list as long as
536 // it is currently waiting on an Event (e.g., `event => now;`)
537 // NOTE the shreduler's blocked list is unsorted; FYI a sorted list of
538 // shreds is maintained by each Event
539 void get_blocked_shreds( std::vector<Chuck_VM_Shred *> & shreds,
540 t_CKBOOL clearVector = TRUE ) const;
541 // retrieve list of blocked shred IDs
542 void get_blocked_shred_ids( std::vector<t_CKUINT> & shredIDs,
543 t_CKBOOL clearVector = TRUE ) const;
544
545 // get currently executing shred
546 // NOTE this can only be non-NULL during a Chuck_VM::compute() cycle
547 Chuck_VM_Shred * get_current_shred() const;
548
549 // retrieve all shreds currently in the shreduler; this includes the
550 // ready list, the blocked list, and the currently executing shred
551 void get_all_shreds( std::vector<Chuck_VM_Shred *> & shreds,
552 t_CKBOOL clearVector = TRUE ) const;
553 // retrieve list of all shred IDs
554 void get_all_shred_ids( std::vector<t_CKUINT> & shredIDs,
555 t_CKBOOL clearVector = TRUE ) const;
556
557 public: // for event related shred queue (shred interface part 4)
558 // (should only be called from under the hood)
559 t_CKBOOL add_blocked( Chuck_VM_Shred * shred );
560 t_CKBOOL remove_blocked( Chuck_VM_Shred * shred );
561
562 //-----------------------------------------------------------------------------
563 // data
564 //-----------------------------------------------------------------------------
565 public:
566 // time and audio -- this is the chuck `now`
567 t_CKTIME now_system;
568 // added ge: 1.3.5.3
569 Chuck_VM * vm_ref;
570
571 // shreds to be shreduled
572 Chuck_VM_Shred * shred_list;
573 // shreds waiting on events
574 std::map<Chuck_VM_Shred *, Chuck_VM_Shred *> blocked;
575 // current shred | TODO: ref count?
576 Chuck_VM_Shred * m_current_shred;
577
578 // ugen
579 Chuck_UGen * m_dac;
580 Chuck_UGen * m_adc;
581 Chuck_UGen * m_bunghole;
582 t_CKUINT m_num_dac_channels;
583 t_CKUINT m_num_adc_channels;
584
585 // status cache
586 Chuck_VM_Status m_status;
587
588 // max block size for adaptive block processing
589 t_CKUINT m_max_block_size;
590 t_CKBOOL m_adaptive;
591 t_CKDUR m_samps_until_next;
592 };
593
594
595
596
597 //-----------------------------------------------------------------------------
598 // name: struct Chuck_VM
599 // desc: ChucK virtual machine
600 //-----------------------------------------------------------------------------
601 struct Chuck_VM : public Chuck_Object
602 {
603 //-----------------------------------------------------------------------------
604 // functions
605 //-----------------------------------------------------------------------------
606 public:
607 // constructor
608 Chuck_VM();
609 // destructor
610 virtual ~Chuck_VM();
611
612 public:
613 // initialize VM
614 t_CKBOOL initialize( t_CKUINT srate, t_CKUINT dac_chan, t_CKUINT adc_chan,
615 t_CKUINT adaptive, t_CKBOOL halt );
616 // initialize synthesis
617 t_CKBOOL initialize_synthesis();
618 // set carrier reference
619 t_CKBOOL setCarrier( Chuck_Carrier * c ) { m_carrier = c; return TRUE; }
620 // shutdown VM
621 t_CKBOOL shutdown();
622 // return whether VM is initialized
623 t_CKBOOL has_init() { return m_init; }
624 // update sample rate (this will also trigger notifications for srate update)
625 t_CKBOOL update_srate( t_CKUINT srate );
626
627 public: // run state; 1.3.5.3
628 // run start
629 t_CKBOOL start();
630 // get run state
631 t_CKBOOL running() const;
632 // run stop
633 t_CKBOOL stop();
634 // const access state directly (should be called from inside VM only)
635 const t_CKBOOL & runningState() const { return m_is_running; }
636
637 public: // shredsuck
638 // spork code as shred; if not immediate, enqueue for next sample
639 // REFACTOR-2017: added immediate flag
640 Chuck_VM_Shred * spork( Chuck_VM_Code * code, Chuck_VM_Shred * parent,
641 t_CKBOOL immediate = FALSE );
642 // get reference to shreduler
643 Chuck_VM_Shreduler * shreduler() const;
644 // the next spork ID
645 t_CKUINT next_id( const Chuck_VM_Shred * shred = NULL );
646 // the last used spork ID
647 t_CKUINT last_id() const;
648 // reset ID to highest current ID + 1; returns what next ID would be
649 t_CKUINT reset_id();
650 // the current chuck time | 1.5.0.8
651 t_CKTIME now() const;
652
653 public: // audio
654 t_CKUINT srate() const;
655
656 public: // running the machine
657 // compute next N frames
658 t_CKBOOL run( t_CKINT numFrames, const SAMPLE * input, SAMPLE * output );
659 // compute all shreds for current time
660 t_CKBOOL compute();
661 // abort current running shred
662 t_CKBOOL abort_current_shred();
663 // get currently executing shred | 1.5.1.8 (ge) now in VM, in addition to shreduler
664 // NOTE this can only be non-NULL during a Chuck_VM::compute() cycle
665 Chuck_VM_Shred * get_current_shred() const;
666 // remove all shreds asap (thread-safe) | 1.5.4.4 (ge) added
667 void remove_all_shreds();
668
669 public: // invoke functions
670 t_CKBOOL invoke_static( Chuck_VM_Shred * shred );
671
672 public: // garbage collection
673 // manually trigger a VM-level garbage collection pass | 1.5.2.0 (ge) added
674 void gc();
675
676 public: // VM message queue
677 // queue message to process at next VM compute block (thread-safe but not synchronous)
678 // NOTE assumes msg is dynamically allocated using `new`; will be deleted by VM
679 // NOTE this defers the processing of the msg to the VM compute thread
680 t_CKBOOL queue_msg( Chuck_Msg * msg, t_CKINT num_msg = 1 );
681 // process a VM message immediately (synchronous but not thread-safe)
682 // NOTE assumes msg is dynamically allocated using `new`; will be deleted by VM
683 // NOTE this processes the msg immediately on calling thread
684 t_CKUINT process_msg( Chuck_Msg * & msg );
685 // get reply from reply buffer
686 Chuck_Msg * get_reply();
687
688 // CBufferSimple added 1.3.0.0 to fix uber-crash
689 t_CKBOOL queue_event( Chuck_Event * event, t_CKINT num_msg = 1, CBufferSimple * buffer = NULL );
690 // added 1.3.0.0 to fix uber-crash (allocates and attaches new buffer)
691 CBufferSimple * create_event_buffer();
692 // added 1.3.0.0 to fix uber-crash (detaches and deletes buffer)
693 void destroy_event_buffer( CBufferSimple * buffer );
694 // added 1.5.1.3 for granularity in handling synchronization
695 void detach_event_buffer_without_delete( CBufferSimple * buffer );
696
697 public: // get error
698 const char * last_error() const
699 { return m_last_error.c_str(); }
700
701 public:
702 // REFACTOR-2017: get associated, per-VM environment, chout, cherr
703 Chuck_Carrier * carrier() const { return m_carrier; }
704 Chuck_Env * env() const { return m_carrier->env; }
705 Chuck_IO_Chout * chout() const { return m_carrier->chout; }
706 Chuck_IO_Cherr * cherr() const { return m_carrier->cherr; }
707 // 1.4.1.0 (jack): get associated globals manager
708 Chuck_Globals_Manager * globals_manager() const { return m_globals_manager; }
709
710 public:
711 // subscribe shreds watcher callback | 1.5.1.5
712 void subscribe_watcher( f_shreds_watcher cb, t_CKUINT options, void * userdata = NULL );
713 // notify watchers | 1.5.1.5
714 void notify_watchers( ckvm_ShredsWatcherFlag which, Chuck_VM_Shred * shred,
715 std::list<Chuck_VM_Shreds_Watcher> & v );
716 // remove shreds watcher callback | 1.5.1.5
717 void remove_watcher( f_shreds_watcher cb );
718
719 public:
720 // register a callback to be called on VM shutdown | 1.5.2.5 (ge) added
721 void register_callback_on_shutdown( f_callback_on_shutdown cb, void * bindle );
722 // register a callback to be called on system sample rate update | 1.5.4.2 (ge) added
723 void register_callback_on_srate_update( f_callback_on_srate_update cb, void * bindle );
724
725 protected:
726 // notify callbacks on VM shutdown | 1.5.2.5 (ge) added
727 void notify_callbacks_on_shutdown();
728 // notify callbacks on sample rate update | 1.5.4.2 (ge) added
729 void notify_callbacks_on_srate_update( t_CKUINT srate );
730
731 //-----------------------------------------------------------------------------
732 // data
733 //-----------------------------------------------------------------------------
734 protected:
735 // REFACTOR-2017: added per-ChucK carrier
736 Chuck_Carrier * m_carrier;
737
738 public:
739 // ugen
740 Chuck_UGen * m_adc;
741 Chuck_UGen * m_dac;
742 Chuck_UGen * m_bunghole;
743 t_CKUINT m_srate;
744 t_CKUINT m_num_adc_channels;
745 t_CKUINT m_num_dac_channels;
746 t_CKBOOL m_halt;
747 t_CKBOOL m_is_running;
748
749 // for shreduler, ge: 1.3.5.3
750 const SAMPLE * input_ref() { return m_input_ref; }
751 SAMPLE * output_ref() { return m_output_ref; }
752 // for shreduler, jack: planar (non-interleaved) audio buffers
753 t_CKUINT most_recent_buffer_length() { return m_current_buffer_frames; }
754
755 protected:
756 // for shreduler, ge: 1.3.5.3
757 const SAMPLE * m_input_ref;
758 SAMPLE * m_output_ref;
759 t_CKUINT m_current_buffer_frames;
760
761 public:
762 // protected, but needs to be accessible from Globals Manager (1.4.1.0)
763 // generally, this should not be called except by internals such as GM
764 Chuck_VM_Shred * spork( Chuck_VM_Shred * shred );
765
766 protected:
767 // remove all shreds from VM
768 void removeAll();
769 // free shred
770 t_CKBOOL free_shred( Chuck_VM_Shred * shred, t_CKBOOL cascade,
771 t_CKBOOL dec = TRUE );
772 void dump_shred( Chuck_VM_Shred * shred );
773 void release_dump();
774
775 // special flag to remove all shreds in the VM...
776 // as soon as safely possible | 1.5.4.4 (ge) added
777 t_CKBOOL m_asap_remove_all_shreds;
778
779 protected:
780 t_CKBOOL m_init;
781 std::string m_last_error;
782
783 // shred
784 Chuck_VM_Shred * m_shreds;
785 t_CKUINT m_num_shreds;
786 t_CKUINT m_shred_id;
787 t_CKBOOL m_shred_check4dupes; // 1.5.1.5
788 Chuck_VM_Shreduler * m_shreduler;
789 // place to put dumped shreds
790 std::vector<Chuck_VM_Shred *> m_shred_dump;
791 t_CKUINT m_num_dumped_shreds;
792
793 // message queue
794 CBufferSimple * m_msg_buffer;
795 CBufferSimple * m_reply_buffer;
796 CBufferSimple * m_event_buffer;
797
798 // TODO: vector? (added 1.3.0.0 to fix uber-crash)
799 std::list<CBufferSimple *> m_event_buffers;
800
801 protected:
802 // 1.5.4.3 (ge) added static initializer run queue
803 // types that need static init are added to this by the emitter
804 // this queue is checked and run in immediate mode (and on a dedicated shred)
805 std::list<Chuck_Type *> m_static_init_queue;
806
807 protected:
808 // 1.4.1.0 (jack): manager for global variables
809 Chuck_Globals_Manager * m_globals_manager;
810
811 protected:
812 // 1.5.1.5 (ge & andrew) shreds watchers
813 std::list<Chuck_VM_Shreds_Watcher> m_shreds_watchers_spork;
814 std::list<Chuck_VM_Shreds_Watcher> m_shreds_watchers_remove;
815 std::list<Chuck_VM_Shreds_Watcher> m_shreds_watchers_suspend;
816 std::list<Chuck_VM_Shreds_Watcher> m_shreds_watchers_activate;
817
818 protected:
819 // 1.5.2.5 (ge) on major VM events callbacks
820 std::list<Chuck_VM_Callback_On_Shutdown> m_callbacks_on_shutdown;
821 // 1.5.4.2 (ge) on system sample rate update
822 std::list<Chuck_VM_Callback_On_SampleRate_Update> m_callbacks_on_srate_update;
823 };
824
825
826
827
828 //-----------------------------------------------------------------------------
829 // name: enum Chuck_Msg_Type
830 // desc: message types
831 //-----------------------------------------------------------------------------
832 enum Chuck_Msg_Type
833 {
834 CK_MSG_ADD = 1,
835 CK_MSG_REMOVE,
836 CK_MSG_REMOVEALL,
837 CK_MSG_REPLACE,
838 CK_MSG_STATUS,
839 CK_MSG_PAUSE,
840 CK_MSG_EXIT,
841 CK_MSG_TIME,
842 CK_MSG_RESET_ID,
843 CK_MSG_DONE,
844 CK_MSG_ABORT,
845 CK_MSG_ERROR, // added 1.3.0.0
846 CK_MSG_CLEARVM,
847 CK_MSG_CLEARGLOBALS,
848 };
849
850
851
852
853 // callback function prototype
854 typedef void (* ck_msg_func)( const Chuck_Msg * msg );
855 //-----------------------------------------------------------------------------
856 // name: struct Chuck_Msg
857 // desc: chuck message, used to communicate with VM
858 //-----------------------------------------------------------------------------
859 struct Chuck_Msg
860 {
861 // type of message
862 t_CKUINT type;
863
864 // messsage parameter, as applicable
865 t_CKUINT param;
866 // VM code pointer, as applicable
867 Chuck_VM_Code * code;
868 // VM shred pointer, as applicable
869 Chuck_VM_Shred * shred;
870 // parent shred, as applicable
871 Chuck_VM_Shred * parent;
872 // time, as applicable
873 t_CKTIME when;
874 // pointer to status struct, as applicable
875 Chuck_VM_Status * status;
876 // whether to always add | 1.5.1.5
877 t_CKBOOL alwaysAdd;
878
879 // reply callback
880 ck_msg_func reply_cb;
881 // if true, reply on queue
882 t_CKBOOL reply_queue;
883 // NOTE if both reply_cb and reply_queue is non-zero, reply_cb will take precedence
884
885 // reply arguments
886 t_CKUINT replyA;
887 t_CKUINT replyB;
888 void * replyC;
889
890 // argument array pointer
891 std::vector<std::string> * args;
892
893 // constructor
894 Chuck_Msg() : args(NULL) { clear(); }
895 // destructor
896 ~Chuck_Msg() { clear(); }
897
898 // added 1.3.0.0
899 void clear()
900 {
901 // clean up existing args
902 CK_SAFE_DELETE(args);
903 // clear
904 memset( this, 0, sizeof(Chuck_Msg) );
905 }
906
907 // copy in args
908 void set( const std::vector<std::string> * vargs )
909 {
910 // clean up existing args
911 CK_SAFE_DELETE(args);
912 // check
913 if( vargs )
914 {
915 // instantiate new
916 args = new std::vector<std::string>;
917 // copy
918 (*args) = *vargs;
919 }
920 }
921 };
922
923
924
925
926 //-----------------------------------------------------------------------------
927 // name: struct Chuck_VM_MFunInvoker | 1.5.1.5 (ge)
928 // desc: aparatus for calling chuck-defined member functions from c++,
929 // either from VM execution or outside the VM execution context
930 //-----------------------------------------------------------------------------
931 struct Chuck_VM_MFunInvoker
932 {
933 public:
934 // constructor
935 Chuck_VM_MFunInvoker();
936 // destructor
937 ~Chuck_VM_MFunInvoker();
938
939 public:
940 // set up the invoker; needed before invoke()
941 t_CKBOOL setup( Chuck_Func * func, t_CKUINT func_vt_offset,
942 Chuck_VM * vm, Chuck_VM_Shred * caller );
943 // invoke the member function
944 Chuck_DL_Return invoke( Chuck_Object * obj,
945 const std::vector<Chuck_DL_Arg> & args,
946 Chuck_VM_Shred * parent_shred );
947 // clean up
948 void cleanup();
949
950 public:
951 // dedicated shred to call the mfun on
952 Chuck_VM_Shred * invoker_shred;
953 // instructions for args (to be filled on invoke)
954 std::vector<Chuck_Instr *> instr_args;
955 // instruction to update on invoke: pushing this pointer
956 Chuck_Instr_Reg_Push_Imm * instr_pushThis;
957 // instruction to update on invoke: pushing the var to receive return
958 Chuck_Instr_Reg_Push_Imm * instr_pushReturnVar;
959 };
960
961
962
963
964 //-----------------------------------------------------------------------------
965 // name: struct Chuck_VM_DtorInvoker | 1.5.2.0 (ge)
966 // desc: aparatus for calling chuck-defined @destruct from c++,
967 // typically called for Object cleanup
968 //-----------------------------------------------------------------------------
969 struct Chuck_VM_DtorInvoker
970 {
971 public:
972 // constructor
973 Chuck_VM_DtorInvoker();
974 // destructor
975 ~Chuck_VM_DtorInvoker();
976
977 public:
978 // set up the invoker; needed before invoke()
979 t_CKBOOL setup( Chuck_Func * func, Chuck_VM * vm );
980 // invoke the dtor
981 void invoke( Chuck_Object * obj, Chuck_VM_Shred * parent_shred = NULL );
982 // clean up
983 void cleanup();
984
985 public:
986 // dedicated shred to call the dtor on, since this could be running "outside of chuck time"
987 Chuck_VM_Shred * invoker_shred;
988 // instruction to update on invoke: pushing this pointer
989 Chuck_Instr_Reg_Push_Imm * instr_pushThis;
990 };
991
992
993
994
995 //-----------------------------------------------------------------------------
996 // name: struct Chuck_VM_SInitInvoker | 1.5.4.3 (ge) added #2024-static-init
997 // desc: aparatus for calling chuck-defined class static initialization
998 //-----------------------------------------------------------------------------
999 struct Chuck_VM_SInitInvoker
1000 {
1001 public:
1002 // constructor
1003 Chuck_VM_SInitInvoker();
1004 // destructor
1005 ~Chuck_VM_SInitInvoker();
1006
1007 public:
1008 // set up the invoker; needed before invoke()
1009 t_CKBOOL setup( Chuck_Type * type, Chuck_VM_Code * static_init_code, Chuck_VM * vm );
1010 // invoke the static function
1011 void invoke( Chuck_VM_Shred * parent_shred = NULL );
1012 // clean up
1013 void cleanup();
1014
1015 public:
1016 // dedicated shred to call the static on, since this could be running "outside of chuck time"
1017 Chuck_VM_Shred * invoker_shred;
1018 // the code to run
1019 Chuck_VM_Code * the_code;
1020 // the class
1021 Chuck_Type * the_class;
1022 };
1023
1024
1025
1026
1027 //-----------------------------------------------------------------------------
1028 // VM debug macros | 1.5.0.5 (ge) added
1029 // these are governed by the presence of __CHUCK_DEBUG__ (e.g., from makefile)
1030 //-----------------------------------------------------------------------------
1031 #ifdef __CHUCK_DEBUG__
1032 // enable VM debug
1033 #define CK_VM_DEBUG_ENABLE 1
1034 #else
1035 // disable VM debug
1036 #define CK_VM_DEBUG_ENABLE 0
1037 #endif
1038
1039 // vm debug macros
1040 #if CK_VM_DEBUG_ENABLE
1041 // calls/access 'member' on the VM_Debug singleton
1042 // e.g., `CK_VM_DEBUGGER( add_ref(obj) )` expands to...
1043 // ----> `(Chuck_VM_Debug::instance())->add_ref(obj)`
1044 #define CK_VM_DEBUGGER( member ) CK_VM_DEBUGGER_INSTANCE->member
1045 // expands to the Chuck_VM_Debug singleton
1046 #define CK_VM_DEBUGGER_INSTANCE (Chuck_VM_Debug::instance())
1047 // expands to the argument
1048 #define CK_VM_DEBUG_ACTION( act ) act
1049 #else
1050 // empty macros; will be compiled-out
1051 #define CK_VM_DEBUGGER( act )
1052 #define CK_VM_DEBUGGER_INSTANCE
1053 #define CK_VM_DEBUG_ACTION( act )
1054 #endif
1055
1056
1057
1058
1059 // control compilation
1060 #if CK_VM_DEBUG_ENABLE
1061 //-----------------------------------------------------------------------------
1062 // name: struct Chuck_VM_Debug | 1.5.0.5 (ge) added
1063 // desc: vm debug helper
1064 //-----------------------------------------------------------------------------
1065 struct Chuck_VM_Debug
1066 {
1067 public:
1068 // call this right after instantiation (tracking)
1069 void construct( Chuck_VM_Object * obj, const std::string & note = "" );
1070 // call this right before destruct (tracking)
1071 void destruct( Chuck_VM_Object * obj, const std::string & note = "" );
1072 // add this on add reference (tracking)
1073 void add_ref( Chuck_VM_Object * obj, const std::string & note = "" );
1074 // add this on release reference (tracking)
1075 void release( Chuck_VM_Object * obj, const std::string & note = "" );
1076
1077 public:
1078 // set log level to print to
1079 void set_log_evel( t_CKUINT level );
1080 // number of objects being tracked
1081 t_CKUINT num_objects();
1082 // print all objects being tracked
1083 void print_all_objects();
1084 // print stats
1085 void print_stats();
1086 // reset stats
1087 void reset_stats();
1088
1089 public:
1090 // one func to get info using runtime types
1091 std::string info( Chuck_VM_Object * obj );
1092
1093 // info by type
1094 std::string info_obj( Chuck_Object * obj );
1095 std::string info_type( Chuck_Type * type );
1096 std::string info_func( Chuck_Func * func );
1097 std::string info_value( Chuck_Value * value );
1098 std::string info_namespace( Chuck_Namespace * nspc );
1099 std::string info_context( Chuck_Context * context );
1100 std::string info_env( Chuck_Env * env );
1101 std::string info_ugen_info( Chuck_UGen_Info * ug );
1102 std::string info_code( Chuck_VM_Code * code );
1103 std::string info_shred( Chuck_VM_Shred * shred );
1104 std::string info_vm( Chuck_VM * vm );
1105
1106 public:
1107 // one func to try to print them all
1108 void print( Chuck_VM_Object * obj, const std::string & note = "" );
1109
1110 public:
1111 // get call stack as string
1112 static std::string info_backtrace( );
1113 // print call stack at this point
1114 void backtrace( const std::string & note = "" );
1115
1116 public:
1117 // get shared instance
1118 static Chuck_VM_Debug * instance();
1119 // destructor
1120 ~Chuck_VM_Debug();
1121
1122 protected:
1123 // shared instance pointer
1124 static Chuck_VM_Debug * our_instance;
1125 // protected constructor
1126 Chuck_VM_Debug();
1127
1128 protected:
1129 // insert into object map
1130 void insert( Chuck_VM_Object * obj );
1131 // insert into object map
1132 void remove( Chuck_VM_Object * obj );
1133 // is present?
1134 t_CKBOOL contains( Chuck_VM_Object * obj );
1135 // get map by type name
1136 std::map<Chuck_VM_Object *, Chuck_VM_Object *> get_objs( const std::string & key);
1137
1138 protected:
1139 // log level
1140 t_CKUINT m_log_level;
1141 // object map by name -> map
1142 std::map< std::string, std::map<Chuck_VM_Object *, Chuck_VM_Object *> > m_objects_map;
1143
1144 protected:
1145 // stats
1146 t_CKUINT m_numConstructed;
1147 t_CKUINT m_numDestructed;
1148 t_CKUINT m_numAddRefs;
1149 t_CKUINT m_numReleases;
1150 };
1151 #endif
1152
1153
1154
1155
1156 #endif