Merge pull request #2218 from jwillemsen/jwi-pthreadsigmask
[ACE_TAO.git] / TAO / examples / RTScheduling / MIF_Scheduler / MIF_Task.cpp
blob7e2fbd38dbf80696f87ace189365ff0c7151a441
1 #include "MIF_Task.h"
2 #include "test.h"
4 #include "ace/OS_NS_errno.h"
5 #include "ace/Countdown_Time.h"
7 MIF_Task::MIF_Task (int importance,
8 time_t start_time,
9 int load,
10 int iter,
11 int dist,
12 char *job_name,
13 DT_Creator *dt_creator)
15 this->load_ = load;
16 this->iter_ = iter;
17 this->start_time_ = start_time;
18 this->importance_ = importance;
19 this->dt_creator_ = dt_creator;
20 this->dist_ = dist;
21 this->job_name_ = CORBA::string_dup (job_name);
23 // create the stat object.
24 ACE_NEW (task_stats_, Task_Stats);
25 task_stats_->init (iter_);
28 MIF_Task::~MIF_Task ()
30 delete task_stats_;
33 void
34 MIF_Task::pre_activate ()
36 DT_TEST::instance ()->scheduler ()->incr_thr_count ();
39 void
40 MIF_Task::post_activate ()
42 DT_TEST::instance ()->scheduler ()->wait ();
45 int
46 MIF_Task::activate_task (RTScheduling::Current_ptr current,
47 CORBA::Policy_ptr sched_param,
48 long flags,
49 ACE_Time_Value* base_time)
51 if (TAO_debug_level > 0)
52 ACE_DEBUG ((LM_DEBUG,
53 "MIF_Task::activate %d\n",
54 importance_));
56 char msg [BUFSIZ];
57 ACE_OS::sprintf (msg, "MIF_Task::activate task\n");
58 dt_creator_->log_msg (msg);
60 base_time_ = base_time;
62 current_ = RTScheduling::Current::_narrow (current);
64 sched_param_ = CORBA::Policy::_duplicate (sched_param);
66 pre_activate ();
68 if (this->activate (flags,
69 1) == -1)
71 if (ACE_OS::last_error () == EPERM)
72 ACE_ERROR_RETURN ((LM_ERROR,
73 ACE_TEXT ("Insufficient privilege to run this test.\n")),
74 -1);
77 post_activate ();
78 return 0;
81 int
82 MIF_Task::perform_task ()
84 try
86 ACE_TCHAR msg [BUFSIZ];
87 ACE_OS::sprintf (msg,
88 ACE_TEXT("MIF_Task::perform_task ")
89 ACE_SIZE_T_FORMAT_SPECIFIER
90 ACE_TEXT("\n"),
91 count_);
92 dt_creator_->log_msg (ACE_TEXT_ALWAYS_CHAR(msg));
94 static CORBA::ULong prime_number = 9619;
95 CORBA::Policy_var sched_param;
96 sched_param = dt_creator_->sched_param (this->importance_);
97 const char * name = 0;
99 for (int i = 0; i < this->iter_; i++)
101 ACE_Time_Value run_time = ACE_OS::gettimeofday () - *base_time_;
102 TASK_STATS::instance ()->sample (run_time.sec (), count_);
104 ACE_Time_Value count_down_time (1);
105 ACE_Countdown_Time count_down (&count_down_time);
107 while (count_down_time > ACE_Time_Value::zero)
109 ACE::is_prime (prime_number,
111 prime_number / 2);
113 count_down.update ();
116 current_->update_scheduling_segment (name,
117 sched_param.in (),
118 sched_param.in ());
121 if (this->dist_)
123 this->job_->work (this->load_,
124 this->importance_);
126 for (int j = 0; j < this->iter_; j++)
128 ACE_Time_Value run_time = ACE_OS::gettimeofday () - *base_time_;
129 TASK_STATS::instance ()->sample (run_time.sec (), count_);
131 ACE_Time_Value count_down_time (1);
132 ACE_Countdown_Time count_down (&count_down_time);
134 while (count_down_time > ACE_Time_Value::zero)
136 ACE::is_prime (prime_number,
138 prime_number / 2);
139 count_down.update ();
142 current_->update_scheduling_segment (name,
143 sched_param.in (),
144 sched_param.in ());
150 catch (const CORBA::Exception& ex)
152 ex._tao_print_exception ("Caught exception:");
153 return -1;
156 if (TAO_debug_level > 0)
157 ACE_DEBUG ((LM_DEBUG,
158 "Thread %d\n",
159 this->count_));
161 if (dist_)
162 job_->shutdown ();
164 return 0;