ijksdl_mutex.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*****************************************************************************
  2. * ijksdl_mutex.c
  3. *****************************************************************************
  4. *
  5. * Copyright (c) 2013 Bilibili
  6. * copyright (c) 2013 Zhang Rui <bbcallen@gmail.com>
  7. *
  8. * This file is part of ijkPlayer.
  9. *
  10. * ijkPlayer is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * ijkPlayer is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with ijkPlayer; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "ijksdl_mutex.h"
  25. #include <errno.h>
  26. #include <assert.h>
  27. #include <sys/time.h>
  28. #include "ijksdl_inc_internal.h"
  29. SDL_mutex *SDL_CreateMutex(void)
  30. {
  31. SDL_mutex *mutex;
  32. mutex = (SDL_mutex *) mallocz(sizeof(SDL_mutex));
  33. if (!mutex)
  34. return NULL;
  35. if (pthread_mutex_init(&mutex->id, NULL) != 0) {
  36. free(mutex);
  37. return NULL;
  38. }
  39. return mutex;
  40. }
  41. void SDL_DestroyMutex(SDL_mutex *mutex)
  42. {
  43. if (mutex) {
  44. pthread_mutex_destroy(&mutex->id);
  45. free(mutex);
  46. }
  47. }
  48. void SDL_DestroyMutexP(SDL_mutex **mutex)
  49. {
  50. if (mutex) {
  51. SDL_DestroyMutex(*mutex);
  52. *mutex = NULL;
  53. }
  54. }
  55. int SDL_LockMutex(SDL_mutex *mutex)
  56. {
  57. assert(mutex);
  58. if (!mutex)
  59. return -1;
  60. return pthread_mutex_lock(&mutex->id);
  61. }
  62. int SDL_UnlockMutex(SDL_mutex *mutex)
  63. {
  64. assert(mutex);
  65. if (!mutex)
  66. return -1;
  67. return pthread_mutex_unlock(&mutex->id);
  68. }
  69. SDL_cond *SDL_CreateCond(void)
  70. {
  71. SDL_cond *cond;
  72. cond = (SDL_cond *) mallocz(sizeof(SDL_cond));
  73. if (!cond)
  74. return NULL;
  75. if (pthread_cond_init(&cond->id, NULL) != 0) {
  76. free(cond);
  77. return NULL;
  78. }
  79. return cond;
  80. }
  81. void SDL_DestroyCond(SDL_cond *cond)
  82. {
  83. if (cond) {
  84. pthread_cond_destroy(&cond->id);
  85. free(cond);
  86. }
  87. }
  88. void SDL_DestroyCondP(SDL_cond **cond)
  89. {
  90. if (cond) {
  91. SDL_DestroyCond(*cond);
  92. *cond = NULL;
  93. }
  94. }
  95. int SDL_CondSignal(SDL_cond *cond)
  96. {
  97. assert(cond);
  98. if (!cond)
  99. return -1;
  100. return pthread_cond_signal(&cond->id);
  101. }
  102. int SDL_CondBroadcast(SDL_cond *cond)
  103. {
  104. assert(cond);
  105. if (!cond)
  106. return -1;
  107. return pthread_cond_broadcast(&cond->id);
  108. }
  109. int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, uint32_t ms)
  110. {
  111. int retval;
  112. struct timeval delta;
  113. struct timespec abstime;
  114. assert(cond);
  115. assert(mutex);
  116. if (!cond || !mutex) {
  117. return -1;
  118. }
  119. gettimeofday(&delta, NULL);
  120. abstime.tv_sec = delta.tv_sec + (ms / 1000);
  121. abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000;
  122. if (abstime.tv_nsec > 1000000000) {
  123. abstime.tv_sec += 1;
  124. abstime.tv_nsec -= 1000000000;
  125. }
  126. while (1) {
  127. retval = pthread_cond_timedwait(&cond->id, &mutex->id, &abstime);
  128. if (retval == 0)
  129. return 0;
  130. else if (retval == EINTR)
  131. continue;
  132. else if (retval == ETIMEDOUT)
  133. return SDL_MUTEX_TIMEDOUT;
  134. else
  135. break;
  136. }
  137. return -1;
  138. }
  139. int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
  140. {
  141. assert(cond);
  142. assert(mutex);
  143. if (!cond || !mutex)
  144. return -1;
  145. return pthread_cond_wait(&cond->id, &mutex->id);
  146. }