2 * src/timer.c - type representing a timer.
\r
4 * Copyright 2008,2009 Martin Quinson, Malek Cherier All right reserved.
\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
10 * This file contains all the definitions of the functions related with
\r
11 * the tesh timer type.
\r
16 #include <command.h>
\r
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
\r
23 timer_start_routine(void* p);
\r
26 timer_new(command_t command)
\r
30 /* TODO : check the parameter */
\r
32 timer = xbt_new0(s_timer_t, 1);
\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
43 timer_free(ttimer_t* ptr)
\r
45 /* TODO : check the parameter */
\r
54 timer_time(ttimer_t timer)
\r
56 timer->thread = xbt_os_thread_create("", timer_start_routine, timer);
\r
60 timer_start_routine(void* p)
\r
62 ttimer_t timer = (ttimer_t)p;
\r
63 command_t command = timer->command;
\r
65 int now = (int)time(NULL);
\r
66 int lead_time = now + command->context->timeout;
\r
68 xbt_os_sem_release(timer->started);
\r
70 while(!command->failed && !command->interrupted && !command->successeded && !timer->timeouted)
\r
72 if(lead_time >= now)
\r
74 xbt_os_thread_yield();
\r
75 now = (int)time(NULL);
\r
79 timer->timeouted = 1;
\r
83 if(timer->timeouted && !command->failed && !command->successeded && !command->interrupted)
\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
96 timer_wait(ttimer_t timer)
\r
98 xbt_os_thread_join(timer->thread, NULL);
\r