Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
taskScheduler.c
Go to the documentation of this file.
1 
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <time.h>
9 #include <pthread.h>
10 #include <unistd.h>
11 #include <signal.h>
12 #include <sys/time.h>
13 
14 #include "taskScheduler.h"
15 
16 void timer_new_task(TASK_S *task,void (*runFunction)(void)){
17  (*task).t_global = 0.0;
18  (*task).T_exec_global = 0.0;
19  (*task).T_mean_global = 0.0;
20  (*task).T_max_global = 0.0;
21  (*task).period_us = 0.0;
22  (*task).isFirstExecution = 1;
23 
24  (*task).run = runFunction;
25 }
26 
27 void timer_start_task(TASK_S *task,void (*alertFunction)(int),int period_us){
28  struct itimerspec itimer;
29  struct sigevent evp;
30  int erno = 0;
31 
32  task->isFirstExecution = 1;
33  task->period_us = period_us;
34 
35  /*evp.sigev_value.sival_ptr = &(task->timer);
36  evp.sigev_notify = SIGEV_SIGNAL;
37  evp.sigev_signo = SIGUSR1;*/
38 
39  //memset (&evp, 0, sizeof (struct sigevent));
40  evp.sigev_value.sival_int = 0;
41  evp.sigev_notify = SIGEV_THREAD;
42  evp.sigev_notify_attributes = NULL;
43  evp.sigev_notify_function = alertFunction;
44 
45  if(timer_create( CLOCK_REALTIME, &evp, &(task->timer))<0)
46  {
47  fprintf(stderr, "[%d]: %s\n", __LINE__, strerror(erno));
48  exit(erno);
49  }
50 
51  /*/ Signal handler configuration
52  struct sigaction satimer;
53  satimer.sa_handler = task->run;
54  sigemptyset( &satimer.sa_mask );
55  satimer.sa_flags = SA_RESTART;*/
56 
57  //if ( sigaction( SIGUSR1, &satimer, NULL ) < 0)
58  if(timer_create(CLOCK_REALTIME, &evp, task->timer) < 0)
59  {
60  //printf( "ERROR: sigaction.\n" );
61  fprintf(stderr, "[%d]: %s\n", __LINE__, strerror(erno));
62  exit(erno);
63  }
64 
65  itimer.it_interval.tv_sec = 0;
66  itimer.it_interval.tv_nsec= task->period_us*1000;
67  itimer.it_value = itimer.it_interval;
68 
69  if(timer_settime(task->timer,0,&itimer,NULL) < 0)
70  {
71  fprintf(stderr,"[%d]: %s\n", __LINE__, strerror(erno));
72  exit(erno);
73  }
74 }
75 
77 {
78  int status = 0;
79  static struct timeval timereset;
80  static struct timeval time;
81  static struct timeval time_exec_start;
82  static struct timeval time_exec_end;
83  static double T_task,t_task,t_task_previous;
84  static double T_task_mean,T_task_min,T_task_max,T_task_exec;
85 
86  gettimeofday(&time_exec_start, NULL);
87 
88  // Time calculation
89  if((*task).isFirstExecution)
90  {
91  gettimeofday(&timereset, NULL);
93  }
94 
95  gettimeofday(&time, NULL);
96  t_task = ((time.tv_sec - timereset.tv_sec) + (time.tv_usec - timereset.tv_usec)*1e-6);
97  T_task = t_task - t_task_previous;
98  t_task_previous = t_task;
99 
100  if((*task).isFirstExecution)
101  {
102  T_task_mean = T_task;
103  T_task_min = 1e200;
104  T_task_max = 0.0;
105  //t0 = t_task;
106  }
107  else
108  {
109  T_task_mean = 0.05*T_task + 0.95*T_task_mean;
110  if(T_task < T_task_min) T_task_min = T_task;
111  if(T_task > T_task_max) T_task_max = T_task;
112  }
113 
114  // Run the thread
115  task->run();
116 
117  gettimeofday(&time_exec_end, NULL);
118  T_task_exec = ((time_exec_end.tv_sec - time_exec_start.tv_sec) + (time_exec_end.tv_usec - time_exec_start.tv_usec)*1e-6);
119 
120  (*task).t_global = t_task;
121  (*task).T_mean_global = T_task_mean;
122  (*task).T_min_global = T_task_min;
123  (*task).T_max_global = T_task_max;
124  (*task).T_exec_global = T_task_exec;
125 
126  (*task).isFirstExecution = 0;
127 }
128 
130  int erno = 0;
131  if(timer_delete((*task).timer)<0){
132  fprintf(stderr,"[%d]:%s\n",__LINE__,strerror(erno));
133  exit(erno);
134  }
135 }
void timer_function_task(TASK_S *task)
Definition: taskScheduler.c:76
volatile int isFirstExecution
Flag to sign first Exec.
Definition: taskScheduler.h:28
void timer_stop_task(TASK_S *task)
Stop some task.
timer_t timer
Definition: taskScheduler.h:21
int status
Definition: communication.c:10
void timer_start_task(TASK_S *task, void(*alertFunction)(int), int period_us)
Start some task.
Definition: taskScheduler.c:27
void timer_new_task(TASK_S *task, void(*runFunction)(void))
Create a task.
Definition: taskScheduler.c:16
Task to schedule definition.
Definition: taskScheduler.h:20
void(* run)()
hook to the action
Definition: taskScheduler.h:30
volatile int period_us
Definition: taskScheduler.h:27