SeExpr
Mutex.h
Go to the documentation of this file.
1 /*
2  Copyright Disney Enterprises, Inc. All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License
6  and the following modification to it: Section 6 Trademarks.
7  deleted and replaced with:
8 
9  6. Trademarks. This License does not grant permission to use the
10  trade names, trademarks, service marks, or product names of the
11  Licensor and its affiliates, except as required for reproducing
12  the content of the NOTICE file.
13 
14  You may obtain a copy of the License at
15  http://www.apache.org/licenses/LICENSE-2.0
16 */
17 #ifndef Mutex_h
18 #define Mutex_h
19 
20 // #define DEBUG_THREADING
21 
22 #include "Platform.h"
23 
25 namespace SeExprInternal2 {
26 #ifndef NDEBUG
27 template <class T>
28 class DebugLock : public T {
29  public:
30  DebugLock() : _locked(0) {}
31  void lock() {
32  T::lock();
33  _locked = 1;
34  }
35  void unlock() {
36  assert(_locked);
37  _locked = 0;
38  T::unlock();
39  }
40  bool locked() { return _locked != 0; }
41 
42  private:
43  int _locked;
44 };
45 #endif
46 
48 template <class T>
49 class AutoLock {
50  public:
51  AutoLock(T& m) : _m(m) { _m.lock(); }
52  ~AutoLock() { _m.unlock(); }
53 
54  private:
55  T& _m;
56 };
57 
58 #ifndef NDEBUG
59 // add debug wrappers to mutex and spinlock
62 #else
63 typedef _Mutex Mutex;
64 typedef _SpinLock SpinLock;
65 #endif
66 
69 }
70 
71 #endif
DebugLock< _SpinLock > SpinLock
Definition: Mutex.h:61
AutoLock< Mutex > AutoMutex
Definition: Mutex.h:67
DebugLock< _Mutex > Mutex
Definition: Mutex.h:60
AutoLock< SpinLock > AutoSpin
Definition: Mutex.h:68
Platform-specific classes, functions, and includes.