2 * Copyright (C) 2009-2010 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"
21 #include <ace/OS_NS_unistd.h>
22 #include <ace/Sched_Params.h>
25 using namespace ACE_Based
;
27 ThreadPriority::ThreadPriority()
29 for (int i
= Idle
; i
< MAXPRIORITYNUM
; ++i
)
30 m_priority
[i
] = ACE_THR_PRI_OTHER_DEF
;
32 m_priority
[Idle
] = ACE_Sched_Params::priority_min(ACE_SCHED_OTHER
);
33 m_priority
[Realtime
] = ACE_Sched_Params::priority_max(ACE_SCHED_OTHER
);
35 std::vector
<int> _tmp
;
37 ACE_Sched_Params::Policy _policy
= ACE_SCHED_OTHER
;
38 ACE_Sched_Priority_Iterator
pr_iter(_policy
);
40 while (pr_iter
.more())
42 _tmp
.push_back(pr_iter
.priority());
46 ASSERT (!_tmp
.empty());
48 if(_tmp
.size() >= MAXPRIORITYNUM
)
50 const size_t max_pos
= _tmp
.size();
53 for (size_t i
= 0; i
< max_pos
; ++i
)
55 if(_tmp
[i
] == ACE_THR_PRI_OTHER_DEF
)
62 //since we have only 7(seven) values in enum Priority
63 //and 3 we know already (Idle, Normal, Realtime) so
64 //we need to split each list [Idle...Normal] and [Normal...Realtime]
66 const size_t _divider
= 4;
67 size_t _div
= (norm_pos
- min_pos
) / _divider
;
71 min_pos
= (norm_pos
- 1);
73 m_priority
[Low
] = _tmp
[min_pos
-= _div
];
74 m_priority
[Lowest
] = _tmp
[min_pos
-= _div
];
76 _div
= (max_pos
- norm_pos
) / _divider
;
80 min_pos
= norm_pos
- 1;
82 m_priority
[High
] = _tmp
[min_pos
+= _div
];
83 m_priority
[Highest
] = _tmp
[min_pos
+= _div
];
87 int ThreadPriority::getPriority(Priority p
) const
99 # define THREADFLAG (THR_NEW_LWP | THR_JOINABLE | THR_SCHED_DEFAULT)
101 # define THREADFLAG (THR_NEW_LWP | THR_JOINABLE)
104 Thread::Thread() : m_task(0), m_iThreadId(0), m_hThreadHandle(0)
109 Thread::Thread(Runnable
* instance
) : m_task(instance
), m_iThreadId(0), m_hThreadHandle(0)
111 // register reference to m_task to prevent it deeltion until destructor
113 m_task
->incReference();
115 bool _start
= start();
123 // deleted runnable object (if no other references)
125 m_task
->decReference();
128 //initialize Thread's class static member
129 Thread::ThreadStorage
Thread::m_ThreadStorage
;
130 ThreadPriority
Thread::m_TpEnum
;
134 if (m_task
== 0 || m_iThreadId
!= 0)
137 bool res
= (ACE_Thread::spawn(&Thread::ThreadTask
, (void*)m_task
, THREADFLAG
, &m_iThreadId
, &m_hThreadHandle
) == 0);
140 m_task
->incReference();
147 if (!m_hThreadHandle
|| !m_task
)
150 ACE_THR_FUNC_RETURN _value
= ACE_THR_FUNC_RETURN(-1);
151 int _res
= ACE_Thread::join(m_hThreadHandle
, &_value
);
159 void Thread::destroy()
161 if (!m_iThreadId
|| !m_task
)
164 if (ACE_Thread::kill(m_iThreadId
, -1) != 0)
170 // reference set at ACE_Thread::spawn
171 m_task
->decReference();
174 void Thread::suspend()
176 ACE_Thread::suspend(m_hThreadHandle
);
179 void Thread::resume()
181 ACE_Thread::resume(m_hThreadHandle
);
184 ACE_THR_FUNC_RETURN
Thread::ThreadTask(void * param
)
186 Runnable
* _task
= (Runnable
*)param
;
189 // task execution complete, free referecne added at
190 _task
->decReference();
192 return (ACE_THR_FUNC_RETURN
)0;
195 ACE_thread_t
Thread::currentId()
197 return ACE_Thread::self();
200 ACE_hthread_t
Thread::currentHandle()
202 ACE_hthread_t _handle
;
203 ACE_Thread::self(_handle
);
208 Thread
* Thread::current()
210 Thread
* _thread
= m_ThreadStorage
.ts_object();
213 _thread
= new Thread();
214 _thread
->m_iThreadId
= Thread::currentId();
215 _thread
->m_hThreadHandle
= Thread::currentHandle();
217 Thread
* _oldValue
= m_ThreadStorage
.ts_object(_thread
);
225 void Thread::setPriority(Priority type
)
228 int _priority
= m_TpEnum
.getPriority(type
);
229 int _ok
= ACE_Thread::setprio(m_hThreadHandle
, _priority
);
230 //remove this ASSERT in case you don't want to know is thread priority change was successful or not
235 void Thread::Sleep(unsigned long msecs
)
237 ACE_OS::sleep(ACE_Time_Value(0, 1000 * msecs
));