thread: add uv_thread_getcpu() (#3803)
Add uv_thread_getcpu() api to get the cpu number on which the calling thread is running.
This commit is contained in:
parent
e141586053
commit
64669fdd8d
@ -119,6 +119,15 @@ Threads
|
|||||||
|
|
||||||
.. versionadded:: 1.45.0
|
.. versionadded:: 1.45.0
|
||||||
|
|
||||||
|
.. c:function:: int uv_thread_getcpu(void)
|
||||||
|
|
||||||
|
Gets the CPU number on which the calling thread is running.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
Currently only implemented on Windows, Linux and FreeBSD.
|
||||||
|
|
||||||
|
.. versionadded:: 1.45.0
|
||||||
|
|
||||||
.. c:function:: uv_thread_t uv_thread_self(void)
|
.. c:function:: uv_thread_t uv_thread_self(void)
|
||||||
.. c:function:: int uv_thread_join(uv_thread_t *tid)
|
.. c:function:: int uv_thread_join(uv_thread_t *tid)
|
||||||
.. c:function:: int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2)
|
.. c:function:: int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2)
|
||||||
|
|||||||
@ -1802,6 +1802,7 @@ UV_EXTERN int uv_thread_setaffinity(uv_thread_t* tid,
|
|||||||
UV_EXTERN int uv_thread_getaffinity(uv_thread_t* tid,
|
UV_EXTERN int uv_thread_getaffinity(uv_thread_t* tid,
|
||||||
char* cpumask,
|
char* cpumask,
|
||||||
size_t mask_size);
|
size_t mask_size);
|
||||||
|
UV_EXTERN int uv_thread_getcpu(void);
|
||||||
UV_EXTERN uv_thread_t uv_thread_self(void);
|
UV_EXTERN uv_thread_t uv_thread_self(void);
|
||||||
UV_EXTERN int uv_thread_join(uv_thread_t *tid);
|
UV_EXTERN int uv_thread_join(uv_thread_t *tid);
|
||||||
UV_EXTERN int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2);
|
UV_EXTERN int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2);
|
||||||
|
|||||||
@ -382,6 +382,19 @@ int uv_thread_getaffinity(uv_thread_t* tid,
|
|||||||
}
|
}
|
||||||
#endif /* defined(__linux__) || defined(UV_BSD_H) */
|
#endif /* defined(__linux__) || defined(UV_BSD_H) */
|
||||||
|
|
||||||
|
int uv_thread_getcpu(void) {
|
||||||
|
#if defined(__linux__) || defined(__FreeBSD__)
|
||||||
|
int cpu;
|
||||||
|
|
||||||
|
cpu = sched_getcpu();
|
||||||
|
if (cpu < 0)
|
||||||
|
return UV__ERR(errno);
|
||||||
|
|
||||||
|
return cpu;
|
||||||
|
#else
|
||||||
|
return UV_ENOTSUP;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
uv_thread_t uv_thread_self(void) {
|
uv_thread_t uv_thread_self(void) {
|
||||||
return pthread_self();
|
return pthread_self();
|
||||||
|
|||||||
@ -252,6 +252,9 @@ int uv_thread_getaffinity(uv_thread_t* tid,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int uv_thread_getcpu(void) {
|
||||||
|
return GetCurrentProcessorNumber();
|
||||||
|
}
|
||||||
|
|
||||||
uv_thread_t uv_thread_self(void) {
|
uv_thread_t uv_thread_self(void) {
|
||||||
uv_thread_t key;
|
uv_thread_t key;
|
||||||
|
|||||||
@ -34,6 +34,8 @@ TEST_IMPL(thread_affinity) {
|
|||||||
char* cpumask;
|
char* cpumask;
|
||||||
int ncpus;
|
int ncpus;
|
||||||
int r;
|
int r;
|
||||||
|
int c;
|
||||||
|
int i;
|
||||||
uv_thread_t threads[3];
|
uv_thread_t threads[3];
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -99,6 +101,24 @@ TEST_IMPL(thread_affinity) {
|
|||||||
ASSERT(cpumask[t2first + 2] == (ncpus >= 3));
|
ASSERT(cpumask[t2first + 2] == (ncpus >= 3));
|
||||||
ASSERT(cpumask[t2first + 3] == 0);
|
ASSERT(cpumask[t2first + 3] == 0);
|
||||||
|
|
||||||
|
c = uv_thread_getcpu();
|
||||||
|
ASSERT_GE(c, 0);
|
||||||
|
|
||||||
|
memset(cpumask, 0, cpumasksize);
|
||||||
|
cpumask[c] = 1;
|
||||||
|
r = uv_thread_setaffinity(&threads[0], cpumask, NULL, cpumasksize);
|
||||||
|
ASSERT_EQ(r, 0);
|
||||||
|
|
||||||
|
memset(cpumask, 0, cpumasksize);
|
||||||
|
r = uv_thread_getaffinity(&threads[0], cpumask, cpumasksize);
|
||||||
|
ASSERT_EQ(r, 0);
|
||||||
|
for (i = 0; i < cpumasksize; i++) {
|
||||||
|
if (i == c)
|
||||||
|
ASSERT_EQ(1, cpumask[i]);
|
||||||
|
else
|
||||||
|
ASSERT_EQ(0, cpumask[i]);
|
||||||
|
}
|
||||||
|
|
||||||
free(cpumask);
|
free(cpumask);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user