po_hi_main.c 1.86 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
18
19

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

pthread_cond_t cond_init;
pthread_mutex_t mutex_init;

int initialized_tasks;
int nb_tasks_to_init;

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

36
37
38
39
40
41
42
43
44
45
46
47
int __po_hi_initialize ()
{
  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;

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

50
51
  initialized_tasks = 0;

52
53
54
55
  if (pthread_cond_init (&cond_init, NULL) != 0)
  {
     return (__PO_HI_ERROR_PTHREAD_COND);
  }
56
57
58
59
60
61
62
63
64
65
66
67
68
69

  __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)
70
  {
71
    return (__PO_HI_ERROR_PTHREAD_MUTEX);
72
  }
73
74

  initialized_tasks++;
75
76
77

  __DEBUGMSG ("[MAIN] %d task(s) initialized\n", initialized_tasks);
 
78
79
80
81
82
83
84
85
  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);
}