summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/platform/windows/win_thread.c
blob: 09cf0c63f798249dbd4a456920ce9f3a3e6fd364 (plain)
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "platform_api_vmcore.h"
#include "platform_api_extension.h"

#define bh_assert(v) assert(v)

#define BH_SEM_COUNT_MAX 0xFFFF

struct os_thread_data;

typedef struct os_thread_wait_node {
    korp_sem sem;
    void *retval;
    os_thread_wait_list next;
} os_thread_wait_node;

typedef struct os_thread_data {
    /* Next thread data */
    struct os_thread_data *next;
    /* Thread data of parent thread */
    struct os_thread_data *parent;
    /* Thread Id */
    DWORD thread_id;
    /* Thread start routine */
    thread_start_routine_t start_routine;
    /* Thread start routine argument */
    void *arg;
    /* Wait node of current thread */
    os_thread_wait_node wait_node;
    /* Wait cond */
    korp_cond wait_cond;
    /* Wait lock */
    korp_mutex wait_lock;
    /* Waiting list of other threads who are joining this thread */
    os_thread_wait_list thread_wait_list;
    /* End node of the waiting list */
    os_thread_wait_node *thread_wait_list_end;
    /* Whether the thread has exited */
    bool thread_exited;
    /* Thread return value */
    void *thread_retval;
} os_thread_data;

static bool is_thread_sys_inited = false;

/* Thread data of supervisor thread */
static os_thread_data supervisor_thread_data;

/* Thread data list lock */
static korp_mutex thread_data_list_lock;

/* Thread data key */
static DWORD thread_data_key;

/* The GetCurrentThreadStackLimits API from "kernel32" */
static void(WINAPI *GetCurrentThreadStackLimits_Kernel32)(PULONG_PTR,
                                                          PULONG_PTR) = NULL;

int
os_sem_init(korp_sem *sem);
int
os_sem_destroy(korp_sem *sem);
int
os_sem_wait(korp_sem *sem);
int
os_sem_reltimed_wait(korp_sem *sem, uint64 useconds);
int
os_sem_signal(korp_sem *sem);

