aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/shared/CMakeLists.txt5
-rw-r--r--src/shared/Database/DatabasePostgre.cpp5
-rw-r--r--src/shared/LockedQueue.h144
-rw-r--r--src/shared/Threading.cpp205
-rw-r--r--src/shared/Threading.h99
5 files changed, 453 insertions, 5 deletions
diff --git a/src/shared/CMakeLists.txt b/src/shared/CMakeLists.txt
index 8ecfccad34f..b271e2b9545 100644
--- a/src/shared/CMakeLists.txt
+++ b/src/shared/CMakeLists.txt
@@ -14,6 +14,7 @@ SET(shared_STAT_SRCS
Log.h
ProgressBar.cpp
ProgressBar.h
+ Threading.cpp
Timer.h
Util.cpp
Util.h
@@ -22,3 +23,7 @@ SET(shared_STAT_SRCS
)
add_library(shared STATIC ${shared_STAT_SRCS})
+target_link_libraries(
+shared
+${ACE_LIBRARY}
+)
diff --git a/src/shared/Database/DatabasePostgre.cpp b/src/shared/Database/DatabasePostgre.cpp
index d4f48b99fb1..12875b33200 100644
--- a/src/shared/Database/DatabasePostgre.cpp
+++ b/src/shared/Database/DatabasePostgre.cpp
@@ -126,13 +126,8 @@ QueryResult* DatabasePostgre::Query(const char *sql)
uint32 fieldCount = 0;
// guarded block for thread-safe request
-<<<<<<< HEAD:src/shared/Database/DatabasePostgre.cpp
- ZThread::Guard<ZThread::FastMutex> query_connection_guard(mMutex);
- #ifdef TRINITY_DEBUG
-=======
ACE_Guard<ACE_Thread_Mutex> query_connection_guard(mMutex);
#ifdef MANGOS_DEBUG
->>>>>>> 00c7d15a78b1dcdbf888b768c55424183b2231e4:src/shared/Database/DatabasePostgre.cpp
uint32 _s = getMSTime();
#endif
// Send the query
diff --git a/src/shared/LockedQueue.h b/src/shared/LockedQueue.h
new file mode 100644
index 00000000000..b085dd09b83
--- /dev/null
+++ b/src/shared/LockedQueue.h
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2009 MaNGOS <http://getmangos.com/>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef LOCKEDQUEUE_H
+#define LOCKEDQUEUE_H
+
+#include <ace/Guard_T.h>
+#include <ace/Thread_Mutex.h>
+#include <deque>
+#include <assert.h>
+#include "Errors.h"
+
+namespace ACE_Based
+{
+
+ template <class T, class LockType, typename StorageType=std::deque<T> >
+ class LockedQueue
+ {
+
+ //! Serialize access to the Queue
+ LockType _lock;
+
+ //! Storage backing the queue
+ StorageType _queue;
+
+ //! Cancellation flag
+ volatile bool _canceled;
+
+ public:
+
+ //! Create a LockedQueue
+ LockedQueue() : _canceled(false) {}
+
+ //! Destroy a LockedQueue
+ virtual ~LockedQueue() { }
+
+ /**
+ * @see Queue::add(const T& item)
+ */
+ void add(const T& item)
+ {
+
+ ACE_Guard<LockType> g(_lock);
+
+ ASSERT(!_canceled);
+ // throw Cancellation_Exception();
+
+ _queue.push_back(item);
+
+ }
+
+ /**
+ * @see Queue::next()
+ */
+ T next()
+ {
+
+ ACE_Guard<LockType> g(_lock);
+
+ ASSERT (!_queue.empty() || !_canceled);
+ // throw Cancellation_Exception();
+
+ T item = _queue.front();
+ _queue.pop_front();
+
+ return item;
+
+ }
+
+ T front()
+ {
+ ACE_Guard<LockType> g(_lock);
+
+ ASSERT (!_queue.empty());
+ // throw NoSuchElement_Exception();
+
+ return _queue.front();
+ }
+
+ /**
+ * @see Queue::cancel()
+ */
+ void cancel()
+ {
+
+ ACE_Guard<LockType> g(_lock);
+
+ _canceled = true;
+
+ }
+
+ /**
+ * @see Queue::isCanceled()
+ */
+ bool isCanceled()
+ {
+
+ // Faster check since the queue will not become un-canceled
+ if(_canceled)
+ return true;
+
+ ACE_Guard<LockType> g(_lock);
+
+ return _canceled;
+
+ }
+
+ /**
+ * @see Queue::size()
+ */
+ size_t size()
+ {
+
+ ACE_Guard<LockType> g(_lock);
+ return _queue.size();
+
+ }
+
+ bool empty()
+ {
+
+ ACE_Guard<LockType> g(_lock);
+ return _queue.empty();
+ }
+
+ };
+
+}
+#endif
diff --git a/src/shared/Threading.cpp b/src/shared/Threading.cpp
new file mode 100644
index 00000000000..496e86353ca
--- /dev/null
+++ b/src/shared/Threading.cpp
@@ -0,0 +1,205 @@
+/*
+ * Copyright (C) 2009 MaNGOS <http://getmangos.com/>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "Threading.h"
+#include <ace/OS_NS_unistd.h>
+#include <ace/Sched_Params.h>
+#include <vector>
+
+using namespace ACE_Based;
+
+ThreadPriority::ThreadPriority()
+{
+ for (int i = Idle; i < MAXPRIORITYNUM; ++i)
+ m_priority[i] = ACE_THR_PRI_OTHER_DEF;
+
+ m_priority[Idle] = ACE_Sched_Params::priority_min(ACE_SCHED_OTHER);
+ m_priority[Realtime] = ACE_Sched_Params::priority_max(ACE_SCHED_OTHER);
+
+ std::vector<int> _tmp;
+
+ ACE_Sched_Params::Policy _policy = ACE_SCHED_OTHER;
+ ACE_Sched_Priority_Iterator pr_iter(_policy);
+
+ while (pr_iter.more())
+ {
+ _tmp.push_back(pr_iter.priority());
+ pr_iter.next();
+ }
+
+ ASSERT (!_tmp.empty());
+
+ if(_tmp.size() >= MAXPRIORITYNUM)
+ {
+ const size_t max_pos = _tmp.size();
+ size_t min_pos = 1;
+ size_t norm_pos = 0;
+ for (size_t i = 0; i < max_pos; ++i)
+ {
+ if(_tmp[i] == ACE_THR_PRI_OTHER_DEF)
+ {
+ norm_pos = i + 1;
+ break;
+ }
+ }
+
+ //since we have only 7(seven) values in enum Priority
+ //and 3 we know already (Idle, Normal, Realtime) so
+ //we need to split each list [Idle...Normal] and [Normal...Realtime]
+ //into ¹ piesces
+ const size_t _divider = 4;
+ size_t _div = (norm_pos - min_pos) / _divider;
+ if(_div == 0)
+ _div = 1;
+
+ min_pos = (norm_pos - 1);
+
+ m_priority[Low] = _tmp[min_pos -= _div];
+ m_priority[Lowest] = _tmp[min_pos -= _div ];
+
+ _div = (max_pos - norm_pos) / _divider;
+ if(_div == 0)
+ _div = 1;
+
+ min_pos = norm_pos - 1;
+
+ m_priority[High] = _tmp[min_pos += _div];
+ m_priority[Highest] = _tmp[min_pos += _div];
+ }
+}
+
+int ThreadPriority::getPriority(Priority p) const
+{
+ if(p < Idle)
+ p = Idle;
+
+ if(p > Realtime)
+ p = Realtime;
+
+ return m_priority[p];
+}
+
+#define THREADFLAG (THR_NEW_LWP | THR_SCHED_DEFAULT| THR_JOINABLE)
+
+Thread::Thread() : m_task(0), m_iThreadId(0), m_hThreadHandle(0)
+{
+
+}
+
+Thread::Thread(Runnable& instance) : m_task(&instance), m_iThreadId(0), m_hThreadHandle(0)
+{
+ bool _start = start();
+ ASSERT (_start);
+}
+
+Thread::~Thread()
+{
+ //Wait();
+}
+
+//initialize Thread's class static member
+Thread::ThreadStorage Thread::m_ThreadStorage;
+ThreadPriority Thread::m_TpEnum;
+
+bool Thread::start()
+{
+ if(m_task == 0 || m_iThreadId != 0)
+ return false;
+
+ return (ACE_Thread::spawn(&Thread::ThreadTask, (void*)m_task, THREADFLAG, &m_iThreadId, &m_hThreadHandle) == 0);
+}
+
+bool Thread::wait()
+{
+ if(!m_hThreadHandle || !m_task)
+ return false;
+
+ ACE_THR_FUNC_RETURN _value = ACE_THR_FUNC_RETURN(-1);
+ int _res = ACE_Thread::join(m_hThreadHandle, &_value);
+
+ m_iThreadId = 0;
+ m_hThreadHandle = 0;
+
+ return (_res == 0);
+}
+
+void Thread::destroy()
+{
+ ACE_Thread::kill(m_iThreadId, -1);
+}
+
+void Thread::suspend()
+{
+ ACE_Thread::suspend(m_hThreadHandle);
+}
+
+void Thread::resume()
+{
+ ACE_Thread::resume(m_hThreadHandle);
+}
+
+ACE_THR_FUNC_RETURN Thread::ThreadTask(void * param)
+{
+ Runnable * _task = (Runnable*)param;
+ _task->run();
+
+ return (ACE_THR_FUNC_RETURN)0;
+}
+
+ACE_thread_t Thread::currentId()
+{
+ return ACE_Thread::self();
+}
+
+ACE_hthread_t Thread::currentHandle()
+{
+ ACE_hthread_t _handle;
+ ACE_Thread::self(_handle);
+
+ return _handle;
+}
+
+Thread * Thread::current()
+{
+ Thread * _thread = m_ThreadStorage.ts_object();
+ if(!_thread)
+ {
+ _thread = new Thread();
+ _thread->m_iThreadId = Thread::currentId();
+ _thread->m_hThreadHandle = Thread::currentHandle();
+
+ Thread * _oldValue = m_ThreadStorage.ts_object(_thread);
+ if(_oldValue)
+ delete _oldValue;
+ }
+
+ return _thread;
+}
+
+void Thread::setPriority(Priority type)
+{
+ int _priority = m_TpEnum.getPriority(type);
+ int _ok = ACE_Thread::setprio(m_hThreadHandle, _priority);
+ //remove this ASSERT in case you don't want to know is thread priority change was successful or not
+ ASSERT (_ok == 0);
+}
+
+void Thread::Sleep(unsigned long msecs)
+{
+ ACE_OS::sleep(ACE_Time_Value(0, 1000 * msecs));
+}
diff --git a/src/shared/Threading.h b/src/shared/Threading.h
new file mode 100644
index 00000000000..eac3c0e8efb
--- /dev/null
+++ b/src/shared/Threading.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2009 MaNGOS <http://getmangos.com/>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef THREADING_H
+#define THREADING_H
+
+#include <ace/Thread.h>
+#include <ace/TSS_T.h>
+#include <assert.h>
+#include "Errors.h"
+
+namespace ACE_Based
+{
+
+ class Runnable
+ {
+ public:
+ virtual ~Runnable() {}
+ virtual void run() = 0;
+ };
+
+ enum Priority
+ {
+ Idle,
+ Lowest,
+ Low,
+ Normal,
+ High,
+ Highest,
+ Realtime,
+ };
+
+#define MAXPRIORITYNUM (Realtime + 1)
+
+ class ThreadPriority
+ {
+ public:
+ ThreadPriority();
+ int getPriority(Priority p) const;
+
+ private:
+ int m_priority[MAXPRIORITYNUM];
+ };
+
+ class Thread
+ {
+ public:
+ Thread();
+ Thread(Runnable& instance);
+ ~Thread();
+
+ bool start();
+ bool wait();
+ void destroy();
+
+ void suspend();
+ void resume();
+
+ void setPriority(Priority type);
+
+ static void Sleep(unsigned long msecs);
+ static ACE_thread_t currentId();
+ static ACE_hthread_t currentHandle();
+ static Thread * current();
+
+ private:
+ Thread(const Thread&);
+ Thread& operator=(const Thread&);
+
+ static ACE_THR_FUNC_RETURN ThreadTask(void * param);
+
+ ACE_thread_t m_iThreadId;
+ ACE_hthread_t m_hThreadHandle;
+ Runnable * m_task;
+
+ typedef ACE_TSS<Thread> ThreadStorage;
+ //global object - container for Thread class representation of every thread
+ static ThreadStorage m_ThreadStorage;
+ //use this object to determine current OS thread priority values mapped to enum Priority{}
+ static ThreadPriority m_TpEnum;
+ };
+
+}
+#endif