1 #include "Thread_Task.h"
3 #include "ace/High_Res_Timer.h"
4 #include "ace/OS_NS_unistd.h"
6 #include "tao/ORB_Core.h"
9 #include "Task_Stats.h"
10 #include "ace/Barrier.h"
12 Thread_Task::Thread_Task ()
17 Thread_Task::activate_task (ACE_Barrier
* barrier
, RTCORBA::PriorityMapping
*priority_mapping
)
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_
),
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")),
47 ACE_TEXT ("(%t) task activation at priority %d failed, ")
48 ACE_TEXT ("exiting!\n%a"),
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_
)
67 ACE_TEXT ("(%t) actual prio of %d equals desired priority\n"),
72 ACE_TEXT ("(%t) actual prio = %d, desired priority_ = %d!\n"),
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 ();
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?
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
);
130 task_stats_
->end_time (ACE_OS::gethrtime ());
132 job_
->shutdown (); // tell the job that we're done.
134 ACTIVITY::instance ()->task_ended (this);