4 * Userspace RCU library - Grace period polling API
6 * Copyright (c) 2023 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
26 #include <urcu/urcu-poll.h>
27 #include <urcu/call-rcu.h>
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
;
37 static struct urcu_poll_worker_state poll_worker_gp_state
= {
38 .lock
= PTHREAD_MUTEX_INITIALIZER
,
42 void urcu_poll_worker_cb(struct rcu_head
*head
__attribute__((unused
)))
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
);
51 /* No user waiting for a target grace period. */
52 poll_worker_gp_state
.active
= false;
54 mutex_unlock(&poll_worker_gp_state
.lock
);
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.
64 * Because it uses call_rcu, it needs to be called from a registered RCU
67 struct urcu_gp_poll_state
start_poll_synchronize_rcu(void)
69 struct urcu_gp_poll_state new_target_gp_state
;
70 bool was_active
= false;
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
;
76 poll_worker_gp_state
.active
= true;
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
;
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
;
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.
91 bool poll_state_synchronize_rcu(struct urcu_gp_poll_state target_gp_state
)
93 bool target_gp_reached
= false;
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
;