1 /*
2 *
3 * Copyright 2013 Rockchip Electronics Co., LTD.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*
19 * @file Rockchip_OSAL_Thread.c
20 * @brief
21 * @author csy(csy@rock-chips.com)
22 * @version 1.0.0
23 * @history
24 * 2013.11.26 : Create
25 */
26 #undef ROCKCHIP_LOG_TAG
27 #define ROCKCHIP_LOG_TAG "omx_osal_thread"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <pthread.h>
33 #include <semaphore.h>
34 #include <errno.h>
35 #include <time.h>
36 #include <unistd.h>
37
38 #include "Rockchip_OSAL_Memory.h"
39 #include "Rockchip_OSAL_Log.h"
40 #include "Rockchip_OSAL_Thread.h"
41
42 typedef struct _ROCKCHIP_THREAD_HANDLE_TYPE {
43 pthread_t pthread;
44 pthread_attr_t attr;
45 struct sched_param schedparam;
46 int stack_size;
47 } ROCKCHIP_THREAD_HANDLE_TYPE;
48
49
Rockchip_OSAL_ThreadCreate(OMX_HANDLETYPE * threadHandle,OMX_PTR function_name,OMX_PTR argument,OMX_PTR thread_name)50 OMX_ERRORTYPE Rockchip_OSAL_ThreadCreate(
51 OMX_HANDLETYPE *threadHandle,
52 OMX_PTR function_name,
53 OMX_PTR argument,
54 OMX_PTR thread_name)
55 {
56 FunctionIn();
57
58 int result = 0;
59 int detach_ret = 0;
60 ROCKCHIP_THREAD_HANDLE_TYPE *thread;
61 OMX_ERRORTYPE ret = OMX_ErrorNone;
62
63 thread = Rockchip_OSAL_Malloc(sizeof(ROCKCHIP_THREAD_HANDLE_TYPE));
64 Rockchip_OSAL_Memset(thread, 0, sizeof(ROCKCHIP_THREAD_HANDLE_TYPE));
65
66 pthread_attr_init(&thread->attr);
67 if (thread->stack_size != 0) {
68 pthread_attr_setstacksize(&thread->attr, thread->stack_size);
69 }
70
71 /* set priority */
72 if (thread->schedparam.sched_priority != 0) {
73 pthread_attr_setschedparam(&thread->attr, &thread->schedparam);
74 }
75
76 detach_ret = pthread_attr_setdetachstate(&thread->attr, PTHREAD_CREATE_JOINABLE);
77 if (detach_ret != 0) {
78 Rockchip_OSAL_Free(thread);
79 *threadHandle = NULL;
80 ret = OMX_ErrorUndefined;
81 goto EXIT;
82 }
83
84 result = pthread_create(&thread->pthread, &thread->attr, function_name, (void *)argument);
85 pthread_setname_np(thread->pthread, thread_name);
86
87 switch (result) {
88 case 0:
89 *threadHandle = (OMX_HANDLETYPE)thread;
90 ret = OMX_ErrorNone;
91 break;
92 case EAGAIN:
93 Rockchip_OSAL_Free(thread);
94 *threadHandle = NULL;
95 ret = OMX_ErrorInsufficientResources;
96 break;
97 default:
98 Rockchip_OSAL_Free(thread);
99 *threadHandle = NULL;
100 ret = OMX_ErrorUndefined;
101 break;
102 }
103
104 EXIT:
105 FunctionOut();
106
107 return ret;
108 }
109
Rockchip_OSAL_ThreadTerminate(OMX_HANDLETYPE threadHandle)110 OMX_ERRORTYPE Rockchip_OSAL_ThreadTerminate(OMX_HANDLETYPE threadHandle)
111 {
112 FunctionIn();
113
114 OMX_ERRORTYPE ret = OMX_ErrorNone;
115 ROCKCHIP_THREAD_HANDLE_TYPE *thread = (ROCKCHIP_THREAD_HANDLE_TYPE *)threadHandle;
116
117 if (!thread) {
118 ret = OMX_ErrorBadParameter;
119 goto EXIT;
120 }
121 if (pthread_join(thread->pthread, NULL) != 0) {
122 ret = OMX_ErrorUndefined;
123 goto EXIT;
124 }
125
126 Rockchip_OSAL_Free(thread);
127 ret = OMX_ErrorNone;
128
129 EXIT:
130 FunctionOut();
131
132 return ret;
133 }
134
Rockchip_OSAL_ThreadCancel(OMX_HANDLETYPE threadHandle)135 OMX_ERRORTYPE Rockchip_OSAL_ThreadCancel(OMX_HANDLETYPE threadHandle)
136 {
137 ROCKCHIP_THREAD_HANDLE_TYPE *thread = (ROCKCHIP_THREAD_HANDLE_TYPE *)threadHandle;
138
139 if (!thread)
140 return OMX_ErrorBadParameter;
141
142 pthread_exit(&thread->pthread);
143 pthread_join(thread->pthread, NULL);
144
145 Rockchip_OSAL_Free(thread);
146 return OMX_ErrorNone;
147 }
148
Rockchip_OSAL_ThreadExit(void * value_ptr)149 void Rockchip_OSAL_ThreadExit(void *value_ptr)
150 {
151 pthread_exit(value_ptr);
152 return;
153 }
154
Rockchip_OSAL_SleepMillisec(OMX_U32 ms)155 void Rockchip_OSAL_SleepMillisec(OMX_U32 ms)
156 {
157 usleep(ms * 1000); // 1000:time shift
158 return;
159 }