forked from bilibili/ijkplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathijksdl_thread.c
105 lines (94 loc) · 3.07 KB
/
ijksdl_thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*****************************************************************************
* ijksdl_thread.c
*****************************************************************************
*
* copyright (c) 2013 Zhang Rui <[email protected]>
*
* This file is part of ijkPlayer.
*
* ijkPlayer is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* ijkPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with ijkPlayer; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include "ijksdl_inc_internal.h"
#include "ijksdl_thread.h"
#ifdef __ANDROID__
#include "ijksdl/android/ijksdl_android_jni.h"
#endif
#if !defined(__APPLE__)
// using ios implement for autorelease
static void *SDL_RunThread(void *data)
{
SDL_Thread *thread = data;
ALOGI("SDL_RunThread: [%d] %s\n", (int)gettid(), thread->name);
pthread_setname_np(pthread_self(), thread->name);
thread->retval = thread->func(thread->data);
#ifdef __ANDROID__
SDL_JNI_DetachThreadEnv();
#endif
return NULL;
}
SDL_Thread *SDL_CreateThreadEx(SDL_Thread *thread, int (*fn)(void *), void *data, const char *name)
{
thread->func = fn;
thread->data = data;
strlcpy(thread->name, name, sizeof(thread->name) - 1);
int retval = pthread_create(&thread->id, NULL, SDL_RunThread, thread);
if (retval)
return NULL;
return thread;
}
#endif
int SDL_SetThreadPriority(SDL_ThreadPriority priority)
{
struct sched_param sched;
int policy;
pthread_t thread = pthread_self();
if (pthread_getschedparam(thread, &policy, &sched) < 0) {
ALOGE("pthread_getschedparam() failed");
return -1;
}
if (priority == SDL_THREAD_PRIORITY_LOW) {
sched.sched_priority = sched_get_priority_min(policy);
} else if (priority == SDL_THREAD_PRIORITY_HIGH) {
sched.sched_priority = sched_get_priority_max(policy);
} else {
int min_priority = sched_get_priority_min(policy);
int max_priority = sched_get_priority_max(policy);
sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
}
if (pthread_setschedparam(thread, policy, &sched) < 0) {
ALOGE("pthread_setschedparam() failed");
return -1;
}
return 0;
}
void SDL_WaitThread(SDL_Thread *thread, int *status)
{
assert(thread);
if (!thread)
return;
pthread_join(thread->id, NULL);
if (status)
*status = thread->retval;
}
void SDL_DetachThread(SDL_Thread *thread)
{
assert(thread);
if (!thread)
return;
pthread_detach(thread->id);
}