HPCToolkit
mcs-lock.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 an API for the MCS lock: a fair queue-based lock.
52 //
53 // Reference:
54 // John M. Mellor-Crummey and Michael L. Scott. 1991. Algorithms for scalable
55 // synchronization on shared-memory multiprocessors. ACM Transactions on
56 // Computing Systems 9, 1 (February 1991), 21-65.
57 // http://doi.acm.org/10.1145/103727.103729
58 //***************************************************************************
59 
60 
61 
62 #ifndef _mcs_lock_h_
63 #define _mcs_lock_h_
64 
65 //******************************************************************************
66 // global includes
67 //******************************************************************************
68 
69 #include "stdatomic.h"
70 #include <stdbool.h>
71 
72 
73 //******************************************************************************
74 // types
75 //******************************************************************************
76 
77 typedef struct mcs_node_s {
78  _Atomic(struct mcs_node_s*) next;
79  atomic_bool blocked;
80 } mcs_node_t;
81 
82 
83 typedef struct {
84  _Atomic(mcs_node_t *) tail;
85 } mcs_lock_t;
86 
87 
88 
89 //******************************************************************************
90 // constants
91 //******************************************************************************
92 
93 #define mcs_nil (struct mcs_node_s*) 0
94 
95 //******************************************************************************
96 // interface functions
97 //******************************************************************************
98 
99 static inline void
101 {
102  atomic_init(&l->tail, mcs_nil);
103 }
104 
105 
106 void
108 
109 
110 bool
112 
113 
114 void
116 
117 #endif
#define mcs_nil
Definition: mcs-lock.h:93
struct mcs_node_s mcs_node_t
bool mcs_trylock(mcs_lock_t *l, mcs_node_t *me)
Definition: mcs-lock.c:122
void mcs_unlock(mcs_lock_t *l, mcs_node_t *me)
Definition: mcs-lock.c:146
#define atomic_init(obj, value)
Definition: stdatomic.h:133
void mcs_lock(mcs_lock_t *l, mcs_node_t *me)
Definition: mcs-lock.c:77
_Atomic(struct mcs_node_s *) next
atomic_bool blocked
Definition: mcs-lock.h:79
static void mcs_init(mcs_lock_t *l)
Definition: mcs-lock.h:100