/
threadlocal.cpp
71 lines (61 loc) · 1.76 KB
/
threadlocal.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
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2018 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#include <nan.h>
using namespace Nan; // NOLINT(build/namespaces)
#define _(e) NAN_TEST_EXPRESSION(e)
// Based on test-thread.c from libuv.
class TlsTest : public AsyncWorker {
public:
explicit TlsTest(Tap *t) : AsyncWorker(NULL), t(t), i(0) {
t->plan(7);
t->ok(_(0 == nauv_key_create(&tls_key)));
t->ok(_(NULL == nauv_key_get(&tls_key)));
nauv_key_set(&tls_key, this);
t->ok(_(this == nauv_key_get(&tls_key)));
}
void Execute() {
ok(_(NULL == nauv_key_get(&tls_key)));
nauv_key_set(&tls_key, &i);
ok(_(&i == nauv_key_get(&tls_key)));
nauv_key_set(&tls_key, NULL);
ok(_(NULL == nauv_key_get(&tls_key)));
}
void WorkComplete() {
HandleScope scope;
for (unsigned j = 0; j < i; ++j)
t->ok(res[j].ok, res[j].msg);
nauv_key_delete(&tls_key);
t->ok(_(NULL == ErrorMessage()));
t->end();
delete t;
}
private:
nauv_key_t tls_key;
Tap *t;
struct { bool ok; const char* msg; } res[3];
unsigned i;
void ok(bool isOk, const char *msg) {
assert(i < sizeof(res)/sizeof(res[0]));
res[i].ok = isOk;
res[i].msg = msg;
++i;
}
};
NAN_METHOD(thread_local_storage) {
Tap *t = new Tap(info[0]);
AsyncQueueWorker(new TlsTest(t));
info.GetReturnValue().SetUndefined();
}
NAN_MODULE_INIT(Init) {
Set(target
, New<v8::String>("thread_local_storage").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(thread_local_storage))
.ToLocalChecked()
);
}
NODE_MODULE(threadlocal, Init)