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 timer = xbt_new0(s_timer_t, 1);
\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
41 timer_free(ttimer_t* ptr)
\r
44 xbt_os_sem_destroy((*ptr)->started);
\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 ERROR3("[%s] `%s' timed out after %d sec", command->context->pos, command->context->command_line, command->context->timeout);
\r
88 unit_set_error(command->unit, ECMDTIMEDOUT, 1);
\r
90 command_kill(command);
\r
91 command_handle_failure(command, csr_timeout);
\r
93 while(!command->reader->done)
\r
94 xbt_os_thread_yield();
\r
96 if(command->output->used)
\r
97 INFO2("[%s] Output on timeout:\n%s",command->context->pos, command->output->data);
\r
99 INFO1("[%s] No output before timeout",command->context->pos);
\r
106 timer_wait(ttimer_t timer)
\r
108 xbt_os_thread_join(timer->thread, NULL);
\r