RDK Documentation (Open Sourced RDK Components)
Thread.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 
22 /**
23 * @defgroup hdmicec
24 * @{
25 * @defgroup osal
26 * @{
27 **/
28 
29 
30 #include <stdlib.h>
31 #include <pthread.h>
32 
33 #include "osal/Runnable.hpp"
34 #include "osal/Thread.hpp"
35 
36 CCEC_OSAL_BEGIN_NAMESPACE
37 
38 void *Thread::CEntry(void * arg)
39 {
40  Runnable *target = static_cast<Runnable *>(arg);
41  target->run();
42  return NULL;
43 }
44 
45 Thread::Thread(Runnable &target) : runnable(target)
46 {
47 }
48 
49 Thread::Thread(Runnable &target, const int8_t* name) : runnable(target), name((const char *)name), nativeHandle(0)
50 {
51 }
52 
54 {
55 }
56 
57 void Thread::run(void)
58 {
59  runnable.run();
60 }
61 
63 {
64  pthread_t tid;
65  pthread_attr_t attr;
66  int ret = 0;
67  (void) pthread_attr_init(&attr);
68  (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
69  (void) pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
70 
71  /*@TODO: Set priority */
72  ret = pthread_create(&tid, &attr, Thread::CEntry, static_cast<void *>(&runnable));
73  pthread_attr_destroy(&attr);
74 
75  if (ret != 0) {
76  /*@TODO: Throw Exception */
77  }
78  else {
79  nativeHandle = (void *)tid;
80  }
81 }
82 
83 void Thread::detach(void)
84 {
85  pthread_detach((pthread_t)nativeHandle);
86 }
87 
88 CCEC_OSAL_END_NAMESPACE
89 
90 
91 /** @} */
92 /** @} */
CCEC_OSAL::Thread::run
void run(void)
Executes the run() method of runnable object.
Definition: Thread.cpp:57
CCEC_OSAL::Thread::runnable
Runnable & runnable
Destructor.
Definition: Thread.hpp:148
CCEC_OSAL::Runnable
Definition: Runnable.hpp:37
Thread.hpp
This file defines interface of Thread class.
CCEC_OSAL::Thread::detach
void detach(void)
Detaches the thread.
Definition: Thread.cpp:83
CCEC_OSAL::Thread::~Thread
virtual ~Thread(void)
Destructor.
Definition: Thread.cpp:53
CCEC_OSAL::Thread::CEntry
static void * CEntry(void *arg)
Destructor.
Definition: Thread.cpp:38
CCEC_OSAL::Thread::start
void start(void)
Starts excecution of the thread.
Definition: Thread.cpp:62
CCEC_OSAL::Thread::Thread
Thread(Runnable &target)
Constructor.
Definition: Thread.cpp:45
CCEC_OSAL::Thread::nativeHandle
void * nativeHandle
Destructor.
Definition: Thread.hpp:150