summaryrefslogtreecommitdiff
path: root/ace/Module.cpp
blob: 9471de5cc8ec1e4f6aef3374e2302d320df28a9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
161
162
163
164
165
166
167
168
// Module.cpp
// $Id$

#if !defined (ACE_MODULE_C)
#define ACE_MODULE_C

#define ACE_BUILD_DLL
#include "ace/Module.h"
#include "ace/Stream_Modules.h"

#if !defined (__ACE_INLINE__)
#include "ace/Module.i"
#endif /* __ACE_INLINE__ */

ACE_ALLOC_HOOK_DEFINE(ACE_Module)

template <ACE_SYNCH_1> void
ACE_Module<ACE_SYNCH_2>::dump (void) const
{
  ACE_TRACE ("ACE_Module<ACE_SYNCH_2>::dump");
}

template <ACE_SYNCH_1> void
ACE_Module<ACE_SYNCH_2>::writer (ACE_Task<ACE_SYNCH_2> *q)
{ 
  ACE_TRACE ("ACE_Module<ACE_SYNCH_2>::writer");
  this->q_pair_[1] = q;
  if (q != 0)
    ACE_CLR_BITS (q->flags_, ACE_Task_Flags::ACE_READER);
}

template <ACE_SYNCH_1> void
ACE_Module<ACE_SYNCH_2>::reader (ACE_Task<ACE_SYNCH_2> *q) 
{
  ACE_TRACE ("ACE_Module<ACE_SYNCH_2>::reader");
  this->q_pair_[0] = q;
  if (q != 0)
    ACE_SET_BITS (q->flags_, ACE_Task_Flags::ACE_READER);
}

// Link this ACE_Module on top of ACE_Module M.

template <ACE_SYNCH_1> void
ACE_Module<ACE_SYNCH_2>::link (ACE_Module<ACE_SYNCH_2> *m)
{
  ACE_TRACE ("ACE_Module<ACE_SYNCH_2>::link");
  this->next (m);
  this->writer ()->next (m->writer ());
  m->reader ()->next (this->reader ());
}

template <ACE_SYNCH_1> int
ACE_Module<ACE_SYNCH_2>::open (const char *mod_name, 
			       ACE_Task<ACE_SYNCH_2> *writer_q, 
			       ACE_Task<ACE_SYNCH_2> *reader_q, 
			       void *arg)
{
  ACE_TRACE ("ACE_Module<ACE_SYNCH_2>::open");
  this->name (mod_name);
  this->arg_ = arg;

  if (writer_q == 0)
    writer_q = new ACE_Thru_Task<ACE_SYNCH_2>;
  if (reader_q == 0)
    reader_q = new ACE_Thru_Task<ACE_SYNCH_2>;

  // Make sure that the memory is allocated before proceding.
  if (writer_q == 0 || reader_q == 0)
    {
      delete writer_q;
      delete reader_q;
      errno = ENOMEM;
      return -1;
    }

  this->reader (reader_q);
  this->writer (writer_q);

  // Setup back pointers.
  reader_q->mod_ = this;
  writer_q->mod_ = this;
  return 0;
}

// Set and get pointer to sibling ACE_Task in ACE_Module.

template <ACE_SYNCH_1> ACE_Task<ACE_SYNCH_2> *
ACE_Module<ACE_SYNCH_2>::sibling (ACE_Task<ACE_SYNCH_2> *orig)
{
  ACE_TRACE ("ACE_Module<ACE_SYNCH_2>::sibling");
  if (this->q_pair_[0] == orig)
    return this->q_pair_[1];
  else if (this->q_pair_[1] == orig)
    return this->q_pair_[0];
  else
    return 0;
}

template <ACE_SYNCH_1> ACE_INLINE
ACE_Module<ACE_SYNCH_2>::ACE_Module (void)
{
  ACE_TRACE ("ACE_Module<ACE_SYNCH_2>::ACE_Module");
  this->name ("<unknown>");
  // Do nothing...
}

// Should never be called... 
template <ACE_SYNCH_1> ACE_INLINE
ACE_Module<ACE_SYNCH_2>::~ACE_Module (void)
{
  ACE_TRACE ("ACE_Module<ACE_SYNCH_2>::~ACE_Module");
  ACE_ERROR ((LM_ERROR, "destructor for %s should never be called!\n", 
	     this->name ()));
}

template <ACE_SYNCH_1> ACE_INLINE
ACE_Module<ACE_SYNCH_2>::ACE_Module (const char *mod_name,
				     ACE_Task<ACE_SYNCH_2> *writer_q, 
				     ACE_Task<ACE_SYNCH_2> *reader_q, 
				     void *flags)
{
  ACE_TRACE ("ACE_Module<ACE_SYNCH_2>::ACE_Module");
  if (this->open (mod_name, writer_q, reader_q, flags) == -1)
    ACE_ERROR ((LM_ERROR, "%p\n", "ACE_Module"));
}
		
template <ACE_SYNCH_1> int
ACE_Module<ACE_SYNCH_2>::close (u_long flags)
{
  ACE_TRACE ("ACE_Module<ACE_SYNCH_2>::close");
  ACE_Task<ACE_SYNCH_2> *reader_q = this->reader ();
  ACE_Task<ACE_SYNCH_2> *writer_q = this->writer ();
  int result = 0;

  if (reader_q != 0)
    {
      if (reader_q->close () == -1)
	result = -1;
      reader_q->flush ();
      reader_q->next (0);
    }

  if (writer_q != 0)
    {
      if (writer_q->close () == -1)
	result = -1;
      writer_q->flush ();
      writer_q->next (0);
    }

  if (ACE_BIT_ENABLED (flags, ACE_Module<ACE_SYNCH_2>::M_DELETE))
    {
      // Only delete the Tasks if there aren't any more threads
      // running in them.
      if (reader_q->thr_count () == 0)
	delete reader_q;
      this->reader (0);

      if (writer_q->thr_count () == 0)
	delete writer_q;
      this->writer (0);

      delete (void *) this; // Darn well better be allocated dynamically!!!
    }
  return result;
}

#endif /* ACE_MODULE_C */