Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
check_list.c File Reference
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "gwlib/gwlib.h"

Go to the source code of this file.

Data Structures

struct  producer_info
 
struct  Item
 

Macros

#define NUM_PRODUCERS   (4)
 
#define NUM_CONSUMERS   (4)
 
#define NUM_ITEMS_PER_PRODUCER   (1*1000)
 

Functions

static Itemnew_item (long producer, long num, long index)
 
static void producer (void *arg)
 
static void consumer (void *arg)
 
static void init_received (void)
 
static void main_for_producer_and_consumer (void)
 
static int compare_cstr (void *item, void *pat)
 
static void main_for_list_add_and_delete (void)
 
static void main_for_extract (void)
 
int main (void)
 

Variables

static char received [NUM_PRODUCERS *NUM_ITEMS_PER_PRODUCER]
 

Macro Definition Documentation

#define NUM_CONSUMERS   (4)

Definition at line 68 of file check_list.c.

Referenced by main_for_producer_and_consumer().

#define NUM_ITEMS_PER_PRODUCER   (1*1000)

Definition at line 69 of file check_list.c.

Referenced by main_for_producer_and_consumer(), and producer().

#define NUM_PRODUCERS   (4)

Definition at line 67 of file check_list.c.

Referenced by main_for_producer_and_consumer().

Function Documentation

static int compare_cstr ( void *  item,
void *  pat 
)
static

Definition at line 181 of file check_list.c.

Referenced by main_for_extract(), and main_for_list_add_and_delete().

181  {
182  /* Remove a macro definition of strcmp to prevent warnings from
183  * a broken version in the glibc libary. */
184  #undef strcmp
185  return strcmp(item, pat) == 0;
186 }
static void consumer ( void *  arg)
static

Definition at line 113 of file check_list.c.

References gwlist_consume(), Item::index, producer_info::list, and received.

Referenced by main_for_producer_and_consumer().

113  {
114  List *list;
115  Item *item;
116 
117  list = arg;
118  for (;;) {
119  item = gwlist_consume(list);
120  if (item == NULL)
121  break;
122  received[item->index] = 1;
123  gw_free(item);
124  }
125 }
static char received[NUM_PRODUCERS *NUM_ITEMS_PER_PRODUCER]
Definition: check_list.c:78
void * gwlist_consume(List *list)
Definition: list.c:427
Definition: list.c:102
long index
Definition: check_list.c:84
static void init_received ( void  )
static

Definition at line 128 of file check_list.c.

References received.

Referenced by main_for_producer_and_consumer().

128  {
129  memset(received, 0, sizeof(received));
130 }
static char received[NUM_PRODUCERS *NUM_ITEMS_PER_PRODUCER]
Definition: check_list.c:78
int main ( void  )

Definition at line 269 of file check_list.c.

References GW_INFO, gwlib_init(), gwlib_shutdown(), log_set_output_level(), main_for_extract(), main_for_list_add_and_delete(), and main_for_producer_and_consumer().

269  {
270  gwlib_init();
275  gwlib_shutdown();
276  return 0;
277 }
static void main_for_list_add_and_delete(void)
Definition: check_list.c:189
void log_set_output_level(enum output_level level)
Definition: log.c:217
static void main_for_extract(void)
Definition: check_list.c:223
Definition: log.h:69
void gwlib_shutdown(void)
Definition: gwlib.c:94
static void main_for_producer_and_consumer(void)
Definition: check_list.c:133
void gwlib_init(void)
Definition: gwlib.c:78
static void main_for_extract ( void  )
static

Definition at line 223 of file check_list.c.

References compare_cstr(), gwlist_append(), gwlist_create, gwlist_destroy(), gwlist_extract_matching(), gwlist_get(), gwlist_len(), producer_info::list, and panic.

Referenced by main().

