Skip to content

Commit 1518809

Browse files
committed
Add nob_nprocs()
1 parent 4397d1f commit 1518809

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

nob.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
# include <sys/types.h>
163163
# include <sys/wait.h>
164164
# include <sys/stat.h>
165+
# include <sys/sysinfo.h>
165166
# include <unistd.h>
166167
# include <fcntl.h>
167168
#endif
@@ -391,6 +392,9 @@ typedef struct {
391392

392393
NOBDEF bool nob_cmd_run_opt(Nob_Cmd *cmd, Nob_Cmd_Opt opt);
393394

395+
// Get amount of processors on the machine.
396+
NOBDEF int nob_nprocs(void);
397+
394398
// See https://x.com/vkrajacic/status/1749816169736073295 for more info on how to use such macros
395399
#define nob_cmd_run(cmd, ...) nob_cmd_run_opt((cmd), (Nob_Cmd_Opt){__VA_ARGS__})
396400

@@ -985,6 +989,17 @@ static void nob__win32_cmd_quote(Nob_Cmd cmd, Nob_String_Builder *quoted)
985989
}
986990
#endif
987991

992+
NOBDEF int nob_nprocs(void)
993+
{
994+
#ifdef _WIN32
995+
SYSTEM_INFO siSysInfo;
996+
GetSystemInfo(&siSysInfo);
997+
return siSysInfo.dwNumberOfProcessors;
998+
#else
999+
return sysconf(_SC_NPROCESSORS_ONLN);
1000+
#endif
1001+
}
1002+
9881003
NOBDEF bool nob_cmd_run_opt(Nob_Cmd *cmd, Nob_Cmd_Opt opt)
9891004
{
9901005
Nob_Proc proc = nob_cmd_start_process(*cmd, opt.fdin, opt.fdout, opt.fderr);
@@ -1014,6 +1029,7 @@ NOBDEF bool nob_cmd_run_opt(Nob_Cmd *cmd, Nob_Cmd_Opt opt)
10141029

10151030
return true;
10161031
}
1032+
10171033
NOBDEF Nob_Proc nob_cmd_run_async_redirect(Nob_Cmd cmd, Nob_Cmd_Redirect redirect)
10181034
{
10191035
return nob_cmd_start_process(cmd, redirect.fdin, redirect.fdout, redirect.fderr);
@@ -2136,14 +2152,20 @@ NOBDEF int closedir(DIR *dirp)
21362152
#define sv_from_parts nob_sv_from_parts
21372153
#define sb_to_sv nob_sb_to_sv
21382154
#define win32_error_message nob_win32_error_message
2155+
#define get_procs_number nob_nprocs
21392156
#endif // NOB_STRIP_PREFIX
21402157
#endif // NOB_STRIP_PREFIX_GUARD_
21412158

21422159
/*
21432160
Revision history:
21442161
2145-
1.23.0 (2025-08-15) Add nob_cmd_run(), nob_cmd_run_opt(), nob_cmd_start_process(), Nob_Cmd_Opt and deprecate all other nob_cmd_run_* functions (by @rexim)
2162+
1.23.0 (2025-08-15) Add nob_cmd_run(),
2163+
nob_cmd_run_opt(),
2164+
nob_cmd_start_process(),
2165+
Nob_Cmd_Opt (by @rexim)
2166+
Deprecate all other nob_cmd_run_* functions (by @rexim)
21462167
Add NOB_DECLTYPE_CAST() for C++-compatible casting of allocation results (by @rexim)
2168+
Add nob_nprocs() (by @rexim)
21472169
Add NOB_DEPRECATED() (by @yuI4140)
21482170
1.22.0 (2025-08-12) Add NOBDEF macro to the beginning of function declarations (by @minefreak19)
21492171
Add more flags to MSVC nob_cc_flags() (by @PieVieRo)

0 commit comments

Comments
 (0)