Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
be more friendly in tests with VPATH builds
[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 int more_info = 0;              /* SET IT TO TRUE TO GET MORE INFO */
21
22 /*
23  * Some additionnal code to let the father wait the childs
24  */
25 xbt_mutex_t mut_end;
26 xbt_cond_t cond_end;
27 int running_threads;
28
29 xbt_mutex_t dead_end;
30
31 /* Code ran by each thread */
32 static void crasher_thread(void *arg)
33 {
34   int id = *(int *) arg;
35   int i;
36
37   for (i = 0; i < test_amount; i++) {
38     if (more_info)
39       INFO10("%03d (%02d|%02d|%02d|%02d|%02d|%02d|%02d|%02d|%02d)",
40              test_amount - i, id, id, id, id, id, id, id, id, id);
41     else
42       INFO0("XXX (XX|XX|XX|XX|XX|XX|XX|XX|XX)");
43   }
44
45   xbt_mutex_acquire(mut_end);
46   running_threads--;
47   xbt_cond_signal(cond_end);
48   xbt_mutex_release(mut_end);
49 }
50
51 int crasher(int argc, char *argv[]);
52 int crasher(int argc, char *argv[])
53 {
54   int i;
55   xbt_thread_t *crashers;
56
57   gras_init(&argc, argv);
58
59   /* initializations of the philosopher mecanisms */
60   id = xbt_new0(int, crasher_amount);
61   crashers = xbt_new(xbt_thread_t, crasher_amount);
62
63   for (i = 0; i < crasher_amount; i++)
64     id[i] = i;
65
66   /* setup the ending mecanism */
67   running_threads = crasher_amount;
68   cond_end = xbt_cond_init();
69   mut_end = xbt_mutex_init();
70
71   /* spawn threads */
72   for (i = 0; i < crasher_amount; i++) {
73     char *name = bprintf("thread %d", i);
74     crashers[i] = xbt_thread_create(name, &crasher_thread, &id[i]);
75     free(name);
76   }
77
78   /* wait for them */
79   xbt_mutex_acquire(mut_end);
80   while (running_threads)
81     xbt_cond_wait(cond_end, mut_end);
82   xbt_mutex_release(mut_end);
83
84   gras_exit();
85   return 0;
86 }
87
88 int main(int argc, char *argv[])
89 {
90   int errcode;
91
92   errcode = crasher(argc, argv);
93
94   return errcode;
95 }