po_hi_task.c 24.7 KB
Newer Older
1
2
3
4
5
/*
 * This is a part of PolyORB-HI-C distribution, a minimal
 * middleware written for generated code from AADL models.
 * You should use it with the Ocarina toolsuite.
 *
yoogx's avatar
yoogx committed
6
 * For more informations, please visit http://taste.tuxfamily.org/wiki
7
 *
yoogx's avatar
yoogx committed
8
 * Copyright (C) 2007-2009 Telecom ParisTech, 2010-2019 ESA & ISAE.
9
10
 */

11
#if defined (__linux__) || defined (RTEMS412)
12
/*  We need GNU extensions to support thread affinify.
13
    These are Linux or RTEMS specific
14
15
16
 */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
yoogx's avatar
yoogx committed
17
#endif /* _GNU_SOURCE */
18
19
20
#endif /* __linux__ || RTEMS412 */

#ifdef __linux__
21
#include <sched.h>
yoogx's avatar
yoogx committed
22
#endif /* __linux__ */
23

24
25
26
27
28
#if defined (SIMULATOR)
#include <um_threads.h>
#undef POSIX
#endif

29
#if defined (RTEMS_POSIX) || defined (__PO_HI_RTEMS_CLASSIC_API)
30
#ifdef RTEMS412
31
32
#include <sys/cpuset.h>
#endif
33
#endif
34

35
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
36
#if defined (__CYGWIN__) || defined (__MINGW32__) || defined (RTEMS_POSIX) || defined (__PO_HI_RTEMS_CLASSIC_API)
yoogx's avatar
yoogx committed
37
#else
38
#include <unistd.h>
yoogx's avatar
yoogx committed
39
40
#endif

41
42
#include <pthread.h>
#include <sched.h>
43
44
#endif

jdelange's avatar
jdelange committed
45
46
47
48
49
#if defined (_WIN32)
#include <tchar.h>
#include <windows.h>
#endif

50
51
52
53
54
55
56
57
#include <errno.h>
/* Headers from the executive */

#include <po_hi_config.h>
#include <po_hi_time.h>
#include <po_hi_task.h>
#include <po_hi_debug.h>
#include <po_hi_returns.h>
58
#include <po_hi_types.h>
59
#include <po_hi_utils.h>
60
61
/* Header files in PolyORB-HI */

62
#if defined (MONITORING)
Antonia Francis's avatar
Antonia Francis committed
63
#include <trace_manager.h>
64
65
66
#endif
/* Headers from run-time verification */

67
#include <deployment.h>
68
/* Header files from generated code */
69

70
71
72
typedef enum __po_hi_task_category_t { TASK_PERIODIC, TASK_SPORADIC, TASK_BACKGROUND }
    __po_hi_task_category;

73
74
75
76
77
78

int nb_tasks; /* number of created tasks */

typedef struct
{
  __po_hi_task_id     id;       /* Identifier of the task in the system */
79
  __po_hi_task_category task_category;
80
  __po_hi_time_t      period;
yoogx's avatar
yoogx committed
81
82
  __po_hi_time_t      offset;

julien.delange's avatar
julien.delange committed
83
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
84
  __po_hi_time_t      timer;
yoogx's avatar
yoogx committed
85
86
87
#if defined (_WIN32)
  DWORD tid;
#else
88
89
  pthread_t           tid;              /* The pthread_t type used by the
                                           POSIX library */
yoogx's avatar
yoogx committed
90
#endif
91
92
  pthread_mutex_t     mutex;
  pthread_cond_t      cond;
jdelange's avatar
jdelange committed
93
94
95
#elif defined (_WIN32)
  __po_hi_time_t      timer;
  DWORD               tid;
96
#elif defined (__PO_HI_RTEMS_CLASSIC_API)
97
  rtems_id            ratemon_period;
98
  rtems_id            rtems_id;
99
100
#elif defined(XENO_NATIVE)
  RT_TASK             xeno_id;
101
102
#elif defined(SIMULATOR)
  um_thread_id        um_id;
103
#endif
104
} __po_hi_task_t;
105
106
107
108
109
110
111
/*
 * Structure of a task, contains platform-dependent members
 */

__po_hi_task_t tasks[__PO_HI_NB_TASKS];
/* Array which contains all tasks informations */

jdelange's avatar
jdelange committed
112
113
114
115
#if defined (_WIN32)
HANDLE  __po_hi_tasks_array[__PO_HI_NB_TASKS];
#endif

