File: threadlocal.cpp

package info (click to toggle)
node-nan 2.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,212 kB
  • ctags: 1,658
  • sloc: cpp: 6,307; python: 3,105; ansic: 1,314; makefile: 104; sh: 33
file content (69 lines) | stat: -rw-r--r-- 1,767 bytes parent folder | download
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
/*********************************************************************
 * NAN - Native Abstractions for Node.js
 *
 * Copyright (c) 2016 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()));
    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()
    , New<v8::FunctionTemplate>(thread_local_storage)->GetFunction()
  );
}

NODE_MODULE(threadlocal, Init)