From df66e484a734c9ac6182855ad0760540ec799c9e Mon Sep 17 00:00:00 2001 From: Georges Basile Stavracas Neto Date: Tue, 22 Aug 2023 17:12:20 -0300 Subject: [PATCH] core: Add global threading utility For now, we only store what's the main thread of the program. --- src/core/gcal-core-macros.h | 31 ++++++++++++++++++++++++ src/core/gcal-global.c | 47 +++++++++++++++++++++++++++++++++++++ src/core/gcal-global.h | 30 +++++++++++++++++++++++ src/core/meson.build | 1 + 4 files changed, 109 insertions(+) create mode 100644 src/core/gcal-core-macros.h create mode 100644 src/core/gcal-global.c create mode 100644 src/core/gcal-global.h diff --git a/src/core/gcal-core-macros.h b/src/core/gcal-core-macros.h new file mode 100644 index 00000000..b5392de6 --- /dev/null +++ b/src/core/gcal-core-macros.h @@ -0,0 +1,31 @@ +/* + * gcal-core-macros.h + * + * Copyright 2023 Georges Basile Stavracas Neto + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include "gcal-global.h" + +G_BEGIN_DECLS + +#define GCAL_IS_THREAD(thread_) (g_thread_self () == (thread_)) +#define GCAL_IS_MAIN_THREAD() (g_thread_self () == gcal_get_main_thread ()) + +G_END_DECLS diff --git a/src/core/gcal-global.c b/src/core/gcal-global.c new file mode 100644 index 00000000..231e5f89 --- /dev/null +++ b/src/core/gcal-global.c @@ -0,0 +1,47 @@ +/* + * gcal-global.c + * + * Copyright 2023 Georges Basile Stavracas Neto + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "gcal-global.h" +#include "gconstructor.h" + +static GThread *main_thread; + +#if defined (G_HAS_CONSTRUCTORS) +# ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA +# pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gcal_init_ctor) +# endif +G_DEFINE_CONSTRUCTOR(gcal_init_ctor) +#else +# error Your platform/compiler is missing constructor support +#endif + +static void +gcal_init_ctor (void) +{ + main_thread = g_thread_self (); +} + +GThread * +gcal_get_main_thread (void) +{ + return main_thread; +} + diff --git a/src/core/gcal-global.h b/src/core/gcal-global.h new file mode 100644 index 00000000..69ba1e73 --- /dev/null +++ b/src/core/gcal-global.h @@ -0,0 +1,30 @@ +/* + * gcal-global.h + * + * Copyright 2023 Georges Basile Stavracas Neto + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +G_BEGIN_DECLS + +GThread * gcal_get_main_thread (void); + +G_END_DECLS diff --git a/src/core/meson.build b/src/core/meson.build index ef39bbc8..de16a737 100644 --- a/src/core/meson.build +++ b/src/core/meson.build @@ -6,6 +6,7 @@ sources += files( 'gcal-clock.c', 'gcal-context.c', 'gcal-event.c', + 'gcal-global.c', 'gcal-log.c', 'gcal-manager.c', 'gcal-range.c',