summaryrefslogtreecommitdiff
path: root/ACE/ace/Service_Object.cpp
blob: 79060626410e0b7dbff63b66fe550f92c8b528eb (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
169
170
171
172
173
174
175
176
177
#include "ace/config-all.h"

#include "ace/Service_Object.h"
#if defined (ACE_HAS_ALLOC_HOOKS)
# include "ace/Malloc_Base.h"
#endif /* ACE_HAS_ALLOC_HOOKS */

#if !defined (__ACE_INLINE__)
#include "ace/Service_Object.inl"
#endif /* __ACE_INLINE__ */

#include "ace/OS_NS_stdio.h"
#include "ace/Service_Types.h"
#include "ace/DLL.h"
#include "ace/ACE.h"
#include "ace/Log_Category.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

ACE_ALLOC_HOOK_DEFINE(ACE_Service_Object)
ACE_ALLOC_HOOK_DEFINE(ACE_Service_Type)

void
ACE_Service_Type::dump () const
{
#if defined (ACE_HAS_DUMP)
  ACE_TRACE ("ACE_Service_Type::dump");
#endif /* ACE_HAS_DUMP */

  // Using printf, since the log facility may not have been
  // initialized yet. Using a "//" prefix, in case the executable
  // happens to be a code generator and the output gets embedded in
  // the generated C++ code.
#ifndef ACE_LACKS_STDERR
  ACE_OS::fprintf(stderr,
                  "// [ST] dump, this=%p, name=%s, type=%p, so=%p, active=%d\n",
                  static_cast<void const *> (this),
                  ACE_TEXT_ALWAYS_CHAR (this->name_),
                  static_cast<void const *> (this->type_),
                  (this->type_ != 0) ? this->type_->object () : 0,
                  this->active_);
#endif
}

ACE_Service_Type::ACE_Service_Type (const ACE_TCHAR *n,
                                    ACE_Service_Type_Impl *t,
                                    const ACE_DLL &dll,
                                    bool active)
  : name_ (0),
    type_ (t),
    dll_ (dll),
    active_ (active),
    fini_already_called_ (false)
{
  ACE_TRACE ("ACE_Service_Type::ACE_Service_Type");
  this->name (n);
}

ACE_Service_Type::ACE_Service_Type (const ACE_TCHAR *n,
                                    ACE_Service_Type_Impl *t,
                                    ACE_SHLIB_HANDLE handle,
                                    bool active)
  : name_ (0),
    type_ (t),
    active_ (active),
    fini_already_called_ (false)
{
  ACE_TRACE ("ACE_Service_Type::ACE_Service_Type");
  this->dll_.set_handle (handle);
  this->name (n);
}

ACE_Service_Type::~ACE_Service_Type ()
{
  ACE_TRACE ("ACE_Service_Type::~ACE_Service_Type");
  this->fini ();

#if defined (ACE_HAS_ALLOC_HOOKS)
  ACE_Allocator::instance()->free(const_cast <ACE_TCHAR *> (this->name_));
#else
  delete [] const_cast <ACE_TCHAR *> (this->name_);
#endif /* ACE_HAS_ALLOC_HOOKS */
}

int
ACE_Service_Type::fini ()
{
  if (ACE::debug ())
    ACELIB_DEBUG ((LM_DEBUG,
                ACE_TEXT ("ACE (%P|%t) ST::fini - destroying name=%s, dll=%s\n"),
                this->name_,
                this->dll_.dll_name_));

  if (this->fini_already_called_)
    return 0;

  this->fini_already_called_ = true;

  if (this->type_ == 0)
    {
      // Returning 1 currently only makes sense for dummy instances, used
      // to "reserve" a spot (kind of like forward-declarations) for a
      // dynamic service. This is necessary to help enforce the correct
      // finalization order, when such service also has any (dependent)
      // static services

      return 1; // No implementation was found.
    }

  int const ret = this->type_->fini ();

  // Ensure type is 0 to prevent invalid access after call to fini.
  this->type_ = 0;

  // Ensure that closing the DLL is done after type_->fini() as it may
  // require access to the code for the service object destructor,
  // which resides in the DLL

  return (ret | this->dll_.close ());
}

int
ACE_Service_Type::suspend () const
{
  ACE_TRACE ("ACE_Service_Type::suspend");
  (const_cast<ACE_Service_Type *> (this))->active_ = false;
  return this->type_->suspend ();
}

int
ACE_Service_Type::resume () const
{
  ACE_TRACE ("ACE_Service_Type::resume");
  (const_cast<ACE_Service_Type *> (this))->active_ = true;
  return this->type_->resume ();
}

ACE_Service_Object::ACE_Service_Object (ACE_Reactor *r)
  : ACE_Event_Handler (r)
{
  ACE_TRACE ("ACE_Service_Object::ACE_Service_Object");
}

ACE_Service_Object::~ACE_Service_Object ()
{
  ACE_TRACE ("ACE_Service_Object::~ACE_Service_Object");
}

int
ACE_Service_Object::suspend ()
{
  ACE_TRACE ("ACE_Service_Object::suspend");
  return 0;
}

int
ACE_Service_Object::resume ()
{
  ACE_TRACE ("ACE_Service_Object::resume");
  return 0;
}

void
ACE_Service_Type::name (const ACE_TCHAR *n)
{
  ACE_TRACE ("ACE_Service_Type::name");

#if defined (ACE_HAS_ALLOC_HOOKS)
  ACE_Allocator::instance()->free(const_cast <ACE_TCHAR *> (this->name_));
#else
  delete [] const_cast <ACE_TCHAR *> (this->name_);
#endif /* ACE_HAS_ALLOC_HOOKS */

  this->name_ = ACE::strnew (n);
}

ACE_END_VERSIONED_NAMESPACE_DECL