
#ifndef ETHERCAT_UTILS_DARRA_ASYNC_H
#define ETHERCAT_UTILS_DARRA_ASYNC_H

#pragma once

#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct thread_s* thread_t;

typedef int (*thread_func_t)(void* arg);

#define ASYNC_OK            0
#define ASYNC_ERR_PARAM    -1
#define ASYNC_ERR_OOM      -2
#define ASYNC_ERR_OS       -3
#define ASYNC_ERR_TIMEOUT  -4
#define ASYNC_ERR_DETACHED -5
#define ASYNC_ERR_NOT_RUN  -6

int thread_create(thread_func_t thread_func, void* arg,
                         thread_t* out_handle);

int thread_join(thread_t handle, uint32_t timeout_ms,
                       int* out_exit_code);

int thread_detach(thread_t handle);

void thread_sleep_ms(uint32_t ms);

void thread_yield(void);

#ifdef __cplusplus
}
#endif

#endif