223  {
224  static char *items[] = {
225  "one",
226  "two",
227  "three",
228  };
229  int num_items = sizeof(items) / sizeof(items[0]);
230  int num_repeats = 3;
231  int i, j;
232  char *p;
233  List *list, *extracted;
234 
235  list = gwlist_create();
236 
237  for (j = 0; j < num_repeats; ++j)
238  for (i = 0; i < num_items; ++i)
239  gwlist_append(list, items[i]);
240 
241  for (j = 0; j < num_items; ++j) {
242  extracted = gwlist_extract_matching(list, items[j],
243  compare_cstr);
244  if (extracted == NULL)
245  panic(0, "no extracted elements, should have!");
246  for (i = 0; i < gwlist_len(list); ++i) {
247  p = gwlist_get(list, i);
248  if (strcmp(p, items[j]) == 0)
249  panic(0, "list contains `%s' after "
250  "extracting it!",
251  items[j]);
252  }
253  for (i = 0; i < gwlist_len(extracted); ++i) {
254  p = gwlist_get(extracted, i);
255  if (strcmp(p, items[j]) != 0)
256  panic(0,
257  "extraction returned wrong element!");
258  }
259  gwlist_destroy(extracted, NULL);
260  }
261 
262  if (gwlist_len(list) != 0)
263  panic(0, "list is not empty after extracting everything");
264 
265  gwlist_destroy(list, NULL);
266 }
void gwlist_append(List *list, void *item)
Definition: list.c:179
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
List * gwlist_extract_matching(List *list, void *pat, gwlist_item_matches_t *cmp)
Definition: list.c:322
#define panic
Definition: log.h:87
static int compare_cstr(void *item, void *pat)
Definition: check_list.c:181
#define gwlist_create()
Definition: list.h:136
Definition: list.c:102
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
static void main_for_list_add_and_delete ( void  )
static

Definition at line 189 of file check_list.c.

References compare_cstr(), gwlist_append(), gwlist_create, gwlist_delete_equal(), gwlist_delete_matching(), gwlist_destroy(), gwlist_get(), gwlist_len(), producer_info::list, and panic.

Referenced by main().

189  {
190  static char *items[] = {
191  "one",
192  "two",
193  "three",
194  };
195  int num_items = sizeof(items) / sizeof(items[0]);
196  int num_repeats = 3;
197  int i, j;
198  char *p;
199  List *list;
200 
201  list = gwlist_create();
202 
203  for (j = 0; j < num_repeats; ++j)
204  for (i = 0; i < num_items; ++i)
205  gwlist_append(list, items[i]);
206  gwlist_delete_matching(list, items[0], compare_cstr);
207  for (i = 0; i < gwlist_len(list); ++i) {
208  p = gwlist_get(list, i);
209  if (strcmp(p, items[0]) == 0)
210  panic(0, "list contains `%s' after deleting it!",
211  items[0]);
212  }
213 
214  for (i = 0; i < num_items; ++i)
215  gwlist_delete_equal(list, items[i]);
216  if (gwlist_len(list) != 0)
217  panic(0, "list is not empty after deleting everything");
218 
219  gwlist_destroy(list, NULL);
220 }
void gwlist_append(List *list, void *item)
Definition: list.c:179
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
long gwlist_delete_equal(List *list, void *item)
Definition: list.c:266
long gwlist_delete_matching(List *list, void *pat, gwlist_item_matches_t *matches)
Definition: list.c:240
#define panic
Definition: log.h:87
static int compare_cstr(void *item, void *pat)
Definition: check_list.c:181
#define gwlist_create()
Definition: list.h:136
Definition: list.c:102
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
static void main_for_producer_and_consumer ( void  )
static

Definition at line 133 of file check_list.c.

References consumer(), error(), gwlist_add_producer(), gwlist_create, gwlist_delete(), gwlist_get(), gwlist_len(), gwthread_create, gwthread_join_every(), producer_info::id, Item::index, init_received(), producer_info::list, Item::num, NUM_CONSUMERS, NUM_ITEMS_PER_PRODUCER, NUM_PRODUCERS, panic, Item::producer, producer(), received, producer_info::start_index, and warning().

Referenced by main().