116
117
118
119
__po_hi_task_id __po_hi_get_task_id (void) {

#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
  pthread_t pthread_id = pthread_self();
120
  int i;
121

122
  for (i = 0; i < __PO_HI_NB_TASKS; i++) {
123
124
125
126
127
    if (pthread_id == tasks[i].tid) {
      return tasks[i].id;
    }
  }
#endif
128

129
130
131
  return (__PO_HI_ERROR_UNKNOWN);

}
132

yoogx's avatar
yoogx committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifdef __PO_HI_USE_GPROF
void __po_hi_wait_end_of_instrumentation ()
{
#ifdef __PO_HI_RTEMS_CLASSIC_API
   rtems_task_wake_after (10000000 / _TOD_Microseconds_per_tick);
#else
   #include <po_hi_time.h>
   #include <unistd.h>

   __po_hi_time_t now;
   __po_hi_time_t ten_secs;
   __po_hi_time_t time_to_wait;
   __po_hi_get_time (&now);
   __po_hi_seconds (&ten_secs, 10);
   __po_hi_add_times (&time_to_wait, &ten_secs, &now);
   __po_hi_delay_until (&time_to_wait);
#endif
  __DEBUGMSG ("Call exit()\n");
  __po_hi_tasks_killall ();
   exit (1);

  __DEBUGMSG ("exit() called\n");
#ifdef __PO_HI_RTEMS_CLASSIC_API
  rtems_task_delete (rtems_self ());
#endif
}
#endif

161
162
void __po_hi_wait_for_tasks ()
{
yoogx's avatar
yoogx committed
163
164
165
166
167
168

#ifdef __PO_HI_USE_GPROF
   __po_hi_wait_end_of_instrumentation ();
#endif


julien.delange's avatar
julien.delange committed
169
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
170
171
172
173
174
175
176
177

#if defined(__PO_HI_USE_VCD) && defined(__unix__)
  /* the atexit function allows the call of the function
   * __po_hi_create_vcd_file when the program terminates
   * normally */
  atexit(__po_hi_create_vcd_file);
#endif 

178
179
  int i;

180
181
182
183
  for (i = 0; i < __PO_HI_NB_TASKS; i++)
    {
      pthread_join( tasks[i].tid , NULL );
    }
184
185
186
187
188
189
190
191
192
193
194
    
#if defined(__PO_HI_USE_VCD) && defined(__unix__)
  /* initialize the index of the vcd trace array 
  * and its maximum size*/
  __po_hi_vcd_trace_array_index = 0;
  __po_hi_vcd_trace_max_nb_events = 0;
  
  if (__po_hi_vcd_trace_array_index == __po_hi_vcd_trace_max_nb_events)
   	   __po_hi_get_larger_array_for_vcd_trace ();
#endif 
   	   
195
#elif defined (__PO_HI_RTEMS_CLASSIC_API)
196
  rtems_task_suspend(RTEMS_SELF);
197

jdelange's avatar
jdelange committed
198
199
#elif defined (_WIN32)
  WaitForMultipleObjects (__PO_HI_NB_TASKS, __po_hi_tasks_array, TRUE, INFINITE);
200

201
202
203
204
#elif defined (XENO_NATIVE)
  int ret;
  while (1)
  {
205
206
207
    ret = rt_task_join (&(tasks[0].xeno_id));
    if (ret != 0)
      {
208
        __PO_HI_DEBUG_CRITICAL ("Error while calling rt_task_suspend in __po_hi_wait_for_tasks (ret=%d)\n", ret);
209
      }
210
  }
211
212
213
214
#elif defined (SIMULATOR)
  start_scheduler();
  return (__PO_HI_SUCCESS);

215
#endif
jdelange's avatar
jdelange committed
216
217
218
219
220
221
222

#ifdef __PO_HI_DEBUG
   while (1)
   {
      __DEBUGMSG ("Should NEVER be called !!!\n");
   }
#endif
223
224
225
226
227
}

/*
 * compute next period for a task
 * The argument is the task-id
228
 * The task must be a cyclic task (periodic or sporadic)
229
230
231
 */
int __po_hi_compute_next_period (__po_hi_task_id task)
{
jdelange's avatar
jdelange committed
232
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX) || defined (_WIN32)
233

234
    /* If we call this function for the first time, we need to configure
yoogx's avatar
yoogx committed
235
       the initial timer to epoch */
