root/trunk/src/framework/Policies/ThreadingModel.h @ 2

Revision 2, 3.5 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef MANGOS_THREADINGMODEL_H
20#define MANGOS_THREADINGMODEL_H
21
22/**
23 * @class ThreadingModel<T>
24 *
25 */
26
27#include "Platform/Define.h"
28
29namespace MaNGOS
30{
31    inline void Guard(void *) {}
32
33    template<typename MUTEX> class MANGOS_DLL_DECL GeneralLock
34    {
35        public:
36            GeneralLock(MUTEX &m) : i_mutex(m)
37            {
38                i_mutex.acquire();
39            }
40
41            ~GeneralLock()
42            {
43                i_mutex.release();
44            }
45        private:
46            GeneralLock(const GeneralLock &);
47            GeneralLock& operator=(const GeneralLock &);
48            MUTEX &i_mutex;
49    };
50
51    template <class T>
52        class MANGOS_DLL_DECL SingleThreaded
53    {
54        public:
55
56            struct Lock                                     // empty object
57            {
58                Lock() {}
59                Lock(const T &) {}
60                Lock(const SingleThreaded<T> &)             // for single threaded we ignore this
61                {
62                }
63            };
64
65            typedef T VolatileType;
66    };
67
68    // object level lockable
69    template<class T, class MUTEX>
70        class MANGOS_DLL_DECL ObjectLevelLockable
71    {
72        public:
73            ObjectLevelLockable() : i_mtx() {}
74
75            friend class Lock;
76
77            class Lock
78            {
79                public:
80                    Lock(ObjectLevelLockable<T, MUTEX> &host) : i_lock(host.i_mtx)
81                    {
82                    }
83
84                private:
85                    GeneralLock<MUTEX> i_lock;
86            };
87
88            typedef volatile T VolatileType;
89
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> &);
94
95            MUTEX i_mtx;
96    };
97
98    template<class T, class MUTEX>
99        class MANGOS_DLL_DECL ClassLevelLockable
100    {
101        public:
102            class Lock;
103            friend class Lock;
104            typedef volatile T VolatileType;
105
106            ClassLevelLockable() {}
107
108            class Lock
109            {
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(); }
115            };
116
117        private:
118            static MUTEX si_mtx;
119    };
120
121}
122
123template<class T, class MUTEX> MUTEX MaNGOS::ClassLevelLockable<T, MUTEX>::si_mtx;
124
125#define INSTANTIATE_CLASS_MUTEX(CTYPE,MUTEX) \
126    template class MANGOS_DLL_DECL MaNGOS::ClassLevelLockable<CTYPE, MUTEX >
127#endif
Note: See TracBrowser for help on using the browser.