2 * Copyright (C) 2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Threading.h"
20 #include <ace/OS_NS_unistd.h>
21 #include <ace/Sched_Params.h>
24 using namespace ACE_Based
;
26 ThreadPriority::ThreadPriority()
28 for (int i
= Idle
; i
< MAXPRIORITYNUM
; ++i
)
29 m_priority
[i
] = ACE_THR_PRI_OTHER_DEF
;
31 m_priority
[Idle
] = ACE_Sched_Params::priority_min(ACE_SCHED_OTHER
);
32 m_priority
[Realtime
] = ACE_Sched_Params::priority_max(ACE_SCHED_OTHER
);
34 std::vector
<int> _tmp
;
36 ACE_Sched_Params::Policy _policy
= ACE_SCHED_OTHER
;
37 ACE_Sched_Priority_Iterator
pr_iter(_policy
);
39 while (pr_iter
.more())
41 _tmp
.push_back(pr_iter
.priority());
45 ASSERT (!_tmp
.empty());
47 if(_tmp
.size() >= MAXPRIORITYNUM
)
49 const size_t max_pos
= _tmp
.size();
52 for (size_t i
= 0; i
< max_pos
; ++i
)
54 if(_tmp
[i
] == ACE_THR_PRI_OTHER_DEF
)
61 //since we have only 7(seven) values in enum Priority
62 //and 3 we know already (Idle, Normal, Realtime) so
63 //we need to split each list [Idle...Normal] and [Normal...Realtime]
65 const size_t _divider
= 4;
66 size_t _div
= (norm_pos
- min_pos
) / _divider
;
70 min_pos
= (norm_pos
- 1);
72 m_priority
[Low
] = _tmp
[min_pos
-= _div
];
73 m_priority
[Lowest
] = _tmp
[min_pos
-= _div
];
75 _div
= (max_pos
- norm_pos
) / _divider
;
79 min_pos
= norm_pos
- 1;
81 m_priority
[High
] = _tmp
[min_pos
+= _div
];
82 m_priority
[Highest
] = _tmp
[min_pos
+= _div
];
86 int ThreadPriority::getPriority(Priority p
) const
97 #define THREADFLAG (THR_NEW_LWP | THR_SCHED_DEFAULT| THR_JOINABLE)
99 Thread::Thread() : m_task(0), m_iThreadId(0), m_hThreadHandle(0)
104 Thread::Thread(Runnable
& instance
) : m_task(&instance
), m_iThreadId(0), m_hThreadHandle(0)
106 bool _start
= start();
115 //initialize Thread's class static member
116 Thread::ThreadStorage
Thread::m_ThreadStorage
;
117 ThreadPriority
Thread::m_TpEnum
;
121 if(m_task
== 0 || m_iThreadId
!= 0)
124 return (ACE_Thread::spawn(&Thread::ThreadTask
, (void*)m_task
, THREADFLAG
, &m_iThreadId
, &m_hThreadHandle
) == 0);
129 if(!m_hThreadHandle
|| !m_task
)
132 ACE_THR_FUNC_RETURN _value
= ACE_THR_FUNC_RETURN(-1);
133 int _res
= ACE_Thread::join(m_hThreadHandle
, &_value
);
141 void Thread::destroy()
143 ACE_Thread::kill(m_iThreadId
, -1);
146 void Thread::suspend()
148 ACE_Thread::suspend(m_hThreadHandle
);
151 void Thread::resume()
153 ACE_Thread::resume(m_hThreadHandle
);
156 ACE_THR_FUNC_RETURN
Thread::ThreadTask(void * param
)
158 Runnable
* _task
= (Runnable
*)param
;
161 return (ACE_THR_FUNC_RETURN
)0;
164 ACE_thread_t
Thread::currentId()
166 return ACE_Thread::self();
169 ACE_hthread_t
Thread::currentHandle()
171 ACE_hthread_t _handle
;
172 ACE_Thread::self(_handle
);
177 Thread
* Thread::current()
179 Thread
* _thread
= m_ThreadStorage
.ts_object();
182 _thread
= new Thread();
183 _thread
->m_iThreadId
= Thread::currentId();
184 _thread
->m_hThreadHandle
= Thread::currentHandle();
186 Thread
* _oldValue
= m_ThreadStorage
.ts_object(_thread
);
194 void Thread::setPriority(Priority type
)
196 int _priority
= m_TpEnum
.getPriority(type
);
197 int _ok
= ACE_Thread::setprio(m_hThreadHandle
, _priority
);
198 //remove this ASSERT in case you don't want to know is thread priority change was successful or not
202 void Thread::Sleep(unsigned long msecs
)
204 ACE_OS::sleep(ACE_Time_Value(0, 1000 * msecs
));