bare-thread 1.1.5 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CMakeLists.txt ADDED
@@ -0,0 +1,13 @@
1
+ cmake_minimum_required(VERSION 4.0)
2
+
3
+ find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare)
4
+
5
+ project(bare_thread C)
6
+
7
+ add_bare_module(bare_thread)
8
+
9
+ target_sources(
10
+ ${bare_thread}
11
+ PRIVATE
12
+ binding.c
13
+ )
package/binding.c ADDED
@@ -0,0 +1,205 @@
1
+ #if defined(__linux__)
2
+ #define _GNU_SOURCE
3
+ #endif
4
+
5
+ #include <assert.h>
6
+ #include <bare.h>
7
+ #include <js.h>
8
+ #include <uv.h>
9
+
10
+ #if defined(__linux__)
11
+ #include <unistd.h>
12
+ #endif
13
+
14
+ static js_value_t *
15
+ bare_thread_get_cpu(js_env_t *env, js_callback_info_t *info) {
16
+ int err;
17
+
18
+ int cpu = uv_thread_getcpu();
19
+
20
+ if (cpu == UV_ENOTSUP) return NULL;
21
+
22
+ if (cpu < 0) {
23
+ err = js_throw_error(env, uv_err_name(cpu), uv_strerror(cpu));
24
+ assert(err == 0);
25
+
26
+ return NULL;
27
+ }
28
+
29
+ js_value_t *result;
30
+ err = js_create_int32(env, cpu, &result);
31
+ assert(err == 0);
32
+
33
+ return result;
34
+ }
35
+
36
+ static js_value_t *
37
+ bare_thread_get_id(js_env_t *env, js_callback_info_t *info) {
38
+ int err;
39
+
40
+ uint64_t id;
41
+
42
+ #if defined(__APPLE__)
43
+ pthread_threadid_np(pthread_self(), &id);
44
+ #elif defined(__linux__)
45
+ id = (uint64_t) gettid();
46
+ #elif defined(_WIN32)
47
+ id = (uint64_t) GetCurrentThreadId();
48
+ #else
49
+ id = (uint64_t) (uintptr_t) uv_thread_self() & 0x1fffffffffffffull;
50
+ #endif
51
+
52
+ js_value_t *result;
53
+ err = js_create_int64(env, id, &result);
54
+ assert(err == 0);
55
+
56
+ return result;
57
+ }
58
+
59
+ static js_value_t *
60
+ bare_thread_get_name(js_env_t *env, js_callback_info_t *info) {
61
+ int err;
62
+
63
+ uv_thread_t thread = uv_thread_self();
64
+
65
+ char name[256];
66
+ err = uv_thread_getname(&thread, name, 256);
67
+ if (err < 0) {
68
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
69
+ assert(err == 0);
70
+
71
+ return NULL;
72
+ }
73
+
74
+ js_value_t *result;
75
+ err = js_create_string_utf8(env, (utf8_t *) name, -1, &result);
76
+ assert(err == 0);
77
+
78
+ return result;
79
+ }
80
+
81
+ static js_value_t *
82
+ bare_thread_set_name(js_env_t *env, js_callback_info_t *info) {
83
+ int err;
84
+
85
+ size_t argc = 1;
86
+ js_value_t *argv[1];
87
+
88
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
89
+ assert(err == 0);
90
+
91
+ assert(argc == 1);
92
+
93
+ utf8_t data[256];
94
+ err = js_get_value_string_utf8(env, argv[0], data, 256, NULL);
95
+ assert(err == 0);
96
+
97
+ err = uv_thread_setname((char *) data);
98
+ if (err < 0) {
99
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
100
+ assert(err == 0);
101
+
102
+ return NULL;
103
+ }
104
+
105
+ return NULL;
106
+ }
107
+
108
+ static js_value_t *
109
+ bare_thread_get_priority(js_env_t *env, js_callback_info_t *info) {
110
+ int err;
111
+
112
+ uv_thread_t thread = uv_thread_self();
113
+
114
+ int priority;
115
+ err = uv_thread_getpriority(thread, &priority);
116
+ if (err < 0) {
117
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
118
+ assert(err == 0);
119
+
120
+ return NULL;
121
+ }
122
+
123
+ js_value_t *result;
124
+ err = js_create_int32(env, priority, &result);
125
+ assert(err == 0);
126
+
127
+ return result;
128
+ }
129
+
130
+ static js_value_t *
131
+ bare_thread_set_priority(js_env_t *env, js_callback_info_t *info) {
132
+ int err;
133
+
134
+ size_t argc = 1;
135
+ js_value_t *argv[1];
136
+
137
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
138
+ assert(err == 0);
139
+
140
+ assert(argc == 1);
141
+
142
+ int priority;
143
+ err = js_get_value_int32(env, argv[0], &priority);
144
+ assert(err == 0);
145
+
146
+ uv_thread_t thread = uv_thread_self();
147
+
148
+ err = uv_thread_setpriority(thread, priority);
149
+ if (err < 0) {
150
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
151
+ assert(err == 0);
152
+
153
+ return NULL;
154
+ }
155
+
156
+ return NULL;
157
+ }
158
+
159
+ static js_value_t *
160
+ bare_thread_exports(js_env_t *env, js_value_t *exports) {
161
+ int err;
162
+
163
+ #define V(name, fn) \
164
+ { \
165
+ js_value_t *val; \
166
+ err = js_create_function(env, name, -1, fn, NULL, &val); \
167
+ assert(err == 0); \
168
+ err = js_set_named_property(env, exports, name, val); \
169
+ assert(err == 0); \
170
+ }
171
+
172
+ V("getCPU", bare_thread_get_cpu)
173
+ V("getID", bare_thread_get_id)
174
+ V("getName", bare_thread_get_name)
175
+ V("setName", bare_thread_set_name)
176
+ V("getPriority", bare_thread_get_priority)
177
+ V("setPriority", bare_thread_set_priority)
178
+ #undef V
179
+
180
+ js_value_t *priority;
181
+ err = js_create_object(env, &priority);
182
+ assert(err == 0);
183
+
184
+ err = js_set_named_property(env, exports, "priority", priority);
185
+ assert(err == 0);
186
+
187
+ #define V(name) \
188
+ { \
189
+ js_value_t *val; \
190
+ err = js_create_int32(env, UV_THREAD_##name, &val); \
191
+ assert(err == 0); \
192
+ err = js_set_named_property(env, priority, #name, val); \
193
+ assert(err == 0); \
194
+ }
195
+
196
+ V(PRIORITY_LOWEST);
197
+ V(PRIORITY_BELOW_NORMAL);
198
+ V(PRIORITY_NORMAL);
199
+ V(PRIORITY_ABOVE_NORMAL);
200
+ V(PRIORITY_HIGHEST);
201
+
202
+ return exports;
203
+ }
204
+
205
+ BARE_MODULE(bare_thread, bare_thread_exports)
package/binding.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require.addon()
package/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  const Bundle = require('bare-bundle')
2
2
  const traverse = require('bare-module-traverse')
3
3
  const { startsWithWindowsDriveLetter } = require('bare-module-resolve')
4
+ const constants = require('./lib/constants')
5
+ const binding = require('./binding')
4
6
 
5
- const { protocol, conditions, imports, resolutions } = module
7
+ const { protocol, imports, resolutions } = module
6
8
 
7
9
  module.exports = exports = class Thread {
8
10
  constructor(entry, opts = {}) {
@@ -18,6 +20,24 @@ module.exports = exports = class Thread {
18
20
  return this._thread.joined
19
21
  }
20
22
 
23
+ get name() {
24
+ return binding.getName()
25
+ }
26
+
27
+ set name(name) {
28
+ if (typeof name !== 'string') name = name.toString()
29
+
30
+ binding.setName(name)
31
+ }
32
+
33
+ get priority() {
34
+ return binding.getPriority()
35
+ }
36
+
37
+ set priority(priority) {
38
+ binding.setPriority(priority)
39
+ }
40
+
21
41
  join() {
22
42
  this._thread.join()
23
43
  }
@@ -45,6 +65,14 @@ module.exports = exports = class Thread {
45
65
  joined: this.joined
46
66
  }
47
67
  }
68
+
69
+ static get cpu() {
70
+ return binding.getCPU()
71
+ }
72
+
73
+ static get id() {
74
+ return binding.getID()
75
+ }
48
76
  }
49
77
 
50
78
  exports.isMainThread = Bare.Thread.isMainThread
@@ -61,7 +89,6 @@ exports.prepare = function prepare(entry, opts) {
61
89
  for (const dependency of traverse(
62
90
  entry,
63
91
  {
64
- conditions,
65
92
  imports,
66
93
  resolutions,
67
94
  resolve: traverse.resolve.bare
@@ -84,3 +111,5 @@ function readModule(url) {
84
111
 
85
112
  return null
86
113
  }
114
+
115
+ exports.constants = constants
@@ -0,0 +1,5 @@
1
+ const binding = require('../binding')
2
+
3
+ module.exports = {
4
+ priority: binding.priority
5
+ }
package/package.json CHANGED
@@ -1,16 +1,24 @@
1
1
  {
2
2
  "name": "bare-thread",
3
- "version": "1.1.5",
3
+ "version": "1.2.0",
4
4
  "description": "Thread support for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
7
7
  ".": "./index.js"
8
8
  },
9
9
  "files": [
10
- "index.js"
10
+ "index.js",
11
+ "binding.c",
12
+ "binding.js",
13
+ "CMakeLists.txt",
14
+ "lib",
15
+ "prebuilds"
11
16
  ],
17
+ "addon": true,
12
18
  "scripts": {
13
- "test": "prettier . --check && bare test.js"
19
+ "format": "prettier --write . && lunte --fix",
20
+ "lint": "prettier --check . && lunte",
21
+ "test": "brittle-bare --coverage test.js"
14
22
  },
15
23
  "repository": {
16
24
  "type": "git",
@@ -29,6 +37,8 @@
29
37
  },
30
38
  "devDependencies": {
31
39
  "brittle": "^3.3.2",
40
+ "cmake-bare": "^1.7.6",
41
+ "lunte": "^1.6.0",
32
42
  "prettier": "^3.4.2",
33
43
  "prettier-config-holepunch": "^2.0.0"
34
44
  }