Parcourir la source

ncd: NCDModule: add a pass-through pointer for interpreter callbacks

ambrop7 il y a 13 ans
Parent
commit
b872ade2c4
3 fichiers modifiés avec 25 ajouts et 16 suppressions
  1. 4 4
      ncd/NCDModule.c
  2. 12 4
      ncd/NCDModule.h
  3. 9 8
      ncd/ncd.c

+ 4 - 4
ncd/NCDModule.c

@@ -297,7 +297,7 @@ void NCDModuleInst_Backend_InterpExit (NCDModuleInst *n, int exit_code)
     DebugObject_Access(&n->d_obj);
     inst_assert_backend(n);
     
-    n->params->iparams->func_interp_exit(exit_code);
+    n->params->iparams->func_interp_exit(n->params->iparams->user, exit_code);
 }
 
 int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValMem *mem, NCDValRef *out_value)
@@ -307,7 +307,7 @@ int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValMem *mem, NCDVa
     ASSERT(mem)
     ASSERT(out_value)
     
-    int res = n->params->iparams->func_interp_getargs(mem, out_value);
+    int res = n->params->iparams->func_interp_getargs(n->params->iparams->user, mem, out_value);
     ASSERT(res == 0 || res == 1)
     ASSERT(res == 0 || (NCDVal_Assert(*out_value), 1))
     
@@ -319,7 +319,7 @@ btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n)
     DebugObject_Access(&n->d_obj);
     inst_assert_backend(n);
     
-    return n->params->iparams->func_interp_getretrytime();
+    return n->params->iparams->func_interp_getretrytime(n->params->iparams->user);
 }
 
 int NCDModuleProcess_InitId (NCDModuleProcess *o, NCDModuleInst *n, NCD_string_id_t template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event)
