Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add the new integrated files version (use xbt data structures instead my own data...
[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 \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         /* TODO : check the parameter */\r
31         \r
32         timer = xbt_new0(s_timer_t, 1);\r
33         \r
34         timer->command = command;\r
35         timer->thread = NULL;\r
36         timer->timeouted = 0;\r
37         timer->started = xbt_os_sem_init(0);\r
38 \r
39         return timer;\r
40 }\r
41 \r
42 int\r
43 timer_free(ttimer_t* ptr)\r
44 {\r
45         /* TODO : check the parameter */\r
46         \r
47         free(*ptr);\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                 error_register("Command timed out", ECMDTIMEDOUT, command->context->command_line, command->unit->fstream->name);\r
86                 command_kill(command);\r
87                 command_handle_failure(command, csr_timeout);\r
88                 \r
89                 \r
90         }\r
91         \r
92         return NULL;\r
93 }\r
94 \r
95 void\r
96 timer_wait(ttimer_t timer)\r
97 {\r
98         xbt_os_thread_join(timer->thread, NULL);\r
99 }\r