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
18 \r\rXBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
19 \r\rstatic void *
\r timer_start_routine(void *p);
20 \r\rttimer_t
\r timer_new(command_t command)
\r
23 \r\rtimer = xbt_new0(s_timer_t, 1);
24 \r\rtimer->command = command;
25 \rtimer->thread = NULL;
26 \rtimer->timeouted = 0;
27 \rtimer->started = xbt_os_sem_init(0);
31 \r\rint
\r timer_free(ttimer_t * ptr)
\r
33 \rif ((*ptr)->started)
34 \rxbt_os_sem_destroy((*ptr)->started);
40 \r\rvoid
\r timer_time(ttimer_t timer)
\r
42 \rtimer->thread = xbt_os_thread_create("", timer_start_routine, timer);
43 \r}
\r\rstatic void *
\r timer_start_routine(void *p)
\r
45 \rttimer_t timer = (ttimer_t) p;
46 \rcommand_t command = timer->command;
47 \r\rint now = (int) time(NULL);
48 \rint lead_time = now + command->context->timeout;
49 \r\rxbt_os_sem_release(timer->started);
50 \r\rwhile (!command->failed && !command->interrupted
51 && !command->successeded && !timer->timeouted)
53 \rif (lead_time >= now)
55 \rxbt_os_thread_yield();
56 \rnow = (int) time(NULL);
61 \rtimer->timeouted = 1;
64 \r\rif (timer->timeouted && !command->failed && !command->successeded
65 && !command->interrupted)
67 \rERROR3("[%s] `%s' timed out after %d sec", command->context->pos,
68 command->context->command_line, command->context->timeout);
69 \r\r\runit_set_error(command->unit, ECMDTIMEDOUT, 1,
70 command->context->pos);
71 \r\rcommand_kill(command);
72 \rcommand_handle_failure(command, csr_timeout);
73 \r\rwhile (!command->reader->done)
74 \rxbt_os_thread_yield();
75 \r\rif (command->output->used)
76 \rINFO2("[%s] Output on timeout:\n%s", command->context->pos,
77 command->output->data);
80 \rINFO1("[%s] No output before timeout", command->context->pos);
85 \r\rvoid
\r timer_wait(ttimer_t timer)
\r
87 \rxbt_os_thread_join(timer->thread, NULL);