HPCToolkit
pfq-rwlock.h
Go to the documentation of this file.
1 // * BeginRiceCopyright *****************************************************
2 //
3 // $HeadURL$
4 // $Id$
5 //
6 // --------------------------------------------------------------------------
7 // Part of HPCToolkit (hpctoolkit.org)
8 //
9 // Information about sources of support for research and development of
10 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
11 // --------------------------------------------------------------------------
12 //
13 // Copyright ((c)) 2002-2019, Rice University
14 // All rights reserved.
15 //
16 // Redistribution and use in source and binary forms, with or without
17 // modification, are permitted provided that the following conditions are
18 // met:
19 //
20 // * Redistributions of source code must retain the above copyright
21 // notice, this list of conditions and the following disclaimer.
22 //
23 // * Redistributions in binary form must reproduce the above copyright
24 // notice, this list of conditions and the following disclaimer in the
25 // documentation and/or other materials provided with the distribution.
26 //
27 // * Neither the name of Rice University (RICE) nor the names of its
28 // contributors may be used to endorse or promote products derived from
29 // this software without specific prior written permission.
30 //
31 // This software is provided by RICE and contributors "as is" and any
32 // express or implied warranties, including, but not limited to, the
33 // implied warranties of merchantability and fitness for a particular
34 // purpose are disclaimed. In no event shall RICE or contributors be
35 // liable for any direct, indirect, incidental, special, exemplary, or
36 // consequential damages (including, but not limited to, procurement of
37 // substitute goods or services; loss of use, data, or profits; or
38 // business interruption) however caused and on any theory of liability,
39 // whether in contract, strict liability, or tort (including negligence
40 // or otherwise) arising in any way out of the use of this software, even
41 // if advised of the possibility of such damage.
42 //
43 // ******************************************************* EndRiceCopyright *
44 
45 //******************************************************************************
46 //
47 // File:
48 // $HeadURL$
49 //
50 // Purpose:
51 // Define the API for a fair, phased reader-writer lock with local spinning
52 //
53 // Reference:
54 // Björn B. Brandenburg and James H. Anderson. 2010. Spin-based reader-writer
55 // synchronization for multiprocessor real-time systems. Real-Time Systems
56 // 46(1):25-87 (September 2010). http://dx.doi.org/10.1007/s11241-010-9097-2
57 //
58 // Notes:
59 // the reference uses a queue for arriving readers. on a cache coherent
60 // machine, the local spinning property for waiting readers can be achieved
61 // by simply using a cacheble flag. the implementation here uses that
62 // simplification.
63 //
64 //******************************************************************************
65 
66 #ifndef __pfq_rwlock_h__
67 #define __pfq_rwlock_h__
68 
69 
70 
71 //******************************************************************************
72 // global includes
73 //******************************************************************************
74 
75 #include <inttypes.h>
76 #include <stdbool.h>
77 
78 
79 
80 //******************************************************************************
81 // local includes
82 //******************************************************************************
83 
84 #include "mcs-lock.h"
85 
86 
87 //******************************************************************************
88 // macros
89 //******************************************************************************
90 
91 // align a variable at the start of a cache line
92 #define cache_aligned __attribute__((aligned(128)))
93 
94 
95 
96 //******************************************************************************
97 // types
98 //******************************************************************************
99 
101 
102 typedef struct bigbool {
103  atomic_bool bit cache_aligned;
104 } bigbool;
105 
106 typedef struct {
107  //----------------------------------------------------------------------------
108  // reader management
109  //----------------------------------------------------------------------------
110  atomic_uint_least32_t rin cache_aligned; // = 0
111  atomic_uint_least32_t rout cache_aligned; // = 0
112  atomic_uint_least32_t last cache_aligned; // = WRITER_PRESENT
113  bigbool writer_blocking_readers[2]; // false
114 
115  //----------------------------------------------------------------------------
116  // writer management
117  //----------------------------------------------------------------------------
118  mcs_lock_t wtail cache_aligned; // init
119  mcs_node_t *whead cache_aligned; // null
120 
121 } pfq_rwlock_t;
122 
123 
124 
125 //******************************************************************************
126 // interface operations
127 //******************************************************************************
128 
130 
132 
134 
136 
138 
139 #endif
void pfq_rwlock_write_lock(pfq_rwlock_t *l, pfq_rwlock_node_t *me)
Definition: pfq-rwlock.c:149
atomic_uint_least32_t rin cache_aligned
Definition: pfq-rwlock.h:110
struct bigbool bigbool
mcs_node_t pfq_rwlock_node_t
Definition: pfq-rwlock.h:100
atomic_uint_least32_t last cache_aligned
Definition: pfq-rwlock.h:112
atomic_uint_least32_t rout cache_aligned
Definition: pfq-rwlock.h:111
void pfq_rwlock_read_unlock(pfq_rwlock_t *l)
Definition: pfq-rwlock.c:134
atomic_bool bit cache_aligned
Definition: pfq-rwlock.h:103
void pfq_rwlock_write_unlock(pfq_rwlock_t *l, pfq_rwlock_node_t *me)
Definition: pfq-rwlock.c:211
void pfq_rwlock_read_lock(pfq_rwlock_t *l)
Definition: pfq-rwlock.c:122
void pfq_rwlock_init(pfq_rwlock_t *l)
Definition: pfq-rwlock.c:110
mcs_node_t *whead cache_aligned
Definition: pfq-rwlock.h:119
static char * last
Definition: tokenize.c:65
mcs_lock_t wtail cache_aligned
Definition: pfq-rwlock.h:118