Zero  0.1.0
critical_section.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-MT -- Multi-threaded port of the SHORE storage manager
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
24 // -*- mode:c++; c-basic-offset:4 -*-
25 /*<std-header orig-src='shore' incl-file-exclusion='STHREAD_H'>
26 
27  $Id: critical_section.h,v 1.1 2010/12/09 15:20:17 nhall Exp $
28 
29 SHORE -- Scalable Heterogeneous Object REpository
30 
31 Copyright (c) 1994-99 Computer Sciences Department, University of
32  Wisconsin -- Madison
33 All Rights Reserved.
34 
35 Permission to use, copy, modify and distribute this software and its
36 documentation is hereby granted, provided that both the copyright
37 notice and this permission notice appear in all copies of the
38 software, derivative works or modified versions, and any portions
39 thereof, and that both notices appear in supporting documentation.
40 
41 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
42 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
43 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
44 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 
46 This software was developed with support by the Advanced Research
47 Project Agency, ARPA order number 018 (formerly 8230), monitored by
48 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
49 Further funding for this work was provided by DARPA through
50 Rome Research Laboratory Contract No. F30602-97-2-0247.
51 
52 */
53 
54 /* -- do not edit anything above this line -- </std-header>*/
55 
56 #ifndef __CRITICAL_SECTION_H
57 #define __CRITICAL_SECTION_H
58 
75 #define CRITICAL_SECTION(name, lock) critical_section<__typeof__(lock)&> name(lock)
76 
77 template<class Lock>
79 
89 template<class Lock>
90 struct critical_section<Lock*&> : public critical_section<Lock&> {
92 };
93 
94 /*
95  * NOTE: I added ExtraInit to make the initialization happen so that
96  * assertions about holding the mutex don't fail.
97  * At the same time, I added a holder to the w_pthread_lock_t
98  * implementation so I could make assertions about the holder outside
99  * the lock implementation itself. This might seem like doubly
100  * asserting, but in the cases where the critical section isn't
101  * based on a pthread mutex, we really should have this clean
102  * initialization and the check the assertions.
103  */
104 
119 #define SPECIALIZE_CS(Lock, Extra, ExtraInit, Acquire, Release) \
120 template<> struct critical_section<Lock&> { \
121 critical_section(Lock &mutex) \
122  : _mutex(&mutex) \
123  { ExtraInit; Acquire; } \
124  ~critical_section() { \
125  if(_mutex) \
126  Release; \
127  _mutex = nullptr; \
128  } \
129  void pause() { Release; } \
130  void resume() { Acquire; }\
131  void exit() { Release; _mutex = nullptr; } \
132  Lock &hand_off() { \
133  Lock* rval = _mutex; \
134  _mutex = nullptr; \
135  return *rval; \
136  } \
137 private: \
138  Lock* _mutex; \
139  Extra; \
140  void operator=(critical_section const &); \
141  critical_section(critical_section const &); \
142 }
143 
144 
145 // I undef-ed this and found all occurrances of CRITICAL_SECTION with this.
146 // and hand-checked them.
147 SPECIALIZE_CS(pthread_mutex_t, int _dummy, (_dummy = 0),
148  pthread_mutex_lock(_mutex), pthread_mutex_unlock(_mutex));
149 
150 #endif // __CRITICAL_SECTION_H </std-footer>*/
Helper class for CRITICAL_SECTION idiom (macro).
Definition: critical_section.h:90
Definition: critical_section.h:78
#define SPECIALIZE_CS(Lock, Extra, ExtraInit, Acquire, Release)
Macro that enables use of CRITICAL_SECTION(name,lock)
Definition: critical_section.h:119