From: Lennart Poettering Date: Mon, 1 Aug 2011 03:05:12 +0000 (+0200) Subject: util: add is_main_thread() call X-Git-Tag: v33~27 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=b636465bc0190448f960791cd059c4dd1fa37717;hp=cd0ed1db9b58900959866e7b265fae2b153b68c0 util: add is_main_thread() call --- diff --git a/src/util.c b/src/util.c index 6382c01c5..4e2a4fab8 100644 --- a/src/util.c +++ b/src/util.c @@ -5536,6 +5536,15 @@ char *join(const char *x, ...) { return r; } +bool is_main_thread(void) { + static __thread int cached = 0; + + if (_unlikely_(cached == 0)) + cached = getpid() == gettid() ? 1 : -1; + + return cached > 0; +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime", diff --git a/src/util.h b/src/util.h index eb0061cdb..407160d52 100644 --- a/src/util.h +++ b/src/util.h @@ -460,6 +460,8 @@ int get_files_in_directory(const char *path, char ***list); char *join(const char *x, ...) _sentinel_; +bool is_main_thread(void); + #define NULSTR_FOREACH(i, l) \ for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)