Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
wpthread.h
Go to the documentation of this file.
1/* Copyright (C) 2024 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * emulate pthreads on Windows.
25 */
26
27#ifndef INCLUDED_WPTHREAD
28#define INCLUDED_WPTHREAD
29
30//
31// <sched.h>
32//
33
35{
37};
38
39enum
40{
44};
45
46// changing will break pthread_setschedparam:
47#define sched_get_priority_max(policy) +2
48#define sched_get_priority_min(policy) -2
49
50
51//
52// <pthread.h>
53//
54
55// thread
56typedef uintptr_t pthread_t;
57
60int pthread_getschedparam(pthread_t thread, int* policy, struct sched_param* param);
61int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param* param);
62int pthread_create(pthread_t* thread, const void* attr, void* (*func)(void*), void* arg);
63int pthread_cancel(pthread_t thread);
64int pthread_join(pthread_t thread, void** value_ptr);
65
66// mutex
67
68typedef void* pthread_mutexattr_t;
71enum { PTHREAD_MUTEX_RECURSIVE }; // the only one we support
72int pthread_mutexattr_gettype(const pthread_mutexattr_t* attr, int* type);
74
75typedef void* pthread_mutex_t; // pointer to critical section
77#define PTHREAD_MUTEX_INITIALIZER pthread_mutex_initializer()
83int pthread_mutex_timedlock(pthread_mutex_t*, const struct timespec*);
84
85// thread-local storage
86typedef unsigned int pthread_key_t;
87
88int pthread_key_create(pthread_key_t*, void (*dtor)(void*));
91int pthread_setspecific(pthread_key_t, const void* value);
92
93
94//
95// <semaphore.h>
96//
97
98typedef uintptr_t sem_t;
99
100#define SEM_FAILED 0
101
102sem_t* sem_open(const char* name, int oflag, ...);
103int sem_close(sem_t* sem);
104int sem_unlink(const char* name);
105int sem_init(sem_t*, int pshared, unsigned value);
106int sem_destroy(sem_t*);
107int sem_post(sem_t*);
108int sem_wait(sem_t*);
109int sem_timedwait(sem_t*, const struct timespec*);
110
111
112// wait until semaphore is locked or a message arrives. non-portable.
113//
114// background: on Win32, UI threads must periodically pump messages, or
115// else deadlock may result (see WaitForSingleObject docs). that entails
116// avoiding any blocking functions. when event waiting is needed,
117// one cheap workaround would be to time out periodically and pump messages.
118// that would work, but either wastes CPU time waiting, or introduces
119// message latency. to avoid this, we provide an API similar to sem_wait and
120// sem_timedwait that gives MsgWaitForMultipleObjects functionality.
121//
122// return value: 0 if the semaphore has been locked (SUS terminology),
123// -1 otherwise. errno differentiates what happened: ETIMEDOUT if a
124// message arrived (this is to ease switching between message waiting and
125// periodic timeout), or an error indication.
126int sem_msgwait_np(sem_t* sem);
127
128#endif // #ifndef INCLUDED_WPTHREAD
Definition: wpthread.h:35
int sched_priority
Definition: wpthread.h:36
std::atomic< dtortype > dtor
Definition: wpthread.cpp:142
int sem_destroy(sem_t *)
Definition: wpthread.cpp:456
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
Definition: wpthread.cpp:104
int pthread_mutex_timedlock(pthread_mutex_t *, const struct timespec *)
Definition: wpthread.cpp:360
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
Definition: wpthread.cpp:280
int sem_wait(sem_t *)
Definition: wpthread.cpp:470
int pthread_mutex_unlock(pthread_mutex_t *)
Definition: wpthread.cpp:349
uintptr_t sem_t
Definition: wpthread.h:98
int sem_close(sem_t *sem)
Definition: wpthread.cpp:431
void * pthread_mutex_t
Definition: wpthread.h:75
uintptr_t pthread_t
Definition: wpthread.h:56
int pthread_key_create(pthread_key_t *, void(*dtor)(void *))
Definition: wpthread.cpp:147
void * pthread_getspecific(pthread_key_t)
Definition: wpthread.cpp:186
pthread_t pthread_self()
Definition: wpthread.cpp:82
int sem_unlink(const char *name)
Definition: wpthread.cpp:440
int pthread_mutex_trylock(pthread_mutex_t *)
Definition: wpthread.cpp:340
unsigned int pthread_key_t
Definition: wpthread.h:86
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
Definition: wpthread.cpp:285
sem_t * sem_open(const char *name, int oflag,...)
Definition: wpthread.cpp:375
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
Definition: wpthread.cpp:291
int pthread_mutex_destroy(pthread_mutex_t *)
Definition: wpthread.cpp:314
int pthread_key_delete(pthread_key_t)
Definition: wpthread.cpp:175
int sem_msgwait_np(sem_t *sem)
Definition: wpthread.cpp:493
int pthread_equal(pthread_t t1, pthread_t t2)
Definition: wpthread.cpp:77
int pthread_cancel(pthread_t thread)
Definition: wpthread.cpp:604
int pthread_mutex_lock(pthread_mutex_t *)
Definition: wpthread.cpp:331
int pthread_join(pthread_t thread, void **value_ptr)
Definition: wpthread.cpp:613
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
Definition: wpthread.cpp:88
pthread_mutex_t pthread_mutex_initializer()
Definition: wpthread.cpp:307
int sem_post(sem_t *)
Definition: wpthread.cpp:463
int sem_timedwait(sem_t *, const struct timespec *)
int pthread_setspecific(pthread_key_t, const void *value)
Definition: wpthread.cpp:212
int pthread_create(pthread_t *thread, const void *attr, void *(*func)(void *), void *arg)
Definition: wpthread.cpp:570
void * pthread_mutexattr_t
Definition: wpthread.h:68
@ PTHREAD_MUTEX_RECURSIVE
Definition: wpthread.h:71
int sem_init(sem_t *, int pshared, unsigned value)
Definition: wpthread.cpp:446
int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *)
Definition: wpthread.cpp:325
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
Definition: wpthread.cpp:275
@ SCHED_OTHER
Definition: wpthread.h:43
@ SCHED_RR
Definition: wpthread.h:41
@ SCHED_FIFO
Definition: wpthread.h:42