[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
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #ifndef LIBURCU_RCU_H_
|
---|
| 37 | #define LIBURCU_RCU_H_
|
---|
| 38 |
|
---|
| 39 | #include <compiler/barrier.h>
|
---|
| 40 | #include <libarch/barrier.h>
|
---|
[fd1b1ce] | 41 | #include <stdbool.h>
|
---|
[a82d33d] | 42 |
|
---|
[1b20da0] | 43 | /** Use to assign a pointer to newly initialized data to a rcu reader
|
---|
[a82d33d] | 44 | * accessible pointer.
|
---|
[1b20da0] | 45 | *
|
---|
[a82d33d] | 46 | * Example:
|
---|
| 47 | * @code
|
---|
| 48 | * typedef struct exam {
|
---|
| 49 | * struct exam *next;
|
---|
| 50 | * int grade;
|
---|
| 51 | * } exam_t;
|
---|
[1b20da0] | 52 | *
|
---|
[a82d33d] | 53 | * exam_t *exam_list;
|
---|
| 54 | * // ..
|
---|
[1b20da0] | 55 | *
|
---|
[a82d33d] | 56 | * // Insert at the beginning of the list.
|
---|
[7473807] | 57 | * exam_t *my_exam = malloc(sizeof(exam_t));
|
---|
[a82d33d] | 58 | * my_exam->grade = 5;
|
---|
| 59 | * my_exam->next = exam_list;
|
---|
| 60 | * rcu_assign(exam_list, my_exam);
|
---|
[1b20da0] | 61 | *
|
---|
[a82d33d] | 62 | * // Changes properly propagate. Every reader either sees
|
---|
| 63 | * // the old version of exam_list or the new version with
|
---|
| 64 | * // the fully initialized my_exam.
|
---|
| 65 | * rcu_synchronize();
|
---|
| 66 | * // Now we can be sure every reader sees my_exam.
|
---|
[1b20da0] | 67 | *
|
---|
[a82d33d] | 68 | * @endcode
|
---|
| 69 | */
|
---|
| 70 | #define rcu_assign(ptr, value) \
|
---|
| 71 | do { \
|
---|
| 72 | memory_barrier(); \
|
---|
| 73 | (ptr) = (value); \
|
---|
| 74 | } while (0)
|
---|
| 75 |
|
---|
| 76 | /** Use to access RCU protected data in a reader section.
|
---|
[1b20da0] | 77 | *
|
---|
[a82d33d] | 78 | * Example:
|
---|
| 79 | * @code
|
---|
| 80 | * exam_t *exam_list;
|
---|
| 81 | * // ...
|
---|
[1b20da0] | 82 | *
|
---|
[a82d33d] | 83 | * rcu_read_lock();
|
---|
| 84 | * exam_t *first_exam = rcu_access(exam_list);
|
---|
[1b20da0] | 85 | * // We can now safely use first_exam, it won't change
|
---|
[a82d33d] | 86 | * // under us while we're using it.
|
---|
| 87 | *
|
---|
| 88 | * // ..
|
---|
| 89 | * rcu_read_unlock();
|
---|
| 90 | * @endcode
|
---|
| 91 | */
|
---|
| 92 | #define rcu_access(ptr) ACCESS_ONCE(ptr)
|
---|
| 93 |
|
---|
[1b7eec9] | 94 | typedef enum blocking_mode {
|
---|
| 95 | BM_BLOCK_FIBRIL,
|
---|
| 96 | BM_BLOCK_THREAD
|
---|
| 97 | } blocking_mode_t;
|
---|
[a82d33d] | 98 |
|
---|
| 99 | extern void rcu_register_fibril(void);
|
---|
| 100 | extern void rcu_deregister_fibril(void);
|
---|
| 101 |
|
---|
| 102 | extern void rcu_read_lock(void);
|
---|
| 103 | extern void rcu_read_unlock(void);
|
---|
| 104 |
|
---|
[a2f42e5] | 105 | extern bool rcu_read_locked(void);
|
---|
| 106 |
|
---|
[1b7eec9] | 107 | #define rcu_synchronize() _rcu_synchronize(BM_BLOCK_FIBRIL)
|
---|
| 108 |
|
---|
| 109 | extern void _rcu_synchronize(blocking_mode_t);
|
---|
[a82d33d] | 110 |
|
---|
| 111 | #endif
|
---|
| 112 |
|
---|
| 113 | /** @}
|
---|
| 114 | */
|
---|