[libc-commits] [libc] [libc] Implement sleep and usleep for Linux (PR #205922)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Tue Jul 21 07:30:08 PDT 2026


================
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Linux implementation of sleep.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/sleep.h"
+#include "hdr/errno_macros.h"
+#include "hdr/types/struct_timespec.h"
+#include "hdr/types/time_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/nanosleep.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(time_t) == sizeof(int64_t),
+              "32-bit time_t is not supported");
+
+LLVM_LIBC_FUNCTION(unsigned int, sleep, (unsigned int seconds)) {
+  if (seconds == 0)
+    return 0;
+
+  unsigned int remaining = seconds;
+  while (remaining > 0) {
----------------
labath wrote:

I don't get the purpose of this loop. I don't think that nanosleep does spurious wakeups. It should either sleep the full amount of time, or return an error.

https://github.com/llvm/llvm-project/pull/205922


More information about the libc-commits mailing list