@@ -351,7 +351,7 @@ int NCDModuleProcess_InitId (NCDModuleProcess *o, NCDModuleInst *n, NCD_string_i
     o->interp_func_getobj = NULL;
     
     // init interpreter part
-    if (!(n->params->iparams->func_initprocess(o, template_name))) {
+    if (!(n->params->iparams->func_initprocess(n->params->iparams->user, o, template_name))) {
         goto fail1;
     }
     

+ 12 - 4
ncd/NCDModule.h

@@ -102,41 +102,45 @@ typedef int (*NCDModuleInst_func_getobj) (struct NCDModuleInst_s *inst, NCD_stri
  * only update its internal state, and visibly react only via jobs that it pushes
  * from within this function.
  * 
+ * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  * @param p handle for the new process backend
  * @param template_name name of the template to create the process from,
  *                      as an {@link NCDStringIndex} identifier
  * @return 1 on success, 0 on failure
  */
-typedef int (*NCDModuleInst_func_initprocess) (struct NCDModuleProcess_s *p, NCD_string_id_t template_name);
+typedef int (*NCDModuleInst_func_initprocess) (void *user, struct NCDModuleProcess_s *p, NCD_string_id_t template_name);
 
 /**
  * Function called when the module instance wants the interpreter to
  * initiate termination, as if it received an external terminatio request (signal).
  * 
+ * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  * @param exit_code exit code to return the the operating system. This overrides any previously
  *                  set exit code, and will be overriden by a signal to the value 1.
  *   
  */
-typedef void (*NCDModuleInst_func_interp_exit) (int exit_code);
+typedef void (*NCDModuleInst_func_interp_exit) (void *user, int exit_code);
 
 /**
  * Function called when the module instance wants the interpreter to
  * provide its extra command line arguments.
  * 
+ * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  * @param mem value memory to use
  * @param out_value write value reference here on success
  * @return 1 if available, 0 if not available. If available, but out of memory, return 1
  *         and an invalid value.
  */
-typedef int (*NCDModuleInst_func_interp_getargs) (NCDValMem *mem, NCDValRef *out_value);
+typedef int (*NCDModuleInst_func_interp_getargs) (void *user, NCDValMem *mem, NCDValRef *out_value);
 
 /**
  * Function called when the module instance wants the interpreter to
  * provide its retry time.
  * 
+ * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  * @return retry time in milliseconds
  */
-typedef btime_t (*NCDModuleInst_func_interp_getretrytime) (void);
+typedef btime_t (*NCDModuleInst_func_interp_getretrytime) (void *user);
 
 #define NCDMODULEPROCESS_EVENT_UP 1
 #define NCDMODULEPROCESS_EVENT_DOWN 2
@@ -295,6 +299,10 @@ struct NCDModuleInst_iparams {
      * String index which keeps a mapping between strings and string identifiers.
      */
     NCDStringIndex *string_index;
+    /**
+     * Pointer passed to the interpreter callbacks below, for state keeping.
+     */
+    void *user;
     /**
      * Callback to create a new template process.
      */

+ 9 - 8
ncd/ncd.c

@@ -194,11 +194,11 @@ static int statement_mem_size (struct statement *ps);
 static int statement_allocate_memory (struct statement *ps, int alloc_size);
 static void statement_instance_func_event (NCDModuleInst *inst, int event);
 static int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname, NCDObject *out_object);
-static int statement_instance_func_initprocess (NCDModuleProcess *mp, NCD_string_id_t template_name);
+static int statement_instance_func_initprocess (void *unused, NCDModuleProcess *mp, NCD_string_id_t template_name);
 static void statement_instance_logfunc (NCDModuleInst *inst);
-static void statement_instance_func_interp_exit (int exit_code);
-static int statement_instance_func_interp_getargs (NCDValMem *mem, NCDValRef *out_value);
-static btime_t statement_instance_func_interp_getretrytime (void);
+static void statement_instance_func_interp_exit (void *unused, int exit_code);
+static int statement_instance_func_interp_getargs (void *unused, NCDValMem *mem, NCDValRef *out_value);
+static btime_t statement_instance_func_interp_getretrytime (void *unused);
 static void process_moduleprocess_func_event (struct process *p, int event);
 static int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object);
 
@@ -400,6 +400,7 @@ int main (int argc, char **argv)
     module_params.func_getobj = statement_instance_func_getobj;
     module_params.logfunc = (BLog_logfunc)statement_instance_logfunc;
     module_params.iparams = &module_iparams;
+    module_iparams.user = NULL;
     module_iparams.func_initprocess = statement_instance_func_initprocess;
     module_iparams.func_interp_exit = statement_instance_func_interp_exit;
     module_iparams.func_interp_getargs = statement_instance_func_interp_getargs;
@@ -1447,7 +1448,7 @@ int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname
     return process_find_object(statement_process(ps), ps->i, objname, out_object);
 }
 
-int statement_instance_func_initprocess (NCDModuleProcess* mp, NCD_string_id_t template_name)
+int statement_instance_func_initprocess (void *unused, NCDModuleProcess* mp, NCD_string_id_t template_name)
 {
     // find process
     NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, template_name);
@@ -1488,12 +1489,12 @@ void statement_instance_logfunc (NCDModuleInst *inst)
     BLog_Append("module: ");
 }
 
-void statement_instance_func_interp_exit (int exit_code)
+void statement_instance_func_interp_exit (void *unused, int exit_code)
 {
     start_terminate(exit_code);
 }
 
-int statement_instance_func_interp_getargs (NCDValMem *mem, NCDValRef *out_value)
+int statement_instance_func_interp_getargs (void *unused, NCDValMem *mem, NCDValRef *out_value)
 {
     *out_value = NCDVal_NewList(mem, options.num_extra_args);
     if (NCDVal_IsInvalid(*out_value)) {
@@ -1518,7 +1519,7 @@ fail:
     return 1;
 }
 
-btime_t statement_instance_func_interp_getretrytime (void)
+btime_t statement_instance_func_interp_getretrytime (void *unused)
 {
     return options.retry_time;
 }