[Suggestion] Add a way to create a process for a func.
This idea come out from a project I have where. I had to compile two dependencies(tree-sitter , tree-sitter-asm) with C, plus the actual main file because I didn't want Make or other thing apart from C and git dependencies. It Would be cool.
An Example
Nob_Cmd cmd = { 0 };
bool
compile_tree_sitter_asm(void* arg)
{
//compile....
}
bool
compile_tree_sitter(void* arg)
{
//compile....
}
bool
setup_dependencies(void)
{
Nob_Procs procs = { 0 };
// git clone tree-sitter and
// tree-sitter-asm with Cmd
if (!nob_procs_flush(&procs))
return false;
/*
Compile at the same time tree-sitter
and tree-sitter-asm.
Example of it:
Example of declaration:
nob_procs_create(
Nob_Procs*procs,
bool(*Nob_VFn)(void*) vfn,
void *arg);
if (!nob_procs_create(&procs,compile_tree_sitter_asm,NULL))
return false;
if (!nob_procs_create(&procs,compile_tree_sitter,NULL))
return false;
if (!nob_procs_flush(&procs))
return false;
*/
return true;
}
I believe what you're describing here is called threads.
I believe what you're describing here is called threads.
Hmm, but there is a threads library portable or cross-platform? I don't know, pthreads are third party on windows so I didn't want to include them. could we add threads? Well maybe is a bad idea to add them on nob. though pthreads are POSIX and Windows has Their own threads. CreateThread WaitForMultipleObjects
I believe what you're describing here is called threads.
Hmm, but there is a threads library portable or cross-platform? I don't know, pthreads are third party on windows so I didn't want to include them. could we add threads? Well maybe is a bad idea to add them on nob. though pthreads are POSIX and Windows has Their own threads. CreateThread WaitForMultipleObjects
Threads inherently are platform-dependent (or rather, kernel dependent), since they need to be operated on within kernel space. A reason to add a threading API to Nob would be to provide a C99 alternative to C11 threads. There are most likely a library out there already for that, including pthreads-win32 which attempts to glue together Windows Threads and pthreads (which could be useful for your project?).
It could be an interesting topic for streams to see an implementation of a threading API using only Linux Syscalls (clone) and then combine with Windows syscalls/API for a library, which then could be implemented in Nob. Similar to the Coroutine series.
But for your project, if you have C11 threads, you can throw that up really quick (untested, just as example):
Details
#include <stdatomic.h>
#include <threads.h>
atomic_bool tree_sitter_asm_done = false;
atomic_bool tree_sitter_done = false;
int get_treesitter()
{
// I've fabricated the urls instead of checking them.
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, "git", "clone", "--depth=1", "https://github.com/tree-sitter/tree-sitter");
nob_cmd_run(&cmd, .no_reset = true);
--cmd.count; // Remove URL
nob_cmd_append(&cmd, "https://github.com/tree-sitter-asm/tree-sitter-asm");
nob_cmd_free(&cmd);
return 0;
}
int compile_tree_sitter_asm(void *)
{
Nob_cmd cmd = {0};
// ...
atomic_store(&tree_sitter_asm_done, true);
return 0;
}
int compile_tree_sitter(void *)
{
Nob_cmd cmd = {0};
// ...
atomic_store(&tree_sitter_done, true);
return 0;
}
int setup_deps(void)
{
if (get_treesitter()) { return 0; }
thrd_t tree_sitter_asm = {0};
if (thrd_success != thrd_create(&tree_sitter_asm, compile_tree_sitter_asm, nullptr)) { return 0; }
thrd_t tree_sitter = {0};
if (thrd_success != thrd_create(&tree_sitter, compile_tree_sitter, nullptr)) { return 0; }
int running_threads = 2;
for (;;)
{
if (tree_sitter_asm_done)
{
int ret = 0;
if (thrd_success != thrd_join(&tree_sitter_asm, &ret)) { return 0; }
if (ret) { return ret; }
atomic_store(&tree_sitter_asm_done, false);
--running_threads;
}
if (tree_sitter_done)
{
int ret = 0;
if (thrd_success != thrd_join(&tree_sitter, &ret)) { return 0; }
if (ret) { return ret; }
atomic_store(&tree_sitter_done, false);
--running_threads;
}
if (running_threads == 0)
{
break;
}
}
return 1;
}