Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Last corrections of Tesh tool.
[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
19 \r
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);\r
21 \r
22 static void*\r
23 timer_start_routine(void* p);\r
24 \r
25 ttimer_t\r
26 timer_new(command_t command)\r
27 {\r
28         ttimer_t timer;\r
29         \r
30         timer = xbt_new0(s_timer_t, 1);\r
31         \r
32         timer->command = command;\r
33         timer->thread = NULL;\r
34         timer->timeouted = 0;\r
35         timer->started = xbt_os_sem_init(0);\r
36 \r
37         return timer;\r
38 }\r
39 \r
40 int\r
41 timer_free(ttimer_t* ptr)\r
42 {\r
43         if((*ptr)->started)\r
44                 xbt_os_sem_destroy((*ptr)->started);\r
45 \r
46         free(*ptr);\r
47 \r
48         *ptr = NULL;\r
49         \r
50         return 0;\r
51 }\r
52 \r
53 void\r
54 timer_time(ttimer_t timer)\r
55 {\r
56         timer->thread = xbt_os_thread_create("", timer_start_routine, timer);\r
57 }\r
58 \r
59 static void*\r
60 timer_start_routine(void* p)\r
61 {\r
62         ttimer_t timer = (ttimer_t)p;\r
63         command_t command = timer->command;\r
64         \r
65         int now = (int)time(NULL);\r
66         int lead_time = now + command->context->timeout;\r
67         \r
68         xbt_os_sem_release(timer->started);\r
69         \r
70         while(!command->failed && !command->interrupted && !command->successeded && !timer->timeouted) \r
71         {\r
72                 if(lead_time >= now)\r
73                 {\r
74                         xbt_os_thread_yield();\r
75                         now = (int)time(NULL);\r
76                 }\r
77                 else\r
78                 {\r
79                         timer->timeouted = 1;\r
80                 }\r
81         }\r
82 \r
83         if(timer->timeouted && !command->failed && !command->successeded  && !command->interrupted)\r
84         {\r
85                 ERROR3("[%s] `%s' timed out after %d sec", command->context->pos, command->context->command_line, command->context->timeout);\r
86 \r
87 \r
88                 unit_set_error(command->unit, ECMDTIMEDOUT, 1);\r
89 \r
90                 command_kill(command);\r
91                 command_handle_failure(command, csr_timeout);\r
92 \r
93                 while(!command->reader->done)\r
94                         xbt_os_thread_yield();\r
95 \r
96                 if(command->output->used)\r
97                         INFO2("[%s] Output on timeout:\n%s",command->context->pos, command->output->data);\r
98                 else\r
99                         INFO1("[%s] No output before timeout",command->context->pos);\r
100         }\r
101         \r
102         return NULL;\r
103 }\r
104 \r
105 void\r
106 timer_wait(ttimer_t timer)\r
107 {\r
108         xbt_os_thread_join(timer->thread, NULL);\r
109 }\r