ijksdl_thread.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*****************************************************************************
  2. * ijksdl_thread.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 <errno.h>
  25. #include <assert.h>
  26. #include <unistd.h>
  27. #include "ijksdl_inc_internal.h"
  28. #include "ijksdl_thread.h"
  29. #ifdef __ANDROID__
  30. #include "ijksdl/android/ijksdl_android_jni.h"
  31. #endif
  32. #if !defined(__APPLE__)
  33. // using ios implement for autorelease
  34. static void *SDL_RunThread(void *data)
  35. {
  36. SDL_Thread *thread = data;
  37. ALOGI("SDL_RunThread: [%d] %s\n", (int)gettid(), thread->name);
  38. pthread_setname_np(pthread_self(), thread->name);
  39. thread->retval = thread->func(thread->data);
  40. #ifdef __ANDROID__
  41. SDL_JNI_DetachThreadEnv();
  42. #endif
  43. return NULL;
  44. }
  45. SDL_Thread *SDL_CreateThreadEx(SDL_Thread *thread, int (*fn)(void *), void *data, const char *name)
  46. {
  47. thread->func = fn;
  48. thread->data = data;
  49. strlcpy(thread->name, name, sizeof(thread->name) - 1);
  50. int retval = pthread_create(&thread->id, NULL, SDL_RunThread, thread);
  51. if (retval)
  52. return NULL;
  53. return thread;
  54. }
  55. #endif
  56. int SDL_SetThreadPriority(SDL_ThreadPriority priority)
  57. {
  58. struct sched_param sched;
  59. int policy;
  60. pthread_t thread = pthread_self();
  61. if (pthread_getschedparam(thread, &policy, &sched) < 0) {
  62. ALOGE("pthread_getschedparam() failed");
  63. return -1;
  64. }
  65. if (priority == SDL_THREAD_PRIORITY_LOW) {
  66. sched.sched_priority = sched_get_priority_min(policy);
  67. } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
  68. sched.sched_priority = sched_get_priority_max(policy);
  69. } else {
  70. int min_priority = sched_get_priority_min(policy);
  71. int max_priority = sched_get_priority_max(policy);
  72. sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
  73. }
  74. if (pthread_setschedparam(thread, policy, &sched) < 0) {
  75. ALOGE("pthread_setschedparam() failed");
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. void SDL_WaitThread(SDL_Thread *thread, int *status)
  81. {
  82. assert(thread);
  83. if (!thread)
  84. return;
  85. pthread_join(thread->id, NULL);
  86. if (status)
  87. *status = thread->retval;
  88. }
  89. void SDL_DetachThread(SDL_Thread *thread)
  90. {
  91. assert(thread);
  92. if (!thread)
  93. return;
  94. pthread_detach(thread->id);
  95. }