History

Merge pull request #142 from ccrma/feature/mainthreadhook

re-add mainthreadhook

Ge Wang committed 6 years ago

Commit 5e02dfc2 parents 2cef6db 7de6cc15 files · +107 −3Browse files

Unified · Split

5 files
  1. src/core/chuck.cpp +21 −0
  2. src/core/chuck.h +6 −0
  3. src/core/chuck_dl.cpp +48 −0
  4. src/core/chuck_dl.h +23 −2
  5. src/host/chuck_main.cpp +9 −1
src/core/chuck.cpp+21−0
@@ -1126,3 +1126,24 @@ void ChucK::poop() · 1125 unmodified lines
11261126 {
11271127 ::uh();
11281128 }
1129+
1130+
1131+
1132+//-----------------------------------------------------------------------------
1133+// name: setMainThreadHook()
1134+// desc:
1135+//-----------------------------------------------------------------------------
1136+bool ChucK::setMainThreadHook(Chuck_DL_MainThreadHook * hook)
1137+{
1138+ m_hook = hook;
1139+ return true;
1140+}
1141+
1142+//-----------------------------------------------------------------------------
1143+// name: getMainThreadHook()
1144+// desc:
1145+//-----------------------------------------------------------------------------
1146+Chuck_DL_MainThreadHook * ChucK::getMainThreadHook()
1147+{
1148+ return m_hook;
1149+}
src/core/chuck.h+6−0
@@ -122,6 +122,10 @@ public: · 121 unmodified lines
122122 public:
123123 // additional native chuck bindings/types (use with extra caution)
124124 bool bind( f_ck_query queryFunc, const std::string & name );
125+
126+ bool setMainThreadHook(Chuck_DL_MainThreadHook * hook);
127+
128+ Chuck_DL_MainThreadHook * getMainThreadHook();
125129
126130 public:
127131 // get VM (dangerous)
@@ -200,6 +204,8 @@ protected: · 72 unmodified lines
200204 std::map< std::string, std::list<std::string> > m_listParams;
201205 // did user init?
202206 t_CKBOOL m_init;
207+ // main thread "hook" (if there is a main thread)
208+ Chuck_DL_MainThreadHook * m_hook = nullptr;
203209 };
204210
205211
src/core/chuck_dl.cpp+48−0
@@ -40,6 +40,7 @@ · 39 unmodified lines
4040 #include "chuck_instr.h"
4141 #include "chuck_compile.h"
4242 #include "chuck_vm.h"
43+#include "chuck.h"
4344 #include <sstream>
4445 using namespace std;
4546
@@ -556,6 +557,17 @@ t_CKBOOL CK_DLL_CALL ck_end_class( Chuck_DL_Query * query ) · 510 unmodified lines
556557 }
557558
558559
560+//-----------------------------------------------------------------------------
561+// name: ck_create_main_thread_hook()
562+// desc: ...
563+//-----------------------------------------------------------------------------
564+Chuck_DL_MainThreadHook * CK_DLL_CALL ck_create_main_thread_hook( Chuck_DL_Query * query,
565+ f_mainthreadhook hook,
566+ f_mainthreadquit quit,
567+ void * bindle )
568+{
569+ return new Chuck_DL_MainThreadHook( hook, quit, bindle, query->carrier() );
570+}
559571
560572
561573 //-----------------------------------------------------------------------------
@@ -935,6 +947,7 @@ Chuck_DL_Query::Chuck_DL_Query( Chuck_Carrier * carrier ) · 373 unmodified lines
935947 doc_class = ck_doc_class;
936948 doc_func = ck_doc_func;
937949 doc_var = ck_doc_var;
950+ create_main_thread_hook = ck_create_main_thread_hook;
938951 m_carrier = carrier;
939952
940953 // memset(reserved2, NULL, sizeof(void*)*RESERVED_SIZE);
@@ -1005,6 +1018,41 @@ Chuck_DL_Func::~Chuck_DL_Func() · 64 unmodified lines
10051018 }
10061019
10071020
1021+/*******************************************************************************
1022+
1023+ Main Thread Hook stuff
1024+
1025+*******************************************************************************/
1026+
1027+t_CKBOOL ck_mthook_activate(Chuck_DL_MainThreadHook *hook)
1028+{
1029+ hook->m_carrier->chuck->setMainThreadHook(hook);
1030+ hook->m_active = true;
1031+ return hook->m_active;
1032+}
1033+
1034+t_CKBOOL ck_mthook_deactivate(Chuck_DL_MainThreadHook *hook)
1035+{
1036+ hook->m_carrier->chuck->setMainThreadHook(nullptr);
1037+ hook->m_active = false;
1038+ return hook->m_active;
1039+}
1040+
1041+//-----------------------------------------------------------------------------
1042+// name: Chuck_DL_MainThreadHook()
1043+// desc: ...
1044+//-----------------------------------------------------------------------------
1045+Chuck_DL_MainThreadHook::Chuck_DL_MainThreadHook(f_mainthreadhook hook, f_mainthreadquit quit,
1046+ void * bindle, Chuck_Carrier * carrier) :
1047+m_hook(hook),
1048+m_quit(quit),
1049+m_carrier(carrier),
1050+m_bindle(bindle),
1051+activate(ck_mthook_activate),
1052+deactivate(ck_mthook_deactivate),
1053+m_active(FALSE)
1054+{ }
1055+
10081056
10091057
10101058 /*******************************************************************************
src/core/chuck_dl.h+23−2
@@ -48,7 +48,7 @@ · 47 unmodified lines
4848 // major version must be the same between chuck:chugin
4949 #define CK_DLL_VERSION_MAJOR (0x0007)
5050 // minor version of chugin must be less than or equal to chuck's
51−#define CK_DLL_VERSION_MINOR (0x0000)
51+#define CK_DLL_VERSION_MINOR (0x0001)
5252 #define CK_DLL_VERSION_MAKE(maj,min) ((t_CKUINT)(((maj) << 16) | (min)))
5353 #define CK_DLL_VERSION_GETMAJOR(v) (((v) >> 16) & 0xFFFF)
5454 #define CK_DLL_VERSION_GETMINOR(v) ((v) & 0xFFFF)
@@ -73,6 +73,7 @@ struct Chuck_DLL; · 18 unmodified lines
7373 struct Chuck_UGen;
7474 struct Chuck_UAna;
7575 struct Chuck_UAnaBlobProxy;
76+struct Chuck_DL_MainThreadHook;
7677 namespace Chuck_DL_Api { struct Api; }
7778
7879
@@ -306,7 +307,9 @@ typedef void (CK_DLL_CALL * f_add_ugen_ctrl)( Chuck_DL_Query * query, f_ctrl ctr · 227 unmodified lines
306307 const char * type, const char * name );
307308 // end class/namespace - must correspondent with begin_class. returns false on error
308309 typedef t_CKBOOL (CK_DLL_CALL * f_end_class)( Chuck_DL_Query * query );
309−
310+// create main thread hook- used for executing a "hook" function in the main thread of a primary chuck instance
311+typedef Chuck_DL_MainThreadHook * (CK_DLL_CALL * f_create_main_thread_hook)( Chuck_DL_Query * query, f_mainthreadhook hook, f_mainthreadquit quit, void * bindle );
312+
310313 // documentation
311314 // set current class documentation
312315 typedef t_CKBOOL (CK_DLL_CALL * f_doc_class)( Chuck_DL_Query * query, const char * doc );
@@ -334,6 +337,7 @@ public: · 21 unmodified lines
334337 Chuck_Compiler * compiler() const { return m_carrier->compiler; }
335338 Chuck_VM * vm() const { return m_carrier->vm; }
336339 Chuck_Env * env() const { return m_carrier->env; }
340+ Chuck_Carrier * carrier() const { return m_carrier; }
337341
338342 public:
339343 // function pointers - to be called from client module
@@ -375,6 +379,9 @@ public: · 35 unmodified lines
375379 f_doc_var doc_var;
376380 f_add_example add_ex;
377381
382+ // re-added 1.4.0.1
383+ f_create_main_thread_hook create_main_thread_hook;
384+
378385 // NOTE: everything below std::anything cannot be reliably accessed
379386 // by offset between dynamic modules, since std::anything could be variable
380387 // size -- put everything need to be accessed across modules above here!
@@ -629,6 +636,20 @@ protected: · 248 unmodified lines
629636 };
630637
631638
639+struct Chuck_DL_MainThreadHook
640+{
641+public:
642+ Chuck_DL_MainThreadHook(f_mainthreadhook hook, f_mainthreadquit quit,
643+ void * bindle, Chuck_Carrier * carrier);
644+ t_CKBOOL (* const activate)(Chuck_DL_MainThreadHook *);
645+ t_CKBOOL (* const deactivate)(Chuck_DL_MainThreadHook *);
646+
647+ Chuck_Carrier * const m_carrier;
648+ f_mainthreadhook const m_hook;
649+ f_mainthreadquit const m_quit;
650+ void * const m_bindle;
651+ t_CKBOOL m_active;
652+};
632653
633654
634655 /* API to ChucK's innards */
src/host/chuck_main.cpp+9−1
@@ -1037,7 +1037,11 @@ bool go( int argc, const char ** argv ) · 1036 unmodified lines
10371037 // real-time audio
10381038 if( g_enable_realtime_audio )
10391039 {
1040− usleep( 10000 );
1040+ Chuck_DL_MainThreadHook * hook = the_chuck->getMainThreadHook();
1041+ if (hook)
1042+ hook->m_hook(hook->m_bindle);
1043+ else
1044+ usleep( 10000 );
10411045 }
10421046 else // silent mode
10431047 {
@@ -1046,6 +1050,10 @@ bool go( int argc, const char ** argv ) · 2 unmodified lines
10461050 }
10471051 }
10481052
1053+ Chuck_DL_MainThreadHook * hook = the_chuck->getMainThreadHook();
1054+ if (hook)
1055+ hook->m_quit(hook->m_bindle);
1056+
10491057 return TRUE;
10501058 }
10511059

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t