blob: 4a8cd7b42084dafa0142874c5653f54601880ee5 [file] [log] [blame]
The Android Open Source Project2eb6af62009-03-03 19:30:11 -08001/* thread_init.cpp
2**
3** Copyright 2007, The Android Open Source Project
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#include "playerdriver.h"
19#include <media/thread_init.h>
20//#include <android_runtime/AndroidRuntime.h>
21#include <utils/threads.h>
22#include "android_log_appender.h"
23#include "pvlogger_time_and_id_layout.h"
24
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080025#include "oscl_mem.h"
26#include "oscl_error.h"
27
Jianhong Jiang5665cbc2009-04-14 11:08:10 -070028#include "OMX_Core.h"
29
30#if (PVLOGGER_INST_LEVEL > 0)
31#include "android_logger_config.h"
32#endif
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080033
34using namespace android;
The Android Open Source Projecta0b18db2009-03-05 17:04:47 -080035static pthread_key_t ptkey=NULL;
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080036
The Android Open Source Projecta0b18db2009-03-05 17:04:47 -080037static void keydestructor(void*)
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080038{
39 // This thread is about to exit, so we can un-initialize
40 // PV for this thread.
41 UninitializeForThread();
42}
43
The Android Open Source Projecta0b18db2009-03-05 17:04:47 -080044static pthread_once_t create_tls_entry_once = PTHREAD_ONCE_INIT;
45
46static void CreateTLSEntry() {
47 LOG_ALWAYS_FATAL_IF(
48 0 != pthread_key_create(&ptkey, keydestructor),
49 "Ran out of TLS entries");
50}
51
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080052template<class DestructClass>
53class LogAppenderDestructDealloc : public OsclDestructDealloc
54{
55public:
56 virtual void destruct_and_dealloc(OsclAny *ptr)
57 {
58 delete((DestructClass*)ptr);
59 }
60};
61
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080062bool InitializeForThread()
63{
The Android Open Source Projecta0b18db2009-03-05 17:04:47 -080064 pthread_once(&create_tls_entry_once, &CreateTLSEntry);
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080065
66 if (NULL == pthread_getspecific(ptkey)) {
67 // PV hasn't yet been initialized for this thread;
68 int error = OsclBase::Init();
69 if(error)
70 {
71 LOGE("OsclBase::Init error %d", error);
72 return false;
73 }
74 error = OsclErrorTrap::Init();
75 if(error)
76 {
77 LOGE("OsclErrorTrap::Init error %d", error);
78 return false;
79 }
80 OsclMem::Init();
81 PVLogger::Init();
82
83 void *data = &ptkey;
84 error = pthread_setspecific(ptkey,data);
85 if(error)
86 {
87 LOGE("pthread_setspecific error %d", error);
88 return false;
89 }
Jianhong Jiang5665cbc2009-04-14 11:08:10 -070090#if (PVLOGGER_INST_LEVEL > 0)
91 PVLoggerConfigFile obj;
92 if(obj.IsLoggerConfigFilePresent())
93 {
94 obj.SetLoggerSettings();
95 }
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080096#endif
97 }
98 return true;
99}
100
101
102void UninitializeForThread() {
103 PVLogger::Cleanup();
104 OsclMem::Cleanup();
105 OsclErrorTrap::Cleanup();
106 OsclBase::Cleanup();
107 // In case this didn't get called from keydestructor(), set the key
108 // to NULL for this thread, which prevents the keydestructor() from
109 // running once the thread actually exits.
110 void *data = NULL;
111 pthread_setspecific(ptkey,data);
112}
113
114