aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Posix/Thread.c
blob: 2c249d8317d1876174649f9712624e809654b995 (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
#include "Thread.h"

#include <limits.h>
#include <stdlib.h>


struct BH_ThreadContext
{
    BH_ThreadCallback callback;
    void *data;
};


static void *threadRun(void *context)
{
    BH_ThreadCallback callback;
    void *data;

    callback = ((struct BH_ThreadContext *)context)->callback;
    data = ((struct BH_ThreadContext *)context)->data;
    free(context);

    callback(data);
    BH_TssCleanup();

    pthread_exit(0);
}


static int threadInit(BH_Thread *thread,
                      size_t stack,
                      BH_ThreadCallback callback,
                      void *data)
{
    struct BH_ThreadContext *context;
    pthread_attr_t attributes;
    int result;

    context = malloc(sizeof(*context));
    if (!context)
        return BH_ERROR;
    context->callback = callback;
    context->data = data;

    /* Create thread with specified stack size */
    pthread_attr_init(&attributes);

    if (!stack)
        result = pthread_create(&thread->handle, NULL, threadRun, context);
    else
    {
        if (stack < PTHREAD_STACK_MIN)
            stack = PTHREAD_STACK_MIN;
        pthread_attr_setstacksize(&attributes, stack);
        result = pthread_create(&thread->handle, &attributes, threadRun, context);
    }

    pthread_attr_destroy(&attributes);
    return result;
}


BH_Thread *BH_ThreadNew(size_t stack,
                        BH_ThreadCallback callback,
                        void *data)
{
    BH_Thread *thread;

    thread = malloc(sizeof(BH_Thread));
    if (thread && threadInit(thread, stack, callback, data))
    {
        free(thread);
        return NULL;
    }

    return thread;
}


int BH_ThreadJoin(BH_Thread *thread)
{
    if (pthread_join(thread->handle, NULL))
        return BH_ERROR;

    free(thread);
    return BH_OK;
}


int BH_ThreadDetach(BH_Thread *thread)
{
    if (pthread_detach(thread->handle))
        return BH_ERROR;

    free(thread);
    return BH_OK;
}