]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frrcu.h
2 * Copyright (c) 2017-19 David Lamparter, for NetDEF, Inc.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 * There's a global sequence counter. Whenever a thread does a
31 * rcu_read_lock(), it is marked as holding the current sequence counter.
32 * When something is cleaned with RCU, the global sequence counter is
33 * increased and the item is queued for cleanup - *after* all threads are
34 * at a more recent sequence counter (or no sequence counter / unheld).
36 * So, by delaying resource cleanup, RCU ensures that things don't go away
37 * while another thread may hold a (stale) reference.
39 * Note that even if a thread is in rcu_read_lock(), it is invalid for that
40 * thread to access bits after rcu_free() & co on them. This is a design
41 * choice to allow no-op'ing out the entire RCU mechanism if we're running
42 * singlethreaded. (Also allows some optimization on the counter bumping.)
44 * differences from Linux Kernel RCU:
45 * - there's no rcu_synchronize(), if you really need to defer something
46 * use rcu_call() (and double check it's really necessary)
47 * - rcu_dereference() and rcu_assign_pointer() don't exist, use atomic_*
48 * instead (ATOM* list structures do the right thing)
54 /* called before new thread creation, sets up rcu thread info for new thread
55 * before it actually exits. This ensures possible RCU references are held
58 * return value must be passed into the new thread's call to rcu_thread_start()
60 extern struct rcu_thread
*rcu_thread_prepare(void);
62 /* cleanup in case pthread_create() fails */
63 extern void rcu_thread_unprepare(struct rcu_thread
*rcu_thread
);
65 /* called early in the new thread, with the return value from the above.
66 * NB: new thread is initially in RCU-held state! (at depth 1)
68 * TBD: maybe inherit RCU state from rcu_thread_prepare()?
70 extern void rcu_thread_start(struct rcu_thread
*rcu_thread
);
72 /* thread exit is handled through pthread_key_create's destructor function */
74 /* global RCU shutdown - must be called with only 1 active thread left. waits
75 * until remaining RCU actions are done & RCU thread has exited.
77 * This is mostly here to get a clean exit without memleaks.
79 extern void rcu_shutdown(void);
81 /* enter / exit RCU-held state. counter-based, so can be called nested. */
82 extern void rcu_read_lock(void);
83 extern void rcu_read_unlock(void);
85 /* for debugging / safety checks */
86 extern void rcu_assert_read_locked(void);
87 extern void rcu_assert_read_unlocked(void);
89 enum rcu_action_type
{
91 /* used internally by the RCU code, shouldn't ever show up outside */
94 /* normal RCU actions, for outside use */
100 /* since rcu_head is intended to be embedded into structs which may exist
101 * with lots of copies, rcu_head is shrunk down to its absolute minimum -
102 * the atomlist pointer + a pointer to this action struct.
105 enum rcu_action_type type
;
114 void (*fptr
)(void *arg
);
120 /* RCU cleanup function queue item */
121 PREDECL_ATOMLIST(rcu_heads
);
123 struct rcu_heads_item head
;
124 const struct rcu_action
*action
;
127 /* special RCU head for delayed fd-close */
128 struct rcu_head_close
{
129 struct rcu_head rcu_head
;
133 /* enqueue RCU action - use the macros below to get the rcu_action set up */
134 extern void rcu_enqueue(struct rcu_head
*head
, const struct rcu_action
*action
);
136 /* RCU free() and file close() operations.
138 * freed memory / closed fds become _immediately_ unavailable to the calling
139 * thread, but will remain available for other threads until they have passed
140 * into RCU-released state.
143 /* may be called with NULL mt to do non-MTYPE free() */
144 #define rcu_free(mtype, ptr, field) \
146 typeof(ptr) _ptr = (ptr); \
149 struct rcu_head *_rcu_head = &_ptr->field; \
150 static const struct rcu_action _rcu_action = { \
154 .offset = offsetof(typeof(*_ptr), field), \
157 rcu_enqueue(_rcu_head, &_rcu_action); \
160 /* use this sparingly, it runs on (and blocks) the RCU thread */
161 #define rcu_call(func, ptr, field) \
163 typeof(ptr) _ptr = (ptr); \
164 void (*fptype)(typeof(ptr)); \
165 struct rcu_head *_rcu_head = &_ptr->field; \
166 static const struct rcu_action _rcu_action = { \
169 .fptr = (void *)func, \
170 .offset = offsetof(typeof(*_ptr), field), \
173 (void)(_fptype = func); \
174 rcu_enqueue(_rcu_head, &_rcu_action); \
177 extern void rcu_close(struct rcu_head_close
*head
, int fd
);
183 #endif /* _FRRCU_H */