core/monitor: Assert we're in the right thread

Right now everything works as expected, but this should catch cases
where we're running functions on the wrong thread.
This commit is contained in:
Georges Basile Stavracas Neto
2023-08-22 17:25:56 -03:00
parent df66e484a7
commit 9e341d87e4
+44
View File
@@ -21,6 +21,7 @@
#define G_LOG_DOMAIN "GcalCalendarMonitor" #define G_LOG_DOMAIN "GcalCalendarMonitor"
#include "gcal-calendar-monitor.h" #include "gcal-calendar-monitor.h"
#include "gcal-core-macros.h"
#include "gcal-date-time-utils.h" #include "gcal-date-time-utils.h"
#include "gcal-debug.h" #include "gcal-debug.h"
#include "gcal-event.h" #include "gcal-event.h"
@@ -188,6 +189,8 @@ add_events_in_idle (GcalCalendarMonitor *self,
{ {
IdleData *idle_data; IdleData *idle_data;
g_assert (GCAL_IS_THREAD (self->thread));
idle_data = g_new0 (IdleData, 1); idle_data = g_new0 (IdleData, 1);
idle_data->monitor = g_object_ref (self); idle_data->monitor = g_object_ref (self);
idle_data->events = g_ptr_array_ref (events); idle_data->events = g_ptr_array_ref (events);
@@ -205,6 +208,8 @@ update_events_in_idle (GcalCalendarMonitor *self,
{ {
IdleData *idle_data; IdleData *idle_data;
g_assert (GCAL_IS_THREAD (self->thread));
idle_data = g_new0 (IdleData, 1); idle_data = g_new0 (IdleData, 1);
idle_data->monitor = g_object_ref (self); idle_data->monitor = g_object_ref (self);
idle_data->events = g_ptr_array_ref (events); idle_data->events = g_ptr_array_ref (events);
@@ -222,6 +227,8 @@ remove_events_in_idle (GcalCalendarMonitor *self,
{ {
IdleData *idle_data; IdleData *idle_data;
g_assert (GCAL_IS_THREAD (self->thread));
idle_data = g_new0 (IdleData, 1); idle_data = g_new0 (IdleData, 1);
idle_data->monitor = g_object_ref (self); idle_data->monitor = g_object_ref (self);
idle_data->event_ids = g_ptr_array_ref (events); idle_data->event_ids = g_ptr_array_ref (events);
@@ -239,6 +246,8 @@ set_complete_in_idle (GcalCalendarMonitor *self,
{ {
IdleData *idle_data; IdleData *idle_data;
g_assert (GCAL_IS_THREAD (self->thread));
idle_data = g_new0 (IdleData, 1); idle_data = g_new0 (IdleData, 1);
idle_data->monitor = g_object_ref (self); idle_data->monitor = g_object_ref (self);
idle_data->complete = complete; idle_data->complete = complete;
@@ -273,6 +282,8 @@ client_instance_generated_cb (ICalComponent *icomponent,
data = user_data; data = user_data;
self = data->monitor; self = data->monitor;
g_assert (GCAL_IS_THREAD (self->thread));
if (g_cancellable_set_error_if_cancelled (cancellable, error)) if (g_cancellable_set_error_if_cancelled (cancellable, error))
return FALSE; return FALSE;
@@ -306,6 +317,8 @@ on_client_view_objects_added_cb (ECalClientView *view,
GCAL_ENTRY; GCAL_ENTRY;
g_assert (GCAL_IS_THREAD (self->thread));
range = get_monitor_ranges (self); range = get_monitor_ranges (self);
maybe_init_event_arrays (self); maybe_init_event_arrays (self);
@@ -444,6 +457,8 @@ on_client_view_objects_modified_cb (ECalClientView *view,
GCAL_ENTRY; GCAL_ENTRY;
g_assert (GCAL_IS_THREAD (self->thread));
if (!self->monitor_thread.populated && self->monitor_thread.events_to_add) if (!self->monitor_thread.populated && self->monitor_thread.events_to_add)
{ {
g_clear_pointer (&self->monitor_thread.events_to_add, g_ptr_array_unref); g_clear_pointer (&self->monitor_thread.events_to_add, g_ptr_array_unref);
@@ -640,6 +655,8 @@ on_client_view_objects_removed_cb (ECalClientView *view,
GCAL_ENTRY; GCAL_ENTRY;
g_assert (GCAL_IS_THREAD (self->thread));
event_ids = g_ptr_array_new_with_free_func (g_free); event_ids = g_ptr_array_new_with_free_func (g_free);
for (l = objects; l; l = l->next) for (l = objects; l; l = l->next)
@@ -701,6 +718,7 @@ on_client_view_complete_cb (ECalClientView *view,
GCAL_ENTRY; GCAL_ENTRY;
g_assert (GCAL_IS_THREAD (self->thread));
g_assert (!self->monitor_thread.populated); g_assert (!self->monitor_thread.populated);
events_to_add = g_steal_pointer (&self->monitor_thread.events_to_add); events_to_add = g_steal_pointer (&self->monitor_thread.events_to_add);
@@ -728,6 +746,8 @@ create_view (GcalCalendarMonitor *self)
GCAL_ENTRY; GCAL_ENTRY;
g_assert (GCAL_IS_THREAD (self->thread));
reader_locker = g_rw_lock_reader_locker_new (&self->shared.lock); reader_locker = g_rw_lock_reader_locker_new (&self->shared.lock);
g_assert (self->cancellable == NULL); g_assert (self->cancellable == NULL);
@@ -788,6 +808,8 @@ remove_view (GcalCalendarMonitor *self)
GCAL_ENTRY; GCAL_ENTRY;
g_assert (GCAL_IS_THREAD (self->thread));
g_clear_object (&self->cancellable); g_clear_object (&self->cancellable);
if (!self->monitor_thread.view) if (!self->monitor_thread.view)
@@ -826,6 +848,8 @@ message_queue_source_prepare (GSource *source,
queue_source = (MessageQueueSource*) source; queue_source = (MessageQueueSource*) source;
self = queue_source->monitor; self = queue_source->monitor;
g_assert (GCAL_IS_THREAD (self->thread));
return g_async_queue_length (self->messages) > 0; return g_async_queue_length (self->messages) > 0;
} }
@@ -844,6 +868,8 @@ message_queue_source_dispatch (GSource *source,
self = queue_source->monitor; self = queue_source->monitor;
event = GPOINTER_TO_INT (g_async_queue_pop (self->messages)); event = GPOINTER_TO_INT (g_async_queue_pop (self->messages));
g_assert (GCAL_IS_THREAD (self->thread));
switch (event) switch (event)
{ {
case FILTER_UPDATED: case FILTER_UPDATED:
@@ -928,6 +954,8 @@ static void
notify_view_thread (GcalCalendarMonitor *self, notify_view_thread (GcalCalendarMonitor *self,
MonitorThreadEvent event) MonitorThreadEvent event)
{ {
g_assert (GCAL_IS_MAIN_THREAD ());
g_async_queue_push (self->messages, GINT_TO_POINTER (event)); g_async_queue_push (self->messages, GINT_TO_POINTER (event));
g_main_context_wakeup (self->thread_context); g_main_context_wakeup (self->thread_context);
} }
@@ -937,6 +965,8 @@ maybe_spawn_view_thread (GcalCalendarMonitor *self)
{ {
g_autofree gchar *thread_name = NULL; g_autofree gchar *thread_name = NULL;
g_assert (GCAL_IS_MAIN_THREAD ());
if (self->thread || !self->shared.range) if (self->thread || !self->shared.range)
return; return;
@@ -955,6 +985,8 @@ remove_events_outside_range (GcalCalendarMonitor *self,
GHashTableIter iter; GHashTableIter iter;
GcalEvent *event; GcalEvent *event;
g_assert (GCAL_IS_MAIN_THREAD ());
GCAL_TRACE_MSG ("Removing events outside range from monitor"); GCAL_TRACE_MSG ("Removing events outside range from monitor");
writer_locker = g_rw_lock_writer_locker_new (&self->shared.lock); writer_locker = g_rw_lock_writer_locker_new (&self->shared.lock);
@@ -984,6 +1016,8 @@ remove_all_events (GcalCalendarMonitor *self)
g_autoptr (GRWLockWriterLocker) writer_locker = NULL; g_autoptr (GRWLockWriterLocker) writer_locker = NULL;
g_autoptr (GPtrArray) events_to_remove = NULL; g_autoptr (GPtrArray) events_to_remove = NULL;
g_assert (GCAL_IS_MAIN_THREAD ());
GCAL_TRACE_MSG ("Removing all events from view"); GCAL_TRACE_MSG ("Removing all events from view");
writer_locker = g_rw_lock_writer_locker_new (&self->shared.lock); writer_locker = g_rw_lock_writer_locker_new (&self->shared.lock);
@@ -998,6 +1032,8 @@ static void
set_complete (GcalCalendarMonitor *self, set_complete (GcalCalendarMonitor *self,
gboolean complete) gboolean complete)
{ {
g_assert (GCAL_IS_MAIN_THREAD ());
if (self->complete == complete) if (self->complete == complete)
return; return;
@@ -1023,6 +1059,8 @@ add_events_to_timeline_in_idle_cb (gpointer user_data)
GCAL_ENTRY; GCAL_ENTRY;
g_assert (GCAL_IS_MAIN_THREAD ());
idle_data = user_data; idle_data = user_data;
self = idle_data->monitor; self = idle_data->monitor;
events = idle_data->events; events = idle_data->events;
@@ -1064,6 +1102,8 @@ update_events_in_idle_cb (gpointer user_data)
GCAL_ENTRY; GCAL_ENTRY;
g_assert (GCAL_IS_MAIN_THREAD ());
idle_data = user_data; idle_data = user_data;
self = idle_data->monitor; self = idle_data->monitor;
events = g_steal_pointer (&idle_data->events); events = g_steal_pointer (&idle_data->events);
@@ -1113,6 +1153,8 @@ remove_events_from_timeline_in_idle_cb (gpointer user_data)
GCAL_ENTRY; GCAL_ENTRY;
g_assert (GCAL_IS_MAIN_THREAD ());
idle_data = user_data; idle_data = user_data;
self = idle_data->monitor; self = idle_data->monitor;
event_ids = g_steal_pointer (&idle_data->event_ids); event_ids = g_steal_pointer (&idle_data->event_ids);
@@ -1168,6 +1210,8 @@ complete_in_idle_cb (gpointer user_data)
GCAL_ENTRY; GCAL_ENTRY;
g_assert (GCAL_IS_MAIN_THREAD ());
idle_data = user_data; idle_data = user_data;
self = idle_data->monitor; self = idle_data->monitor;
g_assert (idle_data->events == NULL); g_assert (idle_data->events == NULL);