Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Indent the rest of the code (examples, buildtools, doc...) except for examples/SMPI...
[simgrid.git] / tools / tesh2 / src / timer.c
1 /*\r
2  * src/timer.c - type representing a timer.\r
3  *\r
4  * Copyright 2008,2009 Martin Quinson, Malek Cherier All right reserved. \r
5  *\r
6  * This program is free software; you can redistribute it and/or modify it \r
7  * under the terms of the license (GNU LGPL) which comes with this package.\r
8  *\r
9  * Purpose:\r
10  *              This file contains all the definitions of the functions related with\r
11  *              the tesh timer type.\r
12  *\r
13  */  \r
14     \r
15 #include <timer.h>\r
16 #include <command.h>\r
17 #include <unit.h>\r
18     \r\rXBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
19 \r\rstatic void *\r timer_start_routine(void *p);
20 \r\rttimer_t \r timer_new(command_t command) \r
21 {
22   \rttimer_t timer;
23   \r\rtimer = xbt_new0(s_timer_t, 1);
24   \r\rtimer->command = command;
25   \rtimer->thread = NULL;
26   \rtimer->timeouted = 0;
27   \rtimer->started = xbt_os_sem_init(0);
28   \r\rreturn timer;
29 \r}
30
31 \r\rint \r timer_free(ttimer_t * ptr) \r
32 {
33   \rif ((*ptr)->started)
34     \rxbt_os_sem_destroy((*ptr)->started);
35   \r\rfree(*ptr);
36   \r\r*ptr = NULL;
37   \r\rreturn 0;
38 \r}
39
40 \r\rvoid \r timer_time(ttimer_t timer) \r
41 {
42   \rtimer->thread = xbt_os_thread_create("", timer_start_routine, timer);
43 \r\r\rstatic void *\r timer_start_routine(void *p) \r
44 {
45   \rttimer_t timer = (ttimer_t) p;
46   \rcommand_t command = timer->command;
47   \r\rint now = (int) time(NULL);
48   \rint lead_time = now + command->context->timeout;
49   \r\rxbt_os_sem_release(timer->started);
50   \r\rwhile (!command->failed && !command->interrupted
51            && !command->successeded && !timer->timeouted)
52     \r {
53     \rif (lead_time >= now)
54       \r {
55       \rxbt_os_thread_yield();
56       \rnow = (int) time(NULL);
57       \r}
58     \r
59     else
60       \r {
61       \rtimer->timeouted = 1;
62       \r}
63     \r}
64   \r\rif (timer->timeouted && !command->failed && !command->successeded
65         && !command->interrupted)
66     \r {
67     \rERROR3("[%s] `%s' timed out after %d sec", command->context->pos,
68             command->context->command_line, command->context->timeout);
69     \r\r\runit_set_error(command->unit, ECMDTIMEDOUT, 1,
70                       command->context->pos);
71     \r\rcommand_kill(command);
72     \rcommand_handle_failure(command, csr_timeout);
73     \r\rwhile (!command->reader->done)
74       \rxbt_os_thread_yield();
75     \r\rif (command->output->used)
76       \rINFO2("[%s] Output on timeout:\n%s", command->context->pos,
77              command->output->data);
78     \r
79     else
80       \rINFO1("[%s] No output before timeout", command->context->pos);
81     \r}
82   \r\rreturn NULL;
83 \r}
84
85 \r\rvoid \r timer_wait(ttimer_t timer) \r
86 {
87   \rxbt_os_thread_join(timer->thread, NULL);
88 \r\r