ZXC 0.15.0
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_threads.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause */
2/*
3 * ZXC - High-performance lossless compression
4 *
5 * Copyright (c) Bertrand Lebonnois and contributors.
6 */
7
19#ifndef ZXC_THREADS_H
20#define ZXC_THREADS_H
21
22#include "zxc_internal.h"
23
24// LCOV_EXCL_START - Windows platform layer, not reachable on POSIX CI
25#if defined(_WIN32)
26
27#include <process.h> /* _beginthreadex */
28#include <windows.h>
29
30typedef CRITICAL_SECTION pthread_mutex_t;
31typedef CONDITION_VARIABLE pthread_cond_t;
32typedef HANDLE pthread_t;
33
34#define pthread_mutex_init(m, a) InitializeCriticalSection(m)
35#define pthread_mutex_destroy(m) DeleteCriticalSection(m)
36#define pthread_mutex_lock(m) EnterCriticalSection(m)
37#define pthread_mutex_unlock(m) LeaveCriticalSection(m)
38
39#define pthread_cond_init(c, a) InitializeConditionVariable(c)
40#define pthread_cond_destroy(c) (void)(0)
41#define pthread_cond_wait(c, m) SleepConditionVariableCS(c, m, INFINITE)
42#define pthread_cond_signal(c) WakeConditionVariable(c)
43#define pthread_cond_broadcast(c) WakeAllConditionVariable(c)
44
52typedef struct {
53 void* (*func)(void*); /* worker to invoke */
54 void* arg; /* argument forwarded to @c func */
55} zxc_win_thread_arg_t;
56
65static inline unsigned __stdcall zxc_win_thread_entry(void* p) {
66 zxc_win_thread_arg_t* a = (zxc_win_thread_arg_t*)p;
67 void* (*f)(void*) = a->func;
68 void* arg = a->arg;
69 ZXC_FREE(a);
70 f(arg);
71 return 0;
72}
73
84static inline int pthread_create(pthread_t* thread, const void* attr, void* (*start_routine)(void*),
85 void* arg) {
86 (void)attr;
87 zxc_win_thread_arg_t* wrapper = ZXC_MALLOC(sizeof(zxc_win_thread_arg_t));
88 if (UNLIKELY(!wrapper)) return ZXC_ERROR_MEMORY;
89 wrapper->func = start_routine;
90 wrapper->arg = arg;
91 uintptr_t handle = _beginthreadex(NULL, 0, zxc_win_thread_entry, wrapper, 0, NULL);
92 if (UNLIKELY(handle == 0)) {
93 ZXC_FREE(wrapper);
94 return ZXC_ERROR_MEMORY;
95 }
96 *thread = (HANDLE)handle;
97 return 0;
98}
99
108static inline int pthread_join(pthread_t thread, void** retval) {
109 (void)retval;
110 WaitForSingleObject(thread, INFINITE);
111 CloseHandle(thread);
112 return 0;
113}
114
119static ZXC_ALWAYS_INLINE int zxc_num_procs(void) {
120 SYSTEM_INFO si;
121 GetSystemInfo(&si);
122 return (int)si.dwNumberOfProcessors;
123}
124
125#else /* POSIX */
126// LCOV_EXCL_STOP
127
128#include <pthread.h>
129#include <unistd.h>
130
136 const long n = sysconf(_SC_NPROCESSORS_ONLN);
137 return (n > 0) ? (int)n : 1;
138}
139
140#endif /* _WIN32 */
141
142#endif /* ZXC_THREADS_H */
@ ZXC_ERROR_MEMORY
Memory allocation failure.
Definition zxc_error.h:41
#define ZXC_MALLOC(size)
Heap allocator.
Definition zxc_deps.h:68
#define ZXC_ALWAYS_INLINE
Forces a function to be inlined (fallback for non-GCC/Clang/MSVC compilers).
#define ZXC_FREE(ptr)
Heap deallocator.
Definition zxc_deps.h:86
Internal definitions, constants, SIMD helpers, and utility functions.
static int zxc_num_procs(void)
Returns the number of online logical processors.