aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/thread.i
blob: bb906614023202c054274bef22a6d917f6f244f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//do not use directly
#pragma once
#include "platform.h"

#if defined(_win_)
    #include "winint.h"
    #include <process.h>

    typedef HANDLE THREADHANDLE;
#else
    #include <pthread.h>

    typedef pthread_t THREADHANDLE;
#endif

#if defined(_freebsd_)
    #include <pthread_np.h>
#endif

#include <util/digest/numeric.h>

static inline size_t SystemCurrentThreadIdImpl() noexcept {
    #if defined(_unix_)
        return (size_t)pthread_self();
    #elif defined(_win_)
        return (size_t)GetCurrentThreadId();
    #else
        #error todo
    #endif
}

template <class T>
static inline T ThreadIdHashFunction(T t) noexcept {
    /*
     * we must permute threadid bits, because some strange platforms(such Linux)
     * have strange threadid numeric properties
     *
     * Because they are alligned pointers to pthread_t rather that tid.
     * Currently there is no way to get tid without syscall (slightly slower)
     * (pthread_getthreadid_np is not implemeted in glibc/musl for some reason).
     */
    return IntHash(t);
}

static inline size_t SystemCurrentThreadId() noexcept {
    return ThreadIdHashFunction(SystemCurrentThreadIdImpl());
}