design_pattern_for_c  V 1.00
chain_element.c
Go to the documentation of this file.
1 
6 #include "dp_util.h"
7 #include "chain_element.h"
8 
10 /* @{ */
11 
20 };
21 
26 /* @} */
27 
34  pthread_mutex_t *lock;
35 };
36 
37 #define CHAIN_ELEMENT_LOCK(instance) DPUTIL_LOCK(instance->lock)
38 #define CHAIN_ELEMENT_UNLOCK DPUTIL_UNLOCK
39 
40 /*************
41  * for ChainElementPart class API definition
42 *************/
44  ChainElementPart element = calloc(1, sizeof(*element));
45  if(!element) {
46  return NULL;
47  }
48 
49  memcpy(&element->data, element_data, sizeof(element->data));
50  return element;
51 }
52 
54  free(this);
55 }
56 
57 ChainElement chain_element_new(int is_threadsafe) {
58  ChainElement element = calloc(1, sizeof(*element) + (is_threadsafe * sizeof(pthread_mutex_t)));
59  if(!element) {
60  return NULL;
61  }
62 
63  if(is_threadsafe) {
64  element->lock = (pthread_mutex_t *)(element + 1);
65  pthread_mutex_init(element->lock, NULL);
66  }
67  return element;
68 }
69 
70 /*************
71  * public API definition
72 *************/
74  int ret = COR_FAILED;
77  if(part) {
79  elemnt_data->result_element_part = part;
80  ret = COR_SUCCESS;
81  }
83  return ret;
84 }
85 
88  ChainElementPart part = this->head;
89  while(part) {
90  if(part->data.func == func) {
91  //free ChainElementPart
92  ChainElementPart free_part = part;
93  part=part->next;
94  dputil_list_pull((DPUtilList)this, (DPUtilListData)free_part);
95  chain_element_part_free(free_part);
96  } else {
97  part=part->next;
98  }
99  }
101 }
102 
104 CHAIN_ELEMENT_LOCK(this)
105  dputil_list_pull((DPUtilList)this, (DPUtilListData)element);
106  chain_element_part_free(element);
108 }
109 
110 void chain_element_call(ChainElement this, void *arg) {
111 CHAIN_ELEMENT_LOCK(this)
112  ChainElementPart part = this->head;
113  while(part) {
114  if((part->data.func) && (part->data.func(arg, part->data.ctx) == CoR_RETURN)) {
115  /*exit*/
116  break;
117  }
118  part=part->next;
119  }
121 }
122 
124 CHAIN_ELEMENT_LOCK(this)
125  ChainElementPart part;
126  do {
129  } while(part);
131  free(this);
132 }
#define CHAIN_ELEMENT_LOCK(instance)
Definition: chain_element.c:37
#define COR_SUCCESS
Definition: chain_element.h:10
#define COR_FAILED
Definition: chain_element.h:11
Utility headers
ChainElementPart tail
list of ChainElementPart
Definition: chain_element.c:33
void dputil_list_pull(DPUtilList this, DPUtilListData data)
list pull
Definition: dp_util.c:59
ChainElementPart result_element_part
Definition: chain_element.h:28
void chain_element_remove_function(ChainElement this, chain_func func)
Definition: chain_element.c:86
void(* free)(EventInstance this)
Definition: event_thread.c:39
void chain_element_delete(ChainElement this)
ChainElement chain_element_new(int is_threadsafe)
Definition: chain_element.c:57
ChainElementPart next
Definition: chain_element.c:17
pthread_mutex_t * lock
Definition: chain_element.c:34
chain_element_data_t data
Definition: chain_element.c:19
struct chain_element_part * ChainElementPart
Definition: chain_element.c:15
int chain_element_add_function(ChainElement this, chain_element_req_t *elemnt_data)
Definition: chain_element.c:73
#define CHAIN_ELEMENT_UNLOCK
Definition: chain_element.c:38
void chain_element_call(ChainElement this, void *arg)
cor_result_e(* chain_func)(void *arg, void *ctx)
chain func
static ChainElementPart chain_element_part_new(chain_element_data_t *element_data)
new ChainElementPart
Definition: chain_element.c:43
DPUtilListData dputil_list_pop(DPUtilList this)
list pop
Definition: dp_util.c:80
void dputil_list_push(DPUtilList this, DPUtilListData data)
list push
Definition: dp_util.c:21
This is API for element of chain.
ChainElementPart prev
Definition: chain_element.c:18
exit to call chain_api
chain function element part class instance definition
ChainElementPart head
Definition: chain_element.c:32
chain_element_data_t element_data
Definition: chain_element.h:27
void chain_element_remove_element_part(ChainElement this, ChainElementPart element)
static void chain_element_part_free(ChainElementPart this)
free ChainElementPart
Definition: chain_element.c:53