Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8749dac50a524f7b67aeaca4c60dfc52e179abc7
[simgrid.git] / teshsuite / xbt / parallel_log_crashtest.c
1 /* $Id$ */
2
3 /* synchro_crashtest -- tries to crash the logging mecanism by doing // logs*/
4
5 /* Copyright (c) 2007 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "gras.h"
11 #include "xbt/synchro.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(synchro_crashtest,"Logs of this example");
14
15
16 int test_amount = 99;    /* Up to 999 to not break the logs (and thus the testing mecanism) */
17 int crasher_amount = 99; /* Up to 99  to not break the logs (and thus the testing mecanism) */
18 int *id; /* to pass a pointer to the threads without race condition */
19
20
21 /*
22  * Some additionnal code to let the father wait the childs
23  */
24 xbt_mutex_t mut_end;
25 xbt_cond_t cond_end;
26 int running_threads;
27
28 xbt_mutex_t dead_end;
29
30 /* Code ran by each thread */
31 static void crasher_thread(void *arg) {
32    int id = *(int*)arg;
33    int i;
34    
35    for (i=0; i<test_amount; i++) {
36       INFO10("%03d (%02d|%02d|%02d|%02d|%02d|%02d|%02d|%02d|%02d)",test_amount-i,id,id,id,id,id,id,id,id,id);
37    }
38    
39    xbt_mutex_lock(mut_end);
40    running_threads--;
41    xbt_cond_signal(cond_end);
42    xbt_mutex_unlock(mut_end);
43 }
44
45 int crasher (int argc,char *argv[]);
46 int crasher (int argc,char *argv[]) {
47   int i;
48   xbt_thread_t *crashers;
49    
50   gras_init(&argc,argv);
51
52   /* initializations of the philosopher mecanisms */
53   id = xbt_new0(int,crasher_amount); 
54   crashers = xbt_new(xbt_thread_t,crasher_amount);
55      
56   for (i=0; i<crasher_amount; i++)
57      id[i] = i;
58    
59   /* setup the ending mecanism */
60   running_threads = crasher_amount; 
61   cond_end = xbt_cond_init();
62   mut_end = xbt_mutex_init();
63   
64   /* spawn threads */
65   for (i=0; i<crasher_amount; i++) {
66      char *name = bprintf("thread %d",i);
67      crashers[i] = xbt_thread_create(name,&crasher_thread,&id[i]);
68      free(name);
69   }
70   
71   /* wait for them */
72   xbt_mutex_lock(mut_end);
73   while (running_threads) 
74      xbt_cond_wait(cond_end,mut_end);
75   xbt_mutex_unlock(mut_end);
76        
77   gras_exit();
78   return 0;
79 }
80
81 int main(int argc, char *argv[]){
82   int errcode;
83
84   errcode=crasher(argc,argv);
85  
86   return errcode;
87 }