libDwm-0.6.0
DwmPthreadLocker.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmPthreadLocker.hh 8401 $
3 // @(#) $Id: DwmPthreadLocker.hh 8401 2016-04-17 06:44:31Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2008, 2016
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMPTHREADLOCKER_HH_
43 #define _DWMPTHREADLOCKER_HH_
44 
45 #include <map>
46 
47 #include "DwmMutex.hh"
48 
49 namespace Dwm {
50 
51  namespace Pthread {
52 
53  typedef std::map<const char *, std::pair<uint32_t,Mutex> > LockerMutexMap;
54  extern Mutex g_LockerMutex;
55  extern LockerMutexMap g_LockerMutexes;
56 
57  //------------------------------------------------------------------------
59  //------------------------------------------------------------------------
60  template <typename T>
61  class Locker
62  {
63  public:
64  //----------------------------------------------------------------------
66  //----------------------------------------------------------------------
67  Locker(const T *obj)
68  {
69  _obj = (const char *)obj;
70  g_LockerMutex.Lock();
71  LockerMutexMap::iterator iter = g_LockerMutexes.find(_obj);
72  if (iter == g_LockerMutexes.end()) {
73  g_LockerMutexes[_obj].first = 1;
74  }
75  else {
76  ++iter->second.first;
77  }
78  g_LockerMutex.Unlock();
79  }
80 
81  //----------------------------------------------------------------------
83  //----------------------------------------------------------------------
84  ~Locker()
85  {
86  g_LockerMutex.Lock();
87  LockerMutexMap::iterator iter = g_LockerMutexes.find(_obj);
88  if (iter != g_LockerMutexes.end()) {
89  --iter->second.first;
90  if (! iter->second.first) {
91  g_LockerMutexes.erase(iter);
92  }
93  }
94  g_LockerMutex.Unlock();
95  }
96 
97  //----------------------------------------------------------------------
99  //----------------------------------------------------------------------
100  bool Lock()
101  {
102  bool rc = false;
103  g_LockerMutex.Lock();
104  LockerMutexMap::iterator iter = g_LockerMutexes.find(_obj);
105  if (iter != g_LockerMutexes.end()) {
106  rc = iter->second.second.Lock();
107  }
108  g_LockerMutex.Unlock();
109  return(rc);
110  }
111 
112  //----------------------------------------------------------------------
114  //----------------------------------------------------------------------
115  bool Unlock()
116  {
117  bool rc = false;
118  g_LockerMutex.Lock();
119  LockerMutexMap::iterator iter = g_LockerMutexes.find(_obj);
120  if (iter != g_LockerMutexes.end()) {
121  rc = iter->second.second.Unlock();
122  }
123  g_LockerMutex.Unlock();
124  return(rc);
125  }
126 
127  private:
128  const char *_obj;
129  };
130 
131  } // namespace Pthread
132 
133 } // namespace Dwm
134 
135 #endif // _DWMPTHREADLOCKER_HH_
Dwm::Pthread::Mutex class definition.
Definition: DwmBZ2IO.hh:67