/* * category: [misc] * apply status: * edit status: * build status: * description: */ #ifndef __TOOLKIT_GLOBAL_H__ #define __TOOLKIT_GLOBAL_H__ #include "config.h" #include #include "errorno.h" #include "memutil.h" #ifdef __cplusplus extern "C" { #endif #ifndef _WIN32 #include "unix.h" #else #include "win.h" #endif //NOT _WIN32 /*version component*/ TOOLKIT_API unsigned int toolkit_version(void); TOOLKIT_API const char* toolkit_version_string(void); /** errno compoent*/ #define TOOLKIT_ERRNO_MAP(XX) \ XX(E2BIG, "argument list too long") \ XX(EACCES, "permission denied") \ XX(EADDRINUSE, "address already in use") \ XX(EADDRNOTAVAIL, "address not available") \ XX(EAFNOSUPPORT, "address family not supported") \ XX(EAGAIN, "resource temporarily unavailable") \ XX(EAI_ADDRFAMILY, "address family not supported") \ XX(EAI_AGAIN, "temporary failure") \ XX(EAI_BADFLAGS, "bad ai_flags value") \ XX(EAI_BADHINTS, "invalid value for hints") \ XX(EAI_CANCELED, "request canceled") \ XX(EAI_FAIL, "permanent failure") \ XX(EAI_FAMILY, "ai_family not supported") \ XX(EAI_MEMORY, "out of memory") \ XX(EAI_NODATA, "no address") \ XX(EAI_NONAME, "unknown node or service") \ XX(EAI_OVERFLOW, "argument buffer overflow") \ XX(EAI_PROTOCOL, "resolved protocol is unknown") \ XX(EAI_SERVICE, "service not available for socket type") \ XX(EAI_SOCKTYPE, "socket type not supported") \ XX(EALREADY, "connection already in progress") \ XX(EBADF, "bad file descriptor") \ XX(EBUSY, "resource busy or locked") \ XX(ECANCELED, "operation canceled") \ XX(ECHARSET, "invalid Unicode character") \ XX(ECONNABORTED, "software caused connection abort") \ XX(ECONNREFUSED, "connection refused") \ XX(ECONNRESET, "connection reset by peer") \ XX(EDESTADDRREQ, "destination address required") \ XX(EEXIST, "file already exists") \ XX(EFAULT, "bad address in system call argument") \ XX(EFBIG, "file too large") \ XX(EHOSTUNREACH, "host is unreachable") \ XX(EINTR, "interrupted system call") \ XX(EINVAL, "invalid argument") \ XX(EIO, "i/o error") \ XX(EISCONN, "socket is already connected") \ XX(EISDIR, "illegal operation on a directory") \ XX(ELOOP, "too many symbolic links encountered") \ XX(EMFILE, "too many open files") \ XX(EMSGSIZE, "message too long") \ XX(ENAMETOOLONG, "name too long") \ XX(ENETDOWN, "network is down") \ XX(ENETUNREACH, "network is unreachable") \ XX(ENFILE, "file table overflow") \ XX(ENOBUFS, "no buffer space available") \ XX(ENODEV, "no such device") \ XX(ENOENT, "no such file or directory") \ XX(ENOMEM, "not enough memory") \ XX(ENONET, "machine is not on the network") \ XX(ENOPROTOOPT, "protocol not available") \ XX(ENOSPC, "no space left on device") \ XX(ENOSYS, "function not implemented") \ XX(ENOTCONN, "socket is not connected") \ XX(ENOTDIR, "not a directory") \ XX(ENOTEMPTY, "directory not empty") \ XX(ENOTSOCK, "socket operation on non-socket") \ XX(ENOTSUP, "operation not supported on socket") \ XX(EPERM, "operation not permitted") \ XX(EPIPE, "broken pipe") \ XX(EPROTO, "protocol error") \ XX(EPROTONOSUPPORT, "protocol not supported") \ XX(EPROTOTYPE, "protocol wrong type for socket") \ XX(ERANGE, "result too large") \ XX(EROFS, "read-only file system") \ XX(ESHUTDOWN, "cannot send after transport endpoint shutdown") \ XX(ESPIPE, "invalid seek") \ XX(ESRCH, "no such process") \ XX(ETIMEDOUT, "connection timed out") \ XX(ETXTBSY, "text file is busy") \ XX(EXDEV, "cross-device link not permitted") \ XX(UNKNOWN, "unknown error") \ XX(EOF, "end of file") \ XX(ENXIO, "no such device or address") \ XX(EMLINK, "too many links") \ XX(EHOSTDOWN, "host is down") \ XX(EREMOTEIO, "remote I/O error") \ XX(ENOTTY, "inappropriate ioctl for device") \ XX(EFTYPE, "inappropriate file type or format") \ XX(EILSEQ, "illegal byte sequence") \ typedef enum { #define XX(code, _) TOOLKIT_ ## code = TOOLKIT__ ## code, TOOLKIT_ERRNO_MAP(XX) #undef XX TOOLKIT_ERRNO_MAX = TOOLKIT__EOF - 1 } toolkit_errno_t; # define TOOLKIT_PRIORITY_LOW 19 # define TOOLKIT_PRIORITY_BELOW_NORMAL 10 # define TOOLKIT_PRIORITY_NORMAL 0 # define TOOLKIT_PRIORITY_ABOVE_NORMAL -7 # define TOOLKIT_PRIORITY_HIGH -14 # define TOOLKIT_PRIORITY_HIGHEST -20 TOOLKIT_API int toolkit_translate_sys_error(int sys_errno); TOOLKIT_API const char* toolkit_strerror(int err); TOOLKIT_API char* toolkit_strerror_r(int err, char* buf, size_t buflen); TOOLKIT_API const char* toolkit_err_name(int err); TOOLKIT_API char* toolkit_err_name_r(int err, char* buf, size_t buflen); TOOLKIT_API void toolkit_once(toolkit_once_t* guard, void (*callback)(void)); TOOLKIT_API int toolkit_mutex_init(toolkit_mutex_t* handle); TOOLKIT_API int toolkit_mutex_init_recursive(toolkit_mutex_t* handle); TOOLKIT_API void toolkit_mutex_destroy(toolkit_mutex_t* handle); TOOLKIT_API void toolkit_mutex_lock(toolkit_mutex_t* handle); TOOLKIT_API int toolkit_mutex_trylock(toolkit_mutex_t* handle); TOOLKIT_API void toolkit_mutex_unlock(toolkit_mutex_t* handle); TOOLKIT_API int toolkit_rwlock_init(toolkit_rwlock_t* rwlock); TOOLKIT_API void toolkit_rwlock_destroy(toolkit_rwlock_t* rwlock); TOOLKIT_API void toolkit_rwlock_rdlock(toolkit_rwlock_t* rwlock); TOOLKIT_API int toolkit_rwlock_tryrdlock(toolkit_rwlock_t* rwlock); TOOLKIT_API void toolkit_rwlock_rdunlock(toolkit_rwlock_t* rwlock); TOOLKIT_API void toolkit_rwlock_wrlock(toolkit_rwlock_t* rwlock); TOOLKIT_API int toolkit_rwlock_trywrlock(toolkit_rwlock_t* rwlock); TOOLKIT_API void toolkit_rwlock_wrunlock(toolkit_rwlock_t* rwlock); TOOLKIT_API int toolkit_sem_init(toolkit_sem_t* sem, unsigned int value); TOOLKIT_API void toolkit_sem_destroy(toolkit_sem_t* sem); TOOLKIT_API void toolkit_sem_post(toolkit_sem_t* sem); TOOLKIT_API void toolkit_sem_wait(toolkit_sem_t* sem); TOOLKIT_API int toolkit_sem_trywait(toolkit_sem_t* sem); TOOLKIT_API int toolkit_cond_init(toolkit_cond_t* cond); TOOLKIT_API void toolkit_cond_destroy(toolkit_cond_t* cond); TOOLKIT_API void toolkit_cond_signal(toolkit_cond_t* cond); TOOLKIT_API void toolkit_cond_broadcast(toolkit_cond_t* cond); TOOLKIT_API int toolkit_barrier_init(toolkit_barrier_t* barrier, unsigned int count); TOOLKIT_API void toolkit_barrier_destroy(toolkit_barrier_t* barrier); TOOLKIT_API int toolkit_barrier_wait(toolkit_barrier_t* barrier); TOOLKIT_API void toolkit_cond_wait(toolkit_cond_t* cond, toolkit_mutex_t* mutex); TOOLKIT_API int toolkit_cond_timedwait(toolkit_cond_t* cond, toolkit_mutex_t* mutex, uint64_t timeout); typedef struct tk_process_s tk_process_t; typedef struct tk_process_option_s tk_process_option_t; typedef void (*tk_exit_cb)(tk_process_t*, int64_t); struct tk_process_s { int pid; HANDLE handle; }; struct tk_process_option_s { tk_exit_cb exit_cb; const char* file; char* params; uint32_t flags; }; TOOLKIT_API int process_init(tk_process_t* proc); TOOLKIT_API int process_spawn(const tk_process_option_t* option, tk_process_t** proc); TOOLKIT_API int process_compare(const tk_process_t* proc1, const tk_process_t* proc2); TOOLKIT_API void process_close(tk_process_t* proc); TOOLKIT_API int process_exist_or_not(int pid); TOOLKIT_API int toolkit_key_create(toolkit_key_t* key); TOOLKIT_API void toolkit_key_delete(toolkit_key_t* key); TOOLKIT_API void* toolkit_key_get(toolkit_key_t* key); TOOLKIT_API void toolkit_key_set(toolkit_key_t* key, void* value); /** thread component*/ typedef struct toolkit_thread_option_s { unsigned int flags; size_t stack_size; } toolkit_thread_option_t; typedef void (*toolkit_thread_cb)(void* arg); TOOLKIT_API int toolkit_thread_create(toolkit_thread_t* tid, toolkit_thread_cb entry, void* arg); TOOLKIT_API int toolkit_thread_create_ex(toolkit_thread_t* tid, const toolkit_thread_option_t* params, toolkit_thread_cb entry, void* arg); TOOLKIT_API toolkit_thread_t toolkit_thread_self(void); TOOLKIT_API int toolkit_thread_join(toolkit_thread_t* tid); TOOLKIT_API int toolkit_thread_equal(const toolkit_thread_t* t1, const toolkit_thread_t* t2); /** library component*/ TOOLKIT_API int toolkit_dlopen(const char* filename, toolkit_lib_t* lib); TOOLKIT_API void toolkit_dlclose(toolkit_lib_t* lib); TOOLKIT_API int toolkit_dlsym(toolkit_lib_t* lib, const char* name, void** ptr); TOOLKIT_API const char* toolkit_dlerror(const toolkit_lib_t* lib); /** environment variable component*/ typedef struct toolkit_env_item_s { char* name; char* value; } toolkit_env_item_t; TOOLKIT_API int toolkit_environ(toolkit_env_item_t** envitems, int* count); TOOLKIT_API void toolkit_free_environ(toolkit_env_item_t* envitems, int count); TOOLKIT_API int toolkit_getenv(const char* name, char* buffer, size_t* size); TOOLKIT_API int toolkit_setenv(const char* name, const char* value); TOOLKIT_API int toolkit_unsetenv(const char* name); /** time component*/ /** high resolution time*/ TOOLKIT_API uint64_t toolkit_hrtime(); struct toolkit_cpu_times_s { uint64_t user; uint64_t nice; uint64_t sys; uint64_t idle; uint64_t irq; }; typedef struct toolkit_cpu_info_s { char* model; int speed; struct toolkit_cpu_times_s cpu_times; } toolkit_cpu_info_t; typedef struct toolkit_interface_address_s { char* name; char phys_addr[6]; int is_internal; union { struct sockaddr_in address4; struct sockaddr_in6 address6; } address; union { struct sockaddr_in netmask4; struct sockaddr_in6 netmask6; } netmask; } toolkit_interface_address_t; TOOLKIT_API int toolkit_cpu_info(toolkit_cpu_info_t** cpu_infos, int* count); TOOLKIT_API void toolkit_free_cpu_info(toolkit_cpu_info_t* cpu_infos, int count); TOOLKIT_API int toolkit_interface_addresses(toolkit_interface_address_t** addresses, int* count); TOOLKIT_API void toolkit_free_interface_addresses(toolkit_interface_address_t* addresses, int count); TOOLKIT_API int toolkit_ip4_name(const struct sockaddr_in* src, char* dst, size_t size); TOOLKIT_API int toolkit_ip6_name(const struct sockaddr_in6* src, char* dst, size_t size); TOOLKIT_API int toolkit_inet_ntop(int af, const void* src, char* dst, size_t size); TOOLKIT_API int toolkit_inet_pton(int af, const char* src, void* dst); TOOLKIT_API void toolkit_sleep(int msec); TOOLKIT_API int toolkit_os_getpriority(toolkit_pid_t pid, int* priority); TOOLKIT_API int toolkit_os_setpriority(toolkit_pid_t pid, int priority); #ifdef __cplusplus } // extern "C" { #endif #include "winfit.h" #endif //__TOOLKIT_GLOBAL_H__