diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/luaproc/luaproc.c | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/libraries/luaproc/luaproc.c b/libraries/luaproc/luaproc.c index ee0764a..7f4b2bb 100644 --- a/libraries/luaproc/luaproc.c +++ b/libraries/luaproc/luaproc.c | |||
@@ -41,6 +41,7 @@ THE SOFTWARE. | |||
41 | #include "sched.h" | 41 | #include "sched.h" |
42 | #include "channel.h" | 42 | #include "channel.h" |
43 | 43 | ||
44 | |||
44 | #define FALSE 0 | 45 | #define FALSE 0 |
45 | #define TRUE 1 | 46 | #define TRUE 1 |
46 | 47 | ||
@@ -67,8 +68,18 @@ struct stluaproc { | |||
67 | int args; | 68 | int args; |
68 | channel chan; | 69 | channel chan; |
69 | int destroyworker; | 70 | int destroyworker; |
71 | void *data; | ||
72 | Ecore_Cb callback; | ||
70 | }; | 73 | }; |
71 | 74 | ||
75 | /* TODO - hack, duplicating something from LuaSL for now. */ | ||
76 | typedef struct | ||
77 | { | ||
78 | void *script; | ||
79 | char message[PATH_MAX]; | ||
80 | } scriptMessage; | ||
81 | |||
82 | |||
72 | /****************************** | 83 | /****************************** |
73 | * library functions prototypes | 84 | * library functions prototypes |
74 | ******************************/ | 85 | ******************************/ |
@@ -90,6 +101,8 @@ static int luaproc_create_worker( lua_State *L ); | |||
90 | static int luaproc_destroy_worker( lua_State *L ); | 101 | static int luaproc_destroy_worker( lua_State *L ); |
91 | /* set amount of lua processes that should be recycled (ie, reused) */ | 102 | /* set amount of lua processes that should be recycled (ie, reused) */ |
92 | static int luaproc_recycle_set( lua_State *L ); | 103 | static int luaproc_recycle_set( lua_State *L ); |
104 | /* send a message back to the main loop */ | ||
105 | static int luaproc_send_back( lua_State *L ); | ||
93 | 106 | ||
94 | /* luaproc function registration array - main (parent) functions */ | 107 | /* luaproc function registration array - main (parent) functions */ |
95 | static const struct luaL_reg luaproc_funcs_parent[] = { | 108 | static const struct luaL_reg luaproc_funcs_parent[] = { |
@@ -98,6 +111,7 @@ static const struct luaL_reg luaproc_funcs_parent[] = { | |||
98 | { "createworker", luaproc_create_worker }, | 111 | { "createworker", luaproc_create_worker }, |
99 | { "destroyworker", luaproc_destroy_worker }, | 112 | { "destroyworker", luaproc_destroy_worker }, |
100 | { "recycle", luaproc_recycle_set }, | 113 | { "recycle", luaproc_recycle_set }, |
114 | { "sendback", luaproc_send_back }, | ||
101 | { NULL, NULL } | 115 | { NULL, NULL } |
102 | }; | 116 | }; |
103 | 117 | ||
@@ -111,6 +125,7 @@ static const struct luaL_reg luaproc_funcs_child[] = { | |||
111 | { "createworker", luaproc_create_worker }, | 125 | { "createworker", luaproc_create_worker }, |
112 | { "destroyworker", luaproc_destroy_worker }, | 126 | { "destroyworker", luaproc_destroy_worker }, |
113 | { "recycle", luaproc_recycle_set }, | 127 | { "recycle", luaproc_recycle_set }, |
128 | { "sendback", luaproc_send_back }, | ||
114 | { NULL, NULL } | 129 | { NULL, NULL } |
115 | }; | 130 | }; |
116 | 131 | ||
@@ -359,7 +374,7 @@ static luaproc luaproc_recycle( luaproc lp, const char *code, int file ) { | |||
359 | } | 374 | } |
360 | 375 | ||
361 | 376 | ||
362 | int newProc(const char *code, int file) | 377 | int newProc(const char *code, int file, Ecore_Cb callback, void *data) |
363 | { | 378 | { |
364 | /* new lua process pointer */ | 379 | /* new lua process pointer */ |
365 | luaproc lp; | 380 | luaproc lp; |
@@ -383,6 +398,10 @@ int newProc(const char *code, int file) | |||
383 | return 1; | 398 | return 1; |
384 | } | 399 | } |
385 | 400 | ||
401 | /* Stash any data and callback given to us. */ | ||
402 | lp->data = data; | ||
403 | lp->callback = callback; | ||
404 | |||
386 | /* increase active luaproc count */ | 405 | /* increase active luaproc count */ |
387 | sched_lpcount_inc(); | 406 | sched_lpcount_inc(); |
388 | 407 | ||
@@ -405,7 +424,7 @@ static int luaproc_create_newproc( lua_State *L ) { | |||
405 | /* check if first argument is a string (lua code) */ | 424 | /* check if first argument is a string (lua code) */ |
406 | const char *code = luaL_checkstring( L, 1 ); | 425 | const char *code = luaL_checkstring( L, 1 ); |
407 | 426 | ||
408 | switch (newProc(code, FALSE)) | 427 | switch (newProc(code, FALSE, NULL, NULL)) |
409 | { | 428 | { |
410 | case 1 : | 429 | case 1 : |
411 | /* in case of errors return nil + error msg */ | 430 | /* in case of errors return nil + error msg */ |
@@ -496,6 +515,28 @@ luaproc luaproc_getself( lua_State *L ) { | |||
496 | return lp; | 515 | return lp; |
497 | } | 516 | } |
498 | 517 | ||
518 | /* send a message to a lua process */ | ||
519 | static int luaproc_send_back( lua_State *L ) { | ||
520 | |||
521 | luaproc self; | ||
522 | const char *message = luaL_checkstring( L, 1 ); | ||
523 | |||
524 | self = luaproc_getself( L ); | ||
525 | if (self && self->callback && self->data) | ||
526 | { | ||
527 | scriptMessage *sm = calloc(1, sizeof(scriptMessage)); | ||
528 | |||
529 | if (sm) | ||
530 | { | ||
531 | sm->script = self->data; | ||
532 | strcpy(sm->message, message); | ||
533 | ecore_main_loop_thread_safe_call_async(self->callback, sm); | ||
534 | } | ||
535 | } | ||
536 | |||
537 | return 0; | ||
538 | } | ||
539 | |||
499 | /* error messages for the sendToChannel function */ | 540 | /* error messages for the sendToChannel function */ |
500 | const char *sendToChannelErrors[] = | 541 | const char *sendToChannelErrors[] = |
501 | { | 542 | { |