Skip to content

[libc][pthreads] pthread_cond_{destroy|init|signal|wait} #88583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions libc/config/linux/api.td
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def PThreadAPI : PublicAPI<"pthread.h"> {
"__pthread_start_t",
"__pthread_tss_dtor_t",
"pthread_attr_t",
"pthread_cond_t",
"pthread_condattr_t",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to land #88987 then rebase this PR on top of that. Marking this as draft for now.

"pthread_mutex_t",
"pthread_mutexattr_t",
"pthread_t",
Expand Down Expand Up @@ -241,10 +243,31 @@ def SysSendfileAPI : PublicAPI<"sys/sendfile.h"> {
}

def SysTypesAPI : PublicAPI<"sys/types.h"> {
let Types = ["blkcnt_t", "blksize_t", "clockid_t", "dev_t", "gid_t", "ino_t",
"mode_t", "nlink_t", "off_t", "pid_t", "pthread_attr_t", "pthread_key_t",
"pthread_mutex_t", "pthread_mutexattr_t", "pthread_once_t", "pthread_t",
"size_t", "ssize_t", "suseconds_t", "time_t", "uid_t"];
let Types = [
"blkcnt_t",
"blksize_t",
"clockid_t",
"dev_t",
"gid_t",
"ino_t",
"mode_t",
"nlink_t",
"off_t",
"pid_t",
"pthread_attr_t",
"pthread_cond_t",
"pthread_condattr_t",
"pthread_key_t",
"pthread_mutex_t",
"pthread_mutexattr_t",
"pthread_once_t",
"pthread_t",
"size_t",
"ssize_t",
"suseconds_t",
"time_t",
"uid_t",
];
}

def SysUtsNameAPI : PublicAPI<"sys/utsname.h"> {
Expand Down
4 changes: 4 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,10 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.pthread.pthread_attr_setguardsize
libc.src.pthread.pthread_attr_setstack
libc.src.pthread.pthread_attr_setstacksize
libc.src.pthread.pthread_cond_destroy
libc.src.pthread.pthread_cond_init
libc.src.pthread.pthread_cond_signal
libc.src.pthread.pthread_cond_wait
libc.src.pthread.pthread_create
libc.src.pthread.pthread_detach
libc.src.pthread.pthread_equal
Expand Down
2 changes: 2 additions & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ add_gen_header(
.llvm-libc-types.__pthread_start_t
.llvm-libc-types.__pthread_tss_dtor_t
.llvm-libc-types.pthread_attr_t
.llvm-libc-types.pthread_cond_t
.llvm-libc-types.pthread_condattr_t
.llvm-libc-types.pthread_mutex_t
.llvm-libc-types.pthread_mutexattr_t
.llvm-libc-types.pthread_t
Expand Down
2 changes: 2 additions & 0 deletions libc/include/llvm-libc-types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ add_header(posix_spawnattr_t HDR posix_spawnattr_t.h)
add_header(pthread_attr_t HDR pthread_attr_t.h DEPENDS .size_t)
add_header(pthread_key_t HDR pthread_key_t.h)
add_header(pthread_mutex_t HDR pthread_mutex_t.h DEPENDS .__futex_word .__mutex_type)
add_header(pthread_cond_t HDR pthread_cond_t.h DEPENDS .pthread_mutex_t)
add_header(pthread_condattr_t HDR pthread_condattr_t.h)
add_header(pthread_t HDR pthread_t.h DEPENDS .__thread_type)
add_header(pthread_mutexattr_t HDR pthread_mutexattr_t.h)
add_header(pthread_once_t HDR pthread_once_t.h DEPENDS .__futex_word)
Expand Down
19 changes: 19 additions & 0 deletions libc/include/llvm-libc-types/pthread_cond_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Definition of pthread_cond_t type ---------------------------------===//
//
// 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_TYPES_PTHREAD_COND_T_H
#define LLVM_LIBC_TYPES_PTHREAD_COND_T_H

#include "pthread_mutex_t.h"

typedef struct {
void *__qfront;
void *__qback;
pthread_mutex_t __mtx;
} pthread_cond_t;

#endif // LLVM_LIBC_TYPES_PTHREAD_COND_T_H
15 changes: 15 additions & 0 deletions libc/include/llvm-libc-types/pthread_condattr_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//===-- Definition of pthread_condattr_t type -----------------------------===//
//
// 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_TYPES_PTHREAD_CONDATTR_T_H
#define LLVM_LIBC_TYPES_PTHREAD_CONDATTR_T_H

typedef struct {
int pshared;
} pthread_condattr_t;

#endif // LLVM_LIBC_TYPES_PTHREAD_CONDATTR_T_H
52 changes: 49 additions & 3 deletions libc/spec/posix.td
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ def POSIX : StandardSpec<"POSIX"> {
ConstType ConstPThreadMutexTPtr = ConstType<PThreadMutexTPtr>;
ConstType ConstRestrictedPThreadMutexTPtr = ConstType<RestrictedPThreadMutexTPtr>;

NamedType PThreadCondTType = NamedType<"pthread_cond_t">;
PtrType PThreadCondTPtr = PtrType<PThreadCondTType>;
NamedType PThreadCondAttrTType = NamedType<"pthread_condattr_t">;
PtrType PThreadCondAttrTPtr = PtrType<PThreadCondAttrTType>;

PtrType PThreadTPtr = PtrType<PThreadTType>;
RestrictedPtrType RestrictedPThreadTPtr = RestrictedPtrType<PThreadTType>;

Expand Down Expand Up @@ -976,6 +981,8 @@ def POSIX : StandardSpec<"POSIX"> {
[
AtForkCallbackT,
PThreadAttrTType,
PThreadCondTType,
PThreadCondAttrTType,
PThreadKeyT,
PThreadMutexAttrTType,
PThreadMutexTType,
Expand Down Expand Up @@ -1042,6 +1049,23 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<IntType>,
[ArgSpec<PThreadAttrTPtr>, ArgSpec<VoidPtr>, ArgSpec<SizeTType>]
>,
FunctionSpec<
"pthread_cond_destroy",
RetValSpec<IntType>,
[ArgSpec<PThreadCondTPtr>]
>,
FunctionSpec<"pthread_cond_init",
RetValSpec<IntType>,
[ArgSpec<PThreadCondTPtr>, ArgSpec<PThreadCondAttrTPtr>]
>,
FunctionSpec<"pthread_cond_signal",
RetValSpec<IntType>,
[ArgSpec<PThreadCondTPtr>]
>,
FunctionSpec<"pthread_cond_wait",
RetValSpec<IntType>,
[ArgSpec<PThreadCondTPtr>, ArgSpec<PThreadMutexTPtr>]
>,
FunctionSpec<
"pthread_create",
RetValSpec<IntType>,
Expand Down Expand Up @@ -1517,9 +1541,31 @@ def POSIX : StandardSpec<"POSIX"> {
HeaderSpec SysTypes = HeaderSpec<
"sys/types.h",
[], // Macros
[BlkCntT, BlkSizeT, ClockIdT, DevT, GidT, InoT, ModeTType, NLinkT, OffTType, PidT,
PThreadAttrTType, PThreadKeyT, PThreadMutexTType, PThreadMutexAttrTType, PThreadOnceT, PThreadTType,
SizeTType, SSizeTType, SuSecondsT, TimeTType, UidT],
[
BlkCntT,
BlkSizeT,
ClockIdT,
DevT,
GidT,
InoT,
ModeTType,
NLinkT,
OffTType,
PThreadAttrTType,
PThreadCondAttrTType,
PThreadCondTType,
PThreadKeyT,
PThreadMutexAttrTType,
PThreadMutexTType,
PThreadOnceT,
PThreadTType,
PidT,
SSizeTType,
SizeTType,
SuSecondsT,
TimeTType,
UidT
], // Types
[], // Enumerations
[] // Functions
>;
Expand Down
48 changes: 48 additions & 0 deletions libc/src/pthread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,54 @@ add_entrypoint_object(
-fno-omit-frame-pointer
)

add_entrypoint_object(
pthread_cond_destroy
SRCS
pthread_cond_destroy.cpp
HDRS
pthread_cond_destroy.h
DEPENDS
libc.include.errno
libc.include.pthread
libc.include.threads
)

add_entrypoint_object(
pthread_cond_init
SRCS
pthread_cond_init.cpp
HDRS
pthread_cond_init.h
DEPENDS
libc.include.errno
libc.include.pthread
libc.include.threads
)

add_entrypoint_object(
pthread_cond_signal
SRCS
pthread_cond_signal.cpp
HDRS
pthread_cond_signal.h
DEPENDS
libc.include.errno
libc.include.pthread
libc.include.threads
)

add_entrypoint_object(
pthread_cond_wait
SRCS
pthread_cond_wait.cpp
HDRS
pthread_cond_wait.h
DEPENDS
libc.include.errno
libc.include.pthread
libc.include.threads
)

add_entrypoint_object(
pthread_join
SRCS
Expand Down
30 changes: 30 additions & 0 deletions libc/src/pthread/pthread_cond_destroy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===-- Linux implementation of the pthread_cond_wait 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 "pthread_cond_destroy.h"

#include "include/llvm-libc-macros/generic-error-number-macros.h" // EINVAL
#include "src/__support/common.h"
#include "src/threads/linux/CndVar.h"

#include <pthread.h> // pthread_cond_t
// TODO: https://github.com/llvm/llvm-project/issues/88580
#include <threads.h> // thrd_success
Comment on lines +16 to +17
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?


namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, pthread_cond_destroy, (pthread_cond_t * cond)) {
if (!cond)
return EINVAL;

CndVar *C = reinterpret_cast<CndVar *>(cond);
CndVar::destroy(C);
return 0;
}

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/pthread/pthread_cond_destroy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for pthread_cond_destroy 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_PTHREAD_PTHREAD_COND_DESTROY_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_COND_DESTROY_H

#include <pthread.h>

namespace LIBC_NAMESPACE {

int pthread_cond_destroy(pthread_cond_t *cond);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_COND_DESTROY_H
37 changes: 37 additions & 0 deletions libc/src/pthread/pthread_cond_init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===-- Linux implementation of the pthread_cond_init 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 "pthread_cond_init.h"

#include "include/llvm-libc-macros/generic-error-number-macros.h" // EINVAL
#include "src/__support/common.h"
#include "src/threads/linux/CndVar.h"

#include <pthread.h> // pthread_cond_t, pthread_condattr_t
// TODO: https://github.com/llvm/llvm-project/issues/88580
#include <threads.h> // thrd_succes

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, pthread_cond_init,
(pthread_cond_t * cond, const pthread_condattr_t *attr)) {
// TODO: properly support pthread_condattr_t.
// https://github.com/llvm/llvm-project/issues/88582
if (attr)
return EINVAL;

CndVar *C = reinterpret_cast<CndVar *>(cond);
int ret = CndVar::init(C);
if (ret == thrd_success)
return 0;

// TODO: translate error codes?
return -1;
}

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/pthread/pthread_cond_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for pthread_cond_init 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_PTHREAD_PTHREAD_COND_INIT_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_COND_INIT_H

#include <pthread.h>

namespace LIBC_NAMESPACE {

int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_COND_INIT_H
35 changes: 35 additions & 0 deletions libc/src/pthread/pthread_cond_signal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===-- Linux implementation of the pthread_cond_signal 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 "pthread_cond_signal.h"

#include "include/llvm-libc-macros/generic-error-number-macros.h" // EINVAL
#include "src/__support/common.h"
#include "src/threads/linux/CndVar.h"

#include <pthread.h> // pthread_cond_t
// TODO: https://github.com/llvm/llvm-project/issues/88580
#include <threads.h> // thrd_success

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, pthread_cond_signal, (pthread_cond_t * cond)) {
if (!cond)
return EINVAL;
Comment on lines +22 to +23
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, bionic does not check for nullptr args.

https://android-review.googlesource.com/c/platform/bionic/+/84016


CndVar *C = reinterpret_cast<CndVar *>(cond);
int ret = C->notify_one();

if (ret == thrd_success)
return 0;

// TODO: translate error codes.
return -1;
}

} // namespace LIBC_NAMESPACE
Loading