236
237
  if (tasks[task].timer.sec == 0 && tasks[task].timer.nsec == 0) {
    tasks[task].timer = get_epoch();
yoogx's avatar
yoogx committed
238
    __po_hi_add_times(&(tasks[task].timer), &(tasks[task].timer), &tasks[task].offset);
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
    return (__PO_HI_SUCCESS);
  }

  /* Otherwise, we increment either using an absoulte timeline
     (periodic), or relative to the latest dispatch time (sporadic task) */

  switch (tasks[task].task_category) {
  case TASK_PERIODIC:
    __po_hi_add_times(&(tasks[task].timer), &(tasks[task].timer), &tasks[task].period );
    break;

  case TASK_SPORADIC: {
      __po_hi_time_t mytime;

      if (__po_hi_get_time (&mytime) != __PO_HI_SUCCESS) {
        return (__PO_HI_ERROR_CLOCK);
      }

      __po_hi_add_times(&(tasks[task].timer), &mytime, &tasks[task].period );
      break;
   }
260
261
  case TASK_BACKGROUND:
    break;
262
  }
263

264
  return (__PO_HI_SUCCESS);
265

266
#elif defined (__PO_HI_RTEMS_CLASSIC_API)
267
268
   rtems_status_code ret;
   rtems_name name;
269

270
271
   if (tasks[task].ratemon_period == RTEMS_INVALID_ID)
   {
272
      name = rtems_build_name ('P', 'R', 'D' + (char)task, ' ');
273

274
275
276
277
278
279
      __DEBUGMSG ("Create monotonic server for task %d\n", task);
      ret = rtems_rate_monotonic_create (name, &(tasks[task].ratemon_period));
      if (ret != RTEMS_SUCCESSFUL)
      {
         __DEBUGMSG ("Error while creating the monotonic server, task=%d, status=%d\n", task, ret);
      }
280
281
   }
  return (__PO_HI_SUCCESS);
282
283
284
285
286
287
288
289
#elif defined (XENO_NATIVE)

  /*
   * In XENO_NATIVE target, we don't need to recompute the next period
   * since the API provides functionnalities to do it automatically.
   */

  return (__PO_HI_SUCCESS);
290
291
292
#else
   return (__PO_HI_UNAVAILABLE);
#endif
293
294
295
296
}

int __po_hi_wait_for_next_period (__po_hi_task_id task)
{
297
298
299
300
301

/*!
 * Entry ports monitoring at dispatch if MONITORING is defined
 */
#if defined (MONITORING)
Antonia Francis's avatar
Antonia Francis committed
302
  record_event(PERIODIC, WAIT_FOR, task, invalid_port_t, invalid_port_t, invalid_local_port_t, invalid_local_port_t, NULL);
303
304
#endif

yoogx's avatar
yoogx committed
305
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
306
307
308
309
310

#if defined(__PO_HI_USE_VCD) && defined(__unix__)
  uint64_t current_timestamp = __po_hi_compute_timestamp();
#endif
  
yoogx's avatar
yoogx committed
311
  int ret;
312
313
314
315
316
317
318
319

#if defined(__PO_HI_USE_VCD) && defined(__unix__)   
  if ((tasks[task].task_category) == TASK_PERIODIC || (tasks[task].task_category) == TASK_SPORADIC)
  {
	__po_hi_save_event_in_vcd_trace(current_timestamp, __po_hi_task_wait_dispatch, task, invalid_local_port_t, -1);
  }
#endif
  
yoogx's avatar
yoogx committed
320
321
  __po_hi_task_delay_until (&(tasks[task].timer), task);

322
323
324
325
326
  if ( (ret = __po_hi_compute_next_period (task)) != 1)
    {
      return (__PO_HI_ERROR_CLOCK);
    }

327
328
329
330
331
332
333
334
#if defined(__PO_HI_USE_VCD) && defined(__unix__)   
  current_timestamp = __po_hi_compute_timestamp();  
  if ((tasks[task].task_category) == TASK_PERIODIC)
  {
    __po_hi_save_event_in_vcd_trace (current_timestamp, __po_hi_task_dispatched, task, invalid_local_port_t, -1);
  }
#endif
   
jdelange's avatar
jdelange committed
335
  return (__PO_HI_SUCCESS);
336

jdelange's avatar
jdelange committed
337
338
339
340
341
342
343
344
#elif defined (_WIN32)
  int ret;
  __po_hi_task_delay_until (&(tasks[task].timer), task);
  if ( (ret = __po_hi_compute_next_period (task)) != 1)
    {
      return (__PO_HI_ERROR_CLOCK);
    }

345
  return (__PO_HI_SUCCESS);
346

347
#elif defined (__PO_HI_RTEMS_CLASSIC_API)
348
   rtems_status_code ret;
349
   ret = rtems_rate_monotonic_period (tasks[task].ratemon_period, (rtems_interval)__PO_HI_TIME_TO_US(tasks[task].period) / rtems_configuration_get_microseconds_per_tick());
jdelange's avatar
jdelange committed
350
   /*
351
   ret = rtems_rate_monotonic_period (tasks[task].ratemon_period, tasks[task].period / _TOD_Microseconds_per_tick);
jdelange's avatar
jdelange committed
352
   */
353

354
355
356
357
358
359
   switch (ret)
   {
      case RTEMS_SUCCESSFUL:
         return (__PO_HI_SUCCESS);
         break;
      case RTEMS_TIMEOUT:
360
         __DEBUGMSG ("Error in rtems_rate_monotonic_period (TIMEOUT, task = %d)\n", task);
361
362
363
         return (__PO_HI_ERROR_TASK_PERIOD);
         break;
      default:
364
         __DEBUGMSG ("Error in rtems_rate_monotonic_period (unknown, error code=%d, task=%d)\n", ret, task);
365
         return (__PO_HI_ERROR_UNKNOWN);
366
367
         break;
   }
368

369
   return (__PO_HI_UNAVAILABLE);
370

371
#elif defined (XENO_NATIVE)
372
373
374
375
   unsigned long overrun;
   int ret;
   ret = rt_task_wait_period (&overrun);
   if ( ret != 0)
376
   {
377
         __DEBUGMSG ("Error in rt_task_period (task=%d;ret=%d,overrun=%lu)\n", task, ret, overrun);
378
379
380
         return (__PO_HI_ERROR_TASK_PERIOD);
   }

381
382
383
384
   if (overrun != 0)
   {
      return (__PO_HI_ERROR_TASK_PERIOD);
   }
yoogx's avatar
yoogx committed
385

386
   return (__PO_HI_SUCCESS);
387
388
389
#else
  return (__PO_HI_UNAVAILABLE);
#endif
390
391
392
393
394
395
396
397
}

