26 changes: 26 additions & 0 deletions libc/src/threads/thrd_equal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Implementation of the thrd_equal function -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/threads/thrd_equal.h"
#include "src/__support/common.h"
#include "src/__support/threads/thread.h"

#include <threads.h> // For thrd_* type definitions.

namespace __llvm_libc {

static_assert(sizeof(thrd_t) == sizeof(__llvm_libc::Thread),
"Mismatch between thrd_t and internal Thread.");

LLVM_LIBC_FUNCTION(int, thrd_equal, (thrd_t lhs, thrd_t rhs)) {
auto *lhs_internal = reinterpret_cast<Thread *>(&lhs);
auto *rhs_internal = reinterpret_cast<Thread *>(&rhs);
return *lhs_internal == *rhs_internal;
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/threads/thrd_equal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for thrd_equal function -----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_THREADS_THRD_EQUAL_H
#define LLVM_LIBC_SRC_THREADS_THRD_EQUAL_H

#include "include/threads.h"

namespace __llvm_libc {

int thrd_equal(thrd_t lhs, thrd_t rhs);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_THREADS_THRD_EQUAL_H
21 changes: 21 additions & 0 deletions libc/test/integration/src/pthread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,24 @@ add_integration_test(
libc.src.pthread.pthread_create
libc.src.pthread.pthread_join
)

add_integration_test(
pthread_equal_test
SUITE
libc-pthread-integration-tests
SRCS
pthread_equal_test.cpp
LOADER
libc.loader.linux.crt1
DEPENDS
libc.include.pthread
libc.src.errno.errno
libc.src.pthread.pthread_mutex_destroy
libc.src.pthread.pthread_mutex_init
libc.src.pthread.pthread_mutex_lock
libc.src.pthread.pthread_mutex_unlock
libc.src.pthread.pthread_create
libc.src.pthread.pthread_equal
libc.src.pthread.pthread_join
libc.src.pthread.pthread_self
)
67 changes: 67 additions & 0 deletions libc/test/integration/src/pthread/pthread_equal_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//===-- Tests for pthread_equal -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/pthread/pthread_create.h"
#include "src/pthread/pthread_equal.h"
#include "src/pthread/pthread_join.h"
#include "src/pthread/pthread_mutex_destroy.h"
#include "src/pthread/pthread_mutex_init.h"
#include "src/pthread/pthread_mutex_lock.h"
#include "src/pthread/pthread_mutex_unlock.h"
#include "src/pthread/pthread_self.h"

#include "utils/IntegrationTest/test.h"

#include <pthread.h>

pthread_t child_thread;
pthread_mutex_t mutex;

static void *child_func(void *arg) {
__llvm_libc::pthread_mutex_lock(&mutex);
int *ret = reinterpret_cast<int *>(arg);
auto self = __llvm_libc::pthread_self();
*ret = __llvm_libc::pthread_equal(child_thread, self);
__llvm_libc::pthread_mutex_unlock(&mutex);
return nullptr;
}

int main() {
// We init and lock the mutex so that we guarantee that the child thread is
// waiting after startup.
ASSERT_EQ(__llvm_libc::pthread_mutex_init(&mutex, nullptr), 0);
ASSERT_EQ(__llvm_libc::pthread_mutex_lock(&mutex), 0);

auto main_thread = __llvm_libc::pthread_self();

// The idea here is that, we start a child thread which will immediately
// wait on |mutex|. The main thread will update the global |child_thread| var
// and unlock |mutex|. This will give the child thread a chance to compare
// the result of pthread_self with the |child_thread|. The result of the
// comparison is returned in the thread arg.
int result = 0;
pthread_t th;
ASSERT_EQ(__llvm_libc::pthread_create(&th, nullptr, child_func, &result), 0);
// This new thread should of course not be equal to the main thread.
ASSERT_EQ(__llvm_libc::pthread_equal(th, main_thread), 0);

// Set the |child_thread| global var and unlock to allow the child to perform
// the comparison.
child_thread = th;
ASSERT_EQ(__llvm_libc::pthread_mutex_unlock(&mutex), 0);

void *retval;
ASSERT_EQ(__llvm_libc::pthread_join(th, &retval), 0);
ASSERT_EQ(uintptr_t(retval), uintptr_t(nullptr));
// The child thread should see that pthread_self return value is the same as
// |child_thread|.
ASSERT_NE(result, 0);

__llvm_libc::pthread_mutex_destroy(&mutex);
return 0;
}
20 changes: 20 additions & 0 deletions libc/test/integration/src/threads/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ add_integration_test(
libc.src.threads.thrd_join
)

add_integration_test(
thrd_equal_test
SUITE
libc-threads-integration-tests
SRCS
thrd_equal_test.cpp
LOADER
libc.loader.linux.crt1
DEPENDS
libc.include.threads
libc.src.threads.mtx_destroy
libc.src.threads.mtx_init
libc.src.threads.mtx_lock
libc.src.threads.mtx_unlock
libc.src.threads.thrd_create
libc.src.threads.thrd_current
libc.src.threads.thrd_equal
libc.src.threads.thrd_join
)

add_integration_test(
thrd_test
SUITE
Expand Down
68 changes: 68 additions & 0 deletions libc/test/integration/src/threads/thrd_equal_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//===-- Tests for thrd_equal ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/threads/mtx_destroy.h"
#include "src/threads/mtx_init.h"
#include "src/threads/mtx_lock.h"
#include "src/threads/mtx_unlock.h"
#include "src/threads/thrd_create.h"
#include "src/threads/thrd_current.h"
#include "src/threads/thrd_equal.h"
#include "src/threads/thrd_join.h"

#include "utils/IntegrationTest/test.h"

#include <threads.h>

thrd_t child_thread;
mtx_t mutex;

static int child_func(void *arg) {
__llvm_libc::mtx_lock(&mutex);
int *ret = reinterpret_cast<int *>(arg);
auto self = __llvm_libc::thrd_current();
*ret = __llvm_libc::thrd_equal(child_thread, self);
__llvm_libc::mtx_unlock(&mutex);
return 0;
}

int main() {
// We init and lock the mutex so that we guarantee that the child thread is
// waiting after startup.
ASSERT_EQ(__llvm_libc::mtx_init(&mutex, mtx_plain), int(thrd_success));
ASSERT_EQ(__llvm_libc::mtx_lock(&mutex), int(thrd_success));

auto main_thread = __llvm_libc::thrd_current();

// The idea here is that, we start a child thread which will immediately
// wait on |mutex|. The main thread will update the global |child_thread| var
// and unlock |mutex|. This will give the child thread a chance to compare
// the result of thrd_self with the |child_thread|. The result of the
// comparison is returned in the thread arg.
int result = 0;
thrd_t th;
ASSERT_EQ(__llvm_libc::thrd_create(&th, child_func, &result),
int(thrd_success));
// This new thread should of course not be equal to the main thread.
ASSERT_EQ(__llvm_libc::thrd_equal(th, main_thread), 0);

// Set the |child_thread| global var and unlock to allow the child to perform
// the comparison.
child_thread = th;
ASSERT_EQ(__llvm_libc::mtx_unlock(&mutex), int(thrd_success));

int retval;
ASSERT_EQ(__llvm_libc::thrd_join(&th, &retval), int(thrd_success));
ASSERT_EQ(retval, 0);
// The child thread should see that thrd_current return value is the same as
// |child_thread|.
ASSERT_NE(result, 0);

__llvm_libc::mtx_destroy(&mutex);
return 0;
}