3 * Copyright 2012 Samsung Electronics S.LSI Co. LTD
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file Exynos_OSAL_Thread.c
21 * @author SeungBeom Kim (sbcrux.kim@samsung.com)
31 #include <semaphore.h>
36 #include "Exynos_OSAL_Memory.h"
37 #include "Exynos_OSAL_Thread.h"
40 #define EXYNOS_LOG_TAG "EXYNOS_LOG_THREAD"
41 //#define EXYNOS_LOG_OFF
42 #include "Exynos_OSAL_Log.h"
45 typedef struct _EXYNOS_THREAD_HANDLE_TYPE
49 struct sched_param schedparam
;
51 } EXYNOS_THREAD_HANDLE_TYPE
;
54 OMX_ERRORTYPE
Exynos_OSAL_ThreadCreate(OMX_HANDLETYPE
*threadHandle
, OMX_PTR function_name
, OMX_PTR argument
)
60 EXYNOS_THREAD_HANDLE_TYPE
*thread
;
61 OMX_ERRORTYPE ret
= OMX_ErrorNone
;
63 thread
= Exynos_OSAL_Malloc(sizeof(EXYNOS_THREAD_HANDLE_TYPE
));
64 Exynos_OSAL_Memset(thread
, 0, sizeof(EXYNOS_THREAD_HANDLE_TYPE
));
66 pthread_attr_init(&thread
->attr
);
67 if (thread
->stack_size
!= 0)
68 pthread_attr_setstacksize(&thread
->attr
, thread
->stack_size
);
71 if (thread
->schedparam
.sched_priority
!= 0)
72 pthread_attr_setschedparam(&thread
->attr
, &thread
->schedparam
);
74 detach_ret
= pthread_attr_setdetachstate(&thread
->attr
, PTHREAD_CREATE_JOINABLE
);
75 if (detach_ret
!= 0) {
76 Exynos_OSAL_Free(thread
);
78 ret
= OMX_ErrorUndefined
;
82 result
= pthread_create(&thread
->pthread
, &thread
->attr
, function_name
, (void *)argument
);
83 /* pthread_setschedparam(thread->pthread, SCHED_RR, &thread->schedparam); */
87 *threadHandle
= (OMX_HANDLETYPE
)thread
;
91 Exynos_OSAL_Free(thread
);
93 ret
= OMX_ErrorInsufficientResources
;
96 Exynos_OSAL_Free(thread
);
98 ret
= OMX_ErrorUndefined
;
108 OMX_ERRORTYPE
Exynos_OSAL_ThreadTerminate(OMX_HANDLETYPE threadHandle
)
112 OMX_ERRORTYPE ret
= OMX_ErrorNone
;
113 EXYNOS_THREAD_HANDLE_TYPE
*thread
= (EXYNOS_THREAD_HANDLE_TYPE
*)threadHandle
;
116 ret
= OMX_ErrorBadParameter
;
119 if (pthread_join(thread
->pthread
, NULL
) != 0) {
120 ret
= OMX_ErrorUndefined
;
124 Exynos_OSAL_Free(thread
);
133 OMX_ERRORTYPE
Exynos_OSAL_ThreadCancel(OMX_HANDLETYPE threadHandle
)
135 EXYNOS_THREAD_HANDLE_TYPE
*thread
= (EXYNOS_THREAD_HANDLE_TYPE
*)threadHandle
;
138 return OMX_ErrorBadParameter
;
140 /* thread_cancel(thread->pthread); */
141 pthread_exit(&thread
->pthread
);
142 pthread_join(thread
->pthread
, NULL
);
144 Exynos_OSAL_Free(thread
);
145 return OMX_ErrorNone
;
148 void Exynos_OSAL_ThreadExit(void *value_ptr
)
150 pthread_exit(value_ptr
);
154 void Exynos_OSAL_SleepMillisec(OMX_U32 ms
)
159 struct timespec time
;
160 time
.tv_sec
= (time_t)(ms
/ 1000);
161 time
.tv_nsec
= (long)(ms
% 1000) * 1000000;
162 nanosleep(&time
, NULL
);