Skip to content
This repository was archived by the owner on May 4, 2018. It is now read-only.

Commit 59658a8

Browse files
neojskisaghul
authored andcommitted
unix, windows: add uv_thread_equal
1 parent 8a8cff4 commit 59658a8

File tree

9 files changed

+65
-4
lines changed

9 files changed

+65
-4
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \
206206
test/test-tcp-writealot.c \
207207
test/test-tcp-try-write.c \
208208
test/test-tcp-write-queue-order.c \
209+
test/test-thread-equal.c \
209210
test/test-thread.c \
210211
test/test-threadpool-cancel.c \
211212
test/test-threadpool.c \

docs/src/threading.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Threads
5858
.. c:function:: int uv_thread_create(uv_thread_t* tid, uv_thread_cb entry, void* arg)
5959
.. c:function:: unsigned long uv_thread_self(void)
6060
.. c:function:: int uv_thread_join(uv_thread_t *tid)
61+
.. c:function:: int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2)
6162
6263
Thread-local storage
6364
^^^^^^^^^^^^^^^^^^^^

include/uv.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,8 +1369,9 @@ UV_EXTERN void uv_key_set(uv_key_t* key, void* value);
13691369
typedef void (*uv_thread_cb)(void* arg);
13701370

13711371
UV_EXTERN int uv_thread_create(uv_thread_t* tid, uv_thread_cb entry, void* arg);
1372-
UV_EXTERN unsigned long uv_thread_self(void);
1372+
UV_EXTERN uv_thread_t uv_thread_self(void);
13731373
UV_EXTERN int uv_thread_join(uv_thread_t *tid);
1374+
UV_EXTERN int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2);
13741375

13751376
/* The presence of these unions force similar struct layout. */
13761377
#define XX(_, name) uv_ ## name ## _t name;

src/unix/thread.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ int uv_thread_join(uv_thread_t *tid) {
3636
}
3737

3838

39+
int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2) {
40+
return pthread_equal(*t1, *t2);
41+
}
42+
43+
3944
int uv_mutex_init(uv_mutex_t* mutex) {
4045
#if defined(NDEBUG) || !defined(PTHREAD_MUTEX_ERRORCHECK)
4146
return -pthread_mutex_init(mutex, NULL);

src/uv-common.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,11 @@ int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
306306
}
307307

308308

309-
unsigned long uv_thread_self(void) {
309+
uv_thread_t uv_thread_self(void) {
310310
#ifdef _WIN32
311-
return (unsigned long) GetCurrentThreadId();
311+
return GetCurrentThreadId();
312312
#else
313-
return (unsigned long) pthread_self();
313+
return pthread_self();
314314
#endif
315315
}
316316

src/win/thread.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ int uv_thread_join(uv_thread_t *tid) {
129129
}
130130

131131

132+
int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2) {
133+
return *t1 == *t2;
134+
}
135+
136+
132137
int uv_mutex_init(uv_mutex_t* mutex) {
133138
InitializeCriticalSection(mutex);
134139
return 0;

test/test-list.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ TEST_DECLARE (thread_local_storage)
258258
TEST_DECLARE (thread_mutex)
259259
TEST_DECLARE (thread_rwlock)
260260
TEST_DECLARE (thread_create)
261+
TEST_DECLARE (thread_equal)
261262
TEST_DECLARE (dlerror)
262263
TEST_DECLARE (poll_duplex)
263264
TEST_DECLARE (poll_unidirectional)
@@ -630,6 +631,7 @@ TASK_LIST_START
630631
TEST_ENTRY (thread_mutex)
631632
TEST_ENTRY (thread_rwlock)
632633
TEST_ENTRY (thread_create)
634+
TEST_ENTRY (thread_equal)
633635
TEST_ENTRY (dlerror)
634636
TEST_ENTRY (ip4_addr)
635637
TEST_ENTRY (ip6_addr_link_local)

test/test-thread-equal.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy
4+
* of this software and associated documentation files (the "Software"), to
5+
* deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
* sell copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in
11+
* all copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19+
* IN THE SOFTWARE.
20+
*/
21+
22+
#include "uv.h"
23+
#include "task.h"
24+
25+
uv_thread_t main_thread_id;
26+
uv_thread_t subthreads[2];
27+
28+
static void check_thread(void* arg) {
29+
uv_thread_t *thread_id = arg;
30+
uv_thread_t self_id = uv_thread_self();
31+
ASSERT(uv_thread_equal(&main_thread_id, &self_id) == 0);
32+
*thread_id = uv_thread_self();
33+
}
34+
35+
TEST_IMPL(thread_equal) {
36+
uv_thread_t threads[2];
37+
main_thread_id = uv_thread_self();
38+
ASSERT(0 != uv_thread_equal(&main_thread_id, &main_thread_id));
39+
ASSERT(0 == uv_thread_create(threads + 0, check_thread, subthreads + 0));
40+
ASSERT(0 == uv_thread_create(threads + 1, check_thread, subthreads + 1));
41+
ASSERT(0 == uv_thread_join(threads + 0));
42+
ASSERT(0 == uv_thread_join(threads + 1));
43+
ASSERT(0 == uv_thread_equal(subthreads + 0, subthreads + 1));
44+
return 0;
45+
}

uv.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@
410410
'test/test-tcp-write-queue-order.c',
411411
'test/test-threadpool.c',
412412
'test/test-threadpool-cancel.c',
413+
'test/test-thread-equal.c',
413414
'test/test-mutexes.c',
414415
'test/test-thread.c',
415416
'test/test-barrier.c',

0 commit comments

Comments
 (0)