Pyrogenesis  trunk
wpthread.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 //
32 // <sched.h>
33 //
34 
36 {
38 };
39 
40 enum
41 {
45 };
46 
47 // changing will break pthread_setschedparam:
48 #define sched_get_priority_max(policy) +2
49 #define sched_get_priority_min(policy) -2
50 
51 
52 //
53 // <pthread.h>
54 //
55 
56 // one-time init
57 typedef intptr_t pthread_once_t; // required for cpu_CAS
58 #define PTHREAD_ONCE_INIT 0 // static pthread_once_t x = PTHREAD_ONCE_INIT;
59 
60 int pthread_once(pthread_once_t*, void (*init_routine)());
61 
62 // thread
63 typedef uintptr_t pthread_t;
64 
65 int pthread_equal(pthread_t t1, pthread_t t2);
66 pthread_t pthread_self();
67 int pthread_getschedparam(pthread_t thread, int* policy, struct sched_param* param);
68 int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param* param);
69 int pthread_create(pthread_t* thread, const void* attr, void* (*func)(void*), void* arg);
70 int pthread_cancel(pthread_t thread);
71 int pthread_join(pthread_t thread, void** value_ptr);
72 
73 // mutex
74 
75 typedef void* pthread_mutexattr_t;
76 int pthread_mutexattr_init(pthread_mutexattr_t* attr);
77 int pthread_mutexattr_destroy(pthread_mutexattr_t* attr);
78 enum { PTHREAD_MUTEX_RECURSIVE }; // the only one we support
79 int pthread_mutexattr_gettype(const pthread_mutexattr_t* attr, int* type);
80 int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type);
81 
82 typedef void* pthread_mutex_t; // pointer to critical section
83 pthread_mutex_t pthread_mutex_initializer();
84 #define PTHREAD_MUTEX_INITIALIZER pthread_mutex_initializer()
85 int pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*);
86 int pthread_mutex_destroy(pthread_mutex_t*);
87 int pthread_mutex_lock(pthread_mutex_t*);
88 int pthread_mutex_trylock(pthread_mutex_t*);
89 int pthread_mutex_unlock(pthread_mutex_t*);
90 int pthread_mutex_timedlock(pthread_mutex_t*, const struct timespec*);
91 
92 // thread-local storage
93 typedef unsigned int pthread_key_t;
94 
95 int pthread_key_create(pthread_key_t*, void (*dtor)(void*));
96 int pthread_key_delete(pthread_key_t);
97 void* pthread_getspecific(pthread_key_t);
98 int pthread_setspecific(pthread_key_t, const void* value);
99 
100 
101 //
102 // <semaphore.h>
103 //
104 
105 typedef uintptr_t sem_t;
106 
107 #define SEM_FAILED 0
108 
109 sem_t* sem_open(const char* name, int oflag, ...);
110 int sem_close(sem_t* sem);
111 int sem_unlink(const char* name);
112 int sem_init(sem_t*, int pshared, unsigned value);
113 int sem_destroy(sem_t*);
114 int sem_post(sem_t*);
115 int sem_wait(sem_t*);
116 int sem_timedwait(sem_t*, const struct timespec*);
117 
118 
119 // wait until semaphore is locked or a message arrives. non-portable.
120 //
121 // background: on Win32, UI threads must periodically pump messages, or
122 // else deadlock may result (see WaitForSingleObject docs). that entails
123 // avoiding any blocking functions. when event waiting is needed,
124 // one cheap workaround would be to time out periodically and pump messages.
125 // that would work, but either wastes CPU time waiting, or introduces
126 // message latency. to avoid this, we provide an API similar to sem_wait and
127 // sem_timedwait that gives MsgWaitForMultipleObjects functionality.
128 //
129 // return value: 0 if the semaphore has been locked (SUS terminology),
130 // -1 otherwise. errno differentiates what happened: ETIMEDOUT if a
131 // message arrived (this is to ease switching between message waiting and
132 // periodic timeout), or an error indication.
133 int sem_msgwait_np(sem_t* sem);
134 
135 #endif // #ifndef INCLUDED_WPTHREAD
void * pthread_mutex_t
Definition: wpthread.h:82
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
Definition: wpthread.cpp:283
void(* dtor)(void *)
Definition: wpthread.cpp:141
int sched_priority
Definition: wpthread.h:37
intptr_t pthread_once_t
Definition: wpthread.h:57
int sem_init(sem_t *, int pshared, unsigned value)
Definition: wpthread.cpp:444
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
Definition: wpthread.cpp:104
pthread_t pthread_self()
Definition: wpthread.cpp:74
Definition: wpthread.h:42
int pthread_mutex_destroy(pthread_mutex_t *)
Definition: wpthread.cpp:312
int sem_msgwait_np(sem_t *sem)
Definition: wpthread.cpp:491
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
Definition: wpthread.cpp:273
Definition: wpthread.h:35
int pthread_once(pthread_once_t *, void(*init_routine)())
Definition: wpthread.cpp:80
int pthread_key_delete(pthread_key_t)
Definition: wpthread.cpp:173
int sem_post(sem_t *)
Definition: wpthread.cpp:461
int pthread_cancel(pthread_t thread)
Definition: wpthread.cpp:604
int sem_unlink(const char *name)
Definition: wpthread.cpp:438
int pthread_mutex_timedlock(pthread_mutex_t *, const struct timespec *)
Definition: wpthread.cpp:358
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
Definition: wpthread.cpp:278
sem_t * sem_open(const char *name, int oflag,...)
Definition: wpthread.cpp:373
int pthread_setspecific(pthread_key_t, const void *value)
Definition: wpthread.cpp:210
void * pthread_mutexattr_t
Definition: wpthread.h:75
int pthread_mutex_unlock(pthread_mutex_t *)
Definition: wpthread.cpp:347
int pthread_mutex_lock(pthread_mutex_t *)
Definition: wpthread.cpp:329
int pthread_key_create(pthread_key_t *, void(*dtor)(void *))
Definition: wpthread.cpp:146
uintptr_t sem_t
Definition: wpthread.h:105
int sem_timedwait(sem_t *, const struct timespec *)
int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *)
Definition: wpthread.cpp:323
int pthread_equal(pthread_t t1, pthread_t t2)
Definition: wpthread.cpp:69
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
Definition: wpthread.cpp:88
int pthread_join(pthread_t thread, void **value_ptr)
Definition: wpthread.cpp:613
int sem_wait(sem_t *)
Definition: wpthread.cpp:468
uintptr_t pthread_t
Definition: wpthread.h:63
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
Definition: wpthread.cpp:289
Definition: wpthread.h:44
void * pthread_getspecific(pthread_key_t)
Definition: wpthread.cpp:184
pthread_mutex_t pthread_mutex_initializer()
Definition: wpthread.cpp:305
int sem_destroy(sem_t *)
Definition: wpthread.cpp:454
Definition: wpthread.h:78
unsigned int pthread_key_t
Definition: wpthread.h:93
Definition: wpthread.h:43
int sem_close(sem_t *sem)
Definition: wpthread.cpp:429
int pthread_mutex_trylock(pthread_mutex_t *)
Definition: wpthread.cpp:338
int pthread_create(pthread_t *thread, const void *attr, void *(*func)(void *), void *arg)
Definition: wpthread.cpp:570