Skip to content

Commit bad3177

Browse files
author
Miqueas
committed
Renamed multi-state to multiple-vm and made small improvements
1 parent 73a88a0 commit bad3177

File tree

5 files changed

+117
-37
lines changed

5 files changed

+117
-37
lines changed

src/12-multi-state.c

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/12-multiple-vm.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <stdio.h>
2+
#include <pthread.h>
3+
4+
#include <lua.h>
5+
#include <lualib.h>
6+
#include <lauxlib.h>
7+
8+
#include "constants.h"
9+
10+
/* Using multiple Lua VM's in the same program */
11+
12+
void *luaThread1(void *_) {
13+
lua_State *L = luaL_newstate();
14+
luaL_openlibs(L);
15+
int error = luaL_dofile(L, THREADED_PATH);
16+
17+
if (error) {
18+
fprintf(stderr, "Thread 3: Error. Code: %d\n", error);
19+
lua_close(L);
20+
return NULL;
21+
}
22+
23+
lua_getglobal(L, "thread1");
24+
lua_call(L, 0, 0);
25+
lua_getglobal(L, "print");
26+
lua_pushstring(L, "\n\n");
27+
lua_call(L, 1, 0);
28+
lua_close(L);
29+
30+
return NULL;
31+
}
32+
33+
void *luaThread2(void *_) {
34+
lua_State *L = luaL_newstate();
35+
luaL_openlibs(L);
36+
int error = luaL_dofile(L, THREADED_PATH);
37+
38+
if (error) {
39+
fprintf(stderr, "Thread 3: Error. Code: %d\n", error);
40+
lua_close(L);
41+
return NULL;
42+
}
43+
44+
lua_getglobal(L, "thread2");
45+
lua_call(L, 0, 0);
46+
lua_getglobal(L, "print");
47+
lua_pushstring(L, "\n\n");
48+
lua_call(L, 1, 0);
49+
lua_close(L);
50+
51+
return NULL;
52+
}
53+
54+
void *luaThread3(void *_) {
55+
lua_State *L = luaL_newstate();
56+
luaL_openlibs(L);
57+
int error = luaL_dofile(L, THREADED_PATH);
58+
59+
if (error) {
60+
fprintf(stderr, "Thread 3: Error. Code: %d\n", error);
61+
lua_close(L);
62+
return NULL;
63+
}
64+
65+
lua_getglobal(L, "thread3");
66+
lua_pushinteger(L, (lua_Integer) 29);
67+
lua_pushinteger(L, (lua_Integer) 69);
68+
lua_call(L, 2, 1);
69+
lua_Integer result = luaL_checkinteger(L, 1);
70+
lua_getglobal(L, "print");
71+
lua_pushstring(L, "Thread 3 result: ");
72+
lua_pushinteger(L, result);
73+
lua_pushstring(L, "\n\n");
74+
lua_call(L, 3, 0);
75+
lua_close(L);
76+
77+
return NULL;
78+
}
79+
80+
int main(void) {
81+
pthread_t thread1;
82+
pthread_t thread2;
83+
pthread_t thread3;
84+
85+
pthread_create(&thread1, NULL, luaThread1, NULL);
86+
pthread_join(thread1, NULL);
87+
pthread_create(&thread2, NULL, luaThread2, NULL);
88+
pthread_join(thread2, NULL);
89+
pthread_create(&thread3, NULL, luaThread3, NULL);
90+
pthread_join(thread3, NULL);
91+
92+
return 0;
93+
}

src/12-multiple-vm.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function thread1()
2+
print("Hello from thread 1!")
3+
end
4+
5+
function thread2()
6+
for k, v in pairs(_G) do
7+
print(k, v)
8+
end
9+
end
10+
11+
function thread3(a, b)
12+
return a * b
13+
end

src/constants.h.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#define TABLE_PATH "@table@"
22
#define METATABLE_PATH "@metatable@"
3-
#define HOOKS_PATH "@hooks@"
3+
#define HOOKS_PATH "@hooks@"
4+
#define THREADED_PATH "@threaded@"

src/meson.build

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ else
1414
endif
1515

1616
# ==== Constants header used by some examples (04, 05 & 11) ==== #
17-
configure_file(
17+
constants_h = configure_file(
1818
input: 'constants.h.in',
1919
output: 'constants.h',
2020
configuration: {
2121
'table': meson.current_source_dir() / '04-table.lua',
2222
'metatable': meson.current_source_dir() / '05-metatable.lua',
23-
'hooks': meson.current_source_dir() / '11-hooks.lua'
23+
'hooks': meson.current_source_dir() / '11-hooks.lua',
24+
'threaded': meson.current_source_dir() / '12-multiple-vm.lua',
2425
}
2526
)
2627
# ==== Example 01: hello world ==== #
@@ -53,10 +54,10 @@ basic_lib_lua = configure_file(
5354
)
5455

5556
# ==== Example 04: table ==== #
56-
table_exe = executable('04-table', '04-table.c', dependencies: lua51_dep)
57+
table_exe = executable('04-table', [ constants_h, '04-table.c' ], dependencies: lua51_dep)
5758

5859
# ==== Example 05: metatable ==== #
59-
metatable_exe = executable('05-metatable', '05-metatable.c', dependencies: lua51_dep)
60+
metatable_exe = executable('05-metatable', [ constants_h, '05-metatable.c'], dependencies: lua51_dep)
6061

6162
# ==== Example 06: userdata ==== #
6263
library('userdata', '06-userdata.c',
@@ -107,10 +108,10 @@ addfn_lua = configure_file(
107108
)
108109

109110
# ==== Example 11: hooks ==== #
110-
hooks_exe = executable('11-hooks', '11-hooks.c', dependencies: lua51_dep)
111+
hooks_exe = executable('11-hooks', [ constants_h, '11-hooks.c' ], dependencies: lua51_dep)
111112

112-
# ==== Example 12: multiple states ==== #
113-
multi_state_exe = executable('12-multi-state', '12-multi-state.c', dependencies: lua51_dep)
113+
# ==== Example 12: multiple vm's ==== #
114+
multiple_vm_exe = executable('12-multiple-vm', [ constants_h, '12-multiple-vm.c' ], dependencies: lua51_dep, link_args: [ '-lpthread' ])
114115

115116
if tests
116117
test('01 hello world', hello_world_exe)
@@ -130,5 +131,5 @@ if tests
130131
test('09 newlib (5.2+)', lua52_bin, args: [ newlib_lua ], env: { 'LUA_CPATH': meson.current_build_dir() / '?.so' })
131132
test('10 functions', lua51_bin, args: [ addfn_lua ], env: { 'LUA_CPATH': meson.current_build_dir() / '?.so' })
132133
test('11 hooks', hooks_exe)
133-
test('12 multi state', multi_state_exe)
134+
test('12 multi state', multiple_vm_exe)
134135
endif

0 commit comments

Comments
 (0)