[libc-commits] [libc] [libc][stdio] Implement putc_unlocked (PR #243329)
Sachin 🗲 via libc-commits
libc-commits at lists.llvm.org
Mon Nov 16 20:48:51 PST 2026
https://github.com/s4chinjha updated https://github.com/llvm/llvm-project/pull/243329
>From 31404a9726d8a1b80843ef54c4a6292173a6b369 Mon Sep 17 00:00:00 2001
From: s4chinjha <s4chinjha at gmail.com>
Date: Sun, 15 Nov 2026 12:50:45 +0530
Subject: [PATCH] [libc][stdio] Implement putc_unlocked
Implements `putc_unlocked` for Linux full builds.
Adds the entrypoint, internal header, stdio.yaml declaration, CMake targets,
entrypoint entries for aarch64/riscv/x86_64, and three unit tests.
Part of #242485
Assisted with automated tooling.
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/include/stdio.yaml | 7 ++
libc/src/stdio/CMakeLists.txt | 1 +
libc/src/stdio/generic/CMakeLists.txt | 14 +++
libc/src/stdio/generic/putc_unlocked.cpp | 40 ++++++++
libc/src/stdio/putc_unlocked.h | 26 +++++
libc/test/src/stdio/CMakeLists.txt | 21 ++++
libc/test/src/stdio/putc_unlocked_test.cpp | 112 +++++++++++++++++++++
10 files changed, 224 insertions(+)
create mode 100644 libc/src/stdio/generic/putc_unlocked.cpp
create mode 100644 libc/src/stdio/putc_unlocked.h
create mode 100644 libc/test/src/stdio/putc_unlocked_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ede4d139b150f..6ad4b95bf22d9 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1398,6 +1398,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.perror
libc.src.stdio.popen
libc.src.stdio.putc
+ libc.src.stdio.putc_unlocked
libc.src.stdio.putchar
libc.src.stdio.puts
libc.src.stdio.rewind
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 863b300f83624..d275b95ddba4b 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1613,6 +1613,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.perror
libc.src.stdio.popen
libc.src.stdio.putc
+ libc.src.stdio.putc_unlocked
libc.src.stdio.putchar
libc.src.stdio.puts
libc.src.stdio.rewind
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 11aa163d63b91..5abcabae0cef5 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1617,6 +1617,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.perror
libc.src.stdio.popen
libc.src.stdio.putc
+ libc.src.stdio.putc_unlocked
libc.src.stdio.putchar
libc.src.stdio.puts
libc.src.stdio.rewind
diff --git a/libc/include/stdio.yaml b/libc/include/stdio.yaml
index 4a54f4673c639..e7f7282a3fda4 100644
--- a/libc/include/stdio.yaml
+++ b/libc/include/stdio.yaml
@@ -373,6 +373,13 @@ functions:
arguments:
- type: int
- type: FILE *
+ - name: putc_unlocked
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: int
+ - type: FILE *
- name: putchar
standards:
- stdc
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index cba596c67345d..fec2b054cf909 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -377,6 +377,7 @@ add_stdio_entrypoint_object(fwrite_unlocked)
add_stdio_entrypoint_object(fwrite)
add_stdio_entrypoint_object(fputc)
add_stdio_entrypoint_object(putc)
+add_stdio_entrypoint_object(putc_unlocked)
add_stdio_entrypoint_object(putchar)
add_stdio_entrypoint_object(printf)
add_stdio_entrypoint_object(fprintf)
diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index 33f7524c93a9b..c1d652a493cae 100644
--- a/libc/src/stdio/generic/CMakeLists.txt
+++ b/libc/src/stdio/generic/CMakeLists.txt
@@ -380,6 +380,20 @@ add_generic_entrypoint_object(
libc.src.__support.File.platform_file
)
+add_generic_entrypoint_object(
+ putc_unlocked
+ SRCS
+ putc_unlocked.cpp
+ HDRS
+ ../putc_unlocked.h
+ DEPENDS
+ libc.hdr.types.FILE
+ libc.hdr.types.size_t
+ libc.src.__support.File.file
+ libc.src.__support.File.platform_file
+ libc.src.errno.errno
+)
+
add_generic_entrypoint_object(
putchar
SRCS
diff --git a/libc/src/stdio/generic/putc_unlocked.cpp b/libc/src/stdio/generic/putc_unlocked.cpp
new file mode 100644
index 0000000000000..376ff5919f233
--- /dev/null
+++ b/libc/src/stdio/generic/putc_unlocked.cpp
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of putc_unlocked.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/putc_unlocked.h"
+#include "src/__support/File/file.h"
+
+#include "hdr/types/FILE.h"
+#include "hdr/types/size_t.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, putc_unlocked, (int c, ::FILE *stream)) {
+ unsigned char uc = static_cast<unsigned char>(c);
+
+ auto result =
+ reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->write_unlocked(&uc, 1);
+ if (result.has_error())
+ libc_errno = result.error;
+ size_t written = result.value;
+
+ if (1 != written) {
+ // The stream should be in an error state in this case.
+ return EOF;
+ }
+ return static_cast<int>(uc);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/putc_unlocked.h b/libc/src/stdio/putc_unlocked.h
new file mode 100644
index 0000000000000..03718e121a0e9
--- /dev/null
+++ b/libc/src/stdio/putc_unlocked.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header of putc_unlocked.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDIO_PUTC_UNLOCKED_H
+#define LLVM_LIBC_SRC_STDIO_PUTC_UNLOCKED_H
+
+#include "hdr/types/FILE.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int putc_unlocked(int c, ::FILE *stream);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDIO_PUTC_UNLOCKED_H
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index e813627f37b8b..f4f8067d75b7d 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -523,6 +523,27 @@ add_libc_test(
libc.src.stdio.fread
)
+add_libc_test(
+ putc_unlocked_test
+ SUITE
+ libc-stdio-tests
+ SRCS
+ putc_unlocked_test.cpp
+ DEPENDS
+ libc.hdr.stdio_macros
+ libc.src.__support.CPP.scope
+ libc.src.stdio.fclose
+ libc.src.stdio.ferror
+ libc.src.stdio.flockfile
+ libc.src.stdio.fopen
+ libc.src.stdio.fread
+ libc.src.stdio.funlockfile
+ libc.src.stdio.putc_unlocked
+ libc.src.string.memcmp
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
if(${LIBC_TARGET_OS} STREQUAL "linux")
add_libc_test(
remove_test
diff --git a/libc/test/src/stdio/putc_unlocked_test.cpp b/libc/test/src/stdio/putc_unlocked_test.cpp
new file mode 100644
index 0000000000000..3ad220390c23e
--- /dev/null
+++ b/libc/test/src/stdio/putc_unlocked_test.cpp
@@ -0,0 +1,112 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for putc_unlocked.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/stdio_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/stdio/fclose.h"
+#include "src/stdio/ferror.h"
+#include "src/stdio/flockfile.h"
+#include "src/stdio/fopen.h"
+#include "src/stdio/fread.h"
+#include "src/stdio/funlockfile.h"
+#include "src/stdio/putc_unlocked.h"
+#include "src/string/memcmp.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LIBC_NAMESPACE::cpp::scope_exit;
+
+class LlvmLibcPutcUnlockedTest
+ : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {};
+
+TEST_F(LlvmLibcPutcUnlockedTest, WriteBoundaryAndMultipleChars) {
+ const auto FILENAME = libc_make_test_file_path("putc_unlocked.test");
+ ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
+ ASSERT_FALSE(file == nullptr);
+
+ constexpr int INPUT_CHARS[] = {0, 1, 'A', 127, 128, 255,
+ 0x141, 'a', 'b', 'c', 0x100, 0x1ff};
+ constexpr size_t NUM_CHARS = sizeof(INPUT_CHARS) / sizeof(INPUT_CHARS[0]);
+ unsigned char expected[NUM_CHARS];
+
+ {
+ scope_exit close_file(
+ [&] { ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds()); });
+
+ for (size_t i = 0; i < NUM_CHARS; ++i) {
+ expected[i] = static_cast<unsigned char>(INPUT_CHARS[i]);
+ int expected_ret = static_cast<int>(expected[i]);
+ ASSERT_THAT(LIBC_NAMESPACE::putc_unlocked(INPUT_CHARS[i], file),
+ Succeeds(expected_ret));
+ }
+ }
+
+ // Data should be visible after fclose.
+ file = LIBC_NAMESPACE::fopen(FILENAME, "r");
+ ASSERT_FALSE(file == nullptr);
+ scope_exit close_file(
+ [&] { ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds()); });
+
+ unsigned char read_buf[NUM_CHARS];
+ ASSERT_THAT(LIBC_NAMESPACE::fread(read_buf, 1, NUM_CHARS, file),
+ Succeeds(NUM_CHARS));
+ EXPECT_EQ(LIBC_NAMESPACE::memcmp(read_buf, expected, NUM_CHARS), 0);
+}
+
+TEST_F(LlvmLibcPutcUnlockedTest, ReadOnlyStream) {
+ const auto FILENAME = libc_make_test_file_path("putc_unlocked_readonly.test");
+ ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
+ ASSERT_FALSE(file == nullptr);
+ ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds());
+
+ file = LIBC_NAMESPACE::fopen(FILENAME, "r");
+ ASSERT_FALSE(file == nullptr);
+ scope_exit close_file(
+ [&] { ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds()); });
+
+ ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
+ ASSERT_THAT(LIBC_NAMESPACE::putc_unlocked('X', file), Fails(EBADF, EOF));
+ EXPECT_NE(LIBC_NAMESPACE::ferror(file), 0);
+}
+
+TEST_F(LlvmLibcPutcUnlockedTest, WriteUnderLock) {
+ const auto FILENAME = libc_make_test_file_path("putc_unlocked_lock.test");
+ ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
+ ASSERT_FALSE(file == nullptr);
+ {
+ scope_exit close_file(
+ [&] { ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds()); });
+
+ LIBC_NAMESPACE::flockfile(file);
+ scope_exit unlock_file([&] { LIBC_NAMESPACE::funlockfile(file); });
+
+ constexpr int chars[] = {'L', 'O', 'C', 'K', 0, 128, 255};
+ for (int c : chars) {
+ ASSERT_THAT(LIBC_NAMESPACE::putc_unlocked(c, file),
+ Succeeds(static_cast<int>(static_cast<unsigned char>(c))));
+ }
+ }
+
+ file = LIBC_NAMESPACE::fopen(FILENAME, "r");
+ ASSERT_FALSE(file == nullptr);
+ scope_exit close_file(
+ [&] { ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds()); });
+
+ unsigned char read_buf[7];
+ ASSERT_THAT(LIBC_NAMESPACE::fread(read_buf, 1, sizeof(read_buf), file),
+ Succeeds(sizeof(read_buf)));
+ unsigned char expected[] = {'L', 'O', 'C', 'K', 0, 128, 255};
+ EXPECT_EQ(LIBC_NAMESPACE::memcmp(read_buf, expected, sizeof(expected)), 0);
+}
More information about the libc-commits
mailing list