Path: blob/21.2-virgl/src/vulkan/wsi/wsi_common_queue.h
7178 views
/*1* Copyright © 2016 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#ifndef VULKAN_WSI_COMMON_QUEUE_H24#define VULKAN_WSI_COMMON_QUEUE_H2526#include <time.h>27#include <pthread.h>28#include "util/u_vector.h"2930struct wsi_queue {31struct u_vector vector;32pthread_mutex_t mutex;33pthread_cond_t cond;34};3536static inline int37wsi_queue_init(struct wsi_queue *queue, int length)38{39int ret;4041uint32_t length_pow2 = 4;42while (length_pow2 < length)43length_pow2 *= 2;4445ret = u_vector_init(&queue->vector, sizeof(uint32_t),46sizeof(uint32_t) * length_pow2);47if (!ret)48return ENOMEM;4950pthread_condattr_t condattr;51ret = pthread_condattr_init(&condattr);52if (ret)53goto fail_vector;5455ret = pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);56if (ret)57goto fail_condattr;5859ret = pthread_cond_init(&queue->cond, &condattr);60if (ret)61goto fail_condattr;6263ret = pthread_mutex_init(&queue->mutex, NULL);64if (ret)65goto fail_cond;6667pthread_condattr_destroy(&condattr);68return 0;6970fail_cond:71pthread_cond_destroy(&queue->cond);72fail_condattr:73pthread_condattr_destroy(&condattr);74fail_vector:75u_vector_finish(&queue->vector);7677return ret;78}7980static inline void81wsi_queue_destroy(struct wsi_queue *queue)82{83u_vector_finish(&queue->vector);84pthread_mutex_destroy(&queue->mutex);85pthread_cond_destroy(&queue->cond);86}8788static inline void89wsi_queue_push(struct wsi_queue *queue, uint32_t index)90{91uint32_t *elem;9293pthread_mutex_lock(&queue->mutex);9495if (u_vector_length(&queue->vector) == 0)96pthread_cond_signal(&queue->cond);9798elem = u_vector_add(&queue->vector);99*elem = index;100101pthread_mutex_unlock(&queue->mutex);102}103104#define NSEC_PER_SEC 1000000000105#define INT_TYPE_MAX(type) ((1ull << (sizeof(type) * 8 - 1)) - 1)106107static inline VkResult108wsi_queue_pull(struct wsi_queue *queue, uint32_t *index, uint64_t timeout)109{110VkResult result;111int32_t ret;112113pthread_mutex_lock(&queue->mutex);114115struct timespec now;116clock_gettime(CLOCK_MONOTONIC, &now);117118uint32_t abs_nsec = now.tv_nsec + timeout % NSEC_PER_SEC;119uint64_t abs_sec = now.tv_sec + (abs_nsec / NSEC_PER_SEC) +120(timeout / NSEC_PER_SEC);121abs_nsec %= NSEC_PER_SEC;122123/* Avoid roll-over in tv_sec on 32-bit systems if the user provided timeout124* is UINT64_MAX125*/126struct timespec abstime;127abstime.tv_nsec = abs_nsec;128abstime.tv_sec = MIN2(abs_sec, INT_TYPE_MAX(abstime.tv_sec));129130while (u_vector_length(&queue->vector) == 0) {131ret = pthread_cond_timedwait(&queue->cond, &queue->mutex, &abstime);132if (ret == 0) {133continue;134} else if (ret == ETIMEDOUT) {135result = VK_TIMEOUT;136goto end;137} else {138/* Something went badly wrong */139result = VK_ERROR_OUT_OF_DATE_KHR;140goto end;141}142}143144uint32_t *elem = u_vector_remove(&queue->vector);145*index = *elem;146result = VK_SUCCESS;147148end:149pthread_mutex_unlock(&queue->mutex);150151return result;152}153154#endif /* VULKAN_WSI_COMMON_QUEUE_H */155156157