[a82d33d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Adam Hraska
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - 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.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup liburcu
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[1b20da0] | 34 | *
|
---|
| 35 | * User space RCU is based on URCU utilizing signals [1]. This
|
---|
| 36 | * implementation does not however signal each thread of the process
|
---|
[a82d33d] | 37 | * to issue a memory barrier. Instead, we introduced a syscall that
|
---|
| 38 | * issues memory barriers (via IPIs) on cpus that are running threads
|
---|
| 39 | * of the current process. First, it does not require us to schedule
|
---|
[1b20da0] | 40 | * and run every thread of the process. Second, IPIs are less intrusive
|
---|
[a82d33d] | 41 | * than switching contexts and entering user space.
|
---|
[1b20da0] | 42 | *
|
---|
[a82d33d] | 43 | * This algorithm is further modified to require a single instead of
|
---|
| 44 | * two reader group changes per grace period. Signal-URCU flips
|
---|
[1b20da0] | 45 | * the reader group and waits for readers of the previous group
|
---|
[a82d33d] | 46 | * twice in succession in order to wait for new readers that were
|
---|
[1b20da0] | 47 | * delayed and mistakenly associated with the previous reader group.
|
---|
[a82d33d] | 48 | * The modified algorithm ensures that the new reader group is
|
---|
| 49 | * always empty (by explicitly waiting for it to become empty).
|
---|
| 50 | * Only then does it flip the reader group and wait for preexisting
|
---|
| 51 | * readers of the old reader group (invariant of SRCU [2, 3]).
|
---|
[1b20da0] | 52 | *
|
---|
| 53 | *
|
---|
[a82d33d] | 54 | * [1] User-level implementations of read-copy update,
|
---|
| 55 | * 2012, appendix
|
---|
| 56 | * http://www.rdrop.com/users/paulmck/RCU/urcu-supp-accepted.2011.08.30a.pdf
|
---|
[1b20da0] | 57 | *
|
---|
[a82d33d] | 58 | * [2] linux/kernel/srcu.c in Linux 3.5-rc2,
|
---|
| 59 | * 2012
|
---|
| 60 | * http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/kernel/srcu.c?v=linux-3.5-rc2-ccs-1.8.3
|
---|
| 61 | *
|
---|
[1b20da0] | 62 | * [3] [RFC PATCH 5/5 single-thread-version] implement
|
---|
[a82d33d] | 63 | * per-domain single-thread state machine,
|
---|
| 64 | * 2012, Lai
|
---|
| 65 | * https://lkml.org/lkml/2012/3/6/586
|
---|
| 66 | */
|
---|
| 67 |
|
---|
| 68 | #include "rcu.h"
|
---|
| 69 | #include <fibril_synch.h>
|
---|
| 70 | #include <fibril.h>
|
---|
| 71 | #include <stdio.h>
|
---|
[582a0b8] | 72 | #include <stddef.h>
|
---|
[a82d33d] | 73 | #include <compiler/barrier.h>
|
---|
| 74 | #include <libarch/barrier.h>
|
---|
| 75 | #include <futex.h>
|
---|
| 76 | #include <macros.h>
|
---|
| 77 | #include <async.h>
|
---|
[a2f42e5] | 78 | #include <adt/list.h>
|
---|
[a82d33d] | 79 | #include <smp_memory_barrier.h>
|
---|
[a2f42e5] | 80 | #include <assert.h>
|
---|
[1b7eec9] | 81 | #include <time.h>
|
---|
[582a0b8] | 82 | #include <thread.h>
|
---|
[a82d33d] | 83 |
|
---|
[d73d992] | 84 | #include "private/fibril.h"
|
---|
| 85 |
|
---|
[a2f42e5] | 86 |
|
---|
| 87 | /** RCU sleeps for RCU_SLEEP_MS before polling an active RCU reader again. */
|
---|
[a82d33d] | 88 | #define RCU_SLEEP_MS 10
|
---|
| 89 |
|
---|
| 90 | #define RCU_NESTING_SHIFT 1
|
---|
| 91 | #define RCU_NESTING_INC (1 << RCU_NESTING_SHIFT)
|
---|
| 92 | #define RCU_GROUP_BIT_MASK (size_t)(RCU_NESTING_INC - 1)
|
---|
| 93 | #define RCU_GROUP_A (size_t)(0 | RCU_NESTING_INC)
|
---|
| 94 | #define RCU_GROUP_B (size_t)(1 | RCU_NESTING_INC)
|
---|
| 95 |
|
---|
| 96 |
|
---|
[a2f42e5] | 97 | /** Fibril local RCU data. */
|
---|
| 98 | typedef struct fibril_rcu_data {
|
---|
[a82d33d] | 99 | size_t nesting_cnt;
|
---|
| 100 | link_t link;
|
---|
[a2f42e5] | 101 | bool registered;
|
---|
| 102 | } fibril_rcu_data_t;
|
---|
[a82d33d] | 103 |
|
---|
[a2f42e5] | 104 | /** Process global RCU data. */
|
---|
[a82d33d] | 105 | typedef struct rcu_data {
|
---|
[99022de] | 106 | size_t cur_gp;
|
---|
[a82d33d] | 107 | size_t reader_group;
|
---|
| 108 | futex_t list_futex;
|
---|
| 109 | list_t fibrils_list;
|
---|
[1b7eec9] | 110 | struct {
|
---|
| 111 | futex_t futex;
|
---|
| 112 | bool locked;
|
---|
| 113 | list_t blocked_fibrils;
|
---|
| 114 | size_t blocked_thread_cnt;
|
---|
| 115 | futex_t futex_blocking_threads;
|
---|
| 116 | } sync_lock;
|
---|
[a82d33d] | 117 | } rcu_data_t;
|
---|
| 118 |
|
---|
[1b7eec9] | 119 | typedef struct blocked_fibril {
|
---|
| 120 | fid_t id;
|
---|
| 121 | link_t link;
|
---|
| 122 | bool is_ready;
|
---|
| 123 | } blocked_fibril_t;
|
---|
| 124 |
|
---|
[a82d33d] | 125 |
|
---|
[a2f42e5] | 126 | /** Fibril local RCU data. */
|
---|
| 127 | static fibril_local fibril_rcu_data_t fibril_rcu = {
|
---|
| 128 | .nesting_cnt = 0,
|
---|
| 129 | .link = {
|
---|
| 130 | .next = NULL,
|
---|
| 131 | .prev = NULL
|
---|
| 132 | },
|
---|
| 133 | .registered = false
|
---|
[a82d33d] | 134 | };
|
---|
| 135 |
|
---|
[a2f42e5] | 136 | /** Process global RCU data. */
|
---|
[a82d33d] | 137 | static rcu_data_t rcu = {
|
---|
[99022de] | 138 | .cur_gp = 0,
|
---|
[a82d33d] | 139 | .reader_group = RCU_GROUP_A,
|
---|
| 140 | .list_futex = FUTEX_INITIALIZER,
|
---|
| 141 | .fibrils_list = LIST_INITIALIZER(rcu.fibrils_list),
|
---|
[1b7eec9] | 142 | .sync_lock = {
|
---|
| 143 | .futex = FUTEX_INITIALIZER,
|
---|
| 144 | .locked = false,
|
---|
| 145 | .blocked_fibrils = LIST_INITIALIZER(rcu.sync_lock.blocked_fibrils),
|
---|
| 146 | .blocked_thread_cnt = 0,
|
---|
| 147 | .futex_blocking_threads = FUTEX_INITIALIZE(0),
|
---|
| 148 | },
|
---|
[a82d33d] | 149 | };
|
---|
| 150 |
|
---|
| 151 |
|
---|
[3679f51a] | 152 | static void wait_for_readers(size_t reader_group);
|
---|
[a82d33d] | 153 | static void force_mb_in_all_threads(void);
|
---|
[a2f42e5] | 154 | static bool is_preexisting_reader(const fibril_rcu_data_t *fib, size_t group);
|
---|
[a82d33d] | 155 |
|
---|
[3679f51a] | 156 | static void lock_sync(void);
|
---|
[1b7eec9] | 157 | static void unlock_sync(void);
|
---|
[3679f51a] | 158 | static void sync_sleep(void);
|
---|
[1b7eec9] | 159 |
|
---|
[a82d33d] | 160 | static bool is_in_group(size_t nesting_cnt, size_t group);
|
---|
| 161 | static bool is_in_reader_section(size_t nesting_cnt);
|
---|
| 162 | static size_t get_other_group(size_t group);
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | /** Registers a fibril so it may start using RCU read sections.
|
---|
[1b20da0] | 166 | *
|
---|
[a82d33d] | 167 | * A fibril must be registered with rcu before it can enter RCU critical
|
---|
| 168 | * sections delineated by rcu_read_lock() and rcu_read_unlock().
|
---|
| 169 | */
|
---|
| 170 | void rcu_register_fibril(void)
|
---|
| 171 | {
|
---|
[a2f42e5] | 172 | assert(!fibril_rcu.registered);
|
---|
[a35b458] | 173 |
|
---|
[38e34273] | 174 | futex_lock(&rcu.list_futex);
|
---|
[a2f42e5] | 175 | list_append(&fibril_rcu.link, &rcu.fibrils_list);
|
---|
[38e34273] | 176 | futex_unlock(&rcu.list_futex);
|
---|
[a35b458] | 177 |
|
---|
[a2f42e5] | 178 | fibril_rcu.registered = true;
|
---|
[a82d33d] | 179 | }
|
---|
| 180 |
|
---|
| 181 | /** Deregisters a fibril that had been using RCU read sections.
|
---|
[1b20da0] | 182 | *
|
---|
[a82d33d] | 183 | * A fibril must be deregistered before it exits if it had
|
---|
| 184 | * been registered with rcu via rcu_register_fibril().
|
---|
| 185 | */
|
---|
| 186 | void rcu_deregister_fibril(void)
|
---|
| 187 | {
|
---|
[a2f42e5] | 188 | assert(fibril_rcu.registered);
|
---|
[a35b458] | 189 |
|
---|
[1b20da0] | 190 | /*
|
---|
[a82d33d] | 191 | * Forcefully unlock any reader sections. The fibril is exiting
|
---|
| 192 | * so it is not holding any references to data protected by the
|
---|
[1b20da0] | 193 | * rcu section. Therefore, it is safe to unlock. Otherwise,
|
---|
[a82d33d] | 194 | * rcu_synchronize() would wait indefinitely.
|
---|
| 195 | */
|
---|
| 196 | memory_barrier();
|
---|
[a2f42e5] | 197 | fibril_rcu.nesting_cnt = 0;
|
---|
[a35b458] | 198 |
|
---|
[38e34273] | 199 | futex_lock(&rcu.list_futex);
|
---|
[a2f42e5] | 200 | list_remove(&fibril_rcu.link);
|
---|
[38e34273] | 201 | futex_unlock(&rcu.list_futex);
|
---|
[a2f42e5] | 202 |
|
---|
| 203 | fibril_rcu.registered = false;
|
---|
[a82d33d] | 204 | }
|
---|
| 205 |
|
---|
[1b20da0] | 206 | /** Delimits the start of an RCU reader critical section.
|
---|
| 207 | *
|
---|
| 208 | * RCU reader sections may be nested.
|
---|
[a82d33d] | 209 | */
|
---|
| 210 | void rcu_read_lock(void)
|
---|
| 211 | {
|
---|
[a2f42e5] | 212 | assert(fibril_rcu.registered);
|
---|
[a35b458] | 213 |
|
---|
[a2f42e5] | 214 | size_t nesting_cnt = ACCESS_ONCE(fibril_rcu.nesting_cnt);
|
---|
[a35b458] | 215 |
|
---|
[a82d33d] | 216 | if (0 == (nesting_cnt >> RCU_NESTING_SHIFT)) {
|
---|
[a2f42e5] | 217 | ACCESS_ONCE(fibril_rcu.nesting_cnt) = ACCESS_ONCE(rcu.reader_group);
|
---|
[a82d33d] | 218 | /* Required by MB_FORCE_L */
|
---|
| 219 | compiler_barrier(); /* CC_BAR_L */
|
---|
| 220 | } else {
|
---|
[a2f42e5] | 221 | ACCESS_ONCE(fibril_rcu.nesting_cnt) = nesting_cnt + RCU_NESTING_INC;
|
---|
[a82d33d] | 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /** Delimits the start of an RCU reader critical section. */
|
---|
| 226 | void rcu_read_unlock(void)
|
---|
| 227 | {
|
---|
[a2f42e5] | 228 | assert(fibril_rcu.registered);
|
---|
[b188002] | 229 | assert(rcu_read_locked());
|
---|
[a35b458] | 230 |
|
---|
[a82d33d] | 231 | /* Required by MB_FORCE_U */
|
---|
| 232 | compiler_barrier(); /* CC_BAR_U */
|
---|
| 233 | /* todo: ACCESS_ONCE(nesting_cnt) ? */
|
---|
[a2f42e5] | 234 | fibril_rcu.nesting_cnt -= RCU_NESTING_INC;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /** Returns true if the current fibril is in an RCU reader section. */
|
---|
| 238 | bool rcu_read_locked(void)
|
---|
| 239 | {
|
---|
| 240 | return 0 != (fibril_rcu.nesting_cnt >> RCU_NESTING_SHIFT);
|
---|
[a82d33d] | 241 | }
|
---|
| 242 |
|
---|
| 243 | /** Blocks until all preexisting readers exit their critical sections. */
|
---|
[3679f51a] | 244 | void rcu_synchronize(void)
|
---|
[a82d33d] | 245 | {
|
---|
[a2f42e5] | 246 | assert(!rcu_read_locked());
|
---|
[a35b458] | 247 |
|
---|
[99022de] | 248 | /* Contain load of rcu.cur_gp. */
|
---|
| 249 | memory_barrier();
|
---|
| 250 |
|
---|
| 251 | /* Approximately the number of the GP in progress. */
|
---|
| 252 | size_t gp_in_progress = ACCESS_ONCE(rcu.cur_gp);
|
---|
[a35b458] | 253 |
|
---|
[3679f51a] | 254 | lock_sync();
|
---|
[a35b458] | 255 |
|
---|
[1b20da0] | 256 | /*
|
---|
| 257 | * Exit early if we were stuck waiting for the mutex for a full grace
|
---|
[99022de] | 258 | * period. Started waiting during gp_in_progress (or gp_in_progress + 1
|
---|
| 259 | * if the value propagated to this cpu too late) so wait for the next
|
---|
| 260 | * full GP, gp_in_progress + 1, to finish. Ie don't wait if the GP
|
---|
| 261 | * after that, gp_in_progress + 2, already started.
|
---|
| 262 | */
|
---|
[a2f42e5] | 263 | /* rcu.cur_gp >= gp_in_progress + 2, but tolerates overflows. */
|
---|
| 264 | if (rcu.cur_gp != gp_in_progress && rcu.cur_gp + 1 != gp_in_progress) {
|
---|
[1b7eec9] | 265 | unlock_sync();
|
---|
[99022de] | 266 | return;
|
---|
| 267 | }
|
---|
[a35b458] | 268 |
|
---|
[99022de] | 269 | ++ACCESS_ONCE(rcu.cur_gp);
|
---|
[a35b458] | 270 |
|
---|
[1b20da0] | 271 | /*
|
---|
| 272 | * Pairs up with MB_FORCE_L (ie CC_BAR_L). Makes changes prior
|
---|
| 273 | * to rcu_synchronize() visible to new readers.
|
---|
[a82d33d] | 274 | */
|
---|
| 275 | memory_barrier(); /* MB_A */
|
---|
[a35b458] | 276 |
|
---|
[1b20da0] | 277 | /*
|
---|
| 278 | * Pairs up with MB_A.
|
---|
| 279 | *
|
---|
[a82d33d] | 280 | * If the memory barrier is issued before CC_BAR_L in the target
|
---|
| 281 | * thread, it pairs up with MB_A and the thread sees all changes
|
---|
| 282 | * prior to rcu_synchronize(). Ie any reader sections are new
|
---|
[1b20da0] | 283 | * rcu readers.
|
---|
| 284 | *
|
---|
[a82d33d] | 285 | * If the memory barrier is issued after CC_BAR_L, it pairs up
|
---|
| 286 | * with MB_B and it will make the most recent nesting_cnt visible
|
---|
| 287 | * in this thread. Since the reader may have already accessed
|
---|
| 288 | * memory protected by RCU (it ran instructions passed CC_BAR_L),
|
---|
[1b20da0] | 289 | * it is a preexisting reader. Seeing the most recent nesting_cnt
|
---|
[a82d33d] | 290 | * ensures the thread will be identified as a preexisting reader
|
---|
| 291 | * and we will wait for it in wait_for_readers(old_reader_group).
|
---|
| 292 | */
|
---|
| 293 | force_mb_in_all_threads(); /* MB_FORCE_L */
|
---|
[a35b458] | 294 |
|
---|
[1b20da0] | 295 | /*
|
---|
[a82d33d] | 296 | * Pairs with MB_FORCE_L (ie CC_BAR_L, CC_BAR_U) and makes the most
|
---|
| 297 | * current fibril.nesting_cnt visible to this cpu.
|
---|
| 298 | */
|
---|
| 299 | read_barrier(); /* MB_B */
|
---|
[a35b458] | 300 |
|
---|
[a82d33d] | 301 | size_t new_reader_group = get_other_group(rcu.reader_group);
|
---|
[3679f51a] | 302 | wait_for_readers(new_reader_group);
|
---|
[a35b458] | 303 |
|
---|
[a82d33d] | 304 | /* Separates waiting for readers in new_reader_group from group flip. */
|
---|
| 305 | memory_barrier();
|
---|
[a35b458] | 306 |
|
---|
[a82d33d] | 307 | /* Flip the group new readers should associate with. */
|
---|
| 308 | size_t old_reader_group = rcu.reader_group;
|
---|
| 309 | rcu.reader_group = new_reader_group;
|
---|
| 310 |
|
---|
| 311 | /* Flip the group before waiting for preexisting readers in the old group.*/
|
---|
| 312 | memory_barrier();
|
---|
[a35b458] | 313 |
|
---|
[3679f51a] | 314 | wait_for_readers(old_reader_group);
|
---|
[a35b458] | 315 |
|
---|
[a82d33d] | 316 | /* MB_FORCE_U */
|
---|
| 317 | force_mb_in_all_threads(); /* MB_FORCE_U */
|
---|
[a35b458] | 318 |
|
---|
[1b7eec9] | 319 | unlock_sync();
|
---|
[a82d33d] | 320 | }
|
---|
| 321 |
|
---|
| 322 | /** Issues a memory barrier in each thread of this process. */
|
---|
| 323 | static void force_mb_in_all_threads(void)
|
---|
| 324 | {
|
---|
[1b20da0] | 325 | /*
|
---|
| 326 | * Only issue barriers in running threads. The scheduler will
|
---|
[a82d33d] | 327 | * execute additional memory barriers when switching to threads
|
---|
| 328 | * of the process that are currently not running.
|
---|
| 329 | */
|
---|
| 330 | smp_memory_barrier();
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | /** Waits for readers of reader_group to exit their readers sections. */
|
---|
[3679f51a] | 334 | static void wait_for_readers(size_t reader_group)
|
---|
[a82d33d] | 335 | {
|
---|
[38e34273] | 336 | futex_lock(&rcu.list_futex);
|
---|
[a35b458] | 337 |
|
---|
[a82d33d] | 338 | list_t quiescent_fibrils;
|
---|
| 339 | list_initialize(&quiescent_fibrils);
|
---|
[a35b458] | 340 |
|
---|
[a82d33d] | 341 | while (!list_empty(&rcu.fibrils_list)) {
|
---|
| 342 | list_foreach_safe(rcu.fibrils_list, fibril_it, next_fibril) {
|
---|
[1b20da0] | 343 | fibril_rcu_data_t *fib = member_to_inst(fibril_it,
|
---|
[1433ecda] | 344 | fibril_rcu_data_t, link);
|
---|
[a35b458] | 345 |
|
---|
[a82d33d] | 346 | if (is_preexisting_reader(fib, reader_group)) {
|
---|
[38e34273] | 347 | futex_unlock(&rcu.list_futex);
|
---|
[3679f51a] | 348 | sync_sleep();
|
---|
[38e34273] | 349 | futex_lock(&rcu.list_futex);
|
---|
[1b7eec9] | 350 | /* Break to while loop. */
|
---|
[a82d33d] | 351 | break;
|
---|
| 352 | } else {
|
---|
| 353 | list_remove(fibril_it);
|
---|
| 354 | list_append(fibril_it, &quiescent_fibrils);
|
---|
| 355 | }
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
[a35b458] | 358 |
|
---|
[a82d33d] | 359 | list_concat(&rcu.fibrils_list, &quiescent_fibrils);
|
---|
[38e34273] | 360 | futex_unlock(&rcu.list_futex);
|
---|
[1b7eec9] | 361 | }
|
---|
| 362 |
|
---|
[3679f51a] | 363 | static void lock_sync(void)
|
---|
[1b7eec9] | 364 | {
|
---|
[38e34273] | 365 | futex_lock(&rcu.sync_lock.futex);
|
---|
[1b7eec9] | 366 | if (rcu.sync_lock.locked) {
|
---|
[3679f51a] | 367 | blocked_fibril_t blocked_fib;
|
---|
| 368 | blocked_fib.id = fibril_get_id();
|
---|
| 369 |
|
---|
| 370 | list_append(&blocked_fib.link, &rcu.sync_lock.blocked_fibrils);
|
---|
| 371 |
|
---|
| 372 | do {
|
---|
| 373 | blocked_fib.is_ready = false;
|
---|
[38e34273] | 374 | futex_unlock(&rcu.sync_lock.futex);
|
---|
[ab6edb6] | 375 | futex_lock(&async_futex);
|
---|
| 376 | fibril_switch(FIBRIL_FROM_BLOCKED);
|
---|
| 377 | futex_unlock(&async_futex);
|
---|
[38e34273] | 378 | futex_lock(&rcu.sync_lock.futex);
|
---|
[3679f51a] | 379 | } while (rcu.sync_lock.locked);
|
---|
| 380 |
|
---|
| 381 | list_remove(&blocked_fib.link);
|
---|
| 382 | rcu.sync_lock.locked = true;
|
---|
[1b7eec9] | 383 | } else {
|
---|
| 384 | rcu.sync_lock.locked = true;
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | static void unlock_sync(void)
|
---|
| 389 | {
|
---|
| 390 | assert(rcu.sync_lock.locked);
|
---|
[a35b458] | 391 |
|
---|
[1b20da0] | 392 | /*
|
---|
[1b7eec9] | 393 | * Blocked threads have a priority over fibrils when accessing sync().
|
---|
| 394 | * Pass the lock onto a waiting thread.
|
---|
| 395 | */
|
---|
| 396 | if (0 < rcu.sync_lock.blocked_thread_cnt) {
|
---|
| 397 | --rcu.sync_lock.blocked_thread_cnt;
|
---|
[38e34273] | 398 | futex_unlock(&rcu.sync_lock.futex_blocking_threads);
|
---|
[1b7eec9] | 399 | } else {
|
---|
| 400 | /* Unlock but wake up any fibrils waiting for the lock. */
|
---|
[a35b458] | 401 |
|
---|
[1b7eec9] | 402 | if (!list_empty(&rcu.sync_lock.blocked_fibrils)) {
|
---|
| 403 | blocked_fibril_t *blocked_fib = member_to_inst(
|
---|
[1433ecda] | 404 | list_first(&rcu.sync_lock.blocked_fibrils), blocked_fibril_t, link);
|
---|
[a35b458] | 405 |
|
---|
[1b7eec9] | 406 | if (!blocked_fib->is_ready) {
|
---|
| 407 | blocked_fib->is_ready = true;
|
---|
| 408 | fibril_add_ready(blocked_fib->id);
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
[a35b458] | 411 |
|
---|
[1b7eec9] | 412 | rcu.sync_lock.locked = false;
|
---|
[38e34273] | 413 | futex_unlock(&rcu.sync_lock.futex);
|
---|
[1b7eec9] | 414 | }
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[3679f51a] | 417 | static void sync_sleep(void)
|
---|
[1b7eec9] | 418 | {
|
---|
| 419 | assert(rcu.sync_lock.locked);
|
---|
[1b20da0] | 420 | /*
|
---|
| 421 | * Release the futex to avoid deadlocks in singlethreaded apps
|
---|
| 422 | * but keep sync locked.
|
---|
[1b7eec9] | 423 | */
|
---|
[38e34273] | 424 | futex_unlock(&rcu.sync_lock.futex);
|
---|
[3679f51a] | 425 | async_usleep(RCU_SLEEP_MS * 1000);
|
---|
[38e34273] | 426 | futex_lock(&rcu.sync_lock.futex);
|
---|
[a82d33d] | 427 | }
|
---|
| 428 |
|
---|
[1b7eec9] | 429 |
|
---|
[a2f42e5] | 430 | static bool is_preexisting_reader(const fibril_rcu_data_t *fib, size_t group)
|
---|
[a82d33d] | 431 | {
|
---|
| 432 | size_t nesting_cnt = ACCESS_ONCE(fib->nesting_cnt);
|
---|
[a35b458] | 433 |
|
---|
[a82d33d] | 434 | return is_in_group(nesting_cnt, group) && is_in_reader_section(nesting_cnt);
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | static size_t get_other_group(size_t group)
|
---|
| 438 | {
|
---|
[1b20da0] | 439 | if (group == RCU_GROUP_A)
|
---|
[a82d33d] | 440 | return RCU_GROUP_B;
|
---|
| 441 | else
|
---|
| 442 | return RCU_GROUP_A;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | static bool is_in_reader_section(size_t nesting_cnt)
|
---|
| 446 | {
|
---|
| 447 | return RCU_NESTING_INC <= nesting_cnt;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | static bool is_in_group(size_t nesting_cnt, size_t group)
|
---|
| 451 | {
|
---|
| 452 | return (nesting_cnt & RCU_GROUP_BIT_MASK) == (group & RCU_GROUP_BIT_MASK);
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 |
|
---|
| 456 |
|
---|
| 457 | /** @}
|
---|
| 458 | */
|
---|