/
suthread.cpp
116 lines (100 loc) · 2.75 KB
/
suthread.cpp
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
// Copyright (c) 2016 Suneido Software Corp. All rights reserved
// Licensed under GPLv2
#include "value.h"
#include "fibers.h"
#include "errlog.h"
#include "exceptimp.h"
#include "suobject.h"
#include "interp.h"
// ReSharper disable once CppUnusedIncludeDirective
#include "dbms.h"
#include "alert.h"
#include "sublock.h"
#include "builtinclass.h"
#include <span>
#pragma warning(disable : 4722) // destructor never returns
struct ThreadCloser {
ThreadCloser() { // to avoid gcc unused warning
}
~ThreadCloser() {
delete tls().thedbms;
tls().thedbms = nullptr;
Fibers::end();
}
};
struct ThreadInfo {
ThreadInfo(Value f) : fn(f) {
}
Value fn;
};
// this is a wrapper that runs inside the fiber to catch exceptions
static void _stdcall thread(void* arg) {
Proc p;
tls().proc = &p;
ThreadCloser closer;
try {
ThreadInfo* ti = static_cast<ThreadInfo*>(arg);
ti->fn.call(ti->fn, CALL);
} catch (const Except& e) {
errlog("ERROR uncaught in thread:", e.str(), e.callstack());
}
}
class SuThread : public SuValue {
public:
static auto methods() {
return std::span<Method<SuThread>>(); // none
}
static Value Count(BuiltinArgs&);
static Value List(BuiltinArgs&);
static Value Name(BuiltinArgs&);
static Value Sleep(BuiltinArgs&);
};
template <>
Value BuiltinClass<SuThread>::instantiate(BuiltinArgs& args) {
except("can't create instance of Thread");
}
template <>
Value BuiltinClass<SuThread>::callclass(BuiltinArgs& args) {
args.usage("Thread(block)");
Value func = args.getValue("block");
args.end();
Fibers::create(thread, new ThreadInfo(func));
return Value();
}
template <>
auto BuiltinClass<SuThread>::static_methods() {
static StaticMethod methods[]{{"Count", &SuThread::Count},
{"List", &SuThread::List}, {"Name", &SuThread::Name},
{"Sleep", &SuThread::Sleep}};
return std::span(methods);
}
Value SuThread::Count(BuiltinArgs& args) {
args.usage("Thread.Count()").end();
return Fibers::size();
}
Value SuThread::List(BuiltinArgs& args) {
args.usage("Thread.List()").end();
SuObject* list = new SuObject();
Fibers::foreach_fiber_info([list](gcstring name, const char* status) {
list->putdata(new SuString(name), status);
});
return list;
}
Value SuThread::Name(BuiltinArgs& args) {
args.usage("Thread.Name() or Thread.Name(string)");
if (Value name = args.getValue("string", Value()))
Fibers::set_name(name.gcstr());
args.end();
return new SuString(Fibers::get_name());
}
Value SuThread::Sleep(BuiltinArgs& args) {
args.usage("Thread.Sleep(ms)");
int ms = args.getint("ms");
args.end();
Fibers::sleep(ms);
return Value();
}
Value su_Thread() {
static BuiltinClass<SuThread> instance("(func)");
return &instance;
}