Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / src / framework / Policies / ThreadingModel.h
blob9f8eddb30a3fc4ae905bd6d34ebd021e7a256a17
1 /*
2 * Copyright (C) 2005-2008 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 #ifndef MANGOS_THREADINGMODEL_H
20 #define MANGOS_THREADINGMODEL_H
22 /**
23 * @class ThreadingModel<T>
27 #include "Platform/Define.h"
29 namespace MaNGOS
31 inline void Guard(void *) {}
33 template<typename MUTEX> class MANGOS_DLL_DECL GeneralLock
35 public:
36 GeneralLock(MUTEX &m) : i_mutex(m)
38 i_mutex.acquire();
41 ~GeneralLock()
43 i_mutex.release();
45 private:
46 GeneralLock(const GeneralLock &);
47 GeneralLock& operator=(const GeneralLock &);
48 MUTEX &i_mutex;
51 template <class T>
52 class MANGOS_DLL_DECL SingleThreaded
54 public:
56 struct Lock // empty object
58 Lock() {}
59 Lock(const T &) {}
60 Lock(const SingleThreaded<T> &) // for single threaded we ignore this
65 typedef T VolatileType;
68 // object level lockable
69 template<class T, class MUTEX>
70 class MANGOS_DLL_DECL ObjectLevelLockable
72 public:
73 ObjectLevelLockable() : i_mtx() {}
75 friend class Lock;
77 class Lock
79 public:
80 Lock(ObjectLevelLockable<T, MUTEX> &host) : i_lock(host.i_mtx)
84 private:
85 GeneralLock<MUTEX> i_lock;
88 typedef volatile T VolatileType;
90 private:
91 // prevent the compiler creating a copy construct
92 ObjectLevelLockable(const ObjectLevelLockable<T, MUTEX> &);
93 ObjectLevelLockable<T, MUTEX>& operator=(const ObjectLevelLockable<T, MUTEX> &);
95 MUTEX i_mtx;
98 template<class T, class MUTEX>
99 class MANGOS_DLL_DECL ClassLevelLockable
101 public:
102 class Lock;
103 friend class Lock;
104 typedef volatile T VolatileType;
106 ClassLevelLockable() {}
108 class Lock
110 public:
111 Lock(T& /*host*/) { ClassLevelLockable<T, MUTEX>::si_mtx.acquire(); }
112 Lock(ClassLevelLockable<T, MUTEX> &) { ClassLevelLockable<T, MUTEX>::si_mtx.acquire(); }
113 Lock() { ClassLevelLockable<T, MUTEX>::si_mtx.acquire(); }
114 ~Lock() { ClassLevelLockable<T, MUTEX>::si_mtx.release(); }
117 private:
118 static MUTEX si_mtx;
123 template<class T, class MUTEX> MUTEX MaNGOS::ClassLevelLockable<T, MUTEX>::si_mtx;
125 #define INSTANTIATE_CLASS_MUTEX(CTYPE,MUTEX) \
126 template class MANGOS_DLL_DECL MaNGOS::ClassLevelLockable<CTYPE, MUTEX >
127 #endif