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
 *
8
 * Copyright (C) 2007-2009 Telecom ParisTech, 2010-2020 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

#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);
176
#endif
177

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
#if defined(__PO_HI_USE_VCD) && defined(__unix__)
186
  /* initialize the index of the vcd trace array
187
188
189
  * and its maximum size*/
  __po_hi_vcd_trace_array_index = 0;
  __po_hi_vcd_trace_max_nb_events = 0;
190

191
  if (__po_hi_vcd_trace_array_index == __po_hi_vcd_trace_max_nb_events)
192
193
194
           __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

#if defined(__PO_HI_USE_VCD) && defined(__unix__)
  uint64_t current_timestamp = __po_hi_compute_timestamp();
#endif
310

yoogx's avatar
yoogx committed
311
  int ret;
312

313
#if defined(__PO_HI_USE_VCD) && defined(__unix__)
314
315
  if ((tasks[task].task_category) == TASK_PERIODIC || (tasks[task].task_category) == TASK_SPORADIC)
  {
316
        __po_hi_save_event_in_vcd_trace(current_timestamp, __po_hi_task_wait_dispatch, task, invalid_local_port_t, -1);
317
318
  }
#endif
319

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
#if defined(__PO_HI_USE_VCD) && defined(__unix__)
  current_timestamp = __po_hi_compute_timestamp();
329
330
331
332
333
  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
334

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
/*
 * 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;

420
#if defined (__linux__)  || defined (__APPLE__) || (defined (RTEMS_POSIX) && defined (RTEMS412))
421
422
423
424
425
426
427
428
429

  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
#if (defined (POSIX) && defined (__linux__)) || (defined (RTEMS_POSIX) && defined (RTEMS412))
457

458
  /* RTEMS SMP support has been confirmed to work on TASTE */
459

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

464
465
466
467
468
469
  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);
    }

470
471
472
473
474
475
476
477
  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);
    }
478
479
480
#else
#warning pthread_affinity managmeent disabled for Compcert
#endif
481
482
483
484
485
486
487
488

#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);
    }

489
490
#endif

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

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

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

518
  policy = SCHED_FIFO;
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
  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.
   */
540
  __DEBUGMSG("ABOUT TO SET PRIORITY FOR TASK %d\n" , nb_tasks );
541
542
543
544
545
546
547
548
549
550
  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;
}

551

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

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

  return (__PO_HI_SUCCESS);
565
}
566
567
568

#endif /* POSIX || RTEMS_POSIX */

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

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

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

  cpu_set_t         cpuset;

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

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

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

626
   return rid;
627
628
629
}
#endif

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

638
639
640
641
642
643
   /*
    * 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))
644
645
646
647
648
649
650
   {
      __DEBUGMSG ("ERROR when creating the task\n");
   }
   return newtask;
}
#endif

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

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

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

  return (__PO_HI_SUCCESS);
}

717

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

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

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

  // 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)
743
744
745
    {
      return (__PO_HI_ERROR_CLOCK);
    }
746
  */
747

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

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

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


  /*
   * 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

774
   return (__PO_HI_SUCCESS);
775
776
}

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

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

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

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

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

806
   ret = rt_task_set_periodic (&(tasks[id].xeno_id), TM_NOW,  tasks[id].period.sec * 1000000000 + tasks[id].period.nsec);
807
808
   if (ret != 0)
   {
jdelange's avatar
jdelange committed
809
      __DEBUGMSG ("ERROR when calling rt_task_set_periodic on task %d, ret=%d\n", id, ret);
810
811
812
813
814
815
816
817
      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
818
   tasks[id].task_category = TASK_SPORADIC;
Antonia Francis's avatar
Antonia Francis committed
819
820
821
822
823
824
825
826
827

  /*
   * 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

828
   return (__PO_HI_SUCCESS);
829
830
}

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

837
  timer.tv_sec = time->sec;
838

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

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

843
844
845
846
847
848
849
850
851
852
853
854
855
856
  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
857
858
859
860
861
862
863
864
865
#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))
    {
866
        __PO_HI_DEBUG_CRITICAL("[DELAY UNTIL] SetWaitableTimer failed (%d)\n", GetLastError());
jdelange's avatar
jdelange committed
867
868
869
870
871
        return 2;
    }

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

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


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

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