int
os_thread_sys_init()
{
    HMODULE module;

    if (is_thread_sys_inited)
        return BHT_OK;

    if ((thread_data_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
        return BHT_ERROR;

    /* Initialize supervisor thread data */
    memset(&supervisor_thread_data, 0, sizeof(os_thread_data));

    supervisor_thread_data.thread_id = GetCurrentThreadId();

    if (os_sem_init(&supervisor_thread_data.wait_node.sem) != BHT_OK)
        goto fail1;

    if (os_mutex_init(&supervisor_thread_data.wait_lock) != BHT_OK)
        goto fail2;

    if (os_cond_init(&supervisor_thread_data.wait_cond) != BHT_OK)
        goto fail3;

    if (!TlsSetValue(thread_data_key, &supervisor_thread_data))
        goto fail4;

    if (os_mutex_init(&thread_data_list_lock) != BHT_OK)
        goto fail5;

    if ((module = GetModuleHandle((LPCTSTR) "kernel32"))) {
        *(void **)&GetCurrentThreadStackLimits_Kernel32 =
            GetProcAddress(module, "GetCurrentThreadStackLimits");
    }

    is_thread_sys_inited = true;
    return BHT_OK;

fail5:
    TlsSetValue(thread_data_key, NULL);
fail4:
    os_cond_destroy(&supervisor_thread_data.wait_cond);
fail3:
    os_mutex_destroy(&supervisor_thread_data.wait_lock);
fail2:
    os_sem_destroy(&supervisor_thread_data.wait_node.sem);
fail1:
    TlsFree(thread_data_key);
    return BHT_ERROR;
}

void
os_thread_sys_destroy()
{
    if (is_thread_sys_inited) {
        os_thread_data *thread_data, *thread_data_next;

        thread_data = supervisor_thread_data.next;
        while (thread_data) {
            thread_data_next = thread_data->next;

            /* Destroy resources of thread data */
            os_cond_destroy(&thread_data->wait_cond);
            os_sem_destroy(&thread_data->wait_node.sem);
            os_mutex_destroy(&thread_data->wait_lock);
            BH_FREE(thread_data);

            thread_data = thread_data_next;
        }

        os_mutex_destroy(&thread_data_list_lock);
        os_cond_destroy(&supervisor_thread_data.wait_cond);
        os_mutex_destroy(&supervisor_thread_data.wait_lock);
        os_sem_destroy(&supervisor_thread_data.wait_node.sem);
        memset(&supervisor_thread_data, 0, sizeof(os_thread_data));
        TlsFree(thread_data_key);
        thread_data_key = 0;
        is_thread_sys_inited = false;
    }
}

static os_thread_data *
thread_data_current()
{
    return (os_thread_data *)TlsGetValue(thread_data_key);
}

static void
os_thread_cleanup(void *retval)
{
    os_thread_data *thread_data = thread_data_current();

    bh_assert(thread_data != NULL);

    os_mutex_lock(&thread_data->wait_lock);
    if (thread_data->thread_wait_list) {
        /* Signal each joining thread */
        os_thread_wait_list head = thread_data->thread_wait_list;
        while (head) {
            os_thread_wait_list next = head->next;
            head->retval = retval;
            os_sem_signal(&head->sem);
            head = next;
        }
        thread_data->thread_wait_list = thread_data->thread_wait_list_end =
            NULL;
    }
    /* Set thread status and thread return value */
    thread_data->thread_exited = true;
    thread_data->thread_retval = retval;
    os_mutex_unlock(&thread_data->wait_lock);
}

static unsigned __stdcall os_thread_wrapper(void *arg)
{
    os_thread_data *thread_data = arg;
    os_thread_data *parent = thread_data->parent;
    void *retval;
    bool result;

#if 0
    os_printf("THREAD CREATED %p\n", thread_data);
#endif

    os_mutex_lock(&parent->wait_lock);
    thread_data->thread_id = GetCurrentThreadId();
    result = TlsSetValue(thread_data_key, thread_data);
#ifdef OS_ENABLE_HW_BOUND_CHECK
    if (result)
        result = os_thread_signal_init() == 0 ? true : false;
#endif
    /* Notify parent thread */
    os_cond_signal(&parent->wait_cond);
    os_mutex_unlock(&parent->wait_lock);

    if (!result)
        return -1;

    retval = thread_data->start_routine(thread_data->arg);

    os_thread_cleanup(retval);
    return 0;
}

int
os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
                           void *arg, unsigned int stack_size, int prio)
{
    os_thread_data *parent = thread_data_current();
    os_thread_data *thread_data;

    if (!p_tid || !start)
        return BHT_ERROR;

    if (stack_size < BH_APPLET_PRESERVED_STACK_SIZE)
        stack_size = BH_APPLET_PRESERVED_STACK_SIZE;

    if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
        return BHT_ERROR;

    memset(thread_data, 0, sizeof(os_thread_data));
    thread_data->parent = parent;
    thread_data->start_routine = start;
    thread_data->arg = arg;

    if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
        goto fail1;

    if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
        goto fail2;

    if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
        goto fail3;

    os_mutex_lock(&parent->wait_lock);
    if (!_beginthreadex(NULL, stack_size, os_thread_wrapper, thread_data, 0,
                        NULL)) {
        os_mutex_unlock(&parent->wait_lock);
        goto fail4;
    }

    /* Add thread data into thread data list */
    os_mutex_lock(&thread_data_list_lock);
    thread_data->next = supervisor_thread_data.next;
    supervisor_thread_data.next = thread_data;
    os_mutex_unlock(&thread_data_list_lock);

    /* Wait for the thread routine to set thread_data's tid
       and add thread_data to thread data list */
    os_cond_wait(&parent->wait_cond, &parent->wait_lock);
    os_mutex_unlock(&parent->wait_lock);

    *p_tid = (korp_tid)thread_data;
    return BHT_OK;

fail4:
    os_cond_destroy(&thread_data->wait_cond);
fail3:
    os_mutex_destroy(&thread_data->wait_lock);
fail2:
    os_sem_destroy(&thread_data->wait_node.sem);
fail1:
    BH_FREE(thread_data);
    return BHT_ERROR;
}

int
os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
                 unsigned int stack_size)
{
    return os_thread_create_with_prio(tid, start, arg, stack_size,
                                      BH_THREAD_DEFAULT_PRIORITY);
}

