]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frrcu.h
1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2017-19 David Lamparter, for NetDEF, Inc.
19 * There's a global sequence counter. Whenever a thread does a
20 * rcu_read_lock(), it is marked as holding the current sequence counter.
21 * When something is cleaned with RCU, the global sequence counter is
22 * increased and the item is queued for cleanup - *after* all threads are
23 * at a more recent sequence counter (or no sequence counter / unheld).
25 * So, by delaying resource cleanup, RCU ensures that things don't go away
26 * while another thread may hold a (stale) reference.
28 * Note that even if a thread is in rcu_read_lock(), it is invalid for that
29 * thread to access bits after rcu_free() & co on them. This is a design
30 * choice to allow no-op'ing out the entire RCU mechanism if we're running
31 * singlethreaded. (Also allows some optimization on the counter bumping.)
33 * differences from Linux Kernel RCU:
34 * - there's no rcu_synchronize(), if you really need to defer something
35 * use rcu_call() (and double check it's really necessary)
36 * - rcu_dereference() and rcu_assign_pointer() don't exist, use atomic_*
37 * instead (ATOM* list structures do the right thing)
43 /* called before new thread creation, sets up rcu thread info for new thread
44 * before it actually exits. This ensures possible RCU references are held
47 * return value must be passed into the new thread's call to rcu_thread_start()
49 extern struct rcu_thread
*rcu_thread_prepare(void);
51 /* cleanup in case pthread_create() fails */
52 extern void rcu_thread_unprepare(struct rcu_thread
*rcu_thread
);
54 /* called early in the new thread, with the return value from the above.
55 * NB: new thread is initially in RCU-held state! (at depth 1)
57 * TBD: maybe inherit RCU state from rcu_thread_prepare()?
59 extern void rcu_thread_start(struct rcu_thread
*rcu_thread
);
61 /* thread exit is handled through pthread_key_create's destructor function */
63 /* global RCU shutdown - must be called with only 1 active thread left. waits
64 * until remaining RCU actions are done & RCU thread has exited.
66 * This is mostly here to get a clean exit without memleaks.
68 extern void rcu_shutdown(void);
70 /* enter / exit RCU-held state. counter-based, so can be called nested. */
71 extern void rcu_read_lock(void);
72 extern void rcu_read_unlock(void);
74 /* for debugging / safety checks */
75 extern void rcu_assert_read_locked(void);
76 extern void rcu_assert_read_unlocked(void);
78 enum rcu_action_type
{
80 /* used internally by the RCU code, shouldn't ever show up outside */
83 /* normal RCU actions, for outside use */
89 /* since rcu_head is intended to be embedded into structs which may exist
90 * with lots of copies, rcu_head is shrunk down to its absolute minimum -
91 * the atomlist pointer + a pointer to this action struct.
94 enum rcu_action_type type
;
103 void (*fptr
)(void *arg
);
109 /* RCU cleanup function queue item */
110 PREDECL_ATOMLIST(rcu_heads
);
112 struct rcu_heads_item head
;
113 const struct rcu_action
*action
;
116 /* special RCU head for delayed fd-close */
117 struct rcu_head_close
{
118 struct rcu_head rcu_head
;
122 /* enqueue RCU action - use the macros below to get the rcu_action set up */
123 extern void rcu_enqueue(struct rcu_head
*head
, const struct rcu_action
*action
);
125 /* RCU free() and file close() operations.
127 * freed memory / closed fds become _immediately_ unavailable to the calling
128 * thread, but will remain available for other threads until they have passed
129 * into RCU-released state.
132 /* may be called with NULL mt to do non-MTYPE free() */
133 #define rcu_free(mtype, ptr, field) \
135 typeof(ptr) _ptr = (ptr); \
138 struct rcu_head *_rcu_head = &_ptr->field; \
139 static const struct rcu_action _rcu_action = { \
143 .offset = offsetof(typeof(*_ptr), field), \
146 rcu_enqueue(_rcu_head, &_rcu_action); \
149 /* use this sparingly, it runs on (and blocks) the RCU thread */
150 #define rcu_call(func, ptr, field) \
152 typeof(ptr) _ptr = (ptr); \
153 void (*fptype)(typeof(ptr)); \
154 struct rcu_head *_rcu_head = &_ptr->field; \
155 static const struct rcu_action _rcu_action = { \
158 .fptr = (void *)func, \
159 .offset = offsetof(typeof(*_ptr), field), \
162 (void)(_fptype = func); \
163 rcu_enqueue(_rcu_head, &_rcu_action); \
166 extern void rcu_close(struct rcu_head_close
*head
, int fd
);
172 #endif /* _FRRCU_H */