133  {
134  List *list;
135  int i;
136  Item *item;
137  struct producer_info tab[NUM_PRODUCERS];
138  long p, n, index;
139  int errors;
140 
141  list = gwlist_create();
142  init_received();
143 
144  for (i = 0; i < NUM_PRODUCERS; ++i) {
145  tab[i].list = list;
146  tab[i].start_index = i * NUM_ITEMS_PER_PRODUCER;
147  gwlist_add_producer(list);
148  tab[i].id = gwthread_create(producer, tab + i);
149  }
150  for (i = 0; i < NUM_CONSUMERS; ++i)
151  gwthread_create(consumer, list);
152 
155 
156  while (gwlist_len(list) > 0) {
157  item = gwlist_get(list, 0);
158  gwlist_delete(list, 0, 1);
159  warning(0, "main: %ld %ld %ld", (long) item->producer,
160  item->num, item->index);
161  }
162 
163  errors = 0;
164  for (p = 0; p < NUM_PRODUCERS; ++p) {
165  for (n = 0; n < NUM_ITEMS_PER_PRODUCER; ++n) {
166  index = p * NUM_ITEMS_PER_PRODUCER + n;
167  if (!received[index]) {
168  error(0, "Not received: producer=%ld "
169  "item=%ld index=%ld",
170  tab[p].id, n, index);
171  errors = 1;
172  }
173  }
174  }
175 
176  if (errors)
177  panic(0, "Not all messages were received.");
178 }
void error(int err, const char *fmt,...)
Definition: log.c:612
long producer
Definition: check_list.c:82
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
#define NUM_CONSUMERS
Definition: check_list.c:68
#define NUM_PRODUCERS
Definition: check_list.c:67
static void producer(void *arg)
Definition: check_list.c:99
void gwthread_join_every(gwthread_func_t *func)
static char received[NUM_PRODUCERS *NUM_ITEMS_PER_PRODUCER]
Definition: check_list.c:78
#define NUM_ITEMS_PER_PRODUCER
Definition: check_list.c:69
List * list
Definition: check_list.c:72
long num
Definition: check_list.c:83
void gwlist_delete(List *list, long pos, long count)
Definition: list.c:232
void warning(int err, const char *fmt,...)
Definition: log.c:624
#define gwthread_create(func, arg)
Definition: gwthread.h:90
static void init_received(void)
Definition: check_list.c:128
static void consumer(void *arg)
Definition: check_list.c:113
#define panic
Definition: log.h:87
#define gwlist_create()
Definition: list.h:136
void gwlist_add_producer(List *list)
Definition: list.c:383
Definition: list.c:102
long index
Definition: check_list.c:84
static Item* new_item ( long  producer,
long  num,
long  index 
)
static

Definition at line 88 of file check_list.c.

References Item::index, Item::num, Item::producer, and producer().

Referenced by add_tid(), and producer().

88  {
89  Item *item;
90 
91  item = gw_malloc(sizeof(Item));
92  item->producer = producer;
93  item->num = num;
94  item->index = index;
95  return item;
96 }
long producer
Definition: check_list.c:82
static void producer(void *arg)
Definition: check_list.c:99
long num
Definition: check_list.c:83
long index
Definition: check_list.c:84
static void producer ( void *  arg)
static

Definition at line 99 of file check_list.c.

References gwlist_produce(), gwlist_remove_producer(), gwthread_self(), info(), producer_info::list, new_item(), NUM_ITEMS_PER_PRODUCER, and producer_info::start_index.

Referenced by main_for_producer_and_consumer(), and new_item().

99  {
100  long i, index;
101  long id;
102  struct producer_info *info;
103 
104  info = arg;
105 
106  id = gwthread_self();
107  index = info->start_index;
108  for (i = 0; i < NUM_ITEMS_PER_PRODUCER; ++i, ++index)
109  gwlist_produce(info->list, new_item(id, i, index));
111 }
void info(int err, const char *fmt,...)
Definition: log.c:636
long gwthread_self(void)
void gwlist_produce(List *list, void *item)
Definition: list.c:411
static Item * new_item(long producer, long num, long index)
Definition: check_list.c:88
#define NUM_ITEMS_PER_PRODUCER
Definition: check_list.c:69
List * list
Definition: check_list.c:72
long start_index
Definition: check_list.c:73
void gwlist_remove_producer(List *list)
Definition: list.c:401

Variable Documentation

char received[NUM_PRODUCERS *NUM_ITEMS_PER_PRODUCER]
static

Definition at line 78 of file check_list.c.

Referenced by consumer(), init_received(), and main_for_producer_and_consumer().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.