Skip to content

Commit

Permalink
runtime: add the MAXPROCS function
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jan 4, 2025
1 parent 1fc2814 commit 81b81bb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions std/runtime/thread.jule
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ const threadSC_EmptySelect = 1 << 0
// in the threads. Subsequent thread generations may use the same allocation
// of closed threads for the new spawned threads.
// threadCases stores special cases for thread management.
// maxprocs stores total number of logical threads.
static threadMutex = fmutex{}
static mut threads = (&thread)(nil)
static mut threadCases = threadSC_NA
static mut maxprocs = 0

// Returns the number of cores.
// Returns zero until data initialized by the runtime.
fn MAXPROCS(): int { ret maxprocs }

// Allocates a new thread and sets state as running.
fn newThread(): &thread {
Expand Down
3 changes: 3 additions & 0 deletions std/runtime/thread_unix.jule
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cpp fn pthread_equal(cpp.pthread_t, cpp.pthread_t): bool
cpp fn pthread_detach(cpp.pthread_t): int
cpp fn pthread_self(): cpp.pthread_t
cpp fn sched_yield(): int
cpp fn sysconf(name: integ::Int): integ::Long

#typedef
cpp struct pthread_t{}
Expand Down Expand Up @@ -66,6 +67,8 @@ fn init() {
// Push the main thread to threads.
// See the documentation of the pushNewThread function.
threadMutex.lock()
// We already locked the mutex, set maxproc too.
maxprocs = unsafe { int(cpp.sysconf(_SC_NPROCESSORS_ONLN)) }
mut t := newThread()
t.os.handle = currentThreadID()
threads = t
Expand Down
6 changes: 6 additions & 0 deletions std/runtime/thread_windows.jule
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ fn init() {
// Push the main thread to threads.
// See the documentation of the pushNewThread function.
threadMutex.lock()
{
// We already locked the mutex, set maxproc too.
let info: cpp.SYSTEM_INFO
unsafe { cpp.GetSystemInfo(&info) }
maxprocs = int(info.dwNumberOfProcessors)
}
mut t := newThread()
t.os.id = currentThreadID()
threads = t
Expand Down

0 comments on commit 81b81bb

Please sign in to comment.