d9c685922a5e05e4c84a74d6771d36c3f888f4e0
[mesa.git] / src / gallium / auxiliary / os / os_thread.h
1 /**************************************************************************
2 *
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 VMware, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27
28 /**
29 * @file
30 *
31 * Thread, mutex, condition variable, barrier, semaphore and
32 * thread-specific data functions.
33 */
34
35
36 #ifndef OS_THREAD_H_
37 #define OS_THREAD_H_
38
39
40 #include "pipe/p_compiler.h"
41 #include "util/u_debug.h" /* for assert */
42 #include "util/u_thread.h"
43
44
45 #define pipe_mutex_assert_locked(mutex) \
46 __pipe_mutex_assert_locked(&(mutex))
47
48 static inline void
49 __pipe_mutex_assert_locked(mtx_t *mutex)
50 {
51 #ifdef DEBUG
52 /* NOTE: this would not work for recursive mutexes, but
53 * mtx_t doesn't support those
54 */
55 int ret = mtx_trylock(mutex);
56 assert(ret == thrd_busy);
57 if (ret == thrd_success)
58 mtx_unlock(mutex);
59 #endif
60 }
61
62
63 /*
64 * Semaphores
65 */
66
67 typedef struct
68 {
69 mtx_t mutex;
70 cnd_t cond;
71 int counter;
72 } pipe_semaphore;
73
74
75 static inline void
76 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
77 {
78 (void) mtx_init(&sema->mutex, mtx_plain);
79 cnd_init(&sema->cond);
80 sema->counter = init_val;
81 }
82
83 static inline void
84 pipe_semaphore_destroy(pipe_semaphore *sema)
85 {
86 mtx_destroy(&sema->mutex);
87 cnd_destroy(&sema->cond);
88 }
89
90 /** Signal/increment semaphore counter */
91 static inline void
92 pipe_semaphore_signal(pipe_semaphore *sema)
93 {
94 mtx_lock(&sema->mutex);
95 sema->counter++;
96 cnd_signal(&sema->cond);
97 mtx_unlock(&sema->mutex);
98 }
99
100 /** Wait for semaphore counter to be greater than zero */
101 static inline void
102 pipe_semaphore_wait(pipe_semaphore *sema)
103 {
104 mtx_lock(&sema->mutex);
105 while (sema->counter <= 0) {
106 cnd_wait(&sema->cond, &sema->mutex);
107 }
108 sema->counter--;
109 mtx_unlock(&sema->mutex);
110 }
111
112
113
114 /*
115 * Thread-specific data.
116 */
117
118 typedef struct {
119 tss_t key;
120 int initMagic;
121 } pipe_tsd;
122
123
124 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
125
126
127 static inline void
128 pipe_tsd_init(pipe_tsd *tsd)
129 {
130 if (tss_create(&tsd->key, NULL/*free*/) != 0) {
131 exit(-1);
132 }
133 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
134 }
135
136 static inline void *
137 pipe_tsd_get(pipe_tsd *tsd)
138 {
139 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
140 pipe_tsd_init(tsd);
141 }
142 return tss_get(tsd->key);
143 }
144
145 static inline void
146 pipe_tsd_set(pipe_tsd *tsd, void *value)
147 {
148 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
149 pipe_tsd_init(tsd);
150 }
151 if (tss_set(tsd->key, value) != 0) {
152 exit(-1);
153 }
154 }
155
156
157
158 /*
159 * Thread statistics.
160 */
161
162 /* Return the time of the current thread's CPU time clock. */
163 static inline int64_t
164 pipe_current_thread_get_time_nano(void)
165 {
166 #if defined(HAVE_PTHREAD)
167 return u_thread_get_time_nano(pthread_self());
168 #else
169 return 0;
170 #endif
171 }
172
173 #endif /* OS_THREAD_H_ */