1#include "clod_thread_config.h"
2#include "thread_impl.h"
14 clod_process_main *main;
17void *clod_process_pthread_main(
void *ptr) {
20 thread_args->main(args->arg_count, args->arg_vector);
25enum clod_process_result clod_process_start_pthread(
31 fprintf(stderr,
"libclod: clod_process_start_pthread: no thread main function given.\n");
33 return CLOD_PROCESS_INVALID;
37 fprintf(stderr,
"libclod: clod_process_start_pthread: given %d, but only CLOD_THREAD (1) is supported here.\n", opts->
type);
39 return CLOD_PROCESS_INVALID;
48 char *data = malloc(ALIGN(
sizeof(
struct thread_args), 16) + args_size(&args_in));
53 args_copy(args, &args_in);
56 pthread_attr_init(&attr);
57 pthread_attr_setdetachstate(&attr, process_out ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED);
59 pthread_attr_setstacksize(&attr, opts->
stack_size);
62 const int res = pthread_create(&thread, &attr, clod_process_pthread_main, data);
66 fprintf(stderr,
"libclod: pthread_create: %d\n", res);
68 if (errno == EAGAIN)
return CLOD_PROCESS_NO_MEMORY;
69 return CLOD_PROCESS_INVALID;
75 process->thread = thread;
76 *process_out = &process->common;
78 pthread_detach(thread);
81 return CLOD_PROCESS_OK;
86 const int res = pthread_join(pthread_process->thread,
nullptr);
87 free(pthread_process);
88 if (res != 0)
return CLOD_PROCESS_INVALID;
89 return CLOD_PROCESS_OK;
clod_process_main * main
Process entry point.
int arg_count
Number of arguments passed to main.
enum clod_process_type type
Type of process to create.