design_pattern_for_c  V 1.00
builder_action.c
Go to the documentation of this file.
1 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "builder_action.h"
9 #include "dp_debug.h"
10 
11 /*************
12  * public define
13 *************/
14 
19 
21 /* @{ */
23 static void * builder_action_run(void * this);
24 /* @} */
25 
26 /*************
27  * for BuilderAction method
28 *************/
29 
30 static void * builder_action_run(void * arg) {
31  BuilderAction this = (BuilderAction) arg;
32  int i=0;
33  int ret = LL_BUILDER_SUCCESS;
34  //call builder methods
35  for( i = 0; i < this->builder_method_cnt; i++) {
36  if(this->builder_methods[i]) {
37  DEBUG_ERRPRINT("methods[%d] call\n", i);
38  ret = this->builder_methods[i](this->initial_parameter);
39  if(ret == LL_BUILDER_FAILED) {
40  break;
41  }
42  }
43  }
44 
45  //call result callback
46  if(this->initial_result) {
47  this->initial_result(ret);
48  }
49  free(this);
50  pthread_exit(NULL);
51  return NULL;
52 }
53 
55 /* @{ */
56 void builder_action_destruct(pthread_t tid) {
57  if(0<tid) {
58  pthread_join(tid, NULL);
59  }
60 }
61 
63 
64  pthread_t tid = 0;
65  BuilderAction instance = calloc(1, sizeof(*instance));
66  if(!instance) {
67  DEBUG_ERRPRINT("failed to new BuilderAction\n");
68  return -1;
69  }
70 
71  memcpy(instance, parameter, sizeof(*instance));
72 
73  pthread_create(&tid, NULL, builder_action_run, instance);
74  //free instance in builder_action_run
75  return tid;
76 }
77 /* @} */
#define LL_BUILDER_FAILED
result code : error
This is API definition of builder action API.
void(* free)(EventInstance this)
Definition: event_thread.c:39
#define LL_BUILDER_SUCCESS
result code : success
void builder_action_destruct(pthread_t tid)
deconstruct action
action builder, to call builder by other thread, create class for it
For using debug log.
pthread_t builder_action_construct(builder_action_parameter_t *parameter)
construct action
static void * builder_action_run(void *this)
run action
builder action parameter
#define DEBUG_ERRPRINT(...)
Definition: dp_debug.h:69
builder_action_parameter_t * BuilderAction