1221828Sgrehan/*
2221828Sgrehan * Copyright 2009-2015 Samy Al Bahra.
3221828Sgrehan * All rights reserved.
4221828Sgrehan *
5221828Sgrehan * Redistribution and use in source and binary forms, with or without
6221828Sgrehan * modification, are permitted provided that the following conditions
7221828Sgrehan * are met:
8221828Sgrehan * 1. Redistributions of source code must retain the above copyright
9221828Sgrehan *    notice, this list of conditions and the following disclaimer.
10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
12221828Sgrehan *    documentation and/or other materials provided with the distribution.
13221828Sgrehan *
14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24221828Sgrehan * SUCH DAMAGE.
25221828Sgrehan */
26245678Sneel
27221828Sgrehan#ifndef CK_BACKOFF_H
28221828Sgrehan#define CK_BACKOFF_H
29221828Sgrehan
30221828Sgrehan#include <ck_cc.h>
31221828Sgrehan#include <ck_pr.h>
32221828Sgrehan
33221828Sgrehan#ifndef CK_BACKOFF_CEILING
34221828Sgrehan#define CK_BACKOFF_CEILING ((1 << 20) - 1)
35221828Sgrehan#endif
36221828Sgrehan
37221828Sgrehan#define CK_BACKOFF_INITIALIZER (1 << 9)
38221828Sgrehan
39221828Sgrehantypedef unsigned int ck_backoff_t;
40221828Sgrehan
41258579Sneel/*
42258075Sneel * This is a exponential back-off implementation.
43221828Sgrehan */
44256072SneelCK_CC_INLINE static void
45256072Sneelck_backoff_eb(unsigned int *c)
46256072Sneel{
47221828Sgrehan	unsigned int i, ceiling;
48240922Sneel
49240922Sneel	ceiling = *c;
50260466Sneel	for (i = 0; i < ceiling; i++)
51221828Sgrehan		ck_pr_barrier();
52259782Sjhb
53256072Sneel	*c = ceiling <<= ceiling < CK_BACKOFF_CEILING;
54256072Sneel	return;
55260619Sneel}
56221828Sgrehan
57221828Sgrehan#endif /* CK_BACKOFF_H */
58221828Sgrehan