-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluaThread.h
More file actions
60 lines (54 loc) · 1.41 KB
/
luaThread.h
File metadata and controls
60 lines (54 loc) · 1.41 KB
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
#pragma once
#include <thread>
#include "lua/include/lua.c++.hpp"
//#include "lua\include\selene.h"
// http://www.jeremyong.com/blog/2014/01/10/interfacing-lua-with-templates-in-c-plus-plus-11/
using namespace fastdelegate;
extern const lua_State *lua_abort;
class luaThread {
HANDLE quitEvent, execEvent;
std::thread thread;
std::string script;
int m_func;
lua_State *m_L;
static void stProc( luaThread *ptr ) { ptr->proc(); };
void proc();
public:
int running;
FastDelegate3<str_c,str_c,void*> delMenuAdd;
FastDelegate1<str_c> delTransmit;
luaThread(FastDelegate3<str_c,str_c,void*> menu_add, FastDelegate1<str_c> transmit)
: quitEvent (CreateEvent(0,FALSE,FALSE,0))
, execEvent (CreateEvent(0,FALSE,FALSE,0))
, delMenuAdd (menu_add)
, delTransmit (transmit)
, thread (stProc, this)
, m_func (0)
, m_L (nullptr)
, running (0)
{};
~luaThread() {
SetEvent(quitEvent);
thread.join();
}
nil exec(int func) {
m_func = func;
SetEvent(execEvent);
};
nil abort() {
lua_abort = m_L;
}
nil reset() {
if( running ) abort();
::SetEvent(quitEvent);
thread.join();
::ResetEvent(quitEvent);
thread = std::thread(stProc, this);
}
private:
static int lua_menu_register( lua_State *L );
static int lua_transmit( lua_State *L );
static int lua_transmit_bytes( lua_State *L );
static int lua_sleep( lua_State *L );
static int lua_timeout( lua_State *L );
};