int __po_hi_initialize_tasking( )
{
  int i;

  for (i = 0; i < __PO_HI_NB_TASKS; i++)
  {
398
     __po_hi_seconds (&(tasks[i].period), 0);
399
     tasks[i].id     = invalid_task_id;
400
#ifdef __PO_HI_RTEMS_CLASSIC_API
401
402
      tasks[i].ratemon_period = RTEMS_INVALID_ID;
#endif
403
404
405
406
407
408
409
  }

  nb_tasks = 0;

  return (__PO_HI_SUCCESS);
}

410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * For multi-core aware systems, this function returns the number of
 * core available
 */

#if defined (POSIX) || defined (RTEMS_POSIX)
int __po_hi_number_of_cpus (void)
{
  int cores = 1;

#if defined (__linux__)  || defined (__APPLE__)

  cores = (int) sysconf (_SC_NPROCESSORS_ONLN);
#endif

  return cores;
}

#endif

430
431
432
433
434
435
/*
 * For each kind of system, we declare a generic function that
 * create a thread. For POSIX-compliant systems, the function
 * is called __po_hi_posix_create_thread
 */

436
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
437
pthread_t __po_hi_posix_create_thread (__po_hi_priority_t priority,
438
                                       __po_hi_stack_t    stack_size,
439
440
                                       const __po_hi_int8_t       core_id,
                                       void*              (*start_routine)(void),
jdelange's avatar
jdelange committed
441
                                       void*              arg)
442
443
444
445
{
  int                policy;
  pthread_t          tid;
  pthread_attr_t     attr;
446
  struct sched_param param;
447
  int err;
448

449
450
  /* Create attributes to store all configuration parameters */

451
452
453
454
455
  if (pthread_attr_init (&attr) != 0)
    {
      return ((pthread_t)__PO_HI_ERROR_PTHREAD_ATTR);
    }

456
457
458
459
460
#if (defined (POSIX) && defined (__linux__))

  /* The following is disabled pending further investigation on affinity support in 4.11.99 */
  /*|| (defined (RTEMS_POSIX) && defined (RTEMS412))) */

461
#ifndef __COMPCERT__
462
463
464
  /* Thread affinity */
  cpu_set_t cpuset;

465
466
467
468
469
470
  if ( core_id > __po_hi_number_of_cpus() )
    {
      __PO_HI_DEBUG_CRITICAL("Invalid CPU core id\n");
      return ((pthread_t)__PO_HI_ERROR_PTHREAD_ATTR);
    }

471
472
473
474
475
476
477
478
  CPU_ZERO(&cpuset);
  CPU_SET(core_id, &cpuset);

  if (pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset) != 0)
    {
      __DEBUGMSG("CANNOT SET AFFINTY\n");
      return ((pthread_t)__PO_HI_ERROR_PTHREAD_ATTR);
    }
