summaryrefslogtreecommitdiff
path: root/Kokyu/DSRT_Schedulers.cpp
blob: f0d469883c4626fbf8cd9222ca5da68b4ddef8ce (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
178
179
180
181
182
183
184
185
186
187
188
189
190
// $Id$

#include "ace/Sched_Params.h"
#include "ace/Arg_Shifter.h"

#include "DSRT_Schedulers.h"

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

ACE_RCSID(Kokyu, DSRT_Schedulers, "$Id$")


namespace Kokyu
{
DSRT_Scheduler_Impl::DSRT_Scheduler_Impl ()
  :sched_policy_ (ACE_SCHED_FIFO),
   sched_scope_ (ACE_SCOPE_THREAD),
   min_prio_ (ACE_Sched_Params::priority_min (sched_policy_, sched_scope_)),
   max_prio_ (ACE_Sched_Params::priority_max (sched_policy_, sched_scope_))
{
#if defined (ACE_WIN32)
  // Count up the number of distinct native priorities on current
  // platform.
  int n;
  Priority_t current_priority = this->min_prio_;
  for (n = 1; current_priority != this->max_prio_; ++n)
    {
      current_priority =
        ACE_Sched_Params::next_priority (this->sched_policy_,
                                         current_priority);
    }

  this->prio_range_ = n-1;

#else
  this->prio_range_ = this->max_prio_ - this->min_prio_ + 1;
#endif /* ACE_WIN32 */

}

int
DSRT_Scheduler_Impl::schedule (guid_t id, const DSRT_QoSDescriptor& qos)
{
  return this->schedule_i (id, qos);
}

MIF_Scheduler_Impl::MIF_Scheduler_Impl ()
  : DSRT_Scheduler_Impl ()
{
}

int
MIF_Scheduler_Impl::init (int argc, ACE_TCHAR* argv[])
{
  ACE_Arg_Shifter arg_shifter (argc, argv);

  //ACE_DEBUG ((LM_DEBUG, "svc::init called\n"));

  while (arg_shifter.is_anything_left ())
    {
      const ACE_TCHAR* arg = arg_shifter.get_current ();

      if (ACE_OS::strcasecmp (arg, ACE_LIB_TEXT("-sched_policy")) == 0)
        {
          arg_shifter.consume_arg ();

          if (arg_shifter.is_parameter_next ())
            {
              const ACE_TCHAR* opt = arg_shifter.get_current ();
              if (ACE_OS::strcasecmp (opt, ACE_LIB_TEXT("FIFO")) == 0)
                {
                  this->sched_policy_ = ACE_SCHED_FIFO;
                }
              else if (ACE_OS::strcasecmp (opt, ACE_LIB_TEXT("OTHER")) == 0)
                {
                  this->sched_policy_ = ACE_SCHED_OTHER;
                }
              else
                {
                  ACE_ERROR ((LM_ERROR,
                              ACE_LIB_TEXT("-sched_policy ")
                              ACE_LIB_TEXT("unsupported sched policy <%s>\n"),
                              opt));
                }
              arg_shifter.consume_arg ();
            }
        }
      else if (ACE_OS::strcasecmp (arg, ACE_LIB_TEXT("-sched_scope")) == 0)
        {
          arg_shifter.consume_arg ();

          if (arg_shifter.is_parameter_next ())
            {
              const ACE_TCHAR* opt = arg_shifter.get_current ();
              if (ACE_OS::strcasecmp (opt, ACE_LIB_TEXT("THREAD")) == 0)
                {
                  this->sched_scope_ = ACE_SCOPE_THREAD;
                }
              else if (ACE_OS::strcasecmp (opt, ACE_LIB_TEXT("PROCESS")) == 0)
                {
                  this->sched_scope_ = ACE_SCOPE_PROCESS;
                }
              else
                {
                  ACE_ERROR ((LM_ERROR,
                              ACE_LIB_TEXT("-sched_scope ")
                              ACE_LIB_TEXT("unsupported sched scope <%s>\n"),
                              opt));
                }
              arg_shifter.consume_arg ();
            }
        }
      else if (ACE_OS::strcasecmp (arg, ACE_LIB_TEXT("-min_importance")) == 0)
        {
          arg_shifter.consume_arg ();

          if (arg_shifter.is_parameter_next ())
            {
              const ACE_TCHAR* opt = arg_shifter.get_current ();
              this->min_importance_ = ACE_OS::atoi (opt);
              arg_shifter.consume_arg ();
            }
        }
      else if (ACE_OS::strcasecmp (arg, ACE_LIB_TEXT("-max_importance")) == 0)
        {
          arg_shifter.consume_arg ();

          if (arg_shifter.is_parameter_next ())
            {
              const ACE_TCHAR* opt = arg_shifter.get_current ();
              this->max_importance_ = ACE_OS::atoi (opt);
              arg_shifter.consume_arg ();
            }
        }
    }

  this->importance_range_ = this->max_importance_ - this->min_importance_;
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT("MIF_Scheduler: importance_range = %d\n"),
              this->importance_range_));
  return 0;
}

int
MIF_Scheduler_Impl::fini (void)
{
  return 0;
}

Priority_t
MIF_Scheduler_Impl::schedule_i (guid_t id, const DSRT_QoSDescriptor& qos)
{
  ACE_UNUSED_ARG ((id));
  //  ACE_DEBUG ((LM_DEBUG, "(%t) request for MIF schedule\n"));

  Priority_t prio = qos.importance_ * this->prio_range_ / this->importance_range_;

  if (prio > max_prio_)
    {
      prio = max_prio_;
    }

  return prio;
}


}

// ****************************************************************

//could not use ACE_FACTORY_DEFINE macro here because of Kokyu namespace
// see new Kokyu::MIF_Scheduler_Impl in _make_xx factory function
//need to revisit later

void _gobble_MIF_Scheduler_Impl (void *p) { \
  ACE_Service_Object *_p = ACE_static_cast (ACE_Service_Object *, p);
  ACE_ASSERT (_p != 0);
  delete _p; }
extern "C" Kokyu_Export ACE_Service_Object *
_make_MIF_Scheduler_Impl (ACE_Service_Object_Exterminator *gobbler)
{
  if (gobbler != 0)
    *gobbler = (ACE_Service_Object_Exterminator) _gobble_MIF_Scheduler_Impl;
  ACE_DEBUG ((LM_DEBUG, "about to create MIF_Scheduler_Impl\n" ));
  return new Kokyu::MIF_Scheduler_Impl;
}

// ****************************************************************