summaryrefslogtreecommitdiff
path: root/core/src/xmake/thread/thread_init.c
blob: 9adda1f5496f89166ab03df20e1ac9189b098aa4 (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
/*!A cross-platform build utility based on Lua
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (C) 2015-present, Xmake Open Source Community.
 *
 * @author      ruki
 * @file        thread_init.c
 *
 */

/* //////////////////////////////////////////////////////////////////////////////////////
 * trace
 */
#define TB_TRACE_MODULE_NAME "thread"
#define TB_TRACE_MODULE_DEBUG (0)

/* //////////////////////////////////////////////////////////////////////////////////////
 * includes
 */
#include "prefix.h"
#include "../engine.h"
#include "../engine_pool.h"

/* //////////////////////////////////////////////////////////////////////////////////////
 * macros
 */
#define XM_THREAD_ENGINE_NAME "xmake"

/* //////////////////////////////////////////////////////////////////////////////////////
 * private implementation
 */

static tb_int_t xm_thread_func(tb_cpointer_t priv) {
    xm_thread_t *thread = (xm_thread_t *)priv;
    tb_assert_and_check_return_val(thread, 0);

    xm_engine_ref_t engine = xm_engine_pool_alloc(xm_engine_pool());
    if (!engine) {
        engine = xm_engine_init(XM_THREAD_ENGINE_NAME, tb_null);
    }
    if (engine) {
        lua_State *lua = xm_engine_lua(engine);
        tb_assert(lua);

        // pass callback
        tb_char_t const *callback_data = tb_string_cstr(&thread->callback);
        tb_size_t callback_size = tb_string_size(&thread->callback);
        if (callback_data && callback_size) {
            lua_pushlstring(lua, callback_data, callback_size);
            lua_setglobal(lua, "_THREAD_CALLBACK");
        }

        // pass callinfo
        tb_char_t const *callinfo_data = tb_string_cstr(&thread->callinfo);
        tb_size_t callinfo_size = tb_string_size(&thread->callinfo);
        if (callinfo_data && callinfo_size) {
            lua_pushlstring(lua, callinfo_data, callinfo_size);
            lua_setglobal(lua, "_THREAD_CALLINFO");
        }

        // start engine
        tb_char_t *argv[] = { XM_THREAD_ENGINE_NAME, tb_null };
        xm_engine_main(engine, 1, argv, tb_null);
        if (!xm_engine_pool_free(xm_engine_pool(), engine)) {
            xm_engine_exit(engine);
        }
    }
    return 0;
}

/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */
tb_int_t xm_thread_init(lua_State *lua) {
    tb_assert_and_check_return_val(lua, 0);

    tb_bool_t ok = tb_false;
    xm_thread_t *thread = tb_null;
    do {
        // get thread name
        tb_char_t const *name = luaL_checkstring(lua, 1);

        // get callback
        size_t callback_size = 0;
        tb_char_t const *callback_data = luaL_checklstring(lua, 2, &callback_size);
        tb_assert_and_check_break(callback_data && callback_size);

        // get callinfo
        size_t callinfo_size = 0;
        tb_char_t const *callinfo_data = luaL_checklstring(lua, 3, &callinfo_size);
        tb_assert_and_check_break(callinfo_data && callinfo_size);

        // get stack size
        tb_size_t stacksize = (tb_size_t)luaL_checkinteger(lua, 4);

        // init thread
        thread = tb_malloc0_type(xm_thread_t);
        tb_assert_and_check_break(thread);

        tb_string_init(&thread->callback);
        tb_string_cstrncpy(&thread->callback, callback_data, callback_size);

        tb_string_init(&thread->callinfo);
        tb_string_cstrncpy(&thread->callinfo, callinfo_data, callinfo_size);

        // create and start thread
        thread->handle = tb_thread_init(name, xm_thread_func, thread, stacksize);
        tb_assert_and_check_break(thread->handle);

        xm_lua_pushpointer(lua, (tb_pointer_t)thread);
        ok = tb_true;

    } while (0);

    if (!ok) {
        if (thread) {
            tb_string_exit(&thread->callback);
            tb_string_exit(&thread->callinfo);
            if (thread->handle) {
                tb_thread_exit(thread->handle);
                thread->handle = tb_null;
            }
            tb_free(thread);
        }
        lua_pushnil(lua);
    }
    return 1;
}