po_hi_main.c 2.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * 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.
 *
 * For more informations, please visit http://ocarina.enst.fr
 *
 * Copyright (C) 2007-2009, GET-Telecom Paris.
 */

#include <pthread.h>
12
/* POSIX files */
13

14
15
#include <deployment.h>
/* included files from the generated code */
16
17

#include <po_hi_config.h>
18
#include <po_hi_common.h>
19
20
#include <po_hi_returns.h>
#include <po_hi_task.h>
21
#include <po_hi_debug.h>
22
#include <po_hi_protected.h>
23
/* included files from PolyORB-HI-C */
24
25
26
27
28
29
30

pthread_cond_t cond_init;
pthread_mutex_t mutex_init;

int initialized_tasks;
int nb_tasks_to_init;

31
32
void __po_hi_initialize_add_task ()
{
33
      __DEBUGMSG ("[MAIN] Add a task to initialize\n");
34
35
36
      nb_tasks_to_init++;
}

37
38
int __po_hi_initialize ()
{
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifdef RTEMS_POSIX
#include <rtems/rtems/clock.h>
   rtems_status_code status;
     rtems_time_of_day time;

     time.year   = 1988;
  time.month  = 12;
  time.day    = 31;
  time.hour   = 9;
  time.minute = 0;
  time.second = 0;
  time.ticks  = 0;

  status = rtems_clock_set( &time );
  
#endif

56
57
58
59
60
61
62
63
64
65
  if (pthread_mutex_init (&mutex_init, NULL) != 0 )
    {
      return (__PO_HI_ERROR_PTHREAD_MUTEX);
    }

  /* The barrier is initialized with __PO_HI_NB_TASKS +1
   * members, because the main function must pass the barrier
   */
  nb_tasks_to_init = __PO_HI_NB_TASKS + 1;

66
67
  __DEBUGMSG ("[MAIN] Have %d tasks to init\n", nb_tasks_to_init);

68
69
  initialized_tasks = 0;

70
71
72
73
  if (pthread_cond_init (&cond_init, NULL) != 0)
  {
     return (__PO_HI_ERROR_PTHREAD_COND);
  }
74
75
76
77
78
79
80
81
82
83
84
85
86
87

  __po_hi_initialize_tasking ();

  /* Initialize protected objects */
#if __PO_HI_NB_PROTECTED > 0
  __po_hi_protected_init();
#endif

  return (__PO_HI_SUCCESS);
}

int __po_hi_wait_initialization ()
{
  if (pthread_mutex_lock (&mutex_init) != 0)
88
  {
89
    return (__PO_HI_ERROR_PTHREAD_MUTEX);
90
  }
91
92

  initialized_tasks++;
93
94
95

  __DEBUGMSG ("[MAIN] %d task(s) initialized\n", initialized_tasks);
 
96
97
98
99
100
101
102
103
  while (initialized_tasks < nb_tasks_to_init)
    {
      pthread_cond_wait (&cond_init, &mutex_init);
    }
  pthread_cond_broadcast (&cond_init);
  pthread_mutex_unlock (&mutex_init);
  return (__PO_HI_SUCCESS);
}
104
105
106
107
108
109
110
111
112
113
114

#ifdef __PO_HI_USE_GPROF
void __po_hi_wait_end_of_instrumentation ()
{
   #include <po_hi_time.h> 
   __po_hi_time_t now;
   __po_hi_get_time (&now);
   __po_hi_delay_until (__po_hi_add_times (now, __po_hi_seconds (10)));
}
#endif