korp_tid
os_self_thread()
{
    return (korp_tid)TlsGetValue(thread_data_key);
}

int
os_thread_join(korp_tid thread, void **p_retval)
{
    os_thread_data *thread_data, *curr_thread_data;

    /* Get thread data of current thread */
    curr_thread_data = thread_data_current();
    curr_thread_data->wait_node.next = NULL;

    /* Get thread data of thread to join */
    thread_data = (os_thread_data *)thread;
    bh_assert(thread_data);

    os_mutex_lock(&thread_data->wait_lock);

    if (thread_data->thread_exited) {
        /* Thread has exited */
        if (p_retval)
            *p_retval = thread_data->thread_retval;
        os_mutex_unlock(&thread_data->wait_lock);
        return BHT_OK;
    }

    /* Thread is running */
    if (!thread_data->thread_wait_list) { /* Waiting list is empty */
        thread_data->thread_wait_list = thread_data->thread_wait_list_end =
            &curr_thread_data->wait_node;
    }
    else { /* Waiting list isn't empty */
        /* Add to end of waiting list */
        thread_data->thread_wait_list_end->next = &curr_thread_data->wait_node;
        thread_data->thread_wait_list_end = &curr_thread_data->wait_node;
    }

    os_mutex_unlock(&thread_data->wait_lock);

    /* Wait the sem */
    os_sem_wait(&curr_thread_data->wait_node.sem);
    if (p_retval)
        *p_retval = curr_thread_data->wait_node.retval;
    return BHT_OK;
}

int
os_thread_detach(korp_tid thread)
{
    /* Do nothing */
    return BHT_OK;
    (void)thread;
}

void
os_thread_exit(void *retval)
{
    os_thread_cleanup(retval);
    _endthreadex(0);
}

int
os_thread_env_init()
{
    os_thread_data *thread_data = TlsGetValue(thread_data_key);

    if (thread_data)
        /* Already created */
        return BHT_OK;

    if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
        return BHT_ERROR;

    memset(thread_data, 0, sizeof(os_thread_data));
    thread_data->thread_id = GetCurrentThreadId();

    if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
        goto fail1;

    if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
        goto fail2;

    if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
        goto fail3;

    if (!TlsSetValue(thread_data_key, thread_data))
        goto fail4;

    return BHT_OK;

fail4:
    os_cond_destroy(&thread_data->wait_cond);
fail3:
    os_mutex_destroy(&thread_data->wait_lock);
fail2:
    os_sem_destroy(&thread_data->wait_node.sem);
fail1:
    BH_FREE(thread_data);
    return BHT_ERROR;
}

void
os_thread_env_destroy()
{
    os_thread_data *thread_data = TlsGetValue(thread_data_key);

    /* Note that supervisor_thread_data's resources will be destroyed
       by os_thread_sys_destroy() */
    if (thread_data && thread_data != &supervisor_thread_data) {
        TlsSetValue(thread_data_key, NULL);
        os_cond_destroy(&thread_data->wait_cond);
        os_mutex_destroy(&thread_data->wait_lock);
        os_sem_destroy(&thread_data->wait_node.sem);
        BH_FREE(thread_data);
    }
}

bool
os_thread_env_inited()
{
    os_thread_data *thread_data = TlsGetValue(thread_data_key);
    return thread_data ? true : false;
}

int
os_sem_init(korp_sem *sem)
{
    bh_assert(sem);
    *sem = CreateSemaphore(NULL, 0, BH_SEM_COUNT_MAX, NULL);
    return (*sem != NULL) ? BHT_OK : BHT_ERROR;
}

int
os_sem_destroy(korp_sem *sem)
{
    bh_assert(sem);
    CloseHandle(*sem);
    return BHT_OK;
}

int
os_sem_wait(korp_sem *sem)
{
    DWORD ret;

    bh_assert(sem);

    ret = WaitForSingleObject(*sem, INFINITE);

    if (ret == WAIT_OBJECT_0)
        return BHT_OK;
    else if (ret == WAIT_TIMEOUT)
        return (int)WAIT_TIMEOUT;
    else /* WAIT_FAILED or others */
        return BHT_ERROR;
}

