Commit 61e76e97 authored by Thomas Haller's avatar Thomas Haller

shared: add nm_utils_gettid() and NM_ASSERT_ON_MAIN_THREAD()

parent 953dd4ac
......@@ -27,6 +27,7 @@
#include <arpa/inet.h>
#include <poll.h>
#include <fcntl.h>
#include <sys/syscall.h>
/*****************************************************************************/
......@@ -38,6 +39,57 @@ const NMIPAddr nm_ip_addr_zero = { 0 };
/*****************************************************************************/
pid_t
nm_utils_gettid (void)
{
return (pid_t) syscall (SYS_gettid);
}
/* Used for asserting that this function is called on the main-thread.
* The main-thread is determined by remembering the thread-id
* of when the function was called the first time.
*
* When forking, the thread-id is again reset upon first call. */
gboolean
_nm_assert_on_main_thread (void)
{
G_LOCK_DEFINE_STATIC (lock);
static pid_t seen_tid;
static pid_t seen_pid;
pid_t tid;
pid_t pid;
gboolean success = FALSE;
tid = nm_utils_gettid ();
nm_assert (tid != 0);
G_LOCK (lock);
if (G_LIKELY (tid == seen_tid)) {
/* we don't care about false positives (when the process forked, and the thread-id
* is accidentally re-used) . It's for assertions only. */
success = TRUE;
} else {
pid = getpid ();
nm_assert (pid != 0);
if ( seen_tid == 0
|| seen_pid != pid) {
/* either this is the first time we call the function, or the process
* forked. In both cases, remember the thread-id. */
seen_tid = tid;
seen_pid = pid;
success = TRUE;
}
}
G_UNLOCK (lock);
return success;
}
/*****************************************************************************/
void
nm_utils_strbuf_append_c (char **buf, gsize *len, char c)
{
......
......@@ -26,6 +26,18 @@
/*****************************************************************************/
pid_t nm_utils_gettid (void);
gboolean _nm_assert_on_main_thread (void);
#if NM_MORE_ASSERTS > 5
#define NM_ASSERT_ON_MAIN_THREAD() G_STMT_START { nm_assert (_nm_assert_on_main_thread ()); } G_STMT_END
#else
#define NM_ASSERT_ON_MAIN_THREAD() G_STMT_START { ; } G_STMT_END
#endif
/*****************************************************************************/
static inline gboolean
_NM_INT_NOT_NEGATIVE (gssize val)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment