xref: /freebsd/sys/contrib/ck/include/ck_cohort.h (revision 4f9d94bf6491250b649f5bc931b6d93e68373005)
1*1fb62fb0SOlivier Houchard /*
2*1fb62fb0SOlivier Houchard  * Copyright 2013-2015 Samy Al Bahra.
3*1fb62fb0SOlivier Houchard  * Copyright 2013 Brendon Scheinman.
4*1fb62fb0SOlivier Houchard  * All rights reserved.
5*1fb62fb0SOlivier Houchard  *
6*1fb62fb0SOlivier Houchard  * Redistribution and use in source and binary forms, with or without
7*1fb62fb0SOlivier Houchard  * modification, are permitted provided that the following conditions
8*1fb62fb0SOlivier Houchard  * are met:
9*1fb62fb0SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
10*1fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
11*1fb62fb0SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
12*1fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
13*1fb62fb0SOlivier Houchard  *    documentation and/or other materials provided with the distribution.
14*1fb62fb0SOlivier Houchard  *
15*1fb62fb0SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*1fb62fb0SOlivier Houchard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*1fb62fb0SOlivier Houchard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*1fb62fb0SOlivier Houchard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*1fb62fb0SOlivier Houchard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*1fb62fb0SOlivier Houchard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*1fb62fb0SOlivier Houchard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*1fb62fb0SOlivier Houchard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*1fb62fb0SOlivier Houchard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*1fb62fb0SOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*1fb62fb0SOlivier Houchard  * SUCH DAMAGE.
26*1fb62fb0SOlivier Houchard  */
27*1fb62fb0SOlivier Houchard 
28*1fb62fb0SOlivier Houchard #ifndef CK_COHORT_H
29*1fb62fb0SOlivier Houchard #define CK_COHORT_H
30*1fb62fb0SOlivier Houchard 
31*1fb62fb0SOlivier Houchard /*
32*1fb62fb0SOlivier Houchard  * This is an implementation of lock cohorts as described in:
33*1fb62fb0SOlivier Houchard  *     Dice, D.; Marathe, V.; and Shavit, N. 2012.
34*1fb62fb0SOlivier Houchard  *     Lock Cohorting: A General Technique for Designing NUMA Locks
35*1fb62fb0SOlivier Houchard  */
36*1fb62fb0SOlivier Houchard 
37*1fb62fb0SOlivier Houchard #include <ck_cc.h>
38*1fb62fb0SOlivier Houchard #include <ck_pr.h>
39*1fb62fb0SOlivier Houchard #include <ck_stddef.h>
40*1fb62fb0SOlivier Houchard 
41*1fb62fb0SOlivier Houchard enum ck_cohort_state {
42*1fb62fb0SOlivier Houchard 	CK_COHORT_STATE_GLOBAL = 0,
43*1fb62fb0SOlivier Houchard 	CK_COHORT_STATE_LOCAL = 1
44*1fb62fb0SOlivier Houchard };
45*1fb62fb0SOlivier Houchard 
46*1fb62fb0SOlivier Houchard #define CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT 10
47*1fb62fb0SOlivier Houchard 
48*1fb62fb0SOlivier Houchard #define CK_COHORT_NAME(N) ck_cohort_##N
49*1fb62fb0SOlivier Houchard #define CK_COHORT_INSTANCE(N) struct CK_COHORT_NAME(N)
50*1fb62fb0SOlivier Houchard #define CK_COHORT_INIT(N, C, GL, LL, P) ck_cohort_##N##_init(C, GL, LL, P)
51*1fb62fb0SOlivier Houchard #define CK_COHORT_LOCK(N, C, GC, LC) ck_cohort_##N##_lock(C, GC, LC)
52*1fb62fb0SOlivier Houchard #define CK_COHORT_UNLOCK(N, C, GC, LC) ck_cohort_##N##_unlock(C, GC, LC)
53*1fb62fb0SOlivier Houchard #define CK_COHORT_TRYLOCK(N, C, GLC, LLC, LUC) ck_cohort_##N##_trylock(C, GLC, LLC, LUC)
54*1fb62fb0SOlivier Houchard #define CK_COHORT_LOCKED(N, C, GC, LC) ck_cohort_##N##_locked(C, GC, LC)
55*1fb62fb0SOlivier Houchard 
56*1fb62fb0SOlivier Houchard #define CK_COHORT_PROTOTYPE(N, GL, GU, GI, LL, LU, LI)				\
57*1fb62fb0SOlivier Houchard 	CK_COHORT_INSTANCE(N) {							\
58*1fb62fb0SOlivier Houchard 		void *global_lock;						\
59*1fb62fb0SOlivier Houchard 		void *local_lock;						\
60*1fb62fb0SOlivier Houchard 		enum ck_cohort_state release_state;				\
61*1fb62fb0SOlivier Houchard 		unsigned int waiting_threads;					\
62*1fb62fb0SOlivier Houchard 		unsigned int acquire_count;					\
63*1fb62fb0SOlivier Houchard 		unsigned int local_pass_limit;					\
64*1fb62fb0SOlivier Houchard 	};									\
65*1fb62fb0SOlivier Houchard 										\
66*1fb62fb0SOlivier Houchard 	CK_CC_INLINE static void						\
67*1fb62fb0SOlivier Houchard 	ck_cohort_##N##_init(struct ck_cohort_##N *cohort,			\
68*1fb62fb0SOlivier Houchard 	    void *global_lock, void *local_lock, unsigned int pass_limit)	\
69*1fb62fb0SOlivier Houchard 	{									\
70*1fb62fb0SOlivier Houchard 		cohort->global_lock = global_lock;				\
71*1fb62fb0SOlivier Houchard 		cohort->local_lock = local_lock;				\
72*1fb62fb0SOlivier Houchard 		cohort->release_state = CK_COHORT_STATE_GLOBAL;			\
73*1fb62fb0SOlivier Houchard 		cohort->waiting_threads = 0;					\
74*1fb62fb0SOlivier Houchard 		cohort->acquire_count = 0;					\
75*1fb62fb0SOlivier Houchard 		cohort->local_pass_limit = pass_limit;				\
76*1fb62fb0SOlivier Houchard 		ck_pr_barrier();						\
77*1fb62fb0SOlivier Houchard 		return;								\
78*1fb62fb0SOlivier Houchard 	}									\
79*1fb62fb0SOlivier Houchard 										\
80*1fb62fb0SOlivier Houchard 	CK_CC_INLINE static void						\
81*1fb62fb0SOlivier Houchard 	ck_cohort_##N##_lock(CK_COHORT_INSTANCE(N) *cohort,			\
82*1fb62fb0SOlivier Houchard 	    void *global_context, void *local_context)				\
83*1fb62fb0SOlivier Houchard 	{									\
84*1fb62fb0SOlivier Houchard 										\
85*1fb62fb0SOlivier Houchard 		ck_pr_inc_uint(&cohort->waiting_threads);			\
86*1fb62fb0SOlivier Houchard 		LL(cohort->local_lock, local_context);				\
87*1fb62fb0SOlivier Houchard 		ck_pr_dec_uint(&cohort->waiting_threads);			\
88*1fb62fb0SOlivier Houchard 										\
89*1fb62fb0SOlivier Houchard 		if (cohort->release_state == CK_COHORT_STATE_GLOBAL) {		\
90*1fb62fb0SOlivier Houchard 			GL(cohort->global_lock, global_context);		\
91*1fb62fb0SOlivier Houchard 		}								\
92*1fb62fb0SOlivier Houchard 										\
93*1fb62fb0SOlivier Houchard 		++cohort->acquire_count;					\
94*1fb62fb0SOlivier Houchard 		return;								\
95*1fb62fb0SOlivier Houchard 	}									\
96*1fb62fb0SOlivier Houchard 										\
97*1fb62fb0SOlivier Houchard 	CK_CC_INLINE static void						\
98*1fb62fb0SOlivier Houchard 	ck_cohort_##N##_unlock(CK_COHORT_INSTANCE(N) *cohort,			\
99*1fb62fb0SOlivier Houchard 	    void *global_context, void *local_context)				\
100*1fb62fb0SOlivier Houchard 	{									\
101*1fb62fb0SOlivier Houchard 										\
102*1fb62fb0SOlivier Houchard 		if (ck_pr_load_uint(&cohort->waiting_threads) > 0		\
103*1fb62fb0SOlivier Houchard 		    && cohort->acquire_count < cohort->local_pass_limit) {	\
104*1fb62fb0SOlivier Houchard 			cohort->release_state = CK_COHORT_STATE_LOCAL;		\
105*1fb62fb0SOlivier Houchard 		} else {							\
106*1fb62fb0SOlivier Houchard 			GU(cohort->global_lock, global_context);		\
107*1fb62fb0SOlivier Houchard 			cohort->release_state = CK_COHORT_STATE_GLOBAL;		\
108*1fb62fb0SOlivier Houchard 			cohort->acquire_count = 0;				\
109*1fb62fb0SOlivier Houchard 		}								\
110*1fb62fb0SOlivier Houchard 										\
111*1fb62fb0SOlivier Houchard 		ck_pr_fence_release();						\
112*1fb62fb0SOlivier Houchard 		LU(cohort->local_lock, local_context);				\
113*1fb62fb0SOlivier Houchard 										\
114*1fb62fb0SOlivier Houchard 		return;								\
115*1fb62fb0SOlivier Houchard 	}									\
116*1fb62fb0SOlivier Houchard 										\
117*1fb62fb0SOlivier Houchard 	CK_CC_INLINE static bool						\
118*1fb62fb0SOlivier Houchard 	ck_cohort_##N##_locked(CK_COHORT_INSTANCE(N) *cohort,			\
119*1fb62fb0SOlivier Houchard 	    void *global_context, void *local_context)				\
120*1fb62fb0SOlivier Houchard 	{									\
121*1fb62fb0SOlivier Houchard 		return GI(cohort->local_lock, local_context) ||			\
122*1fb62fb0SOlivier Houchard 		    LI(cohort->global_lock, global_context);			\
123*1fb62fb0SOlivier Houchard 	}
124*1fb62fb0SOlivier Houchard 
125*1fb62fb0SOlivier Houchard #define CK_COHORT_TRYLOCK_PROTOTYPE(N, GL, GU, GI, GTL, LL, LU, LI, LTL)	\
126*1fb62fb0SOlivier Houchard 	CK_COHORT_PROTOTYPE(N, GL, GU, GI, LL, LU, LI)				\
127*1fb62fb0SOlivier Houchard 	CK_CC_INLINE static bool						\
128*1fb62fb0SOlivier Houchard 	ck_cohort_##N##_trylock(CK_COHORT_INSTANCE(N) *cohort,			\
129*1fb62fb0SOlivier Houchard 	    void *global_context, void *local_context,				\
130*1fb62fb0SOlivier Houchard 	    void *local_unlock_context)						\
131*1fb62fb0SOlivier Houchard 	{									\
132*1fb62fb0SOlivier Houchard 										\
133*1fb62fb0SOlivier Houchard 		bool trylock_result;						\
134*1fb62fb0SOlivier Houchard 										\
135*1fb62fb0SOlivier Houchard 		ck_pr_inc_uint(&cohort->waiting_threads);			\
136*1fb62fb0SOlivier Houchard 		trylock_result = LTL(cohort->local_lock, local_context);	\
137*1fb62fb0SOlivier Houchard 		ck_pr_dec_uint(&cohort->waiting_threads);			\
138*1fb62fb0SOlivier Houchard 		if (trylock_result == false) {					\
139*1fb62fb0SOlivier Houchard 			return false;						\
140*1fb62fb0SOlivier Houchard 		}								\
141*1fb62fb0SOlivier Houchard 										\
142*1fb62fb0SOlivier Houchard 		if (cohort->release_state == CK_COHORT_STATE_GLOBAL &&		\
143*1fb62fb0SOlivier Houchard 		    GTL(cohort->global_lock, global_context) == false) {	\
144*1fb62fb0SOlivier Houchard 		    	LU(cohort->local_lock, local_unlock_context);		\
145*1fb62fb0SOlivier Houchard 			return false;						\
146*1fb62fb0SOlivier Houchard 		}								\
147*1fb62fb0SOlivier Houchard 										\
148*1fb62fb0SOlivier Houchard 		++cohort->acquire_count;					\
149*1fb62fb0SOlivier Houchard 		return true;							\
150*1fb62fb0SOlivier Houchard 	}
151*1fb62fb0SOlivier Houchard 
152*1fb62fb0SOlivier Houchard #define CK_COHORT_INITIALIZER {							\
153*1fb62fb0SOlivier Houchard 	.global_lock = NULL,							\
154*1fb62fb0SOlivier Houchard 	.local_lock = NULL,							\
155*1fb62fb0SOlivier Houchard 	.release_state = CK_COHORT_STATE_GLOBAL,				\
156*1fb62fb0SOlivier Houchard 	.waiting_threads = 0,							\
157*1fb62fb0SOlivier Houchard 	.acquire_count = 0,							\
158*1fb62fb0SOlivier Houchard 	.local_pass_limit = CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT			\
159*1fb62fb0SOlivier Houchard }
160*1fb62fb0SOlivier Houchard 
161*1fb62fb0SOlivier Houchard #endif /* CK_COHORT_H */
162