1/*
2 *
3 * Copyright 2012 Samsung Electronics S.LSI 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        Exynos_OSAL_Thread.c
20 * @brief
21 * @author      SeungBeom Kim (sbcrux.kim@samsung.com)
22 * @version     2.0.0
23 * @history
24 *   2012.02.20 : Create
25 */
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <pthread.h>
31#include <semaphore.h>
32#include <errno.h>
33#include <time.h>
34#include <unistd.h>
35
36#include "Exynos_OSAL_Memory.h"
37#include "Exynos_OSAL_Thread.h"
38
39#undef EXYNOS_LOG_TAG
40#define EXYNOS_LOG_TAG    "EXYNOS_LOG_THREAD"
41#define EXYNOS_LOG_OFF
42#include "Exynos_OSAL_Log.h"
43
44
45typedef struct _EXYNOS_THREAD_HANDLE_TYPE
46{
47    pthread_t          pthread;
48    pthread_attr_t     attr;
49    struct sched_param schedparam;
50    int                stack_size;
51} EXYNOS_THREAD_HANDLE_TYPE;
52
53
54OMX_ERRORTYPE Exynos_OSAL_ThreadCreate(OMX_HANDLETYPE *threadHandle, OMX_PTR function_name, OMX_PTR argument)
55{
56    FunctionIn();
57
58    int result = 0;
59    int detach_ret = 0;
60    EXYNOS_THREAD_HANDLE_TYPE *thread;
61    OMX_ERRORTYPE ret = OMX_ErrorNone;
62
63    thread = Exynos_OSAL_Malloc(sizeof(EXYNOS_THREAD_HANDLE_TYPE));
64    Exynos_OSAL_Memset(thread, 0, sizeof(EXYNOS_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    /* set priority */
71    if (thread->schedparam.sched_priority != 0)
72        pthread_attr_setschedparam(&thread->attr, &thread->schedparam);
73
74    detach_ret = pthread_attr_setdetachstate(&thread->attr, PTHREAD_CREATE_JOINABLE);
75    if (detach_ret != 0) {
76        Exynos_OSAL_Free(thread);
77        *threadHandle = NULL;
78        ret = OMX_ErrorUndefined;
79        goto EXIT;
80    }
81
82    result = pthread_create(&thread->pthread, &thread->attr, function_name, (void *)argument);
83    /* pthread_setschedparam(thread->pthread, SCHED_RR, &thread->schedparam); */
84
85    switch (result) {
86    case 0:
87        *threadHandle = (OMX_HANDLETYPE)thread;
88        ret = OMX_ErrorNone;
89        break;
90    case EAGAIN:
91        Exynos_OSAL_Free(thread);
92        *threadHandle = NULL;
93        ret = OMX_ErrorInsufficientResources;
94        break;
95    default:
96        Exynos_OSAL_Free(thread);
97        *threadHandle = NULL;
98        ret = OMX_ErrorUndefined;
99        break;
100    }
101
102EXIT:
103    FunctionOut();
104
105    return ret;
106}
107
108OMX_ERRORTYPE Exynos_OSAL_ThreadTerminate(OMX_HANDLETYPE threadHandle)
109{
110    FunctionIn();
111
112    OMX_ERRORTYPE ret = OMX_ErrorNone;
113    EXYNOS_THREAD_HANDLE_TYPE *thread = (EXYNOS_THREAD_HANDLE_TYPE *)threadHandle;
114
115    if (!thread) {
116        ret = OMX_ErrorBadParameter;
117        goto EXIT;
118    }
119    if (pthread_join(thread->pthread, NULL) != 0) {
120        ret = OMX_ErrorUndefined;
121        goto EXIT;
122    }
123
124    Exynos_OSAL_Free(thread);
125    ret = OMX_ErrorNone;
126
127EXIT:
128    FunctionOut();
129
130    return ret;
131}
132
133OMX_ERRORTYPE Exynos_OSAL_ThreadCancel(OMX_HANDLETYPE threadHandle)
134{
135    EXYNOS_THREAD_HANDLE_TYPE *thread = (EXYNOS_THREAD_HANDLE_TYPE *)threadHandle;
136
137    if (!thread)
138        return OMX_ErrorBadParameter;
139
140    /* thread_cancel(thread->pthread); */
141    pthread_exit(&thread->pthread);
142    pthread_join(thread->pthread, NULL);
143
144    Exynos_OSAL_Free(thread);
145    return OMX_ErrorNone;
146}
147
148void Exynos_OSAL_ThreadExit(void *value_ptr)
149{
150    pthread_exit(value_ptr);
151    return;
152}
153
154void Exynos_OSAL_SleepMillisec(OMX_U32 ms)
155{
156    usleep(ms * 1000);
157    return;
158}
159