Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
ThreadingPolicy.h
Go to the documentation of this file.
1 #ifndef Protium_ThreadingPolicy_h_
2 #define Protium_ThreadingPolicy_h_
3 
5 
6 
7 //TODO: Add in the functionality for single threaded objects as seen in Android where multiple thread access produces an error and causes parent thread to exit
8 
9 
10 namespace Protium{
11 
12  namespace Threads{
13 
15 
55  template<class Host>
57  public:
59  typedef Host VolatileType;
60  };
61 
63  template<class Host, class MutexPolicy=Mutex>
64  class InSingleThread : public ThreadingPrototype<Host> {
65  public:
67  class Lock{
68  public:
70  Lock(){}
72  explicit Lock(const InSingleThread& ){}
74  explicit Lock(const InSingleThread* ){}
75  };
76  };
77 
79  template<class Host, class MutexPolicy=Mutex>
80  class InstanceLocked : public ThreadingPrototype<Host>{
82  mutable MutexPolicy fMtx;
83  public:
90 
92  class Lock;
93 
95  friend class Lock;
96 
97  class Lock{
101  Lock(){}
103  Lock(const Lock&){}
104 
106  Lock& operator=(const Lock&){}
107  public:
109  explicit Lock(const InstanceLocked& host) : fHost(host){
110  fHost.fMtx.Lock();
111  }
112 
114  explicit Lock(const InstanceLocked* host) : fHost(*host){
115  fHost.fMtx.Lock();
116  }
117 
119  ~Lock(){
120  fHost.fMtx.Unlock();
121  }
122  };
123 
124  };
125 
127 
130  template<class Host, class MutexPolicy=Mutex>
131  class StaticLocked : public ThreadingPrototype<Host>{
132 
134  static struct Initializer{
135 
137  bool fIsInit;
138 
140  MutexPolicy fMtx;
141 
143  Initializer() : fIsInit(false), fMtx()
144  {
145  fIsInit = true;
146  }
148  ~Initializer()
149  {
150  assert(fIsInit);
151  }
152  } fInitializer;
153 
154  public:
155  class Lock;
156  friend class Lock;
157 
159  class Lock{
160  public:
161 
162  Lock(){
163  assert(fInitializer.fIsInit);
164  fInitializer.fMtx.Lock();
165  }
166 
168  explicit Lock(const StaticLocked&){
169  assert(fInitializer.fIsInit);
170  fInitializer.fMtx.Lock();
171  }
172 
174  explicit Lock(const StaticLocked*)
175  {
176  assert(fInitializer.fIsInit);
177  fInitializer.fMtx.Lock();
178  }
179 
181  ~Lock(){
182  assert(fInitializer.fIsInit);
183  fInitializer.fMtx.Unlock();
184  }
185 
186  private:
187  Lock(const Lock&);
188  Lock& operator=(const Lock&);
189  };
190 
191  };
192 
193  template < class Host, class MutexPolicy >
194  typename StaticLocked< Host, MutexPolicy >::Initializer
196  }
197 
198 }
199 
200 #endif //File Guardian
Lock & operator=(const Lock &)
No Assignment locking.
Use to specify instance-locked items.
Prototype of threading policy.
Use to specify non-thread safe classes.
Use to specify objects which lock by class.
Lock(const StaticLocked &)
For COmpatibility with instance locked.
InstanceLocked()
Defaults to Mutex being unlocked.
const InstanceLocked & fHost
Reference to hosted class.
Lock on instance creation uses the static initializer to lock all instances of host.
Lock(const Lock &)
No Copy locking.
Lock(const InstanceLocked &host)
Lock on host when constructed.
static struct Protium::Threads::StaticLocked::Initializer fInitializer
~InstanceLocked()
Destruction unlocks class.
Lock(const InSingleThread &)
For instance locking.
Host VolatileType
Use to access Host class from derived templates.
Lock()
On creation, does nothing.
InstanceLocked(const InstanceLocked &)
On copy, the copy instance is unlocked.
Lock(const StaticLocked *)
For compatibility with instance locked.
Defines mutexes and policies to be used by threaded objects.
MutexPolicy fMtx
Contains the mutex used to lock this class.
Lock(const InSingleThread *)
For static locking.
Lock(const InstanceLocked *host)
Lock on host pointer when constructed.
Dummy internal class which locks nothing.