Logo AND Algorithmique Numérique Distribuée

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