-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[libc][sched] Implement CPU_ZERO, CPU_ISSET, CPU_SET macros
#131524
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
Changes from 11 commits
365fe47
862d702
b0af3f5
4dc2371
a21734f
dd7f3f6
b21ccb3
597b872
96e2378
4e33001
9a6ad67
c94712f
522c669
9b0d165
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //===-- Definition of macros from sched.h ---------------------------------===// | ||
| // | ||
| // 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_HDR_SCHED_MACROS_H | ||
| #define LLVM_LIBC_HDR_SCHED_MACROS_H | ||
|
|
||
| #ifdef LIBC_FULL_BUILD | ||
|
|
||
| #include "include/llvm-libc-macros/sched-macros.h" | ||
|
|
||
| #else // Overlay mode | ||
|
|
||
| #include <sched.h> | ||
|
|
||
| #endif // LLVM_LIBC_FULL_BUILD | ||
|
|
||
| #endif // LLVM_LIBC_HDR_SCHED_MACROS_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //===-- Proxy for cpu_set_t -----------------------------------------------===// | ||
| // | ||
| // 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_HDR_TYPES_CPU_SET_T_H | ||
| #define LLVM_LIBC_HDR_TYPES_CPU_SET_T_H | ||
|
|
||
| #ifdef LIBC_FULL_BUILD | ||
|
|
||
| #include "include/llvm-libc-types/cpu_set_t.h" | ||
|
|
||
| #else // Overlay mode | ||
|
|
||
| #include <sched.h> | ||
|
|
||
| #endif // LLVM_LIBC_FULL_BUILD | ||
|
|
||
| #endif // LLVM_LIBC_HDR_TYPES_CPU_SET_T_H |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||||||||||||||||
| //===-- Implementation of sched_getcpuisset -------------------------------===// | ||||||||||||||||||
| // | ||||||||||||||||||
| // 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/sched/sched_getcpuisset.h" | ||||||||||||||||||
|
|
||||||||||||||||||
| #include "src/__support/common.h" // LLVM_LIBC_FUNCTION | ||||||||||||||||||
| #include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||||||||||||||||||
|
|
||||||||||||||||||
| #include "hdr/sched_macros.h" // NCPUBITS | ||||||||||||||||||
| #include "hdr/types/cpu_set_t.h" | ||||||||||||||||||
| #include "hdr/types/size_t.h" | ||||||||||||||||||
|
|
||||||||||||||||||
| namespace LIBC_NAMESPACE_DECL { | ||||||||||||||||||
|
|
||||||||||||||||||
| LLVM_LIBC_FUNCTION(int, __sched_getcpuisset, | ||||||||||||||||||
| (int cpu, const size_t cpuset_size, cpu_set_t *set)) { | ||||||||||||||||||
| if (static_cast<size_t>(cpu) / 8 < cpuset_size) { | ||||||||||||||||||
| const size_t element_index = static_cast<size_t>(cpu) / NCPUBITS; | ||||||||||||||||||
| const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS; | ||||||||||||||||||
|
|
||||||||||||||||||
| const unsigned long mask = 1UL << bit_position; | ||||||||||||||||||
| return (set->__mask[element_index] & mask) != 0; | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does the standard say when these pointers are null?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these macros are glibc extensions, the man page itself doesn't define a way to deal with null pointers. i found an implementation of those in glibc's code and surprisingly even that doesn't handle null pointers. ref: #define __CPU_ISSET_S(cpu, setsize, cpusetp) \
(__extension__ \
({ size_t __cpu = (cpu); \
__cpu / 8 < (setsize) \
? ((((const __cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)] \
& __CPUMASK (__cpu))) != 0 \
: 0; }))for implementing these macros ( llvm-project/libc/src/sched/linux/sched_getcpucount.cpp Lines 19 to 26 in c94712f
and even that doesn't handle null pointers! should i implement a nullptr guard for these?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, can you add null check with https://github.com/llvm/llvm-project/blob/main/libc/src/__support/macros/null_check.h for these functions in this PR? Thanks,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done! 522c6697 |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return 0; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| } // namespace LIBC_NAMESPACE_DECL | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //===-- Implementation of sched_setcpuset ---------------------------------===// | ||
| // | ||
| // 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/sched/sched_setcpuset.h" | ||
|
|
||
| #include "src/__support/common.h" // LLVM_LIBC_FUNCTION | ||
| #include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
|
|
||
| #include "hdr/sched_macros.h" // NCPUBITS | ||
| #include "hdr/types/cpu_set_t.h" | ||
| #include "hdr/types/size_t.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(void, __sched_setcpuset, | ||
| (int cpu, const size_t cpuset_size, cpu_set_t *set)) { | ||
| if (static_cast<size_t>(cpu) / 8 < cpuset_size) { | ||
| const size_t element_index = static_cast<size_t>(cpu) / NCPUBITS; | ||
| const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS; | ||
|
|
||
| const unsigned long mask = 1UL << bit_position; | ||
| set->__mask[element_index] |= mask; | ||
| } | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //===-- Implementation of sched_setcpuzero --------------------------------===// | ||
| // | ||
| // 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/sched/sched_setcpuzero.h" | ||
|
|
||
| #include "src/__support/common.h" // LLVM_LIBC_FUNCTION | ||
| #include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
|
|
||
| #include "hdr/types/cpu_set_t.h" | ||
| #include "hdr/types/size_t.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(void, __sched_setcpuzero, | ||
| (const size_t cpuset_size, cpu_set_t *set)) { | ||
| __builtin_memset(set, 0, cpuset_size); | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //===-- Implementation header for sched_getcpuisset -------------*- 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_SCHED_SCHED_GETCPUISSET_H | ||
| #define LLVM_LIBC_SRC_SCHED_SCHED_GETCPUISSET_H | ||
|
|
||
| #include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
|
|
||
| #include "hdr/types/cpu_set_t.h" | ||
| #include "hdr/types/size_t.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| // for internal use in the CPU_ISSET macro | ||
| int __sched_getcpuisset(int cpu, const size_t cpuset_size, cpu_set_t *set); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_SCHED_SCHED_GETCPUISSET_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //===-- Implementation header for sched_setcpuset ---------------*- 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_SCHED_SCHED_SETCPUSET_H | ||
| #define LLVM_LIBC_SRC_SCHED_SCHED_SETCPUSET_H | ||
|
|
||
| #include "src/__support/macros/config.h" | ||
|
|
||
| #include "hdr/types/cpu_set_t.h" | ||
| #include "hdr/types/size_t.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| // for internal use in the CPU_SET macro | ||
| void __sched_setcpuset(int cpu, const size_t cpuset_size, cpu_set_t *set); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_SCHED_SCHED_SETCPUSET_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //===-- Implementation header for sched_setcpuzero --------------*- 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_SCHED_SCHED_SETCPUZERO_H | ||
| #define LLVM_LIBC_SRC_SCHED_SCHED_SETCPUZERO_H | ||
|
|
||
| #include "src/__support/macros/config.h" | ||
|
|
||
| #include "hdr/types/cpu_set_t.h" | ||
| #include "hdr/types/size_t.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| // for internal use in the CPU_ZERO macro | ||
| void __sched_setcpuzero(const size_t cpuset_size, cpu_set_t *set); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_SCHED_SCHED_SETCPUZERO_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need parens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in c94712f1