int
os_sem_reltimed_wait(korp_sem *sem, uint64 useconds)
{
    uint64 mseconds_64;
    DWORD ret, mseconds;

    bh_assert(sem);

    if (useconds == BHT_WAIT_FOREVER)
        mseconds = INFINITE;
    else {
        mseconds_64 = useconds / 1000;

        if (mseconds_64 < (uint64)(UINT32_MAX - 1)) {
            mseconds = (uint32)mseconds_64;
        }
        else {
            mseconds = UINT32_MAX - 1;
            os_printf("Warning: os_sem_reltimed_wait exceeds limit, "
                      "set to max timeout instead\n");
        }
    }

    ret = WaitForSingleObject(*sem, mseconds);

    if (ret == WAIT_OBJECT_0)
        return BHT_OK;
    else if (ret == WAIT_TIMEOUT)
        return (int)WAIT_TIMEOUT;
    else /* WAIT_FAILED or others */
        return BHT_ERROR;
}

int
os_sem_signal(korp_sem *sem)
{
    bh_assert(sem);
    return ReleaseSemaphore(*sem, 1, NULL) != FALSE ? BHT_OK : BHT_ERROR;
}

int
os_mutex_init(korp_mutex *mutex)
{
    bh_assert(mutex);
    *mutex = CreateMutex(NULL, FALSE, NULL);
    return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
}

int
os_recursive_mutex_init(korp_mutex *mutex)
{
    bh_assert(mutex);
    *mutex = CreateMutex(NULL, FALSE, NULL);
    return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
}

int
os_mutex_destroy(korp_mutex *mutex)
{
    assert(mutex);
    return CloseHandle(*mutex) ? BHT_OK : BHT_ERROR;
}

int
os_mutex_lock(korp_mutex *mutex)
{
    int ret;

    assert(mutex);

    if (*mutex == NULL) { /* static initializer? */
        HANDLE p = CreateMutex(NULL, FALSE, NULL);

        if (!p) {
            return BHT_ERROR;
        }

        if (InterlockedCompareExchangePointer((PVOID *)mutex, (PVOID)p, NULL)
            != NULL) {
            /* lock has been created by other threads */
            CloseHandle(p);
        }
    }

    ret = WaitForSingleObject(*mutex, INFINITE);
    return ret != WAIT_FAILED ? BHT_OK : BHT_ERROR;
}

int
os_mutex_unlock(korp_mutex *mutex)
{
    bh_assert(mutex);
    return ReleaseMutex(*mutex) ? BHT_OK : BHT_ERROR;
}

int
os_cond_init(korp_cond *cond)
{
    bh_assert(cond);
    if (os_mutex_init(&cond->wait_list_lock) != BHT_OK)
        return BHT_ERROR;

    cond->thread_wait_list = cond->thread_wait_list_end = NULL;
    return BHT_OK;
}

int
os_cond_destroy(korp_cond *cond)
{
    bh_assert(cond);
    os_mutex_destroy(&cond->wait_list_lock);
    return BHT_OK;
}

static int
os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed,
                      uint64 useconds)
{
    os_thread_wait_node *node = &thread_data_current()->wait_node;

    node->next = NULL;

    bh_assert(cond);
    bh_assert(mutex);
    os_mutex_lock(&cond->wait_list_lock);
    if (!cond->thread_wait_list) { /* Waiting list is empty */
        cond->thread_wait_list = cond->thread_wait_list_end = node;
    }
    else { /* Waiting list isn't empty */
        /* Add to end of wait list */
        cond->thread_wait_list_end->next = node;
        cond->thread_wait_list_end = node;
    }
    os_mutex_unlock(&cond->wait_list_lock);

    /* Unlock mutex, wait sem and lock mutex again */
    os_mutex_unlock(mutex);
    int wait_result;
    if (timed)
        wait_result = os_sem_reltimed_wait(&node->sem, useconds);
    else
        wait_result = os_sem_wait(&node->sem);
    os_mutex_lock(mutex);

    /* Remove wait node from wait list */
    os_mutex_lock(&cond->wait_list_lock);
    if (cond->thread_wait_list == node) {
        cond->thread_wait_list = node->next;

        if (cond->thread_wait_list_end == node) {
            bh_assert(node->next == NULL);
            cond->thread_wait_list_end = NULL;
        }
    }
    else {
        /* Remove from the wait list */
        os_thread_wait_node *p = cond->thread_wait_list;
        while (p->next != node)
            p = p->next;
        p->next = node->next;

        if (cond->thread_wait_list_end == node) {
            cond->thread_wait_list_end = p;
        }
    }
    os_mutex_unlock(&cond->wait_list_lock);

    return wait_result;
}

