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.
This commit is contained in:
Christian Hergert
2020-02-20 11:01:02 -08:00
parent 1017fed467
commit d89a689ab4
3 changed files with 32 additions and 14 deletions

View File

@ -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)

View File

@ -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

View File

@ -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