1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 1999-2006 Brian Paul 4bf215546Sopenharmony_ci * Copyright 2008 VMware, Inc. 5bf215546Sopenharmony_ci * All Rights Reserved. 6bf215546Sopenharmony_ci * 7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 9bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 10bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 12bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included 15bf215546Sopenharmony_ci * in all copies or substantial portions of the Software. 16bf215546Sopenharmony_ci * 17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 24bf215546Sopenharmony_ci * 25bf215546Sopenharmony_ci **************************************************************************/ 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci/** 29bf215546Sopenharmony_ci * @file 30bf215546Sopenharmony_ci * 31bf215546Sopenharmony_ci * Thread, mutex, condition variable, barrier, semaphore and 32bf215546Sopenharmony_ci * thread-specific data functions. 33bf215546Sopenharmony_ci */ 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci#ifndef OS_THREAD_H_ 37bf215546Sopenharmony_ci#define OS_THREAD_H_ 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci#include "pipe/p_compiler.h" 41bf215546Sopenharmony_ci#include "util/u_debug.h" /* for assert */ 42bf215546Sopenharmony_ci#include "util/u_thread.h" 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci#define pipe_mutex_assert_locked(mutex) \ 46bf215546Sopenharmony_ci __pipe_mutex_assert_locked(&(mutex)) 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_cistatic inline void 49bf215546Sopenharmony_ci__pipe_mutex_assert_locked(mtx_t *mutex) 50bf215546Sopenharmony_ci{ 51bf215546Sopenharmony_ci#ifdef DEBUG 52bf215546Sopenharmony_ci /* NOTE: this would not work for recursive mutexes, but 53bf215546Sopenharmony_ci * mtx_t doesn't support those 54bf215546Sopenharmony_ci */ 55bf215546Sopenharmony_ci int ret = mtx_trylock(mutex); 56bf215546Sopenharmony_ci assert(ret == thrd_busy); 57bf215546Sopenharmony_ci if (ret == thrd_success) 58bf215546Sopenharmony_ci mtx_unlock(mutex); 59bf215546Sopenharmony_ci#else 60bf215546Sopenharmony_ci (void)mutex; 61bf215546Sopenharmony_ci#endif 62bf215546Sopenharmony_ci} 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci/* 66bf215546Sopenharmony_ci * Semaphores 67bf215546Sopenharmony_ci */ 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_citypedef struct 70bf215546Sopenharmony_ci{ 71bf215546Sopenharmony_ci mtx_t mutex; 72bf215546Sopenharmony_ci cnd_t cond; 73bf215546Sopenharmony_ci int counter; 74bf215546Sopenharmony_ci} pipe_semaphore; 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_cistatic inline void 78bf215546Sopenharmony_cipipe_semaphore_init(pipe_semaphore *sema, int init_val) 79bf215546Sopenharmony_ci{ 80bf215546Sopenharmony_ci (void) mtx_init(&sema->mutex, mtx_plain); 81bf215546Sopenharmony_ci cnd_init(&sema->cond); 82bf215546Sopenharmony_ci sema->counter = init_val; 83bf215546Sopenharmony_ci} 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_cistatic inline void 86bf215546Sopenharmony_cipipe_semaphore_destroy(pipe_semaphore *sema) 87bf215546Sopenharmony_ci{ 88bf215546Sopenharmony_ci mtx_destroy(&sema->mutex); 89bf215546Sopenharmony_ci cnd_destroy(&sema->cond); 90bf215546Sopenharmony_ci} 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci/** Signal/increment semaphore counter */ 93bf215546Sopenharmony_cistatic inline void 94bf215546Sopenharmony_cipipe_semaphore_signal(pipe_semaphore *sema) 95bf215546Sopenharmony_ci{ 96bf215546Sopenharmony_ci mtx_lock(&sema->mutex); 97bf215546Sopenharmony_ci sema->counter++; 98bf215546Sopenharmony_ci cnd_signal(&sema->cond); 99bf215546Sopenharmony_ci mtx_unlock(&sema->mutex); 100bf215546Sopenharmony_ci} 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci/** Wait for semaphore counter to be greater than zero */ 103bf215546Sopenharmony_cistatic inline void 104bf215546Sopenharmony_cipipe_semaphore_wait(pipe_semaphore *sema) 105bf215546Sopenharmony_ci{ 106bf215546Sopenharmony_ci mtx_lock(&sema->mutex); 107bf215546Sopenharmony_ci while (sema->counter <= 0) { 108bf215546Sopenharmony_ci cnd_wait(&sema->cond, &sema->mutex); 109bf215546Sopenharmony_ci } 110bf215546Sopenharmony_ci sema->counter--; 111bf215546Sopenharmony_ci mtx_unlock(&sema->mutex); 112bf215546Sopenharmony_ci} 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci#endif /* OS_THREAD_H_ */ 115