From d89a689ab4b2feafd6175cf4bf685b173f6f6f7c Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 20 Feb 2020 11:01:02 -0800 Subject: [PATCH] sysprofd: remove use of GAtomicRCBox Using an embedded ref count allows us to backport to older operating systems for which GLib is restricted to 2.56. --- src/sysprofd/ipc-rapl-profiler.c | 2 +- src/sysprofd/sysprof-turbostat.c | 39 +++++++++++++++++++++++--------- src/sysprofd/sysprof-turbostat.h | 5 ++-- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/sysprofd/ipc-rapl-profiler.c b/src/sysprofd/ipc-rapl-profiler.c index 259016b4..9b91847e 100644 --- a/src/sysprofd/ipc-rapl-profiler.c +++ b/src/sysprofd/ipc-rapl-profiler.c @@ -84,7 +84,7 @@ ipc_rapl_profiler_stop_locked (IpcRaplProfiler *self) if (self->turbostat != NULL) sysprof_turbostat_stop (self->turbostat); - g_clear_pointer (&self->turbostat, sysprof_turbostat_free); + g_clear_pointer (&self->turbostat, sysprof_turbostat_unref); g_clear_pointer (&self->counter_ids, g_array_unref); if (self->writer != NULL) diff --git a/src/sysprofd/sysprof-turbostat.c b/src/sysprofd/sysprof-turbostat.c index 4c6301d7..5c80a534 100644 --- a/src/sysprofd/sysprof-turbostat.c +++ b/src/sysprofd/sysprof-turbostat.c @@ -32,11 +32,12 @@ struct _SysprofTurbostat { - GPid pid; - GIOChannel *channel; - guint channel_watch; - GFunc sample_func; - gpointer sample_data; + volatile gint ref_count; + GPid pid; + GIOChannel *channel; + guint channel_watch; + GFunc sample_func; + gpointer sample_data; }; enum { @@ -51,7 +52,8 @@ sysprof_turbostat_new (GFunc sample_func, { SysprofTurbostat *self; - self = g_rc_box_new0 (SysprofTurbostat); + self = g_slice_new0 (SysprofTurbostat); + self->ref_count = 1; self->pid = 0; self->channel = NULL; self->sample_func = sample_func; @@ -61,21 +63,36 @@ sysprof_turbostat_new (GFunc sample_func, } static void -sysprof_turbostat_finalize (gpointer data) +sysprof_turbostat_finalize (SysprofTurbostat *self) { - SysprofTurbostat *self = data; - if (self->pid != 0) sysprof_turbostat_stop (self); g_assert (self->pid == 0); g_assert (self->channel == NULL); + + g_slice_free (SysprofTurbostat, self); +} + +SysprofTurbostat * +sysprof_turbostat_ref (SysprofTurbostat *self) +{ + g_return_val_if_fail (self != NULL, NULL); + g_return_val_if_fail (self->ref_count > 0, NULL); + + g_atomic_int_inc (&self->ref_count); + + return self; } void -sysprof_turbostat_free (SysprofTurbostat *self) +sysprof_turbostat_unref (SysprofTurbostat *self) { - g_rc_box_release_full (self, sysprof_turbostat_finalize); + g_return_if_fail (self != NULL); + g_return_if_fail (self->ref_count > 1); + + if (g_atomic_int_dec_and_test (&self->ref_count)) + sysprof_turbostat_finalize (self); } static gboolean diff --git a/src/sysprofd/sysprof-turbostat.h b/src/sysprofd/sysprof-turbostat.h index 3b94e75b..200e6ad4 100644 --- a/src/sysprofd/sysprof-turbostat.h +++ b/src/sysprofd/sysprof-turbostat.h @@ -43,8 +43,9 @@ gboolean sysprof_turbostat_start (SysprofTurbostat *self, void sysprof_turbostat_stop (SysprofTurbostat *self); gboolean sysprof_turbostat_sample (SysprofTurbostat *self, GError **error); -void sysprof_turbostat_free (SysprofTurbostat *self); +SysprofTurbostat *sysprof_turbostat_ref (SysprofTurbostat *self); +void sysprof_turbostat_unref (SysprofTurbostat *self); -G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofTurbostat, sysprof_turbostat_free) +G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofTurbostat, sysprof_turbostat_unref) G_END_DECLS