Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tesh version 2
[simgrid.git] / tools / tesh2 / src / timer.c
1 #include <timer.h>
2 #include <command.h>
3
4 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
5
6 static void*
7 timer_start_routine(void* p);
8
9 ttimer_t
10 timer_new(command_t command)
11 {
12         ttimer_t timer = xbt_new0(s_timer_t, 1);
13         
14         timer->command = command;
15         timer->thread = NULL;
16         timer->timeouted = 0;
17         timer->started = xbt_os_sem_init(0);
18
19         return timer;
20 }
21
22 void
23 timer_free(ttimer_t* timer)
24 {
25         free(*timer);
26         *timer = NULL;
27 }
28
29 void
30 timer_time(ttimer_t timer)
31 {
32         timer->thread = xbt_os_thread_create("", timer_start_routine, timer);
33 }
34
35 static void*
36 timer_start_routine(void* p)
37 {
38         ttimer_t timer = (ttimer_t)p;
39         command_t command = timer->command;
40         
41         int now = (int)time(NULL);
42         int lead_time = now + command->context->timeout;
43         
44         xbt_os_sem_release(timer->started);
45         
46         while(!command->failed && !command->interrupted && !command->successeded && !timer->timeouted) 
47         {
48                 if(lead_time >= now)
49                 {
50                         xbt_os_thread_yield();
51                         /*usleep(100);*/
52                         now = (int)time(NULL);
53                 }
54                 else
55                 {
56                         /*printf("the timer is timeouted\n");*/
57                         timer->timeouted = 1;
58                 }
59         }
60
61         if(timer->timeouted && !command->failed && !command->successeded  && !command->interrupted)
62         {
63                 command_handle_failure(command, csr_timeout);
64                 command_kill(command);
65                 exit_code = ETIMEOUT;
66                 
67         }
68         
69         return NULL;
70 }
71
72 void
73 timer_wait(ttimer_t timer)
74 {
75         xbt_os_thread_join(timer->thread, NULL);
76 }