int
os_cond_wait(korp_cond *cond, korp_mutex *mutex)
{
    return os_cond_wait_internal(cond, mutex, false, 0);
}

int
os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
{
    if (useconds == BHT_WAIT_FOREVER) {
        return os_cond_wait_internal(cond, mutex, false, 0);
    }
    else {
        return os_cond_wait_internal(cond, mutex, true, useconds);
    }
}

int
os_cond_signal(korp_cond *cond)
{
    /* Signal the head wait node of wait list */
    os_mutex_lock(&cond->wait_list_lock);
    if (cond->thread_wait_list)
        os_sem_signal(&cond->thread_wait_list->sem);
    os_mutex_unlock(&cond->wait_list_lock);

    return BHT_OK;
}

int
os_cond_broadcast(korp_cond *cond)
{
    /* Signal all of the wait node of wait list */
    os_mutex_lock(&cond->wait_list_lock);
    if (cond->thread_wait_list) {
        os_thread_wait_node *p = cond->thread_wait_list;
        while (p) {
            os_sem_signal(&p->sem);
            p = p->next;
        }
    }

    os_mutex_unlock(&cond->wait_list_lock);

    return BHT_OK;
}

static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;

static ULONG
GetCurrentThreadStackLimits_Win7(PULONG_PTR p_low_limit,
                                 PULONG_PTR p_high_limit)
{
    MEMORY_BASIC_INFORMATION mbi;
    NT_TIB *tib = (NT_TIB *)NtCurrentTeb();

    if (!tib) {
        os_printf("warning: NtCurrentTeb() failed\n");
        return -1;
    }

    *p_high_limit = (ULONG_PTR)tib->StackBase;

    if (VirtualQuery(tib->StackLimit, &mbi, sizeof(mbi))) {
        *p_low_limit = (ULONG_PTR)mbi.AllocationBase;
        return 0;
    }

    os_printf("warning: VirtualQuery() failed\n");
    return GetLastError();
}

uint8 *
os_thread_get_stack_boundary()
{
    ULONG_PTR low_limit = 0, high_limit = 0;
    uint32 page_size;

    if (thread_stack_boundary)
        return thread_stack_boundary;

    page_size = os_getpagesize();
    if (GetCurrentThreadStackLimits_Kernel32) {
        GetCurrentThreadStackLimits_Kernel32(&low_limit, &high_limit);
    }
    else {
        if (0 != GetCurrentThreadStackLimits_Win7(&low_limit, &high_limit))
            return NULL;
    }

    /* 4 pages are set unaccessible by system, we reserved
       one more page at least for safety */
    thread_stack_boundary = (uint8 *)(uintptr_t)low_limit + page_size * 5;
    return thread_stack_boundary;
}

#ifdef OS_ENABLE_HW_BOUND_CHECK
static os_thread_local_attribute bool thread_signal_inited = false;

int
os_thread_signal_init()
{
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
    ULONG StackSizeInBytes = 16 * 1024;
#endif
    bool ret;

    if (thread_signal_inited)
        return 0;

#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
    ret = SetThreadStackGuarantee(&StackSizeInBytes);
#else
    ret = true;
#endif
    if (ret)
        thread_signal_inited = true;
    return ret ? 0 : -1;
}

void
os_thread_signal_destroy()
{
    /* Do nothing */
}

bool
os_thread_signal_inited()
{
    return thread_signal_inited;
}
#endif