uatomic/x86: Remove redundant memory barriers
[urcu.git] / src / urcu-poll-impl.h
1 /*
2 * urcu-poll-impl.h
3 *
4 * Userspace RCU library - Grace period polling API
5 *
6 * Copyright (c) 2023 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <pthread.h>
24 #include <stdbool.h>
25
26 #include <urcu/urcu-poll.h>
27 #include <urcu/call-rcu.h>
28
29 struct urcu_poll_worker_state {
30 struct urcu_gp_poll_state current_state;
31 struct urcu_gp_poll_state latest_target; /* Most recent snapshot taken */
32 struct rcu_head rcu_head;
33 pthread_mutex_t lock;
34 bool active;
35 };
36
37 static struct urcu_poll_worker_state poll_worker_gp_state = {
38 .lock = PTHREAD_MUTEX_INITIALIZER,
39 };
40
41 static
42 void urcu_poll_worker_cb(struct rcu_head *head __attribute__((unused)))
43 {
44 mutex_lock(&poll_worker_gp_state.lock);
45 /* A new grace period has been reached. */
46 poll_worker_gp_state.current_state.grace_period_id++;
47 if ((long)(poll_worker_gp_state.latest_target.grace_period_id - poll_worker_gp_state.current_state.grace_period_id) >= 0) {
48 /* Need to re-queue. */
49 call_rcu(&poll_worker_gp_state.rcu_head, urcu_poll_worker_cb);
50 } else {
51 /* No user waiting for a target grace period. */
52 poll_worker_gp_state.active = false;
53 }
54 mutex_unlock(&poll_worker_gp_state.lock);
55 }
56
57 /*
58 * Start polling on grace period. If no worker is currently active,
59 * snapshot the current value and start a worker callback. If the worker
60 * is currently active, snapshot the current value + 1, and set this
61 * value as the latest snapshot, which will cause the worker to re-queue
62 * itself with call_rcu to issue one more grace period.
63 *
64 * Because it uses call_rcu, it needs to be called from a registered RCU
65 * thread.
66 */
67 struct urcu_gp_poll_state start_poll_synchronize_rcu(void)
68 {
69 struct urcu_gp_poll_state new_target_gp_state;
70 bool was_active = false;
71
72 mutex_lock(&poll_worker_gp_state.lock);
73 new_target_gp_state.grace_period_id = poll_worker_gp_state.current_state.grace_period_id;
74 was_active = poll_worker_gp_state.active;
75 if (!was_active)
76 poll_worker_gp_state.active = true;
77 else
78 new_target_gp_state.grace_period_id++;
79 poll_worker_gp_state.latest_target.grace_period_id = new_target_gp_state.grace_period_id;
80 if (!was_active)
81 call_rcu(&poll_worker_gp_state.rcu_head, urcu_poll_worker_cb);
82 mutex_unlock(&poll_worker_gp_state.lock);
83 return new_target_gp_state;
84 }
85
86 /*
87 * Poll the grace period state. Return true if quiescence was reached
88 * since the snapshot was taken, return false if quiescence was not
89 * reached since snapshot.
90 */
91 bool poll_state_synchronize_rcu(struct urcu_gp_poll_state target_gp_state)
92 {
93 bool target_gp_reached = false;
94
95 mutex_lock(&poll_worker_gp_state.lock);
96 if ((long)(target_gp_state.grace_period_id - poll_worker_gp_state.current_state.grace_period_id) < 0)
97 target_gp_reached = true;
98 mutex_unlock(&poll_worker_gp_state.lock);
99 return target_gp_reached;
100 }
This page took 0.031233 seconds and 4 git commands to generate.