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 e11f1a1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions std/runtime/sysconf_darwin.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2025 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

const _SC_NPROCESSORS_ONLN = 58
5 changes: 5 additions & 0 deletions std/runtime/sysconf_linux.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2025 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

const _SC_NPROCESSORS_ONLN = 84
10 changes: 10 additions & 0 deletions std/runtime/sysconf_windows.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2025 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

cpp unsafe fn GetSystemInfo(info: *cpp.SYSTEM_INFO)

#typedef
cpp struct SYSTEM_INFO {
dwNumberOfProcessors: _DWORD
}
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 e11f1a1

Please sign in to comment.