479
480
481
#else
#warning pthread_affinity managmeent disabled for Compcert
#endif
482
483
484
485
486
487
488
489

#else
  if ( core_id != 0 )
    {
      __PO_HI_DEBUG_CRITICAL("This platform does not support CPU affinity setting\n");
      return ((pthread_t)__PO_HI_ERROR_PTHREAD_ATTR);
    }

490
491
#endif

julien.delange's avatar
julien.delange committed
492
#if defined (POSIX) || defined (XENO_POSIX)
493
494
495
496
  if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM) != 0)
    {
      return ((pthread_t)__PO_HI_ERROR_PTHREAD_ATTR);
    }
497
498
499
500
501
502
503
#elif defined (RTEMS_POSIX)
  if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS) != 0)
  {
    return ((pthread_t)__PO_HI_ERROR_PTHREAD_ATTR);
  }
#endif

504
505
506
  if (stack_size != 0)
    {
      if (pthread_attr_setstacksize (&attr, stack_size) != 0)
yoogx's avatar
yoogx committed
507
508
        {
          return ((pthread_t)__PO_HI_ERROR_PTHREAD_ATTR);
509
        }
510
511
    }

512
513
    err = pthread_create (&tid, &attr, (void* (*)(void*))start_routine, arg);
    if (err != 0)
514
    {
515
      __PO_HI_DEBUG_CRITICAL ("Thread creation failed - pthread_create returned %d\n", err);
516
517
518
      return ((pthread_t)__PO_HI_ERROR_PTHREAD_CREATE);
    }

519
  policy = SCHED_FIFO;
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
  param.sched_priority = priority;

#ifdef __PO_HI_DEBUG
  if (priority < sched_get_priority_min (policy))
  {
      __DEBUGMSG("PRIORITY IS TOO LOW\n");
  }

  if (priority > sched_get_priority_max (policy))
  {
      __DEBUGMSG("PRIORITY IS TOO HIGH\n");
  }
#endif

  /*
   * We print a message that the user has to be root on
   * its computer. In fact, most of the time, the
   * function pthread_setschedparam fails because
   * the user is not root. On many systems, only root
   * can change the priority of the threads.
   */
541
  __DEBUGMSG("ABOUT TO SET PRIORITY FOR TASK %d\n" , nb_tasks );
542
543
544
545
546
547
548
549
550
551
  if (pthread_setschedparam (tid, policy, &param)!=0)
    {
      __DEBUGMSG("CANNOT SET PRIORITY FOR TASK %d\n" , nb_tasks );
      __DEBUGMSG("IF YOU ARE USING POSIX IMPLEMENTATION\n");
      __DEBUGMSG("BE SURE TO BE LOGGED AS ROOT\n");
    }

  return tid;
}

552

553
554
int __po_hi_posix_initialize_task (__po_hi_task_t* task)
{
555
556
557
558
  if (pthread_mutex_init (&(task->mutex), NULL) != 0)
    {
      return (__PO_HI_ERROR_PTHREAD_MUTEX);
    }
559

560
561
562
563
564
565
  if (pthread_cond_init (&(task->cond), NULL) != 0)
    {
      return (__PO_HI_ERROR_PTHREAD_COND);
    }

  return (__PO_HI_SUCCESS);
566
}
567
568
569

#endif /* POSIX || RTEMS_POSIX */

jdelange's avatar
jdelange committed
570
571
#if defined (_WIN32)
DWORD __po_hi_win32_create_thread (__po_hi_task_id    id,
572
                                   __po_hi_priority_t priority,
jdelange's avatar
jdelange committed
573
574
575
576
577
578
                                   __po_hi_stack_t    stack_size,
                                   void*              (*start_routine)(void),
                                   void*              arg)
{
   DWORD tid;
   HANDLE h;
yoogx's avatar
yoogx committed
579
   h = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) start_routine, NULL, 0, &tid);
jdelange's avatar
jdelange committed
580
581
582
583
584
   __po_hi_tasks_array[id] = h;
   return tid;
}
#endif

585
#ifdef __PO_HI_RTEMS_CLASSIC_API
586
rtems_id __po_hi_rtems_create_thread (__po_hi_priority_t priority,
587
                                      __po_hi_stack_t    stack_size,
588
                                      __po_hi_int8_t       core_id,
jdelange's avatar
jdelange committed
589
590
                                      void*              (*start_routine)(void),
                                      void*              arg)
