ijksdl_thread.c 3.1 KB

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