Skip to content

Commit

Permalink
@th -> @system_handle
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Oct 31, 2023
1 parent 60e9920 commit 40a5aef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/crystal/system/thread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Thread
# all thread objects, so the GC can see them (it doesn't scan thread locals)
protected class_getter(threads) { Thread::LinkedList(Thread).new }

@th : Crystal::System::Thread::Handle
@system_handle : Crystal::System::Thread::Handle
@exception : Exception?
@detached = Atomic::Flag.new

Expand All @@ -55,15 +55,15 @@ class Thread

# Creates and starts a new system thread.
def initialize(&@func : ->)
@th = uninitialized Crystal::System::Thread::Handle
@system_handle = uninitialized Crystal::System::Thread::Handle
init_handle
end

# Used once to initialize the thread object representing the main thread of
# the process (that already exists).
def initialize
@func = ->{}
@th = Crystal::System::Thread.current_handle
@system_handle = Crystal::System::Thread.current_handle
@main_fiber = Fiber.new(stack_address, self)

Thread.threads.push(self)
Expand Down
22 changes: 11 additions & 11 deletions src/crystal/system/unix/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ module Crystal::System::Thread
alias Handle = LibC::PthreadT

def to_unsafe
@th
@system_handle
end

private def init_handle
# NOTE: the thread may start before `pthread_create` returns, so `@th` must
# be set as soon as possible; we cannot use a separate handle and assign it
# to `@th`, which would have been too late
# NOTE: the thread may start before `pthread_create` returns, so
# `@system_handle` must be set as soon as possible; we cannot use a separate
# handle and assign it to `@system_handle`, which would have been too late
ret = GC.pthread_create(
thread: pointerof(@th),
thread: pointerof(@system_handle),
attr: Pointer(LibC::PthreadAttrT).null,
start: ->(data : Void*) { data.as(::Thread).start; Pointer(Void).null },
arg: self.as(Void*),
Expand Down Expand Up @@ -63,40 +63,40 @@ module Crystal::System::Thread
{% end %}

private def system_join : Exception?
ret = GC.pthread_join(@th)
ret = GC.pthread_join(@system_handle)
RuntimeError.from_os_error("pthread_join", Errno.new(ret)) unless ret == 0
end

private def system_close
GC.pthread_detach(@th)
GC.pthread_detach(@system_handle)
end

private def stack_address : Void*
address = Pointer(Void).null

{% if flag?(:darwin) %}
# FIXME: pthread_get_stacksize_np returns bogus value on macOS X 10.9.0:
address = LibC.pthread_get_stackaddr_np(@th) - LibC.pthread_get_stacksize_np(@th)
address = LibC.pthread_get_stackaddr_np(@system_handle) - LibC.pthread_get_stacksize_np(@system_handle)
{% elsif flag?(:bsd) && !flag?(:openbsd) %}
ret = LibC.pthread_attr_init(out attr)
unless ret == 0
LibC.pthread_attr_destroy(pointerof(attr))
raise RuntimeError.from_os_error("pthread_attr_init", Errno.new(ret))
end

if LibC.pthread_attr_get_np(@th, pointerof(attr)) == 0
if LibC.pthread_attr_get_np(@system_handle, pointerof(attr)) == 0
LibC.pthread_attr_getstack(pointerof(attr), pointerof(address), out _)
end
ret = LibC.pthread_attr_destroy(pointerof(attr))
raise RuntimeError.from_os_error("pthread_attr_destroy", Errno.new(ret)) unless ret == 0
{% elsif flag?(:linux) %}
if LibC.pthread_getattr_np(@th, out attr) == 0
if LibC.pthread_getattr_np(@system_handle, out attr) == 0
LibC.pthread_attr_getstack(pointerof(attr), pointerof(address), out _)
end
ret = LibC.pthread_attr_destroy(pointerof(attr))
raise RuntimeError.from_os_error("pthread_attr_destroy", Errno.new(ret)) unless ret == 0
{% elsif flag?(:openbsd) %}
ret = LibC.pthread_stackseg_np(@th, out stack)
ret = LibC.pthread_stackseg_np(@system_handle, out stack)
raise RuntimeError.from_os_error("pthread_stackseg_np", Errno.new(ret)) unless ret == 0

address =
Expand Down
10 changes: 5 additions & 5 deletions src/crystal/system/win32/thread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ module Crystal::System::Thread
alias Handle = LibC::HANDLE

def to_unsafe
@th
@system_handle
end

private def init_handle
@th = GC.beginthreadex(
@system_handle = GC.beginthreadex(
security: Pointer(Void).null,
stack_size: LibC::UInt.zero,
start_address: ->(data : Void*) { data.as(::Thread).start; LibC::UInt.zero },
Expand Down Expand Up @@ -37,16 +37,16 @@ module Crystal::System::Thread
class_property current_thread : ::Thread { ::Thread.new }

private def system_join : Exception?
if LibC.WaitForSingleObject(@th, LibC::INFINITE) != LibC::WAIT_OBJECT_0
if LibC.WaitForSingleObject(@system_handle, LibC::INFINITE) != LibC::WAIT_OBJECT_0
return RuntimeError.from_winerror("WaitForSingleObject")
end
if LibC.CloseHandle(@th) == 0
if LibC.CloseHandle(@system_handle) == 0
return RuntimeError.from_winerror("CloseHandle")
end
end

private def system_close
LibC.CloseHandle(@th)
LibC.CloseHandle(@system_handle)
end

private def stack_address : Void*
Expand Down

0 comments on commit 40a5aef

Please sign in to comment.