Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / examples / RTCORBA / Activity / Thread_Task.cpp
blobd94ab040f49226e425ace9680d4609553968f312
1 #include "Thread_Task.h"
3 #include "ace/High_Res_Timer.h"
4 #include "ace/OS_NS_unistd.h"
5 #include "tao/debug.h"
6 #include "tao/ORB_Core.h"
8 #include "Activity.h"
9 #include "Task_Stats.h"
10 #include "ace/Barrier.h"
12 Thread_Task::Thread_Task ()
16 int
17 Thread_Task::activate_task (ACE_Barrier* barrier, RTCORBA::PriorityMapping *priority_mapping)
19 barrier_ = barrier;
21 // Convert the priority specified to this class to its native number.
22 RTCORBA::NativePriority native_priority;
24 if (priority_mapping->to_native (this->task_priority_, native_priority) == 0)
25 ACE_ERROR_RETURN ((LM_ERROR,
26 "Cannot convert CORBA priority %d to native priority\n",
27 this->task_priority_),
28 -1);
30 long flags =
31 THR_NEW_LWP |
32 THR_JOINABLE |
33 ACTIVITY::instance()->orb ()->orb_core ()->orb_params ()->thread_creation_flags ();
35 // Become an active object.
36 if (this->activate (flags,
39 native_priority) == -1)
41 if (ACE_OS::last_error () == EPERM)
42 ACE_ERROR_RETURN ((LM_ERROR,
43 ACE_TEXT ("Insufficient privilege to run this test.\n")),
44 -1);
45 else
46 ACE_DEBUG ((LM_ERROR,
47 ACE_TEXT ("(%t) task activation at priority %d failed, ")
48 ACE_TEXT ("exiting!\n%a"),
49 native_priority,
50 -1));
52 return 0;
55 int
56 Thread_Task::svc ()
58 // if debugging, dump the priority that we're actually at.
59 if (TAO_debug_level > 0)
61 // Get the priority of the current thread.
62 RTCORBA::Priority prio =
63 ACTIVITY::instance()->current ()->the_priority ();
65 if (prio == this->task_priority_)
66 ACE_DEBUG ((LM_DEBUG,
67 ACE_TEXT ("(%t) actual prio of %d equals desired priority\n"),
68 prio));
69 else
71 ACE_DEBUG ((LM_ERROR,
72 ACE_TEXT ("(%t) actual prio = %d, desired priority_ = %d!\n"),
73 prio,
74 this->task_priority_));
78 if (TAO_debug_level > 0)
79 ACE_DEBUG ((LM_DEBUG, "Thread_Task (%t) - wait\n"));
81 // First, wait for other threads.
82 this->barrier_->wait ();
84 // first thread here inits the Base_Time.
85 task_stats_->base_time (BASE_TIME::instance ()->base_time_);
87 // now wait till the phase_ period expires.
88 ACE_OS::sleep (ACE_Time_Value (0, phase_));
90 ACE_High_Res_Timer::global_scale_factor_type gsf =
91 ACE_High_Res_Timer::global_scale_factor ();
93 ACE_hrtime_t before, after;
95 for (int i = 0; i < iter_ ; ++i)
97 before = ACE_OS::gethrtime ();
99 job_->work (load_);
101 after = ACE_OS::gethrtime ();
103 task_stats_->sample (before, after);
105 if (period_ != 0) // blast mode, no sleep.
107 // convert to microseconds
108 ACE_UINT32 elapsed_microseconds = ACE_UINT32((after - before) / gsf);
110 #if defined (ACE_WIN32)
111 elapsed_microseconds*=1000; // convert to uSec on Win32
112 #endif /* ACE_WIN32 */
114 // did we miss any deadlines?
116 int const missed =
117 elapsed_microseconds > period_ ? elapsed_microseconds/period_ : 0;
119 long sleep_time = (missed + 1)*period_ ;
120 sleep_time -= elapsed_microseconds;
122 if (TAO_debug_level > 0)
123 ACE_DEBUG ((LM_DEBUG, "(%t) sleep time = %d\n", sleep_time));
125 ACE_Time_Value t_sleep (0, sleep_time);
126 ACE_OS::sleep (t_sleep);
127 } /* period != 0 */
128 } /* for */
130 task_stats_->end_time (ACE_OS::gethrtime ());
132 job_->shutdown (); // tell the job that we're done.
134 ACTIVITY::instance ()->task_ended (this);
136 return 0;