/* * unix_compat.c * Copyright (C) 2017 Kovid Goyal * * Distributed under terms of the GPL3 license. */ #include "choose-data-types.h" #include #include #include #include #include #ifdef __APPLE__ #ifndef _SC_NPROCESSORS_ONLN #define _SC_NPROCESSORS_ONLN 58 #endif #endif int cpu_count(void) { return sysconf(_SC_NPROCESSORS_ONLN); } void* alloc_threads(size_t num_threads) { return calloc(num_threads, sizeof(pthread_t)); } bool start_thread(void* threads, size_t i, void *(*start_routine) (void *), void *arg) { int rc; if ((rc = pthread_create(((pthread_t*)threads) + i, NULL, start_routine, arg))) { fprintf(stderr, "Failed to create thread, with error: %s\n", strerror(rc)); return false; } return true; } void wait_for_thread(void *threads, size_t i) { pthread_join(((pthread_t*)(threads))[i], NULL); } void free_threads(void *threads) { free(threads); }