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

Revision 102, 3.6 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef TRINITY_THREADINGMODEL_H
22#define TRINITY_THREADINGMODEL_H
23
24/**
25 * @class ThreadingModel<T>
26 *
27 */
28
29#include "Platform/Define.h"
30
31namespace Trinity
32{
33    inline void Guard(void *) {}
34
35    template<typename MUTEX> class TRINITY_DLL_DECL GeneralLock
36    {
37        public:
38            GeneralLock(MUTEX &m) : i_mutex(m)
39            {
40                i_mutex.acquire();
41            }
42
43            ~GeneralLock()
44            {
45                i_mutex.release();
46            }
47        private:
48            GeneralLock(const GeneralLock &);
49            GeneralLock& operator=(const GeneralLock &);
50            MUTEX &i_mutex;
51    };
52
53    template <class T>
54        class TRINITY_DLL_DECL SingleThreaded
55    {
56        public:
57
58            struct Lock                                     // empty object
59            {
60                Lock() {}
61                Lock(const T &) {}
62                Lock(const SingleThreaded<T> &)             // for single threaded we ignore this
63                {
64                }
65            };
66
67            typedef T VolatileType;
68    };
69
70    // object level lockable
71    template<class T, class MUTEX>
72        class TRINITY_DLL_DECL ObjectLevelLockable
73    {
74        public:
75            ObjectLevelLockable() : i_mtx() {}
76
77            friend class Lock;
78
79            class Lock
80            {
81                public:
82                    Lock(ObjectLevelLockable<T, MUTEX> &host) : i_lock(host.i_mtx)
83                    {
84                    }
85
86                private:
87                    GeneralLock<MUTEX> i_lock;
88            };
89
90            typedef volatile T VolatileType;
91
92        private:
93            // prevent the compiler creating a copy construct
94            ObjectLevelLockable(const ObjectLevelLockable<T, MUTEX> &);
95            ObjectLevelLockable<T, MUTEX>& operator=(const ObjectLevelLockable<T, MUTEX> &);
96
97            MUTEX i_mtx;
98    };
99
100    template<class T, class MUTEX>
101        class TRINITY_DLL_DECL ClassLevelLockable
102    {
103        public:
104            class Lock;
105            friend class Lock;
106            typedef volatile T VolatileType;
107
108            ClassLevelLockable() {}
109
110            class Lock
111            {
112                public:
113                    Lock(T& /*host*/) { ClassLevelLockable<T, MUTEX>::si_mtx.acquire(); }
114                    Lock(ClassLevelLockable<T, MUTEX> &) { ClassLevelLockable<T, MUTEX>::si_mtx.acquire(); }
115                    Lock() { ClassLevelLockable<T, MUTEX>::si_mtx.acquire(); }
116                    ~Lock() { ClassLevelLockable<T, MUTEX>::si_mtx.release(); }
117            };
118
119        private:
120            static MUTEX si_mtx;
121    };
122
123}
124
125template<class T, class MUTEX> MUTEX Trinity::ClassLevelLockable<T, MUTEX>::si_mtx;
126
127#define INSTANTIATE_CLASS_MUTEX(CTYPE,MUTEX) \
128    template class TRINITY_DLL_DECL Trinity::ClassLevelLockable<CTYPE, MUTEX >
129#endif
Note: See TracBrowser for help on using the browser.