591
{
592
  rtems_id rid;
593
594
595
  if (rtems_task_create (rtems_build_name( 'T', 'A', '0' + nb_tasks, ' '),
                         priority,
                         stack_size,
596
597
598
599
                         RTEMS_DEFAULT_MODES,
                         RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, &rid)
      != RTEMS_SUCCESSFUL)
    {
600
      __DEBUGMSG ("ERROR when creating the task\n");
601
      return __PO_HI_ERROR_CREATE_TASK;
602
603
    }

604
#ifdef RTEMS412
605
  /* Thread affinity API for SMP systems appeared in RTEMS 4.11,
606
607
     section 25 of RTEMS Applications C User's Guide .
  */
608
609
610
611

  cpu_set_t         cpuset;

  CPU_ZERO(&cpuset);
612
  CPU_SET(core_id, &cpuset);
613

614
  if (rtems_task_set_affinity(rid, sizeof(cpuset), &cpuset) != RTEMS_SUCCESSFUL)
615
616
617
618
619
    {
      __DEBUGMSG ("ERROR setting thread affinity\n");
      return __PO_HI_ERROR_CREATE_TASK;
    }
#endif
620

621
  if (rtems_task_start (rid, (rtems_task_entry)start_routine, arg ) != RTEMS_SUCCESSFUL)
622
    {
623
      __DEBUGMSG ("ERROR when starting the task\n");
624
      return __PO_HI_ERROR_CREATE_TASK;
625
    }
626

627
   return rid;
628
629
630
}
#endif

631
#ifdef XENO_NATIVE
632
RT_TASK __po_hi_xenomai_create_thread (__po_hi_priority_t priority,
jdelange's avatar
jdelange committed
633
634
635
                                       __po_hi_stack_t    stack_size,
                                       void*              (*start_routine)(void),
                                       void*              arg)
636
637
638
{
   RT_TASK newtask;

639
640
641
642
643
644
   /*
    * Uses T_JOINABLE at this time, should avoid that later and put 0 instead
    * to be able to use kernel-based threads.
    */

   if (rt_task_create (&newtask, NULL, stack_size, priority, T_JOINABLE))
645
646
647
648
649
650
651
   {
      __DEBUGMSG ("ERROR when creating the task\n");
   }
   return newtask;
}
#endif

652
653
654
int __po_hi_create_generic_task (const __po_hi_task_id      id,
                                 const __po_hi_time_t*      period,
                                 const __po_hi_priority_t   priority,
655
                                 const __po_hi_stack_t      stack_size,
656
                                 const __po_hi_int8_t       core_id,
jdelange's avatar
jdelange committed
657
658
                                 void*                      (*start_routine)(void),
                                 void*                      arg)
659
{
660

661
  if (id == -1)
662
    {
663
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
664
      __po_hi_posix_create_thread (priority, stack_size, core_id, start_routine, arg);
665
      return (__PO_HI_SUCCESS);
jdelange's avatar
jdelange committed
666
667
668
#elif defined (_WIN32)
      __po_hi_win32_create_thread (id, priority, stack_size, start_routine, arg);
      return (__PO_HI_SUCCESS);
669
#elif defined (XENO_NATIVE)
670
      RT_TASK t;
jdelange's avatar
jdelange committed
671
      (void) arg;
jdelange's avatar
jdelange committed
672
      t = __po_hi_xenomai_create_thread (priority, stack_size, start_routine, arg);
673
674
675
676
      if (rt_task_start (&(t), (void*)start_routine, NULL))
      {
         __DEBUGMSG ("ERROR when starting the task\n");
      }
677
      return (__PO_HI_SUCCESS);
678
#elif defined (__PO_HI_RTEMS_CLASSIC_API)
jdelange's avatar
jdelange committed
679
      (void) arg;
680
      __po_hi_rtems_create_thread (priority, stack_size, core_id, start_routine, arg);
681
682
683
684
      return (__PO_HI_SUCCESS);
#else
      return (__PO_HI_UNAVAILABLE);
#endif
685
    }
686
687
  else
    {
688
      __po_hi_task_t* my_task;
689
      my_task         = &(tasks[id]);
690
      __po_hi_time_copy (&(my_task->period), period);
691
      my_task->id     = id;
692

693
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
694
695
      my_task->tid    = __po_hi_posix_create_thread
        (priority, stack_size, core_id, start_routine, arg);
696
      my_task->timer = ORIGIN_OF_TIME;
697
      __po_hi_posix_initialize_task (my_task);
698
#elif defined (__PO_HI_RTEMS_CLASSIC_API)
699
700
      my_task->rtems_id = __po_hi_rtems_create_thread
        (priority, stack_size, core_id, start_routine, arg);
jdelange's avatar
jdelange committed
701
#elif defined (_WIN32)
702
703
      my_task->tid = __po_hi_win32_create_thread
        (id, priority, stack_size, start_routine, arg);
704
#elif defined (XENO_NATIVE)
705
706
707
708
      my_task->xeno_id = __po_hi_xenomai_create_thread
        (priority, stack_size, start_routine, arg);
#elif defined (SIMULATOR)
      my_task->um_id = um_thread_create (start_routine, stack_size, priority);
709
710
711
#else
      return (__PO_HI_UNAVAILABLE);
#endif
712
713
714
715
716
717
      nb_tasks++;
    }

  return (__PO_HI_SUCCESS);
}

