summaryrefslogtreecommitdiff
path: root/TAO/tao/Time_Policy_Manager.cpp
blob: 8bd8945a444cbba9e2fb4522dee1e117df14619e (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
// $Id$

#include "tao/Time_Policy_Manager.h"
#include "tao/ORB_Time_Policy.h"
#include "tao/debug.h"

#include "ace/Dynamic_Service.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_strings.h"

#if (TAO_HAS_TIME_POLICY == 1)

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_Time_Policy_Manager::TAO_Time_Policy_Manager ()
  : time_policy_strategy_ (0)
#if defined(TAO_USE_HR_TIME_POLICY_STRATEGY)
  , time_policy_setting_ (TAO_HR_TIME_POLICY)
#else
  , time_policy_setting_ (TAO_OS_TIME_POLICY)
#endif
{
}

TAO_Time_Policy_Manager::~TAO_Time_Policy_Manager ()
{
  TAO::ORB_Time_Policy::reset_time_policy ();
}

// = Service Configurator hooks.
/// Dynamic linking hook
int
TAO_Time_Policy_Manager::init (int argc, ACE_TCHAR* argv[])
{
  return this->parse_args (argc, argv);
}

/// Parse svc.conf arguments
int
TAO_Time_Policy_Manager::parse_args (int argc, ACE_TCHAR* argv[])
{
  ACE_TRACE ("TAO_Time_Policy_Manager::parse_args");

  int curarg;

  for (curarg = 0; curarg < argc && argv[curarg]; ++curarg)
    {
      if (ACE_OS::strcasecmp (argv[curarg],
                                   ACE_TEXT("-ORBTimePolicyStrategy")) == 0)
        {
          curarg++;
          if (curarg < argc)
            {
              ACE_TCHAR* name = argv[curarg];

              if (ACE_OS::strcasecmp (name,
                                      ACE_TEXT("OS")) == 0)
                this->time_policy_setting_ = TAO_OS_TIME_POLICY;
              else if (ACE_OS::strcasecmp (name,
                                           ACE_TEXT("HR")) == 0)
                this->time_policy_setting_ = TAO_HR_TIME_POLICY;
              else
                {
                  this->time_policy_setting_ = TAO_DYN_TIME_POLICY;
                  this->time_policy_name_ = name;
                }
            }
        }
    }
  return 0;
}

ACE_Timer_Queue * TAO_Time_Policy_Manager::create_timer_queue (void)
{
  if (this->time_policy_setting_ != TAO_OS_TIME_POLICY)
    {
      // locking scope
      {
        ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                          monitor,
                          this->lock_,
                          0);

        // check if time policy strategy has already been initialized
        if (this->time_policy_strategy_ == 0)
          {
            // load strategy
            if (this->time_policy_setting_ == TAO_HR_TIME_POLICY)
              {
                this->time_policy_name_ = ACE_TEXT ("TAO_HR_TIME_POLICY");
                this->time_policy_strategy_ =
                    ACE_Dynamic_Service<TAO_Time_Policy_Strategy>::instance (
                        this->time_policy_name_.c_str ());
              }
            else
              this->time_policy_strategy_ =
                  ACE_Dynamic_Service<TAO_Time_Policy_Strategy>::instance (
                      this->time_policy_name_.c_str ());
            if (this->time_policy_strategy_ == 0)
              {
                ACE_ERROR ((LM_ERROR,
                            ACE_TEXT ("TAO (%P|%t) - TAO_Time_Policy_Manager: ")
                            ACE_TEXT ("FAILED to load time policy strategy '%C'\n"),
                            this->time_policy_name_.c_str ()));
                return 0;
              }

            if (TAO_debug_level > 1)
              {
                ACE_DEBUG ((LM_INFO,
                    ACE_TEXT ("TAO (%P|%t) - TAO_Time_Policy_Manager: ")
                    ACE_TEXT ("loaded time policy strategy '%C'\n"),
                    this->time_policy_name_.c_str ()));
              }

            // handle one time initialization of ORB_Time_Policy
            TAO::ORB_Time_Policy::set_time_policy (
                this->time_policy_strategy_->get_time_policy ());
          }
      }

      return this->time_policy_strategy_->create_timer_queue ();
    }

  // OS time policy is builtin default
  return 0;
}

void
TAO_Time_Policy_Manager::destroy_timer_queue (ACE_Timer_Queue *tmq)
{
  if (this->time_policy_setting_ != TAO_OS_TIME_POLICY)
    {
      // locking scope
      {
        ACE_GUARD (TAO_SYNCH_MUTEX,
                   monitor,
                   this->lock_);

        // check if time policy strategy has already been initialized
        if (this->time_policy_strategy_ == 0)
          {
            return;
          }
      }

      this->time_policy_strategy_->destroy_timer_queue (tmq);
    }
}


ACE_STATIC_SVC_DEFINE (TAO_Time_Policy_Manager,
                       ACE_TEXT ("Time_Policy_Manager"),
                       ACE_SVC_OBJ_T,
                       &ACE_SVC_NAME (TAO_Time_Policy_Manager),
                       ACE_Service_Type::DELETE_THIS |
                                  ACE_Service_Type::DELETE_OBJ,
                       0)

ACE_FACTORY_DEFINE (TAO, TAO_Time_Policy_Manager)

TAO_END_VERSIONED_NAMESPACE_DECL

#endif /* TAO_HAS_TIME_POLICY */