xref: /freebsd/sys/contrib/ck/include/ck_tflock.h (revision 4f9d94bf6491250b649f5bc931b6d93e68373005)
1 /*
2  * Copyright 2014 Samy Al Bahra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef CK_TFLOCK_TICKET_H
28 #define CK_TFLOCK_TICKET_H
29 
30 /*
31  * This is an implementation of task-fair locks derived from the work
32  * described in:
33  *	John M. Mellor-Crummey and Michael L. Scott. 1991.
34  *	Scalable reader-writer synchronization for shared-memory
35  *	multiprocessors. SIGPLAN Not. 26, 7 (April 1991), 106-113.
36  */
37 
38 #include <ck_cc.h>
39 #include <ck_pr.h>
40 
41 struct ck_tflock_ticket {
42 	uint32_t request;
43 	uint32_t completion;
44 };
45 typedef struct ck_tflock_ticket ck_tflock_ticket_t;
46 
47 #define CK_TFLOCK_TICKET_INITIALIZER { 0, 0 }
48 
49 #define CK_TFLOCK_TICKET_RC_INCR	0x10000U	/* Read-side increment. */
50 #define CK_TFLOCK_TICKET_WC_INCR	0x1U		/* Write-side increment. */
51 #define CK_TFLOCK_TICKET_W_MASK		0xffffU		/* Write-side mask. */
52 #define CK_TFLOCK_TICKET_WC_TOPMSK	0x8000U		/* Write clear mask for overflow. */
53 #define CK_TFLOCK_TICKET_RC_TOPMSK	0x80000000U	/* Read clear mask for overflow. */
54 
55 CK_CC_INLINE static uint32_t
ck_tflock_ticket_fca_32(uint32_t * target,uint32_t mask,uint32_t delta)56 ck_tflock_ticket_fca_32(uint32_t *target, uint32_t mask, uint32_t delta)
57 {
58 	uint32_t snapshot = ck_pr_load_32(target);
59 	uint32_t goal;
60 
61 	for (;;) {
62 		goal = (snapshot & ~mask) + delta;
63 		if (ck_pr_cas_32_value(target, snapshot, goal, &snapshot) == true)
64 			break;
65 
66 		ck_pr_stall();
67 	}
68 
69 	return snapshot;
70 }
71 
72 CK_CC_INLINE static void
ck_tflock_ticket_init(struct ck_tflock_ticket * pf)73 ck_tflock_ticket_init(struct ck_tflock_ticket *pf)
74 {
75 
76 	pf->request = pf->completion = 0;
77 	ck_pr_barrier();
78 	return;
79 }
80 
81 CK_CC_INLINE static void
ck_tflock_ticket_write_lock(struct ck_tflock_ticket * lock)82 ck_tflock_ticket_write_lock(struct ck_tflock_ticket *lock)
83 {
84 	uint32_t previous;
85 
86 	previous = ck_tflock_ticket_fca_32(&lock->request, CK_TFLOCK_TICKET_WC_TOPMSK,
87 	    CK_TFLOCK_TICKET_WC_INCR);
88 	ck_pr_fence_atomic_load();
89 	while (ck_pr_load_32(&lock->completion) != previous)
90 		ck_pr_stall();
91 
92 	ck_pr_fence_lock();
93 	return;
94 }
95 
96 CK_CC_INLINE static void
ck_tflock_ticket_write_unlock(struct ck_tflock_ticket * lock)97 ck_tflock_ticket_write_unlock(struct ck_tflock_ticket *lock)
98 {
99 
100 	ck_pr_fence_unlock();
101 	ck_tflock_ticket_fca_32(&lock->completion, CK_TFLOCK_TICKET_WC_TOPMSK,
102 	    CK_TFLOCK_TICKET_WC_INCR);
103 	return;
104 }
105 
106 CK_CC_INLINE static void
ck_tflock_ticket_read_lock(struct ck_tflock_ticket * lock)107 ck_tflock_ticket_read_lock(struct ck_tflock_ticket *lock)
108 {
109 	uint32_t previous;
110 
111 	previous = ck_tflock_ticket_fca_32(&lock->request,
112 	    CK_TFLOCK_TICKET_RC_TOPMSK, CK_TFLOCK_TICKET_RC_INCR) &
113 	    CK_TFLOCK_TICKET_W_MASK;
114 
115 	ck_pr_fence_atomic_load();
116 
117 	while ((ck_pr_load_32(&lock->completion) &
118 	    CK_TFLOCK_TICKET_W_MASK) != previous) {
119 		ck_pr_stall();
120 	}
121 
122 	ck_pr_fence_lock();
123 	return;
124 }
125 
126 CK_CC_INLINE static void
ck_tflock_ticket_read_unlock(struct ck_tflock_ticket * lock)127 ck_tflock_ticket_read_unlock(struct ck_tflock_ticket *lock)
128 {
129 
130 	ck_pr_fence_unlock();
131 	ck_tflock_ticket_fca_32(&lock->completion, CK_TFLOCK_TICKET_RC_TOPMSK,
132 	    CK_TFLOCK_TICKET_RC_INCR);
133 	return;
134 }
135 
136 #endif /* CK_TFLOCK_TICKET_H */
137