Home | History | Annotate | Download | only in sec_osal
      1 /*
      2  *
      3  * Copyright 2010 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        SEC_OSAL_Thread.c
     20  * @brief
     21  * @author      SeungBeom Kim (sbcrux.kim (at) samsung.com)
     22  * @version     1.0
     23  * @history
     24  *   2010.7.15 : 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 
     34 #include "SEC_OSAL_Memory.h"
     35 #include "SEC_OSAL_Thread.h"
     36 
     37 #undef SEC_LOG_TAG
     38 #define SEC_LOG_TAG    "SEC_LOG_THREAD"
     39 #define SEC_LOG_OFF
     40 #include "SEC_OSAL_Log.h"
     41 
     42 
     43 typedef struct _SEC_THREAD_HANDLE_TYPE
     44 {
     45     pthread_t          pthread;
     46     pthread_attr_t     attr;
     47     struct sched_param schedparam;
     48     int                stack_size;
     49 } SEC_THREAD_HANDLE_TYPE;
     50 
     51 
     52 OMX_ERRORTYPE SEC_OSAL_ThreadCreate(OMX_HANDLETYPE *threadHandle, OMX_PTR function_name, OMX_PTR argument)
     53 {
     54     FunctionIn();
     55 
     56     int result = 0;
     57     int detach_ret = 0;
     58     SEC_THREAD_HANDLE_TYPE *thread;
     59     OMX_ERRORTYPE ret = OMX_ErrorNone;
     60 
     61     thread = SEC_OSAL_Malloc(sizeof(SEC_THREAD_HANDLE_TYPE));
     62     SEC_OSAL_Memset(thread, 0, sizeof(SEC_THREAD_HANDLE_TYPE));
     63 
     64     pthread_attr_init(&thread->attr);
     65     if (thread->stack_size != 0)
     66         pthread_attr_setstacksize(&thread->attr, thread->stack_size);
     67 
     68     /* set priority */
     69     if (thread->schedparam.sched_priority != 0)
     70         pthread_attr_setschedparam(&thread->attr, &thread->schedparam);
     71 
     72     detach_ret = pthread_attr_setdetachstate(&thread->attr, PTHREAD_CREATE_JOINABLE);
     73     if (detach_ret != 0) {
     74         ret = OMX_ErrorUndefined;
     75         goto EXIT;
     76     }
     77 
     78     result = pthread_create(&thread->pthread, &thread->attr, function_name, (void *)argument);
     79     /* pthread_setschedparam(thread->pthread, SCHED_RR, &thread->schedparam); */
     80 
     81     switch (result) {
     82     case 0:
     83         *threadHandle = (OMX_HANDLETYPE)thread;
     84         ret = OMX_ErrorNone;
     85         break;
     86     case EAGAIN:
     87         *threadHandle = NULL;
     88         ret = OMX_ErrorInsufficientResources;
     89         break;
     90     default:
     91         *threadHandle = NULL;
     92         ret = OMX_ErrorUndefined;
     93         break;
     94     }
     95 
     96 EXIT:
     97     FunctionOut();
     98 
     99     return ret;
    100 }
    101 
    102 OMX_ERRORTYPE SEC_OSAL_ThreadTerminate(OMX_HANDLETYPE threadHandle)
    103 {
    104     FunctionIn();
    105 
    106     OMX_ERRORTYPE ret = OMX_ErrorNone;
    107     SEC_THREAD_HANDLE_TYPE *thread = (SEC_THREAD_HANDLE_TYPE *)threadHandle;
    108 
    109     if (!thread) {
    110         ret = OMX_ErrorBadParameter;
    111         goto EXIT;
    112     }
    113     if (pthread_join(thread->pthread, NULL) != 0) {
    114         ret = OMX_ErrorUndefined;
    115         goto EXIT;
    116     }
    117 
    118     SEC_OSAL_Free(thread);
    119     ret = OMX_ErrorNone;
    120 
    121 EXIT:
    122     FunctionOut();
    123 
    124     return ret;
    125 
    126 }
    127 
    128 OMX_ERRORTYPE SEC_OSAL_ThreadCancle(OMX_HANDLETYPE threadHandle)
    129 {
    130     SEC_THREAD_HANDLE_TYPE *thread = (SEC_THREAD_HANDLE_TYPE *)threadHandle;
    131 
    132     if (!thread)
    133         return OMX_ErrorBadParameter;
    134 
    135     /* thread_cancel(thread->pthread); */
    136     pthread_exit(thread->pthread);
    137     pthread_join(thread->pthread, NULL);
    138 
    139     SEC_OSAL_Free(thread);
    140     return OMX_ErrorNone;
    141 }
    142 
    143 void SEC_OSAL_TheadExit(void *value_ptr)
    144 {
    145     pthread_exit(value_ptr);
    146     return;
    147 }
    148 
    149 void SEC_OSAL_SleepMillisec(OMX_U32 ms)
    150 {
    151     usleep(ms * 1000);
    152     return;
    153 }
    154