Nevcairiel

Untitled

Jul 23rd, 2021 (edited)
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. /*
  2.  * This file is part of libplacebo.
  3.  *
  4.  * libplacebo is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Lesser General Public
  6.  * License as published by the Free Software Foundation; either
  7.  * version 2.1 of the License, or (at your option) any later version.
  8.  *
  9.  * libplacebo is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU Lesser General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Lesser General Public
  15.  * License along with libplacebo. If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. #pragma once
  19.  
  20. #include "common.h"
  21.  
  22. enum pl_mutex_type {
  23.     PL_MUTEX_TYPE_NORMAL = 0,
  24.     PL_MUTEX_TYPE_RECURSIVE,
  25.     PL_MUTEX_TYPE_ERRORCHECK,
  26. };
  27.  
  28. #ifdef PL_HAVE_WIN32
  29. #include "pl_thread_win32.h"
  30. #else
  31.  
  32. #include <pthread.h>
  33.  
  34. typedef pthread_mutex_t pl_mutex;
  35. typedef pthread_cond_t  pl_cond;
  36.  
  37. static inline int pl_mutex_init_type_internal(pl_mutex *mutex, enum pl_mutex_type mtype)
  38. {
  39.     pthread_mutexattr_t _attr;
  40.     PL_RET_ERR(pthread_mutexattr_init(&_attr));
  41.     pthread_mutexattr_settype(&_attr,
  42.         (mtype == PL_MUTEX_TYPE_RECURSIVE)  ? PTHREAD_MUTEX_RECURSIVE :
  43.         (mtype == PL_MUTEX_TYPE_ERRORCHECK) ? PTHREAD_MUTEX_ERRORCHECK :
  44.                                               PTHREAD_MUTEX_DEFAULT);
  45.     PL_RET_ERR(pthread_mutex_init(mutex, &_attr));
  46.     pthread_mutexattr_destroy(&_attr);
  47.     return 0;
  48. }
  49.  
  50. #define pl_mutex_init_type(mutex, mtype) \
  51.     PL_CHECK_ERR(pl_mutex_init_type_internal(mutex, mtype))
  52.  
  53. #ifndef NDEBUG
  54. #define pl_mutex_init(mutex) \
  55.     pl_mutex_init_type(mutex, PL_MUTEX_TYPE_ERRORCHECK)
  56. #else
  57. #define pl_mutex_init(mutex) \
  58.     PL_CHECK_ERR(pthread_mutex_init(mutex, NULL))
  59. #endif
  60.  
  61. #define pl_mutex_destroy    pthread_mutex_destroy
  62. #define pl_mutex_lock       pthread_mutex_lock
  63. #define pl_mutex_unlock     pthread_mutex_unlock
  64.  
  65. #define pl_cond_init(cond)  pthread_cond_init(cond, NULL)
  66. #define pl_cond_destroy     pthread_cond_destroy
  67.  
  68. #define pl_cond_broadcast   pthread_cond_broadcast
  69. #define pl_cond_signal      pthread_cond_signal
  70.  
  71. #define pl_cond_wait        pthread_cond_wait
  72.  
  73. static inline int pl_cond_timedwait(pl_cond *cond, pl_mutex *mutex, uint64_t timeout)
  74. {
  75.     if (timeout == UINT64_MAX)
  76.         return pthread_cond_wait(cond, mutex);
  77.    
  78.     return pthread_cond_timedwait(cond, mutex, &(struct timespec) {
  79.             .tv_sec  = (timeout) / 1000000000LLU,
  80.             .tv_nsec = (timeout) % 1000000000LLU,
  81.         })
  82. }
  83.  
  84. #endif
  85.  
Add Comment
Please, Sign In to add comment