unix, windows: add uv_thread_equal

This commit is contained in:
Tomasz Kołodziejski 2014-10-04 22:01:03 -07:00 committed by Saúl Ibarra Corretgé
parent 8a8cff4b34
commit 59658a8de7
9 changed files with 65 additions and 4 deletions

View File

@ -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 \

View File

@ -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
^^^^^^^^^^^^^^^^^^^^

View File

@ -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;

View File

@ -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);

View File

@ -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
}

View File

@ -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;

View File

@ -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)
@ -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)

45
test/test-thread-equal.c Normal file
View File

@ -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
uv.gyp
View File

@ -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',