po_hi_main.c 2.01 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
20
21

#include <po_hi_config.h>
#include <po_hi_returns.h>
#include <po_hi_task.h>
#include <po_hi_protected.h>

22
#if __PO_HI_NB_PORTS > 1
23
#include <po_hi_transport.h>
24
#include <drivers/po_hi_driver_sockets.h>
25
#endif
26

27
/* included files from PolyORB-HI-C */
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void __po_hi_initialize_transport ();

pthread_cond_t cond_init;
pthread_mutex_t mutex_init;

int initialized_tasks;
int nb_tasks_to_init;

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
#ifdef __PO_HI_NEED_DRIVER_SOCKETS
49
#if __PO_HI_NB_PORTS > 1
50
  if (__PO_HI_TRANSPORT_SOCKET_NEED_RECEIVER_TASK ())
51
52
53
  {
     nb_tasks_to_init++;
  }
54
#endif
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#endif

  initialized_tasks = 0;

  if (pthread_cond_init (&cond_init, 
			 NULL) != 0)
    {
      return (__PO_HI_ERROR_PTHREAD_COND);
    }

  __po_hi_initialize_tasking ();

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

#if __PO_HI_NB_NODES > 1
  /* We initialize the transport only if
   * the node have servers*/
  __po_hi_initialize_transport ();
#endif

  return (__PO_HI_SUCCESS);
}

int __po_hi_wait_initialization ()
{
  if (pthread_mutex_lock (&mutex_init) != 0)
    return (__PO_HI_ERROR_PTHREAD_MUTEX);

  initialized_tasks++;
  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);
}