2 * Copyright 2013 John Wittrock.
3 * Copyright 2013-2015 Samy Al Bahra.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * This is an implementation of phase-fair locks derived from the work
34 * Brandenburg, B. and Anderson, J. 2010. Spin-Based
35 * Reader-Writer Synchronization for Multiprocessor Real-Time Systems
47 typedef struct ck_pflock ck_pflock_t;
49 #define CK_PFLOCK_LSB 0xFFFFFFF0
50 #define CK_PFLOCK_RINC 0x100 /* Reader increment value. */
51 #define CK_PFLOCK_WBITS 0x3 /* Writer bits in reader. */
52 #define CK_PFLOCK_PRES 0x2 /* Writer present bit. */
53 #define CK_PFLOCK_PHID 0x1 /* Phase ID bit. */
55 #define CK_PFLOCK_INITIALIZER {0, 0, 0, 0}
57 CK_CC_INLINE static void
58 ck_pflock_init(struct ck_pflock *pf)
70 CK_CC_INLINE static void
71 ck_pflock_write_unlock(ck_pflock_t *pf)
76 /* Migrate from write phase to read phase. */
77 ck_pr_and_32(&pf->rin, CK_PFLOCK_LSB);
79 /* Allow other writers to continue. */
80 ck_pr_faa_32(&pf->wout, 1);
84 CK_CC_INLINE static void
85 ck_pflock_write_lock(ck_pflock_t *pf)
89 /* Acquire ownership of write-phase. */
90 ticket = ck_pr_faa_32(&pf->win, 1);
91 while (ck_pr_load_32(&pf->wout) != ticket)
95 * Acquire ticket on read-side in order to allow them
96 * to flush. Indicates to any incoming reader that a
97 * write-phase is pending.
99 ticket = ck_pr_faa_32(&pf->rin,
100 (ticket & CK_PFLOCK_PHID) | CK_PFLOCK_PRES);
102 /* Wait for any pending readers to flush. */
103 while (ck_pr_load_32(&pf->rout) != ticket)
110 CK_CC_INLINE static void
111 ck_pflock_read_unlock(ck_pflock_t *pf)
114 ck_pr_fence_unlock();
115 ck_pr_faa_32(&pf->rout, CK_PFLOCK_RINC);
119 CK_CC_INLINE static void
120 ck_pflock_read_lock(ck_pflock_t *pf)
125 * If no writer is present, then the operation has completed
128 w = ck_pr_faa_32(&pf->rin, CK_PFLOCK_RINC) & CK_PFLOCK_WBITS;
132 /* Wait for current write phase to complete. */
133 while ((ck_pr_load_32(&pf->rin) & CK_PFLOCK_WBITS) == w)
137 /* Acquire semantics with respect to readers. */
142 #endif /* CK_PFLOCK_H */