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

Commit

Permalink
unix, windows: add uv_thread_equal
Browse files Browse the repository at this point in the history
  • Loading branch information
neojski authored and saghul committed Oct 20, 2014
1 parent 8a8cff4 commit 59658a8
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \
test/test-tcp-writealot.c \
test/test-tcp-try-write.c \
test/test-tcp-write-queue-order.c \
test/test-thread-equal.c \
test/test-thread.c \
test/test-threadpool-cancel.c \
test/test-threadpool.c \
Expand Down
1 change: 1 addition & 0 deletions docs/src/threading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Threads
.. c:function:: int uv_thread_create(uv_thread_t* tid, uv_thread_cb entry, void* arg)
.. c:function:: unsigned long uv_thread_self(void)
.. 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)
Thread-local storage
^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion include/uv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1369,8 +1369,9 @@ UV_EXTERN void uv_key_set(uv_key_t* key, void* value);
typedef void (*uv_thread_cb)(void* arg);

UV_EXTERN int uv_thread_create(uv_thread_t* tid, uv_thread_cb entry, void* arg);
UV_EXTERN unsigned long 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_equal(const uv_thread_t* t1, const uv_thread_t* t2);

/* The presence of these unions force similar struct layout. */
#define XX(_, name) uv_ ## name ## _t name;
Expand Down
5 changes: 5 additions & 0 deletions src/unix/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ int uv_thread_join(uv_thread_t *tid) {
}


int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2) {
return pthread_equal(*t1, *t2);
}


int uv_mutex_init(uv_mutex_t* mutex) {
#if defined(NDEBUG) || !defined(PTHREAD_MUTEX_ERRORCHECK)
return -pthread_mutex_init(mutex, NULL);
Expand Down
6 changes: 3 additions & 3 deletions src/uv-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,11 @@ int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
}


unsigned long uv_thread_self(void) {
uv_thread_t uv_thread_self(void) {
#ifdef _WIN32
return (unsigned long) GetCurrentThreadId();
return GetCurrentThreadId();
#else
return (unsigned long) pthread_self();
return pthread_self();
#endif
}

Expand Down
5 changes: 5 additions & 0 deletions src/win/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ int uv_thread_join(uv_thread_t *tid) {
}


int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2) {
return *t1 == *t2;
}


int uv_mutex_init(uv_mutex_t* mutex) {
InitializeCriticalSection(mutex);
return 0;
Expand Down
2 changes: 2 additions & 0 deletions test/test-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ TEST_DECLARE (thread_local_storage)
TEST_DECLARE (thread_mutex)
TEST_DECLARE (thread_rwlock)
TEST_DECLARE (thread_create)
TEST_DECLARE (thread_equal)
TEST_DECLARE (dlerror)
TEST_DECLARE (poll_duplex)
TEST_DECLARE (poll_unidirectional)
Expand Down Expand Up @@ -630,6 +631,7 @@ TASK_LIST_START
TEST_ENTRY (thread_mutex)
TEST_ENTRY (thread_rwlock)
TEST_ENTRY (thread_create)
TEST_ENTRY (thread_equal)
TEST_ENTRY (dlerror)
TEST_ENTRY (ip4_addr)
TEST_ENTRY (ip6_addr_link_local)
Expand Down
45 changes: 45 additions & 0 deletions test/test-thread-equal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "uv.h"
#include "task.h"

uv_thread_t main_thread_id;
uv_thread_t subthreads[2];

static void check_thread(void* arg) {
uv_thread_t *thread_id = arg;
uv_thread_t self_id = uv_thread_self();
ASSERT(uv_thread_equal(&main_thread_id, &self_id) == 0);
*thread_id = uv_thread_self();
}

TEST_IMPL(thread_equal) {
uv_thread_t threads[2];
main_thread_id = uv_thread_self();
ASSERT(0 != uv_thread_equal(&main_thread_id, &main_thread_id));
ASSERT(0 == uv_thread_create(threads + 0, check_thread, subthreads + 0));
ASSERT(0 == uv_thread_create(threads + 1, check_thread, subthreads + 1));
ASSERT(0 == uv_thread_join(threads + 0));
ASSERT(0 == uv_thread_join(threads + 1));
ASSERT(0 == uv_thread_equal(subthreads + 0, subthreads + 1));
return 0;
}
1 change: 1 addition & 0 deletions uv.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@
'test/test-tcp-write-queue-order.c',
'test/test-threadpool.c',
'test/test-threadpool-cancel.c',
'test/test-thread-equal.c',
'test/test-mutexes.c',
'test/test-thread.c',
'test/test-barrier.c',
Expand Down

0 comments on commit 59658a8

Please sign in to comment.