Skip to content
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

Fix error handling for LibC.clock_gettime(CLOCK_MONOTONIC) calls #15309

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/crystal/system/unix/pthread_condition_variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class Thread

LibC.pthread_cond_timedwait_relative_np(self, mutex, pointerof(ts))
{% else %}
LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out ts)
clock_gettime_ret = LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out ts)
raise RuntimeError.from_errno("clock_gettime(CLOCK_MONOTONIC)") unless clock_gettime_ret == 0
ts.tv_sec += time.to_i
ts.tv_nsec += time.nanoseconds

Expand Down
8 changes: 4 additions & 4 deletions src/crystal/system/unix/time.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ module Crystal::System::Time
nanoseconds = total_nanoseconds.remainder(NANOSECONDS_PER_SECOND)
{seconds.to_i64, nanoseconds.to_i32}
{% else %}
if LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out tp) == 1
raise RuntimeError.from_errno("clock_gettime(CLOCK_MONOTONIC)")
end
ret = LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out tp)
raise RuntimeError.from_errno("clock_gettime(CLOCK_MONOTONIC)") unless ret == 0
{tp.tv_sec.to_i64, tp.tv_nsec.to_i32}
{% end %}
end
Expand All @@ -51,7 +50,8 @@ module Crystal::System::Time
info = mach_timebase_info
LibC.mach_absolute_time &* info.numer // info.denom
{% else %}
LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out tp)
ret = LibC.clock_gettime(LibC::CLOCK_MONOTONIC, out tp)
raise RuntimeError.from_errno("clock_gettime(CLOCK_MONOTONIC)") unless ret == 0
Copy link
Contributor

@ysbaddaden ysbaddaden Dec 22, 2024

Choose a reason for hiding this comment

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

This specific method mustn't raise. It could make sure to initialize tp to zero. That's fine.

tp.tv_sec.to_u64! &* NANOSECONDS_PER_SECOND &+ tp.tv_nsec.to_u64!
{% end %}
end
Expand Down
Loading