718

719
720
721
int __po_hi_create_periodic_task (const __po_hi_task_id     id,
                                  const __po_hi_time_t*     period,
                                  const __po_hi_priority_t  priority,
722
                                  const __po_hi_stack_t     stack_size,
723
                                  const __po_hi_int8_t      core_id,
724
                                  void*                     (*start_routine)(void))
725
{
726

727
  if (__po_hi_create_generic_task( id, period , priority , stack_size, core_id, start_routine, NULL) != 1)
728
729
   {
      __DEBUGMSG ("ERROR when creating generic task (task id=%d)\n", id);
730
      return (__PO_HI_ERROR_CREATE_TASK);
731
   }
732
733

  /*
734
   * Compute the next period of the task, using the
735
736
   *__po_hi_time* functions.
   */
737
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
738
739
740
741
742
743

  // XXX The following is (a priori) not necessary, it should be
  // reviewed for all runtimes: the body of a periodic task already
  // call __po_hi_compute_next_period() as part of its skeleton

  /*if (__po_hi_compute_next_period (id) != __PO_HI_SUCCESS)
744
745
746
    {
      return (__PO_HI_ERROR_CLOCK);
    }
747
  */
748

749
#elif defined (XENO_NATIVE)
750
751
   int ret;

752
   ret = rt_task_set_periodic (&(tasks[id].xeno_id), TM_NOW,  (tasks[id].period.sec * 1000000000) + tasks[id].period.nsec);
753
   if (ret != 0)
754
   {
jdelange's avatar
jdelange committed
755
      __DEBUGMSG ("ERROR when calling rt_task_set_periodic on task %d, ret=%d\n", id, ret);
756
757
      return (__PO_HI_ERROR_CLOCK);
   }
758
759
760
761
762

   if (rt_task_start (&(tasks[id].xeno_id), (void*)start_routine, NULL))
   {
      __DEBUGMSG ("ERROR when starting the task\n");
   }
763
#endif
764
   tasks[id].task_category = TASK_PERIODIC;
Antonia Francis's avatar
Antonia Francis committed
765
766
767
768
769
770
771
772
773
774


  /*
   * Send Task type to trace manager, if there is monitoring.
   */

#if defined (MONITORING)
  record_event(PERIODIC, CREATION, id, invalid_port_t, invalid_port_t, invalid_local_port_t, invalid_local_port_t, NULL);
#endif

775
   return (__PO_HI_SUCCESS);
776
777
}

jdelange's avatar
jdelange committed
778
779
void __po_hi_task_wait_offset (const __po_hi_time_t* time)
{
780

yoogx's avatar
yoogx committed
781
782
  if (time->sec == 0 && time->nsec == 0)
    return; // No need to wait, exit immediately
783

yoogx's avatar
yoogx committed
784
  tasks[__po_hi_get_task_id()].offset = *time;
jdelange's avatar
jdelange committed
785
786
}

787
int __po_hi_create_sporadic_task (const __po_hi_task_id     id,
788
789
                                  const __po_hi_time_t*     period,
                                  const __po_hi_priority_t  priority,
790
                                  const __po_hi_stack_t     stack_size,
791
                                  const __po_hi_int8_t      core_id,
792
                                  void*                     (*start_routine)(void) )
