xref: /freebsd/sys/contrib/ck/include/ck_backoff.h (revision 74e9b5f2)
11fb62fb0SOlivier Houchard /*
21fb62fb0SOlivier Houchard  * Copyright 2009-2015 Samy Al Bahra.
31fb62fb0SOlivier Houchard  * All rights reserved.
41fb62fb0SOlivier Houchard  *
51fb62fb0SOlivier Houchard  * Redistribution and use in source and binary forms, with or without
61fb62fb0SOlivier Houchard  * modification, are permitted provided that the following conditions
71fb62fb0SOlivier Houchard  * are met:
81fb62fb0SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
91fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
101fb62fb0SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
111fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
121fb62fb0SOlivier Houchard  *    documentation and/or other materials provided with the distribution.
131fb62fb0SOlivier Houchard  *
141fb62fb0SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151fb62fb0SOlivier Houchard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161fb62fb0SOlivier Houchard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171fb62fb0SOlivier Houchard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181fb62fb0SOlivier Houchard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191fb62fb0SOlivier Houchard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201fb62fb0SOlivier Houchard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211fb62fb0SOlivier Houchard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221fb62fb0SOlivier Houchard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231fb62fb0SOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241fb62fb0SOlivier Houchard  * SUCH DAMAGE.
251fb62fb0SOlivier Houchard  */
261fb62fb0SOlivier Houchard 
271fb62fb0SOlivier Houchard #ifndef CK_BACKOFF_H
281fb62fb0SOlivier Houchard #define CK_BACKOFF_H
291fb62fb0SOlivier Houchard 
301fb62fb0SOlivier Houchard #include <ck_cc.h>
311fb62fb0SOlivier Houchard #include <ck_pr.h>
321fb62fb0SOlivier Houchard 
331fb62fb0SOlivier Houchard #ifndef CK_BACKOFF_CEILING
341fb62fb0SOlivier Houchard #define CK_BACKOFF_CEILING ((1 << 20) - 1)
351fb62fb0SOlivier Houchard #endif
361fb62fb0SOlivier Houchard 
371fb62fb0SOlivier Houchard #define CK_BACKOFF_INITIALIZER (1 << 9)
381fb62fb0SOlivier Houchard 
391fb62fb0SOlivier Houchard typedef unsigned int ck_backoff_t;
401fb62fb0SOlivier Houchard 
411fb62fb0SOlivier Houchard /*
421fb62fb0SOlivier Houchard  * This is a exponential back-off implementation.
431fb62fb0SOlivier Houchard  */
441fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_backoff_eb(unsigned int * c)451fb62fb0SOlivier Houchard ck_backoff_eb(unsigned int *c)
461fb62fb0SOlivier Houchard {
471fb62fb0SOlivier Houchard 	unsigned int i, ceiling;
481fb62fb0SOlivier Houchard 
491fb62fb0SOlivier Houchard 	ceiling = *c;
501fb62fb0SOlivier Houchard 	for (i = 0; i < ceiling; i++)
511fb62fb0SOlivier Houchard 		ck_pr_barrier();
521fb62fb0SOlivier Houchard 
53*74e9b5f2SOlivier Houchard 	*c = ceiling << (ceiling < CK_BACKOFF_CEILING);
541fb62fb0SOlivier Houchard 	return;
551fb62fb0SOlivier Houchard }
561fb62fb0SOlivier Houchard 
571fb62fb0SOlivier Houchard #endif /* CK_BACKOFF_H */
58