aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/thread.cpp
blob: 8b56b2302c978a67caaf3b0fa493953b0b0cebbc (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include "tls.h"
#include "thread.h"
#include "thread.i"

#include <util/generic/ptr.h>
#include <util/generic/ymath.h>
#include <util/generic/ylimits.h>
#include <util/generic/yexception.h>
#include "yassert.h"
#include <utility>

#if defined(_glibc_) 
    #if !__GLIBC_PREREQ(2, 30) 
        #include <sys/syscall.h> 
    #endif 
#endif 
 
#if defined(_unix_) 
    #include <pthread.h>
    #include <sys/types.h> 
#elif defined(_win_) 
    #include "dynlib.h"
    #include <util/charset/wide.h>
    #include <util/generic/scope.h>
#else 
    #error "FIXME" 
#endif

bool SetHighestThreadPriority() {
#ifdef _win_
    return SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
#else
    struct sched_param sch;
    memset(&sch, 0, sizeof(sch));
    sch.sched_priority = 31;
    return pthread_setschedparam(pthread_self(), SCHED_RR, &sch) == 0;
#endif
}

namespace {
    using TParams = TThread::TParams;
    using TId = TThread::TId;

    inline void SetThrName(const TParams& p) {
        try {
            if (p.Name) {
                TThread::SetCurrentThreadName(p.Name.data());
            }
        } catch (...) {
            // ¯\_(ツ)_/¯
        }
    }

    inline size_t StackSize(const TParams& p) noexcept {
        if (p.StackSize) {
            return FastClp2(p.StackSize);
        }

        return 0;
    }

#if defined(_win_)
    class TWinThread {
        struct TMyParams: public TParams, public TThrRefBase {
            inline TMyParams(const TParams& p)
                : TParams(p)
                , Result(0)
            {
            }

            void* Result;
        };

        using TParamsRef = TIntrusivePtr<TMyParams>;

    public:
        inline TWinThread(const TParams& params)
            : P_(new TMyParams(params))
            , Handle(0)
    #if _WIN32_WINNT < 0x0502
            , ThreadId(0)
    #endif
        {
        }

        inline bool Running() const noexcept {
            return Handle != 0;
        }

        inline TId SystemThreadId() const noexcept {
    #if _WIN32_WINNT < 0x0502
            return (TId)ThreadId;
    #else
            return (TId)GetThreadId(Handle);
    #endif
        }

        inline void* Join() {
            ::WaitForSingleObject(Handle, INFINITE);
            ::CloseHandle(Handle);

            return P_->Result;
        }

        inline void Detach() {
            ::CloseHandle(Handle);
        }

        static ui32 __stdcall Proxy(void* ptr) {
            NTls::TCleaner cleaner;

            (void)cleaner;

            {
                TParamsRef p((TMyParams*)(ptr));

                //drop counter, gotten in Start()
                p->UnRef();

                SetThrName(*p);
                p->Result = p->Proc(p->Data);
            }

            return 0;
        }

        inline void Start() {
            //do not do this, kids, at home
            P_->Ref();
    #if _WIN32_WINNT < 0x0502
            Handle = reinterpret_cast<HANDLE>(::_beginthreadex(nullptr, (unsigned)StackSize(*P_), Proxy, (void*)P_.Get(), 0, &ThreadId));
    #else
            Handle = reinterpret_cast<HANDLE>(::_beginthreadex(nullptr, (unsigned)StackSize(*P_), Proxy, (void*)P_.Get(), 0, nullptr));
    #endif

            if (!Handle) {
                P_->UnRef();
                ythrow yexception() << "failed to create a thread";
            }
        }

    private:
        TParamsRef P_;
        HANDLE Handle;
    #if _WIN32_WINNT < 0x0502
        ui32 ThreadId;
    #endif
    };

    using TThreadBase = TWinThread;
#else
    //unix

    #define PCHECK(x, y)                                    \
        {                                                   \
            const int err_ = x;                             \
            if (err_) {                                     \
                ythrow TSystemError(err_) << TStringBuf(y); \
            }                                               \
        }

    class TPosixThread {
    public:
        inline TPosixThread(const TParams& params)
            : P_(new TParams(params))
            , H_()
        {
            static_assert(sizeof(H_) == sizeof(TId), "expect sizeof(H_) == sizeof(TId)");
        }

        inline TId SystemThreadId() const noexcept {
            return (TId)H_;
        }

        inline void* Join() {
            void* tec = nullptr;
            PCHECK(pthread_join(H_, &tec), "can not join thread");

            return tec;
        }

        inline void Detach() {
            PCHECK(pthread_detach(H_), "can not detach thread");
        }

        inline bool Running() const noexcept {
            return (bool)H_;
        }

        inline void Start() {
            pthread_attr_t* pattrs = nullptr;
            pthread_attr_t attrs;

            if (P_->StackSize > 0) {
                Zero(attrs);
                pthread_attr_init(&attrs);
                pattrs = &attrs;

                if (P_->StackPointer) {
                    pthread_attr_setstack(pattrs, P_->StackPointer, P_->StackSize);
                } else {
                    pthread_attr_setstacksize(pattrs, StackSize(*P_));
                }
            }

            {
                TParams* holdP = P_.Release();
                int err = pthread_create(&H_, pattrs, ThreadProxy, holdP);
                if (err) {
                    H_ = {};
                    P_.Reset(holdP);
                    PCHECK(err, "failed to create thread");
                }
            }
        }

    private:
        static void* ThreadProxy(void* arg) {
            THolder<TParams> p((TParams*)arg);

            SetThrName(*p);

            return p->Proc(p->Data);
        }

    private:
        THolder<TParams> P_;
        pthread_t H_;
    };

    #undef PCHECK

    using TThreadBase = TPosixThread;
#endif

    template <class T>
    static inline typename T::TValueType* Impl(T& t, const char* op, bool check = true) {
        if (!t) {
            ythrow yexception() << "can not " << op << " dead thread";
        }

        if (t->Running() != check) {
            static const char* const msg[] = {"running", "not running"};

            ythrow yexception() << "can not " << op << " " << msg[check] << " thread";
        }

        return t.Get();
    }
}

class TThread::TImpl: public TThreadBase {
public:
    inline TImpl(const TParams& params, THolder<TCallableBase> callable = {})
        : TThreadBase(params)
        , Callable_(std::move(callable))
    {
    }

    inline TId Id() const noexcept {
        return ThreadIdHashFunction(SystemThreadId());
    }

    static THolder<TImpl> Create(THolder<TCallableBase> callable) {
        TParams params(TCallableBase::ThreadWorker, callable.Get());
        return MakeHolder<TImpl>(std::move(params), std::move(callable));
    }

private:
    THolder<TCallableBase> Callable_;
};

TThread::TThread(const TParams& p)
    : Impl_(new TImpl(p))
{
}

TThread::TThread(TThreadProc threadProc, void* param)
    : Impl_(new TImpl(TParams(threadProc, param)))
{
}

TThread::TThread(TPrivateCtor, THolder<TCallableBase> callable)
    : Impl_(TImpl::Create(std::move(callable)))
{
}

TThread::~TThread() {
    Join();
}

void TThread::Start() {
    Impl(Impl_, "start", false)->Start();
}

void* TThread::Join() {
    if (Running()) {
        void* ret = Impl_->Join();

        Impl_.Destroy();

        return ret;
    }

    return nullptr;
}

void TThread::Detach() {
    if (Running()) {
        Impl_->Detach();
        Impl_.Destroy();
    }
}

bool TThread::Running() const noexcept {
    return Impl_ && Impl_->Running();
}

TThread::TId TThread::Id() const noexcept {
    if (Running()) {
        return Impl_->Id();
    }

    return ImpossibleThreadId();
}

TThread::TId TThread::CurrentThreadId() noexcept {
    return SystemCurrentThreadId();
}

TThread::TId TThread::CurrentThreadNumericId() noexcept { 
#if defined(_win_) 
    return GetCurrentThreadId(); 
#elif defined(_darwin_) 
    // There is no gettid() on MacOS and SYS_gettid returns completely unrelated numbers. 
    // See: http://elliotth.blogspot.com/2012/04/gettid-on-mac-os.html 
    uint64_t threadId; 
    pthread_threadid_np(nullptr, &threadId); 
    return threadId; 
#elif defined(_musl_) || defined(_bionic_) 
    // both musl and android libc provide gettid() function 
    return gettid(); 
#elif defined(_glibc_) 
    #if __GLIBC_PREREQ(2, 30) 
    return gettid(); 
    #else 
    // gettid() was introduced in glibc=2.30, previous versions lack neat syscall wrapper 
    return syscall(SYS_gettid); 
    #endif 
#else 
    #error "Implement me" 
#endif 
} 
 
TThread::TId TThread::ImpossibleThreadId() noexcept {
    return Max<TThread::TId>();
}

namespace {
    template <class T>
    static void* ThreadProcWrapper(void* param) {
        return reinterpret_cast<T*>(param)->ThreadProc();
    }
}

ISimpleThread::ISimpleThread(size_t stackSize)
    : TThread(TParams(ThreadProcWrapper<ISimpleThread>, reinterpret_cast<void*>(this), stackSize))
{
}

#if defined(_MSC_VER)
    // This beautiful piece of code is borrowed from
    // http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx

    //
    // Usage: WindowsCurrentSetThreadName (-1, "MainThread");
    //
    #include <windows.h>
    #include <processthreadsapi.h>

const DWORD MS_VC_EXCEPTION = 0x406D1388;

    #pragma pack(push, 8)
typedef struct tagTHREADNAME_INFO {
    DWORD dwType;     // Must be 0x1000.
    LPCSTR szName;    // Pointer to name (in user addr space).
    DWORD dwThreadID; // Thread ID (-1=caller thread).
    DWORD dwFlags;    // Reserved for future use, must be zero.
} THREADNAME_INFO;
    #pragma pack(pop)

static void WindowsCurrentSetThreadName(DWORD dwThreadID, const char* threadName) {
    THREADNAME_INFO info;
    info.dwType = 0x1000;
    info.szName = threadName;
    info.dwThreadID = dwThreadID;
    info.dwFlags = 0;

    __try {
        RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
    } __except (EXCEPTION_EXECUTE_HANDLER) {
    }
}
#endif

#if defined(_win_)
namespace {
    struct TWinThreadDescrAPI {
        TWinThreadDescrAPI()
            : Kernel32Dll("kernel32.dll")
            , SetThreadDescription((TSetThreadDescription)Kernel32Dll.SymOptional("SetThreadDescription"))
            , GetThreadDescription((TGetThreadDescription)Kernel32Dll.SymOptional("GetThreadDescription"))
        {
        }

        // This API is for Windows 10+ only:
        // https://msdn.microsoft.com/en-us/library/windows/desktop/mt774972(v=vs.85).aspx
        bool HasAPI() noexcept {
            return SetThreadDescription && GetThreadDescription;
        }

        // Should always succeed, unless something very strange is passed in `descr'
        void SetDescr(const char* descr) {
            auto hr = SetThreadDescription(GetCurrentThread(), (const WCHAR*)UTF8ToWide(descr).data());
            Y_VERIFY(SUCCEEDED(hr), "SetThreadDescription failed");
        }

        TString GetDescr() {
            PWSTR wideName;
            auto hr = GetThreadDescription(GetCurrentThread(), &wideName);
            Y_VERIFY(SUCCEEDED(hr), "GetThreadDescription failed");
            Y_DEFER {
                LocalFree(wideName);
            };
            return WideToUTF8((const wchar16*)wideName);
        }

        typedef HRESULT(__cdecl* TSetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription);
        typedef HRESULT(__cdecl* TGetThreadDescription)(HANDLE hThread, PWSTR* ppszThreadDescription);

        TDynamicLibrary Kernel32Dll;
        TSetThreadDescription SetThreadDescription;
        TGetThreadDescription GetThreadDescription;
    };
}
#endif // _win_

void TThread::SetCurrentThreadName(const char* name) {
    (void)name;

#if defined(_freebsd_)
    pthread_t thread = pthread_self();
    pthread_set_name_np(thread, name);
#elif defined(_linux_)
    prctl(PR_SET_NAME, name, 0, 0, 0);
#elif defined(_darwin_)
    pthread_setname_np(name);
#elif defined(_win_)
    auto api = Singleton<TWinThreadDescrAPI>();
    if (api->HasAPI()) {
        api->SetDescr(name);
    } else {
    #if defined(_MSC_VER)
        WindowsCurrentSetThreadName(DWORD(-1), name);
    #endif
    }
#else
// no idea
#endif // OS
}

TString TThread::CurrentThreadName() {
#if defined(_freebsd_)
// TODO: check pthread_get_name_np API availability
#elif defined(_linux_)
    // > The buffer should allow space for up to 16 bytes; the returned string  will be
    // > null-terminated.
    // via `man prctl`
    char name[16];
    memset(name, 0, sizeof(name));
    Y_VERIFY(prctl(PR_GET_NAME, name, 0, 0, 0) == 0, "pctl failed: %s", strerror(errno));
    return name;
#elif defined(_darwin_)
    // available on Mac OS 10.6+
    const auto thread = pthread_self();
    char name[256];
    memset(name, 0, sizeof(name));
    Y_VERIFY(pthread_getname_np(thread, name, sizeof(name)) == 0, "pthread_getname_np failed: %s", strerror(errno));
    return name;
#elif defined(_win_)
    auto api = Singleton<TWinThreadDescrAPI>();
    if (api->HasAPI()) {
        return api->GetDescr();
    }
    return {};
#else
// no idea
#endif // OS

    return {};
}

bool TThread::CanGetCurrentThreadName() {
#if defined(_linux_) || defined(_darwin_)
    return true;
#elif defined(_win_)
    return Singleton<TWinThreadDescrAPI>()->HasAPI();
#else
    return false;
#endif // OS
}

TCurrentThreadLimits::TCurrentThreadLimits() noexcept
    : StackBegin(nullptr)
    , StackLength(0)
{
#if defined(_linux_) || defined(_cygwin_) || defined(_freebsd_)
    pthread_attr_t attr;
    pthread_attr_init(&attr);

    #if defined(_linux_) || defined(_cygwin_)
    Y_VERIFY(pthread_getattr_np(pthread_self(), &attr) == 0, "pthread_getattr failed");
    #else
    Y_VERIFY(pthread_attr_get_np(pthread_self(), &attr) == 0, "pthread_attr_get_np failed");
    #endif
    pthread_attr_getstack(&attr, (void**)&StackBegin, &StackLength);
    pthread_attr_destroy(&attr);

#elif defined(_darwin_)
    StackBegin = pthread_get_stackaddr_np(pthread_self());
    StackLength = pthread_get_stacksize_np(pthread_self());
#elif defined(_MSC_VER)

    #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
    ULONG_PTR b = 0;
    ULONG_PTR e = 0;

    GetCurrentThreadStackLimits(&b, &e);

    StackBegin = (const void*)b;
    StackLength = e - b;

    #else
    // Copied from https://github.com/llvm-mirror/compiler-rt/blob/release_40/lib/sanitizer_common/sanitizer_win.cc#L91
    void* place_on_stack = alloca(16);
    MEMORY_BASIC_INFORMATION memory_info;
    Y_VERIFY(VirtualQuery(place_on_stack, &memory_info, sizeof(memory_info)));

    StackBegin = memory_info.AllocationBase;
    StackLength = static_cast<const char*>(memory_info.BaseAddress) + memory_info.RegionSize - static_cast<const char*>(StackBegin);

    #endif

#else
    #error port me
#endif
}