Path: blob/21.2-virgl/src/gallium/auxiliary/os/os_thread.h
4561 views
/**************************************************************************1*2* Copyright 1999-2006 Brian Paul3* Copyright 2008 VMware, Inc.4* All Rights Reserved.5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the "Software"),8* to deal in the Software without restriction, including without limitation9* the rights to use, copy, modify, merge, publish, distribute, sublicense,10* and/or sell copies of the Software, and to permit persons to whom the11* Software is furnished to do so, subject to the following conditions:12*13* The above copyright notice and this permission notice shall be included14* in all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS17* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL19* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR20* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,21* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR22* OTHER DEALINGS IN THE SOFTWARE.23*24**************************************************************************/252627/**28* @file29*30* Thread, mutex, condition variable, barrier, semaphore and31* thread-specific data functions.32*/333435#ifndef OS_THREAD_H_36#define OS_THREAD_H_373839#include "pipe/p_compiler.h"40#include "util/u_debug.h" /* for assert */41#include "util/u_thread.h"424344#define pipe_mutex_assert_locked(mutex) \45__pipe_mutex_assert_locked(&(mutex))4647static inline void48__pipe_mutex_assert_locked(mtx_t *mutex)49{50#ifdef DEBUG51/* NOTE: this would not work for recursive mutexes, but52* mtx_t doesn't support those53*/54int ret = mtx_trylock(mutex);55assert(ret == thrd_busy);56if (ret == thrd_success)57mtx_unlock(mutex);58#else59(void)mutex;60#endif61}626364/*65* Semaphores66*/6768typedef struct69{70mtx_t mutex;71cnd_t cond;72int counter;73} pipe_semaphore;747576static inline void77pipe_semaphore_init(pipe_semaphore *sema, int init_val)78{79(void) mtx_init(&sema->mutex, mtx_plain);80cnd_init(&sema->cond);81sema->counter = init_val;82}8384static inline void85pipe_semaphore_destroy(pipe_semaphore *sema)86{87mtx_destroy(&sema->mutex);88cnd_destroy(&sema->cond);89}9091/** Signal/increment semaphore counter */92static inline void93pipe_semaphore_signal(pipe_semaphore *sema)94{95mtx_lock(&sema->mutex);96sema->counter++;97cnd_signal(&sema->cond);98mtx_unlock(&sema->mutex);99}100101/** Wait for semaphore counter to be greater than zero */102static inline void103pipe_semaphore_wait(pipe_semaphore *sema)104{105mtx_lock(&sema->mutex);106while (sema->counter <= 0) {107cnd_wait(&sema->cond, &sema->mutex);108}109sema->counter--;110mtx_unlock(&sema->mutex);111}112113114115/*116* Thread-specific data.117*/118119typedef struct {120tss_t key;121int initMagic;122} pipe_tsd;123124125#define PIPE_TSD_INIT_MAGIC 0xff8adc98126127128static inline void129pipe_tsd_init(pipe_tsd *tsd)130{131if (tss_create(&tsd->key, NULL/*free*/) != 0) {132exit(-1);133}134tsd->initMagic = PIPE_TSD_INIT_MAGIC;135}136137static inline void *138pipe_tsd_get(pipe_tsd *tsd)139{140if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {141pipe_tsd_init(tsd);142}143return tss_get(tsd->key);144}145146static inline void147pipe_tsd_set(pipe_tsd *tsd, void *value)148{149if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {150pipe_tsd_init(tsd);151}152if (tss_set(tsd->key, value) != 0) {153exit(-1);154}155}156157#endif /* OS_THREAD_H_ */158159160