Panda3D
mutexDebug.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file mutexDebug.h
10  * @author drose
11  * @date 2006-02-13
12  */
13 
14 #ifndef MUTEXDEBUG_H
15 #define MUTEXDEBUG_H
16 
17 #include "pandabase.h"
18 #include "mutexTrueImpl.h"
19 #include "conditionVarImpl.h"
20 #include "thread.h"
21 #include "namable.h"
22 #include "pmap.h"
23 
24 #ifdef DEBUG_THREADS
25 
26 /**
27  * This class implements a standard mutex the hard way, by doing everything by
28  * hand. This does allow fancy things like deadlock detection, however.
29  */
30 class EXPCL_PANDA_PIPELINE MutexDebug : public Namable {
31 protected:
32  MutexDebug(const std::string &name, bool allow_recursion, bool lightweight);
33  MutexDebug(const MutexDebug &copy) = delete;
34  virtual ~MutexDebug();
35 
36  void operator = (const MutexDebug &copy) = delete;
37 
38 public:
39  INLINE void lock();
40  INLINE bool try_lock();
41  INLINE void unlock();
42 
43 PUBLISHED:
44  BLOCKING INLINE void acquire(Thread *current_thread = Thread::get_current_thread()) const;
45  BLOCKING INLINE bool try_acquire(Thread *current_thread = Thread::get_current_thread()) const;
46  INLINE void elevate_lock() const;
47  INLINE void release() const;
48  INLINE bool debug_is_locked() const;
49 
50  virtual void output(std::ostream &out) const;
51  void output_with_holder(std::ostream &out) const;
52 
53  typedef void VoidFunc();
54 
55 public:
56  static void increment_pstats();
57  static void decrement_pstats();
58 
59 private:
60  void do_lock(Thread *current_thread);
61  bool do_try_lock(Thread *current_thread);
62  void do_unlock();
63  bool do_debug_is_locked() const;
64 
65  void report_deadlock(Thread *current_thread);
66 
67 private:
68  INLINE static MutexTrueImpl *get_global_lock();
69 
70  bool _allow_recursion;
71  bool _lightweight;
72  Thread *_locking_thread;
73  int _lock_count;
74  char *_deleted_name; // To help I.D. a destructed mutex.
75 
76  // For _lightweight mutexes.
77  typedef pmap<Thread *, int> MissedThreads;
78  MissedThreads _missed_threads;
79 
80  ConditionVarImpl _cvar_impl;
81 
82  static int _pstats_count;
83  static MutexTrueImpl *_global_lock;
84 
85  friend class ConditionVarDebug;
86  friend class ConditionVarFullDebug;
87 };
88 
89 INLINE std::ostream &
90 operator << (std::ostream &out, const MutexDebug &m) {
91  m.output(out);
92  return out;
93 }
94 
95 #include "mutexDebug.I"
96 
97 #endif // DEBUG_THREADS
98 
99 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void output(std::ostream &out) const
Outputs the Namable.
Definition: namable.I:61
A base class for all things which can have a name.
Definition: namable.h:26
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A thread; that is, a lightweight process.
Definition: thread.h:46
A fake mutex implementation for single-threaded applications that don't need any synchronization cont...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.