Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 0a502c0

Browse files
Merge pull request justadudewhohacks#445 from oyyd/threads
src: add threads related API
2 parents b97d024 + c093442 commit 0a502c0

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

cc/core/core.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ NAN_MODULE_INIT(Core::Init) {
4242
Nan::SetMethod(target, "cartToPolarAsync", CartToPolarAsync);
4343
Nan::SetMethod(target, "polarToCart", PolarToCart);
4444
Nan::SetMethod(target, "polarToCartAsync", PolarToCartAsync);
45+
Nan::SetMethod(target, "getNumThreads", GetNumThreads);
46+
Nan::SetMethod(target, "setNumThreads", SetNumThreads);
47+
Nan::SetMethod(target, "getThreadNum", GetThreadNum);
4548
};
4649

4750
NAN_METHOD(Core::GetBuildInformation) {
@@ -158,3 +161,25 @@ NAN_METHOD(Core::PolarToCartAsync) {
158161
info
159162
);
160163
}
164+
165+
NAN_METHOD(Core::GetNumThreads) {
166+
FF_METHOD_CONTEXT("Core::GetNumThreads");
167+
FF_RETURN(IntConverter::wrap(cv::getNumThreads()));
168+
}
169+
170+
NAN_METHOD(Core::SetNumThreads) {
171+
FF_METHOD_CONTEXT("Core::SetNumThreads");
172+
173+
if(!FF_IS_INT(info[0])) {
174+
return Nan::ThrowError("Core::SetNumThreads expected arg0 to an int");
175+
}
176+
177+
int32_t num = FF_CAST_INT(info[0]);
178+
179+
cv::setNumThreads(num);
180+
}
181+
182+
NAN_METHOD(Core::GetThreadNum) {
183+
FF_METHOD_CONTEXT("Core::GetNumThreads");
184+
FF_RETURN(IntConverter::wrap(cv::getThreadNum()));
185+
}

cc/core/core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Core : public Nan::ObjectWrap {
2222
static NAN_METHOD(CartToPolarAsync);
2323
static NAN_METHOD(PolarToCart);
2424
static NAN_METHOD(PolarToCartAsync);
25+
static NAN_METHOD(GetNumThreads);
26+
static NAN_METHOD(SetNumThreads);
27+
static NAN_METHOD(GetThreadNum);
2528
};
2629

2730
#endif

lib/typings/cv.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ export function partition(data: Mat[], predicate: (mat1: Mat, mat2: Mat) => bool
116116
export function plot1DHist(hist: Mat, plotImg: Mat, color: Vec3, lineType?: number, thickness?: number, shift?: number): Mat;
117117
export function polarToCart(magnitude: Mat, angle: Mat, angleInDegrees?: boolean): { x: Mat, y: Mat };
118118
export function polarToCartAsync(magnitude: Mat, angle: Mat, angleInDegrees?: boolean): Promise<{ x: Mat, y: Mat }>;
119+
export function getNumThreads(): number;
120+
export function setNumThreads(nthreads: number): void;
121+
export function getThreadNum(): number;
119122
export function projectPoints(objectPoints: Point3[], imagePoints: Point2[], rvec: Vec3, tvec: Vec3, cameraMatrix: Mat, distCoeffs: number[], aspectRatio?: number): { imagePoints: Point2[], jacobian: Mat };
120123
export function projectPointsAsync(objectPoints: Point3[], imagePoints: Point2[], rvec: Vec3, tvec: Vec3, cameraMatrix: Mat, distCoeffs: number[], aspectRatio?: number): Promise<{ imagePoints: Point2[], jacobian: Mat }>;
121124
export function readNetFromCaffe(prototxt: string, modelPath?: string): Net;

test/tests/core/core.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,50 @@ describe('core', () => {
170170
});
171171
});
172172

173+
describe('getNumThreads', () => {
174+
it('should return the number of threads that used by OpenCV', () => {
175+
expect(cv.getNumThreads()).to.be.an('number');
176+
});
177+
});
178+
179+
describe('setNumThreads', () => {
180+
it('should try to set the number of threads'
181+
+ ' that used by OpenCV', () => {
182+
const number = 2;
183+
cv.setNumThreads(number);
184+
// OpenCV will **try** to set the number of threads for the
185+
// next parallel region so that `cv.getNumThreads()` don't react
186+
// to this immediately.
187+
// expect(cv.getNumThreads()).to.be.equal(number);
188+
});
189+
190+
it('should throw when the argument is not integer', () => {
191+
let err;
192+
193+
const expectError = (fn, msg) => {
194+
try {
195+
fn();
196+
} catch (e) {
197+
err = e;
198+
}
199+
200+
expect(err.message).to.be
201+
.equal(msg);
202+
};
203+
204+
expectError(() => cv.setNumThreads('hello'),
205+
'Core::SetNumThreads expected arg0 to an int');
206+
expectError(() => cv.setNumThreads(1.1),
207+
'Core::SetNumThreads expected arg0 to an int');
208+
});
209+
});
210+
211+
describe('GetThreadNum', () => {
212+
it('should returns the index of the currently executed thread', () => {
213+
expect(cv.getThreadNum()).to.be.an('number');
214+
});
215+
});
216+
173217
if (asyncHooks) {
174218
describe('async_hooks', () => {
175219
it('should trigger `init` callback in async_hooks', () => {

0 commit comments

Comments
 (0)