Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / RTScheduling / Thread_Cancel / Thread_Task.cpp
blob66cdc33242b6c8970913c9d7009dae39be3dec19
1 #include "Thread_Task.h"
2 #include "ace/Atomic_Op.h"
3 #include "ace/OS_NS_errno.h"
4 #include "ace/OS_NS_unistd.h"
6 ACE_Atomic_Op<TAO_SYNCH_MUTEX, long> guid_index;
8 RTScheduling::Current::IdType*
9 Thread_Task::guids ()
11 return this->guid_;
14 int
15 Thread_Task::activate_task (CORBA::ORB_ptr orb)
17 try
19 this->orb_ = CORBA::ORB::_duplicate (orb);
21 CORBA::Object_var current_obj = this->orb_->resolve_initial_references ("RTScheduler_Current");
23 this->current_ = RTScheduling::Current::_narrow (current_obj.in ());
25 catch (const CORBA::Exception& ex)
27 ex._tao_print_exception ("Exception:");
30 long flags = THR_NEW_LWP | THR_JOINABLE;
31 if (this->ACE_Task <ACE_SYNCH>::activate (flags,
32 4) == -1)
34 if (ACE_OS::last_error () == EPERM)
35 ACE_ERROR_RETURN ((LM_ERROR,
36 ACE_TEXT ("Insufficient privilege to run this test.\n")),
37 -1);
39 active_thread_count_ = 4;
41 return 0;
44 #if defined (THREAD_CANCELLED)
45 #undef THREAD_CANCELLED
46 #endif /* THREAD_CANCELLED */
48 int
49 Thread_Task::svc ()
51 try
53 const char * name = 0;
54 CORBA::Policy_ptr sched_param = 0;
55 CORBA::Policy_ptr implicit_sched_param = 0;
57 //Start - Nested Scheduling Segment
58 this->current_->begin_scheduling_segment ("Chamber of Secrets",
59 sched_param,
60 implicit_sched_param);
62 size_t count = 0;
63 RTScheduling::Current::IdType_var id = this->current_->id ();
64 ACE_OS::memcpy (&count,
65 id->get_buffer (),
66 id->length ());
69 this->current_->begin_scheduling_segment ("Potter",
70 sched_param,
71 implicit_sched_param);
73 this->guid_[guid_index++] = id.in ();
75 //Start - Nested Scheduling Segment
76 this->current_->begin_scheduling_segment ("Harry",
77 sched_param,
78 implicit_sched_param);
82 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->mutex_, -1);
83 RTScheduling::Current::NameList_var name_list = this->current_->current_scheduling_segment_names ();
85 ACE_DEBUG ((LM_DEBUG,
86 "Scheduling Segments for DT %d :\n",
87 count));
89 for (unsigned int i = 0; i < name_list->length ();++i)
91 ACE_DEBUG ((LM_DEBUG,
92 "Scheduling Segment Name - %C\n",
93 (*name_list) [i].in ()));
97 ACE_OS::sleep (10);
99 this->current_->end_scheduling_segment (name);
100 // End - Nested Scheduling Segment
103 this->current_->end_scheduling_segment (name);
104 // End - Nested Scheduling Segment
106 this->current_->end_scheduling_segment (name);
108 catch (const CORBA::THREAD_CANCELLED& )
110 ACE_DEBUG ((LM_DEBUG,
111 "Distributable Thread Cancelled - Expected Exception\n"));
113 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->mutex_, -1);
114 --active_thread_count_;
115 if (active_thread_count_ == 0)
116 orb_->shutdown ();
119 return 0;
121 catch (const CORBA::Exception& ex)
123 ex._tao_print_exception ("Caught exception:");
125 return 0;