-#include <timer.h>
-#include <command.h>
-
-XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
-
-static void*
-timer_start_routine(void* p);
-
-ttimer_t
-timer_new(command_t command)
-{
- ttimer_t timer = xbt_new0(s_timer_t, 1);
-
- timer->command = command;
- timer->thread = NULL;
- timer->timeouted = 0;
- timer->started = xbt_os_sem_init(0);
-
- return timer;
-}
-
-void
-timer_free(ttimer_t* timer)
-{
- free(*timer);
- *timer = NULL;
-}
-
-void
-timer_time(ttimer_t timer)
-{
- timer->thread = xbt_os_thread_create("", timer_start_routine, timer);
-}
-
-static void*
-timer_start_routine(void* p)
-{
- ttimer_t timer = (ttimer_t)p;
- command_t command = timer->command;
-
- int now = (int)time(NULL);
- int lead_time = now + command->context->timeout;
-
- xbt_os_sem_release(timer->started);
-
- while(!command->failed && !command->interrupted && !command->successeded && !timer->timeouted)
- {
- if(lead_time >= now)
- {
- xbt_os_thread_yield();
- /*usleep(100);*/
- now = (int)time(NULL);
- }
- else
- {
- /*printf("the timer is timeouted\n");*/
- timer->timeouted = 1;
- }
- }
-
- if(timer->timeouted && !command->failed && !command->successeded && !command->interrupted)
- {
- command_handle_failure(command, csr_timeout);
- command_kill(command);
- exit_code = ETIMEOUT;
-
- }
-
- return NULL;
-}
-
-void
-timer_wait(ttimer_t timer)
-{
- xbt_os_thread_join(timer->thread, NULL);
-}
+/*\r
+ * src/timer.c - type representing a timer.\r
+ *\r
+ * Copyright 2008,2009 Martin Quinson, Malek Cherier All right reserved. \r
+ *\r
+ * This program is free software; you can redistribute it and/or modify it \r
+ * under the terms of the license (GNU LGPL) which comes with this package.\r
+ *\r
+ * Purpose:\r
+ * This file contains all the definitions of the functions related with\r
+ * the tesh timer type.\r
+ *\r
+ */\r
+ \r
+#include <timer.h>\r
+#include <command.h>\r
+#include <unit.h>\r
+\r
+\r
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);\r
+\r
+static void*\r
+timer_start_routine(void* p);\r
+\r
+ttimer_t\r
+timer_new(command_t command)\r
+{\r
+ ttimer_t timer;\r
+ \r
+ timer = xbt_new0(s_timer_t, 1);\r
+ \r
+ timer->command = command;\r
+ timer->thread = NULL;\r
+ timer->timeouted = 0;\r
+ timer->started = xbt_os_sem_init(0);\r
+\r
+ return timer;\r
+}\r
+\r
+int\r
+timer_free(ttimer_t* ptr)\r
+{\r
+ if((*ptr)->started)\r
+ xbt_os_sem_destroy((*ptr)->started);\r
+\r
+ free(*ptr);\r
+\r
+ *ptr = NULL;\r
+ \r
+ return 0;\r
+}\r
+\r
+void\r
+timer_time(ttimer_t timer)\r
+{\r
+ timer->thread = xbt_os_thread_create("", timer_start_routine, timer);\r
+}\r
+\r
+static void*\r
+timer_start_routine(void* p)\r
+{\r
+ ttimer_t timer = (ttimer_t)p;\r
+ command_t command = timer->command;\r
+ \r
+ int now = (int)time(NULL);\r
+ int lead_time = now + command->context->timeout;\r
+ \r
+ xbt_os_sem_release(timer->started);\r
+ \r
+ while(!command->failed && !command->interrupted && !command->successeded && !timer->timeouted) \r
+ {\r
+ if(lead_time >= now)\r
+ {\r
+ xbt_os_thread_yield();\r
+ now = (int)time(NULL);\r
+ }\r
+ else\r
+ {\r
+ timer->timeouted = 1;\r
+ }\r
+ }\r
+\r
+ if(timer->timeouted && !command->failed && !command->successeded && !command->interrupted)\r
+ {\r
+ ERROR3("[%s] `%s' timed out after %d sec", command->context->pos, command->context->command_line, command->context->timeout);\r
+\r
+\r
+ unit_set_error(command->unit, ECMDTIMEDOUT, 1);\r
+\r
+ command_kill(command);\r
+ command_handle_failure(command, csr_timeout);\r
+\r
+ while(!command->reader->done)\r
+ xbt_os_thread_yield();\r
+\r
+ if(command->output->used)\r
+ INFO2("[%s] Output on timeout:\n%s",command->context->pos, command->output->data);\r
+ else\r
+ INFO1("[%s] No output before timeout",command->context->pos);\r
+ }\r
+ \r
+ return NULL;\r
+}\r
+\r
+void\r
+timer_wait(ttimer_t timer)\r
+{\r
+ xbt_os_thread_join(timer->thread, NULL);\r
+}\r