Skip to content

Commit

Permalink
work around gettid not being available on glibc 2.27 and macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Kha committed Oct 28, 2024
1 parent e298702 commit c7086cd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/Init/System/IO.lean
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,11 @@ def run (args : SpawnArgs) : IO String := do

end Process

/-- Returns the thread ID of the calling thread. -/
@[extern "lean_io_get_tid"] opaque getTID : BaseIO UInt32
/--
Returns an ID identifying the current thread. May not be identical to the operating system's thread
id.
-/
@[extern "lean_io_get_thread_id"] opaque getLeanThreadID : BaseIO UInt64

structure AccessRight where
read : Bool := false
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Elab/MutualDef.lean
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ which is reported in the returned snapshot.
-/
def runAsyncAsSnapshot (act : TermElabM Unit) (desc := "") : TermElabM (Task Language.SnapshotTree) := do
let t ← runAsync do
let tid ← IO.getTID
let tid ← IO.getLeanThreadID
modifyTraceState fun _ => { tid }
try
withTraceNode `Elab.async (fun _ => return desc) do
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Util/Trace.lean
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ structure TraceElem where

structure TraceState where
/-- Thread ID, used by `trace.profiler.output`. -/
tid : UInt32 := 0
tid : UInt64 := 0
traces : PersistentArray TraceElem := {}
deriving Inhabited

Expand Down
14 changes: 11 additions & 3 deletions src/runtime/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,17 @@ extern "C" LEAN_EXPORT obj_res lean_io_process_get_pid(obj_arg) {
return lean_io_result_mk_ok(box_uint32(getpid()));
}

extern "C" LEAN_EXPORT obj_res lean_io_get_tid(obj_arg) {
static_assert(sizeof(pid_t) == sizeof(uint32), "pid_t is expected to be a 32-bit type"); // NOLINT
return lean_io_result_mk_ok(box_uint32(gettid()));
extern "C" LEAN_EXPORT obj_res lean_io_get_thread_id(obj_arg) {
uint64_t tid;
#ifdef LEAN_WINDOWS
tid = GetCurrentThreadId();
#elif defined(__APPLE__)
pthread_threadid_np(NULL, &tid);
#else
// should use gettid, but that is glibc 2.30+
tid = static_cast<uint64>(pthread_self())));
#endif
return lean_io_result_mk_ok(box_uint64(static_cast<uint64>(pthread_self())));
}

extern "C" LEAN_EXPORT obj_res lean_io_process_child_wait(b_obj_arg, b_obj_arg child, obj_arg) {
Expand Down

0 comments on commit c7086cd

Please sign in to comment.