summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/lib/Scheduler_Factory.cpp
blob: dd197beafe9c0bd00af63f5e68ec1adc702b38d5 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// ============================================================================
//
// $Id$
//
// ============================================================================

#include <ace/OS.h>

#include "Runtime_Scheduler.h"
#include "Scheduler_Factory.h"

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

RtecScheduler::Scheduler_ptr ACE_Scheduler_Factory::server_ = 0;

static int entry_count = -1;
static ACE_Scheduler_Factory::POD_RT_Info* rt_info = 0;

int ACE_Scheduler_Factory::use_runtime (int ec,
					POD_RT_Info rti[])
{
  if (server_ != 0 || entry_count != -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
			 "ACE_Scheduler_Factory::use_runtime - "
			 "server already configured\n"), -1);
    }

  entry_count = ec;
  rt_info = rti;

  return 0;
}

RtecScheduler::Scheduler_ptr static_server ()
{
  RtecScheduler::Scheduler_ptr server_ = 0;

  typedef RtecScheduler::RT_Info* RT_Info_ptr;
  RtecScheduler::RT_Info** info;
  ACE_NEW_RETURN (info, RT_Info_ptr[entry_count], 0);
  for (int i = 0; i < entry_count; ++i)
    {
      info[i] = new RtecScheduler::RT_Info;
      if (info[i] == 0)
	{
	  for (int j = 0; j < i; ++j)
	    {
	      delete info[i];
	    }
	  delete[] info;
	  ACE_ERROR_RETURN ((LM_ERROR,
			     "ACE_Scheduler_Factory::config_runtime - "
			     "cannot allocate RT_Info\n"), 0);
	}
      info[i]->entry_point = rt_info[i].entry_point;
      info[i]->handle = rt_info[i].handle;
      info[i]->worst_case_execution_time = rt_info[i].worst_case_execution_time;
      info[i]->typical_execution_time = rt_info[i].typical_execution_time;
      info[i]->cached_execution_time = rt_info[i].cached_execution_time;
      info[i]->period = rt_info[i].period;
      info[i]->importance = rt_info[i].importance;
      info[i]->quantum = rt_info[i].quantum;
      info[i]->threads = rt_info[i].threads;
      info[i]->priority = rt_info[i].priority;
      info[i]->subpriority = rt_info[i].subpriority;
      info[i]->preemption_priority = rt_info[i].preemption_priority;
    }
  server_ = new ACE_Runtime_Scheduler (entry_count, info);

  if (server_ == 0)
    {
      for (int i = 0; i < entry_count; ++i)
	{
	  delete info[i];
	}
      delete[] info;
      ACE_ERROR_RETURN ((LM_ERROR,
			 "ACE_Scheduler_Factory::config_runtime - "
			 "cannot allocate server\n"), 0);
    }
  ACE_DEBUG ((LM_DEBUG,
	      "ACE_Scheduler_Factory - configured static server\n"));
  CORBA::Object::_duplicate (server_);
  return server_;
}

int
ACE_Scheduler_Factory::use_config (CosNaming::NamingContext_ptr naming)
{
  if (server_ != 0 || entry_count != -1)
    {
      // No errors, runtime execution simply takes precedence over
      // config runs.
      return 0;
    }

  ACE_TRY
    {
      CosNaming::Name schedule_name (1);
      schedule_name[0].id = CORBA::string_dup ("ScheduleService");
      schedule_name.length (1);
      CORBA::Object_ptr objref =
	naming->resolve (schedule_name, ACE_TRY_ENV);
      ACE_CHECK_ENV;

      server_ =
	RtecScheduler::Scheduler::_narrow(objref, ACE_TRY_ENV);
      ACE_CHECK_ENV;

      RtecScheduler::Scheduler::_duplicate (server_);
      ACE_CHECK_ENV;
    }
  ACE_CATCHANY
    {
      server_ = 0;
      ACE_ERROR_RETURN ((LM_ERROR,
			 "ACE_Scheduler_Factory::use_context - "
			 " exception while resolving server\n"), -1);
    }
  ACE_ENDTRY;
  return 0;
}

int
ACE_Scheduler_Factory::use_config (CORBA::ORB_ptr orb)
{
  if (server_ != 0 || entry_count != -1)
    {
      // No errors, runtime execution simply takes precedence over
      // config runs.
      return 0;
    }

  ACE_TRY
    {
      CORBA::Object_ptr objref =
	orb->resolve_initial_references ("ScheduleService");
      ACE_CHECK_ENV;

      server_ =
	RtecScheduler::Scheduler::_narrow(objref, ACE_TRY_ENV);
      ACE_CHECK_ENV;

      RtecScheduler::Scheduler::_duplicate (server_);
      ACE_CHECK_ENV;
    }
  ACE_CATCHANY
    {
      server_ = 0;
      ACE_ERROR_RETURN ((LM_ERROR,
			 "ACE_Scheduler_Factory::use_context - "
			 " exception while resolving server\n"), -1);
    }
  ACE_ENDTRY;
  return 0;
}


RtecScheduler::Scheduler_ptr
ACE_Scheduler_Factory::server (void)
{
  if (server_ == 0 && entry_count != -1)
    {
      server_ = static_server ();
    }
	
  if (server_ == 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
			 "ACE_Scheduler_Factor::server - "
			 "no scheduling service configured\n"), 0);
    }
  return server_;
}

static char header[] =
"// This file was automatically generated by Scheduler_Factory\n"
"// before editing the file please consider generating it again\n"
"\n"
"#include \"Scheduler_Factory.h\"\n"
"\n";

static char footer[] = 
"\n"
"// This setups Scheduler_Factory to use the runtime version\n"
"static int scheduler_factory_setup = \n"
"  ACE_Scheduler_Factory::use_runtime (sizeof (infos)/sizeof (infos[0]),\n"
"                                      infos);\n"
"\n"
"// EOF\n";

static char start_infos[] = 
"static ACE_Scheduler_Factory::POD_RT_Info infos[] = {\n";

static char end_infos[] = 
"};\n";

int ACE_Scheduler_Factory::dump_schedule
    (const RtecScheduler::RT_Info_Set& infos,
     const char* filename)
{
  FILE* file = stdin;
  if (filename != 0)
    {
      file = ACE_OS::fopen (filename, "w");
      if (file == 0)
	{
	  return -1;
	}
    }
  ACE_OS::fprintf(file, header);

  ACE_OS::fprintf(file, start_infos);
  for (u_int i = 0; i < infos.length (); ++i)
    {
      if (i != 0)
	{
	  // Finish previous line
	  ACE_OS::fprintf(file, ",\n");
	}
      const RtecScheduler::RT_Info& info = infos[i];
      ACE_OS::fprintf (file,
"{ \"%s\", %d, %f, %f, %f, %d, %d, %f, %d, %d, %d, %d }",
		       (const char*)info.entry_point,
		       info.handle,
		       info.worst_case_execution_time,
		       info.typical_execution_time,
		       info.cached_execution_time,
		       info.period,
		       info.importance,
		       info.quantum,
		       info.threads,
		       info.priority,
		       info.subpriority,
		       info.preemption_priority);
    }
  // finish last line.
  ACE_OS::fprintf(file, "\n");
  ACE_OS::fprintf(file, end_infos);
  ACE_OS::fprintf(file, footer);
  ACE_OS::fclose (file);
  return 0;
}