summaryrefslogtreecommitdiff
path: root/TAO/tests/RTScheduling/DT_Spawn/test.cpp
blob: 2f73288cd1a6140db1036f90a4bfe02765369524 (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
//$Id$
#include "../Scheduler.h"
#include "tao/RTScheduling/RTScheduler_Manager.h"
#include "tao/ORB.h"
#include "Thread_Action.h"
#include "ace/Thread_Manager.h"
#include "ace/SString.h"
#include "ace/Argv_Type_Converter.h"

int
ACE_TMAIN (int argc, ACE_TCHAR* argv [])
{
  ACE_Argv_Type_Converter convert (argc, argv);

  CORBA::ORB_var orb;
  RTScheduling::Current_var current;
		    
  const char * name = 0;
  CORBA::Policy_ptr sched_param = 0;
  CORBA::Policy_ptr implicit_sched_param = 0;

  Test_Thread_Action thread_action;

  ACE_TRY_NEW_ENV
    {
      orb = CORBA::ORB_init (convert.get_argc(),
                         convert.get_ASCII_argv(),
			     ""
			     ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      CORBA::Object_ptr manager_obj = orb->resolve_initial_references ("RTSchedulerManager"
								       ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);

      TAO_RTScheduler_Manager_var manager = TAO_RTScheduler_Manager::_narrow (manager_obj
									      ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      TAO_Scheduler* scheduler;
      ACE_NEW_RETURN (scheduler,
		      TAO_Scheduler (orb.in ()),
		      -1);
  
      manager->rtscheduler (scheduler);

      CORBA::Object_ptr current_obj = orb->resolve_initial_references ("RTScheduler_Current"
								       ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
      
      current = RTScheduling::Current::_narrow (current_obj
						ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
      
      ACE_TRY_EX (block1)
	{

	  ACE_DEBUG ((LM_DEBUG,
		      "Invoking DT spawn without calling begin_scheduling_segment...\n"));

	  ACE_CString data ("Harry Potter");
	  current->spawn (&thread_action,
			  const_cast<char *> (data.c_str ()),
			  name,
			  sched_param,
			  implicit_sched_param,
			  0,
			  0
			  ACE_ENV_ARG_PARAMETER);
	  ACE_TRY_CHECK_EX (block1);
	}
      ACE_CATCH (CORBA::BAD_INV_ORDER, thr_ex)
	{
	  ACE_DEBUG ((LM_DEBUG,
		      "Spawn should be in the context of a Scheduling Segment - Expected Exception\n"));
	}
      ACE_CATCHANY
	{
	  ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
			       "\n");
	}
      ACE_ENDTRY;
      
      ACE_DEBUG ((LM_DEBUG,
		  "Start - Scheduling Segment...\n"));

      current->begin_scheduling_segment ("Potter",
					 sched_param,
					 implicit_sched_param
					 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
      
      size_t count = 0;
      ACE_OS::memcpy (&count,
		      current->id ()->get_buffer (),
		      current->id ()->length ());
      
      ACE_DEBUG ((LM_DEBUG,
		  "The Current DT Guid is %d\n",
		  count));
      
      //Initialize data to be passed to the Thread_Action::do method
      Data spawn_data;
      spawn_data.data = CORBA::string_dup ("Harry Potter");
      spawn_data.current = RTScheduling::Current::_duplicate (current.in ());
      
      ACE_DEBUG ((LM_DEBUG,
		  "Spawning a new DT...\n"));
      current->spawn (&thread_action,
		      &spawn_data,
		      "Chamber of Secrets",
		      sched_param,
		      implicit_sched_param,
		      0,
		      0
		      ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
      
      current->end_scheduling_segment (name
				       ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
      ACE_DEBUG ((LM_DEBUG,
		  "End - Scheduling Segment %d\n",
		  count));
     
    } 
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
			   "Caught Exception\n");
    }
  ACE_ENDTRY; 
  
  ACE_Thread_Manager::instance ()->wait ();
  
  return 0;
}