793
794
795
796
797
798
{
  /*
   * Create generic task which will execute the routine given in the
   * last parameter. Typically, a sporadic thread will wait on a
   * mutex.
   */
799
  if (__po_hi_create_generic_task( id, period , priority , stack_size, core_id, start_routine, NULL) != 1)
800
801
802
    {
      return (__PO_HI_ERROR_CREATE_TASK);
    }
803

804
805
806
#if defined (XENO_NATIVE)
   int ret;

807
   ret = rt_task_set_periodic (&(tasks[id].xeno_id), TM_NOW,  tasks[id].period.sec * 1000000000 + tasks[id].period.nsec);
808
809
   if (ret != 0)
   {
jdelange's avatar
jdelange committed
810
      __DEBUGMSG ("ERROR when calling rt_task_set_periodic on task %d, ret=%d\n", id, ret);
811
812
813
814
815
816
817
818
      return (__PO_HI_ERROR_CLOCK);
   }

   if (rt_task_start (&(tasks[id].xeno_id), (void*)start_routine, NULL))
   {
      __DEBUGMSG ("ERROR when starting the task\n");
   }
#endif
819
   tasks[id].task_category = TASK_SPORADIC;
Antonia Francis's avatar
Antonia Francis committed
820
821
822
823
824
825
826
827
828

  /*
   * Send Task type to trace manager, if there is monitoring.
   */

#if defined (MONITORING)
  record_event(SPORADIC, CREATION, id, invalid_port_t, invalid_port_t, invalid_local_port_t, invalid_local_port_t, NULL);
#endif

829
   return (__PO_HI_SUCCESS);
830
831
}

832
int __po_hi_task_delay_until (__po_hi_time_t* time, __po_hi_task_id task)
833
{
834
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
835
836
837
  struct timespec timer;
  int ret;

838
  timer.tv_sec = time->sec;
839

840
  timer.tv_nsec = time->nsec;
841
842

  pthread_mutex_lock (&tasks[task].mutex);
843

844
845
846
847
848
849
850
851
852
853
854
855
856
857
  ret = pthread_cond_timedwait (&tasks[task].cond, &tasks[task].mutex, &timer);

  if ( (ret != 0) && (ret != ETIMEDOUT))
    {
      ret = __PO_HI_ERROR_PTHREAD_COND;
    }
  else
    {
      ret = __PO_HI_SUCCESS;
    }

  pthread_mutex_unlock (&tasks[task].mutex);

  return (ret);
jdelange's avatar
jdelange committed
858
859
860
861
862
863
864
865
866
#elif defined (_WIN32)
   HANDLE hTimer = NULL;
   LARGE_INTEGER ularge;

   hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
   ularge = __po_hi_unix_seconds_to_windows_tick (time->sec, time->nsec);

    if (!SetWaitableTimer(hTimer, &ularge, 0, NULL, NULL, 0))
    {
867
        __PO_HI_DEBUG_CRITICAL("[DELAY UNTIL] SetWaitableTimer failed (%d)\n", GetLastError());
jdelange's avatar
jdelange committed
868
869
870
871
872
        return 2;
    }

    if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
    {
873
        __PO_HI_DEBUG_CRITICAL("[DELAY UNTIL] WaitForSingleObject failed (%d)\n", GetLastError());
jdelange's avatar
jdelange committed
874
875
876
877
    }

    if (CloseHandle(hTimer) != TRUE)
    {
878
        __PO_HI_DEBUG_CRITICAL("[DELAY UNTIL] CloseHandle failed (%d)\n", GetLastError());
jdelange's avatar
jdelange committed
879
880
881
882
    }


  return __PO_HI_SUCCESS;
883
884
#elif defined (XENO_NATIVE)
  int ret;
885
  ret =  rt_task_sleep_until (time->sec * 1000000000 + time->nsec);
886
887
888
889
890
891
  if (ret)
  {
      __DEBUGMSG ("[TASK] Error in rt_task_sleep_until, ret=%d\n", ret);
      return (__PO_HI_ERROR_PTHREAD_COND);
  }
  return (__PO_HI_SUCCESS);
892
893
#endif
  return (__PO_HI_UNAVAILABLE);
894
}
895
896
897
898
899
900
901

void __po_hi_tasks_killall ()
{
   int i;
   for (i = 0; i < __PO_HI_NB_TASKS; i++)
    {
       __DEBUGMSG ("Kill task %d\n", i);
902
#ifdef __PO_HI_RTEMS_CLASSIC_API
903
      rtems_task_delete (tasks[i].rtems_id);
904
#elif defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
905
      pthread_cancel (tasks[i].tid);
yoogx's avatar
yoogx committed
906
      __DEBUGMSG ("[TASKS] Cancel thread %p\n", tasks[i].tid);
907
908
909
#endif
    }
}