Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update CMakeLists for windows
[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, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "gras.h"
10 #include "xbt/synchro.h"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(synchro_crashtest, "Logs of this example");
13
14
15 int test_amount = 99;           /* Up to 999 to not break the logs (and thus the testing mecanism) */
16 int crasher_amount = 99;        /* Up to 99  to not break the logs (and thus the testing mecanism) */
17 int *id;                        /* to pass a pointer to the threads without race condition */
18
19 int more_info = 0;              /* SET IT TO TRUE TO GET MORE INFO */
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 {
33   int id = *(int *) arg;
34   int i;
35
36   for (i = 0; i < test_amount; i++) {
37     if (more_info)
38       INFO10("%03d (%02d|%02d|%02d|%02d|%02d|%02d|%02d|%02d|%02d)",
39              test_amount - i, id, id, id, id, id, id, id, id, id);
40     else
41       INFO0("XXX (XX|XX|XX|XX|XX|XX|XX|XX|XX)");
42   }
43
44   xbt_mutex_acquire(mut_end);
45   running_threads--;
46   xbt_cond_signal(cond_end);
47   xbt_mutex_release(mut_end);
48 }
49
50 int crasher(int argc, char *argv[]);
51 int crasher(int argc, char *argv[])
52 {
53   int i;
54   xbt_thread_t *crashers;
55
56   gras_init(&argc, argv);
57
58   /* initializations of the philosopher mecanisms */
59   id = xbt_new0(int, crasher_amount);
60   crashers = xbt_new(xbt_thread_t, crasher_amount);
61
62   for (i = 0; i < crasher_amount; i++)
63     id[i] = i;
64
65   /* setup the ending mecanism */
66   running_threads = crasher_amount;
67   cond_end = xbt_cond_init();
68   mut_end = xbt_mutex_init();
69
70   /* spawn threads */
71   for (i = 0; i < crasher_amount; i++) {
72     char *name = bprintf("thread %d", i);
73     crashers[i] = xbt_thread_create(name, &crasher_thread, &id[i],0/*not joinable*/);
74     free(name);
75   }
76
77   /* wait for them */
78   xbt_mutex_acquire(mut_end);
79   while (running_threads)
80     xbt_cond_wait(cond_end, mut_end);
81   xbt_mutex_release(mut_end);
82
83   gras_exit();
84   return 0;
85 }
86
87 int main(int argc, char *argv[])
88 {
89   int errcode;
90
91   errcode = crasher(argc, argv);
92
93   return errcode;
94 }