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

Add Crystal::System::Thread.current_thread?, #scheduler? #14660

Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions src/crystal/system/thread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Crystal::System::Thread

# def self.current_thread : ::Thread

# def self.current_thread? : ::Thread?

# def self.current_thread=(thread : ::Thread)

# private def system_join : Exception?
Expand Down Expand Up @@ -102,6 +104,11 @@ class Thread
Crystal::System::Thread.current_thread
end

# :nodoc:
def self.current? : Thread?
Crystal::System::Thread.current_thread?
end

# Associates the Thread object to the running system thread.
protected def self.current=(current : Thread) : Thread
Crystal::System::Thread.current_thread = current
Expand All @@ -122,6 +129,11 @@ class Thread
# :nodoc:
getter scheduler : Crystal::Scheduler { Crystal::Scheduler.new(self) }

# :nodoc:
def scheduler? : ::Crystal::Scheduler?
@scheduler
end

protected def start
Thread.threads.push(self)
Thread.current = self
Expand Down
10 changes: 10 additions & 0 deletions src/crystal/system/unix/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ module Crystal::System::Thread
end
end

def self.current_thread? : ::Thread?
if ptr = LibC.pthread_getspecific(@@current_key)
ptr.as(::Thread)
end
end

def self.current_thread=(thread : ::Thread)
ret = LibC.pthread_setspecific(@@current_key, thread.as(Void*))
raise RuntimeError.from_os_error("pthread_setspecific", Errno.new(ret)) unless ret == 0
Expand All @@ -63,6 +69,10 @@ module Crystal::System::Thread
{% else %}
@[ThreadLocal]
class_property current_thread : ::Thread { ::Thread.new }

def self.current_thread? : ::Thread?
@@current_thread
end
{% end %}

private def system_join : Exception?
Expand Down
4 changes: 4 additions & 0 deletions src/crystal/system/win32/thread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ module Crystal::System::Thread
@[ThreadLocal]
class_property current_thread : ::Thread { ::Thread.new }

def self.current_thread? : ::Thread?
@@current_thread
end

private def system_join : Exception?
if LibC.WaitForSingleObject(@system_handle, LibC::INFINITE) != LibC::WAIT_OBJECT_0
return RuntimeError.from_winerror("WaitForSingleObject")
Expand Down
Loading