2 * Copyright 2014 Samy Al Bahra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #ifndef CK_TFLOCK_TICKET_H
28 #define CK_TFLOCK_TICKET_H
31 * This is an implementation of task-fair locks derived from the work
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.
41 struct ck_tflock_ticket {
45 typedef struct ck_tflock_ticket ck_tflock_ticket_t;
47 #define CK_TFLOCK_TICKET_INITIALIZER { 0, 0 }
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. */
55 CK_CC_INLINE static uint32_t
56 ck_tflock_ticket_fca_32(uint32_t *target, uint32_t mask, uint32_t delta)
58 uint32_t snapshot = ck_pr_load_32(target);
62 goal = (snapshot & ~mask) + delta;
63 if (ck_pr_cas_32_value(target, snapshot, goal, &snapshot) == true)
72 CK_CC_INLINE static void
73 ck_tflock_ticket_init(struct ck_tflock_ticket *pf)
76 pf->request = pf->completion = 0;
81 CK_CC_INLINE static void
82 ck_tflock_ticket_write_lock(struct ck_tflock_ticket *lock)
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)
96 CK_CC_INLINE static void
97 ck_tflock_ticket_write_unlock(struct ck_tflock_ticket *lock)
100 ck_pr_fence_unlock();
101 ck_tflock_ticket_fca_32(&lock->completion, CK_TFLOCK_TICKET_WC_TOPMSK,
102 CK_TFLOCK_TICKET_WC_INCR);
106 CK_CC_INLINE static void
107 ck_tflock_ticket_read_lock(struct ck_tflock_ticket *lock)
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;
115 ck_pr_fence_atomic_load();
117 while ((ck_pr_load_32(&lock->completion) &
118 CK_TFLOCK_TICKET_W_MASK) != previous) {
126 CK_CC_INLINE static void
127 ck_tflock_ticket_read_unlock(struct ck_tflock_ticket *lock)
130 ck_pr_fence_unlock();
131 ck_tflock_ticket_fca_32(&lock->completion, CK_TFLOCK_TICKET_RC_TOPMSK,
132 CK_TFLOCK_TICKET_RC_INCR);
136 #endif /* CK_TFLOCK_TICKET_H */