47 changes: 2 additions & 45 deletions libc/src/threads/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ add_header_library(
HDRS
CndVar.h
Futex.h
Mutex.h
Thread.h
DEPENDS
.thread_start_args_h
libc.include.sys_syscall
libc.include.threads
libc.src.__support.CPP.atomic
libc.src.__support.OSUtil.osutil
libc.src.__support.threads.thread
)

add_entrypoint_object(
Expand Down Expand Up @@ -74,50 +74,6 @@ add_entrypoint_object(
libc.src.sys.mman.munmap
)

add_entrypoint_object(
mtx_init
SRCS
mtx_init.cpp
HDRS
../mtx_init.h
DEPENDS
.threads_utils
libc.include.threads
)

add_entrypoint_object(
mtx_destroy
SRCS
mtx_destroy.cpp
HDRS
../mtx_destroy.h
DEPENDS
.threads_utils
libc.include.threads
)

add_entrypoint_object(
mtx_lock
SRCS
mtx_lock.cpp
HDRS
../mtx_lock.h
DEPENDS
.threads_utils
libc.include.threads
)

add_entrypoint_object(
mtx_unlock
SRCS
mtx_unlock.cpp
HDRS
../mtx_unlock.h
DEPENDS
.threads_utils
libc.include.threads
)

add_entrypoint_object(
cnd_init
SRCS
Expand Down Expand Up @@ -149,6 +105,7 @@ add_entrypoint_object(
DEPENDS
.threads_utils
libc.include.threads
libc.src.__support.threads.thread
)

add_entrypoint_object(
Expand Down
13 changes: 7 additions & 6 deletions libc/src/threads/linux/CndVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
#ifndef LLVM_LIBC_SRC_THREADS_LINUX_CNDVAR_H
#define LLVM_LIBC_SRC_THREADS_LINUX_CNDVAR_H

#include "Mutex.h"

#include "include/sys/syscall.h" // For syscall numbers.
#include "include/threads.h" // For values like thrd_success etc.
#include "src/__support/CPP/atomic.h"
#include "src/__support/OSUtil/syscall.h" // For syscall functions.
#include "src/__support/threads/mutex.h"

#include <linux/futex.h> // For futex operations.
#include <stdint.h>
Expand All @@ -38,7 +37,8 @@ struct CndVar {

static int init(CndVar *cv) {
cv->waitq_front = cv->waitq_back = nullptr;
return Mutex::init(&cv->qmtx, mtx_plain);
auto err = Mutex::init(&cv->qmtx, false, false, false);
return err == MutexError::NONE ? thrd_success : thrd_error;
}

static void destroy(CndVar *cv) {
Expand Down Expand Up @@ -67,7 +67,7 @@ struct CndVar {
waitq_back = &waiter;
}

if (m->unlock() != thrd_success) {
if (m->unlock() != MutexError::NONE) {
// If we do not remove the queued up waiter before returning,
// then another thread can potentially signal a non-existing
// waiter. Note also that we do this with |qmtx| locked. This
Expand All @@ -88,7 +88,8 @@ struct CndVar {

// At this point, if locking |m| fails, we can simply return as the
// queued up waiter would have been removed from the queue.
return m->lock();
auto err = m->lock();
return err == MutexError::NONE ? thrd_success : thrd_error;
}

int notify_one() {
Expand All @@ -105,7 +106,7 @@ struct CndVar {
if (waitq_front == nullptr)
waitq_back = nullptr;

qmtx.futex_word = Mutex::MS_Free;
qmtx.futex_word = Mutex::FutexWordType(Mutex::LockState::Free);

__llvm_libc::syscall(
SYS_futex, &qmtx.futex_word.val, FUTEX_WAKE_OP, 1, 1,
Expand Down
122 changes: 0 additions & 122 deletions libc/src/threads/linux/Mutex.h

This file was deleted.

4 changes: 2 additions & 2 deletions libc/src/threads/linux/cnd_wait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
//===----------------------------------------------------------------------===//

#include "CndVar.h"
#include "Mutex.h"

#include "src/threads/cnd_wait.h"
#include "src/__support/common.h"
#include "src/__support/threads/mutex.h"
#include "src/threads/cnd_wait.h"

namespace __llvm_libc {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "src/threads/mtx_destroy.h"
#include "include/threads.h" // For mtx_t definition.
#include "src/__support/common.h"
#include "src/threads/linux/Mutex.h"
#include "src/__support/threads/mutex.h"

namespace __llvm_libc {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#include "src/threads/mtx_init.h"
#include "include/threads.h" // For mtx_t definition.
#include "src/__support/common.h"
#include "src/threads/linux/Mutex.h"
#include "src/__support/threads/mutex.h"

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, mtx_init, (mtx_t * mutex, int type)) {
auto *m = reinterpret_cast<Mutex *>(mutex);
return Mutex::init(m, type);
LLVM_LIBC_FUNCTION(int, mtx_init, (mtx_t * m, int type)) {
auto err = Mutex::init(m, type | mtx_timed, type | mtx_recursive, 0);
return err == MutexError::NONE ? thrd_success : thrd_error;
}

} // namespace __llvm_libc
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
//===----------------------------------------------------------------------===//

#include "src/threads/mtx_lock.h"
#include "include/threads.h" // For mtx_t definition.
#include "include/threads.h" // For mtx_t definition.
#include "src/__support/common.h"
#include "src/threads/linux/Mutex.h"
#include "src/__support/threads/mutex.h"

namespace __llvm_libc {

// The implementation currently handles only plain mutexes.
LLVM_LIBC_FUNCTION(int, mtx_lock, (mtx_t * mutex)) {
auto *m = reinterpret_cast<Mutex *>(mutex);
return m->lock();
auto err = m->lock();
return err == MutexError::NONE ? thrd_success : thrd_error;
}

} // namespace __llvm_libc
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
//===----------------------------------------------------------------------===//

#include "src/threads/mtx_unlock.h"
#include "include/threads.h" // For mtx_t definition.
#include "include/threads.h" // For mtx_t definition.
#include "src/__support/common.h"
#include "src/threads/linux/Mutex.h"
#include "src/__support/threads/mutex.h"

namespace __llvm_libc {

// The implementation currently handles only plain mutexes.
LLVM_LIBC_FUNCTION(int, mtx_unlock, (mtx_t * mutex)) {
auto *m = reinterpret_cast<Mutex *>(mutex);
return m->unlock();
auto err = m->unlock();
return err == MutexError::NONE ? thrd_success : thrd_error;
}

} // namespace __llvm_libc