| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- /**
- * @file call.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @section DESCRIPTION
- *
- * Synopsis:
- * callrefhere()
- *
- * Description:
- * Exposes variables and objects to call() statements as seen from this
- * callrefhere() statement.
- *
- * Synopsis:
- * call(string template_name, list args)
- * callhefhere::call(string template_name, list args)
- *
- * Description:
- * Module which allows using a single statement to represent multiple statements
- * in a process template, allowing reuse of repetitive code.
- * The created template process can access variables and objects as seen from the
- * call statement via "_caller.variable".
- * The second form also exposes variables and objects from the corresponding
- * callrefhere() statement via "_ref.variable".
- * If template_name is "<none>", then the call() is a no-op - it goes up
- * immediately and immediately terminates on request.
- *
- * Variables:
- * Exposes variables as seen from the end of the called process template.
- *
- * Behavior in detail (assuming template_name is not "<none>"):
- * - On initialization, creates a new process from the template named
- * template_name, with arguments args.
- * - When all the statements in the created process go UP, transitions UP.
- * - When one of the statements is no longer UP, transitions DOWN. The
- * created process remais paused until the call statement receives the
- * clean signal, to wait for following statements to deinitialize.
- * - On deinitialization, initiates termination of the created process and waits
- * for all its statements to deinitialize.
- */
- #include <stdlib.h>
- #include <misc/string_begins_with.h>
- #include <misc/offset.h>
- #include <structure/LinkedList0.h>
- #include <ncd/NCDModule.h>
- #include <ncd/value_utils.h>
- #include <generated/blog_channel_ncd_call.h>
- #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
- #define STATE_WORKING 1
- #define STATE_UP 2
- #define STATE_WAITING 3
- #define STATE_TERMINATING 4
- #define STATE_NONE 5
- struct callrefhere_instance {
- NCDModuleInst *i;
- LinkedList0 calls_list;
- };
- struct instance {
- NCDModuleInst *i;
- NCDValMem args_mem;
- NCDModuleProcess process;
- int state;
- struct callrefhere_instance *crh;
- LinkedList0Node calls_list_node;
- };
- static void instance_free (struct instance *o);
- static int caller_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object);
- static int ref_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object);
- enum {STRING_CALLER, STRING_REF};
- static struct NCD_string_request strings[] = {
- {"_caller"}, {"_ref"}, {NULL}
- };
- static void process_handler_event (NCDModuleProcess *process, int event)
- {
- struct instance *o = UPPER_OBJECT(process, struct instance, process);
-
- switch (event) {
- case NCDMODULEPROCESS_EVENT_UP: {
- ASSERT(o->state == STATE_WORKING)
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
-
- // set state up
- o->state = STATE_UP;
- } break;
-
- case NCDMODULEPROCESS_EVENT_DOWN: {
- ASSERT(o->state == STATE_UP)
-
- // signal down
- NCDModuleInst_Backend_Down(o->i);
-
- // set state waiting
- o->state = STATE_WAITING;
- } break;
-
- case NCDMODULEPROCESS_EVENT_TERMINATED: {
- ASSERT(o->state == STATE_TERMINATING)
-
- // die finally
- instance_free(o);
- return;
- } break;
-
- default: ASSERT(0);
- }
- }
- static int process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
- {
- struct instance *o = UPPER_OBJECT(process, struct instance, process);
-
- if (name == strings[STRING_CALLER].id) {
- *out_object = NCDObject_Build(-1, o, NULL, (NCDObject_func_getobj)caller_obj_func_getobj);
- return 1;
- }
-
- if (name == strings[STRING_REF].id) {
- *out_object = NCDObject_Build(-1, o, NULL, (NCDObject_func_getobj)ref_obj_func_getobj);
- return 1;
- }
-
- return 0;
- }
- static void callrefhere_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- struct callrefhere_instance *o = vo;
- o->i = i;
-
- // init calls list
- LinkedList0_Init(&o->calls_list);
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
- }
- static void callrefhere_func_die (void *vo)
- {
- struct callrefhere_instance *o = vo;
-
- // disconnect calls
- while (!LinkedList0_IsEmpty(&o->calls_list)) {
- struct instance *inst = UPPER_OBJECT(LinkedList0_GetFirst(&o->calls_list), struct instance, calls_list_node);
- ASSERT(inst->crh == o)
- LinkedList0_Remove(&o->calls_list, &inst->calls_list_node);
- inst->crh = NULL;
- }
-
- NCDModuleInst_Backend_Dead(o->i);
- }
- static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- struct instance *o = vo;
- o->i = i;
-
- // check arguments
- NCDValRef template_name_arg;
- NCDValRef args_arg;
- if (!NCDVal_ListRead(params->args, 2, &template_name_arg, &args_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
- if (!NCDVal_IsString(template_name_arg) || !NCDVal_IsList(args_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail0;
- }
-
- // calling none?
- if (ncd_is_none(template_name_arg)) {
- // signal up
- NCDModuleInst_Backend_Up(o->i);
-
- // set state none
- o->state = STATE_NONE;
- } else {
- // init args mem
- NCDValMem_Init(&o->args_mem);
-
- // copy arguments
- NCDValRef args = NCDVal_NewCopy(&o->args_mem, args_arg);
- if (NCDVal_IsInvalid(args)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
- NCDValMem_Free(&o->args_mem);
- goto fail0;
- }
-
- // create process
- if (!NCDModuleProcess_InitValue(&o->process, o->i, template_name_arg, args, process_handler_event)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
- NCDValMem_Free(&o->args_mem);
- goto fail0;
- }
-
- // set special functions
- NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj);
-
- // set callrefhere
- o->crh = (params->method_user ? NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user) : NULL);
-
- // add to callrefhere's calls list
- if (o->crh) {
- LinkedList0_Prepend(&o->crh->calls_list, &o->calls_list_node);
- }
-
- // set state working
- o->state = STATE_WORKING;
- }
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- void instance_free (struct instance *o)
- {
- if (o->state != STATE_NONE) {
- // remove from callrefhere's calls list
- if (o->crh) {
- LinkedList0_Remove(&o->crh->calls_list, &o->calls_list_node);
- }
-
- // free args mem
- NCDValMem_Free(&o->args_mem);
-
- // free process
- NCDModuleProcess_Free(&o->process);
- }
-
- NCDModuleInst_Backend_Dead(o->i);
- }
- static void func_die (void *vo)
- {
- struct instance *o = vo;
- ASSERT(o->state != STATE_TERMINATING)
-
- // if none, die now
- if (o->state == STATE_NONE) {
- instance_free(o);
- return;
- }
-
- // request process to terminate
- NCDModuleProcess_Terminate(&o->process);
-
- // set state terminating
- o->state = STATE_TERMINATING;
- }
- static void func_clean (void *vo)
- {
- struct instance *o = vo;
- if (o->state != STATE_WAITING) {
- return;
- }
-
- // allow process to continue
- NCDModuleProcess_Continue(&o->process);
-
- // set state working
- o->state = STATE_WORKING;
- }
- static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
- {
- struct instance *o = vo;
-
- if (o->state == STATE_NONE) {
- return 0;
- }
-
- return NCDModuleProcess_GetObj(&o->process, name, out_object);
- }
- static int caller_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
- {
- return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
- }
- static int ref_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
- {
- if (!o->crh) {
- return 0;
- }
-
- return NCDModuleInst_Backend_GetObj(o->crh->i, name, out_object);
- }
- static struct NCDModule modules[] = {
- {
- .type = "callrefhere",
- .func_new2 = callrefhere_func_new,
- .func_die = callrefhere_func_die,
- .alloc_size = sizeof(struct callrefhere_instance)
- }, {
- .type = "call",
- .func_new2 = func_new,
- .func_die = func_die,
- .func_clean = func_clean,
- .func_getobj = func_getobj,
- .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
- .alloc_size = sizeof(struct instance)
- }, {
- .type = "callrefhere::call",
- .func_new2 = func_new,
- .func_die = func_die,
- .func_clean = func_clean,
- .func_getobj = func_getobj,
- .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
- .alloc_size = sizeof(struct instance)
- }, {
- .type = NULL
- }
- };
- const struct NCDModuleGroup ncdmodule_call = {
- .modules = modules,
- .strings = strings
- };
|