@@ -26,7 +26,6 @@ type callReturnSafeWork struct {
2626
2727type callSafeWork struct {
2828 function string
29- size int
3029 args []interface {}
3130 ret chan callReturnSafeWork
3231}
@@ -38,7 +37,7 @@ var toggle chan struct{}
3837var lock sync.Mutex
3938var wg sync.WaitGroup
4039
41- func Initialize () error {
40+ func InitializeUnsafe () error {
4241 // TODO: Remove this once go loader is implemented
4342 if result := int (C .metacall_initialize ()); result != 0 {
4443 return fmt .Errorf ("initializing MetaCall (error code %d)" , result )
@@ -48,7 +47,7 @@ func Initialize() error {
4847}
4948
5049// Start starts the metacall adapter
51- func InitializeSafe () error {
50+ func Initialize () error {
5251 lock .Lock ()
5352 defer lock .Unlock ()
5453
@@ -65,7 +64,7 @@ func InitializeSafe() error {
6564 runtime .LockOSThread ()
6665
6766 // Initialize MetaCall
68- if err := Initialize (); err != nil {
67+ if err := InitializeUnsafe (); err != nil {
6968 initErr <- err
7069 return
7170 }
@@ -76,11 +75,21 @@ func InitializeSafe() error {
7675 select {
7776 case <- toggle :
7877 // Shutdown
79- Destroy ()
78+ DestroyUnsafe ()
8079 return
8180 case w := <- queue :
82- // Send work
83- fmt .Printf ("DEBUG: send work: %#v\n " , w )
81+ switch v := w .(type ) {
82+ case loadFromFileSafeWork :
83+ {
84+ err := LoadFromFileUnsafe (v .tag , v .scripts )
85+ v .err <- err
86+ }
87+ case callSafeWork :
88+ {
89+ value , err := CallUnsafe (v .function , v .args ... )
90+ v .ret <- callReturnSafeWork {value , err }
91+ }
92+ }
8493 wg .Done ()
8594 }
8695 }
@@ -89,7 +98,7 @@ func InitializeSafe() error {
8998 return <- initErr
9099}
91100
92- func LoadFromFile (tag string , scripts []string ) error {
101+ func LoadFromFileUnsafe (tag string , scripts []string ) error {
93102 size := len (scripts )
94103
95104 cTag := C .CString (tag )
@@ -112,7 +121,7 @@ func LoadFromFile(tag string, scripts []string) error {
112121 return nil
113122}
114123
115- func LoadFromFileSafe (tag string , scripts []string ) error {
124+ func LoadFromFile (tag string , scripts []string ) error {
116125 result := make (chan error , 1 )
117126 w := loadFromFileSafeWork {
118127 tag ,
@@ -125,7 +134,7 @@ func LoadFromFileSafe(tag string, scripts []string) error {
125134 return <- result
126135}
127136
128- func Call (function string , args ... interface {}) (interface {}, error ) {
137+ func CallUnsafe (function string , args ... interface {}) (interface {}, error ) {
129138
130139 cFunction := C .CString (function )
131140 defer C .free (unsafe .Pointer (cFunction ))
@@ -210,26 +219,27 @@ func Call(function string, args ...interface{}) (interface{}, error) {
210219}
211220
212221// Call sends work and blocks until it's processed
213- func CallSafe (function string , args ... interface {}) (interface {}, error ) {
222+ func Call (function string , args ... interface {}) (interface {}, error ) {
214223 ret := make (chan callReturnSafeWork , 1 )
215224 w := callSafeWork {
216225 function : function ,
217- size : len (args ),
218226 args : args ,
219227 ret : ret ,
220228 }
221229 wg .Add (1 )
222230 queue <- w
223231
224- return nil , nil // TODO
232+ result := <- ret
233+
234+ return result .value , result .err
225235}
226236
227- func Destroy () {
237+ func DestroyUnsafe () {
228238 C .metacall_destroy ()
229239}
230240
231241// Shutdown disables the metacall adapter waiting for all calls to complete
232- func DestroySafe () {
242+ func Destroy () {
233243 lock .Lock ()
234244 close (toggle )
235245 toggle = nil
0 commit comments