OSDN Git Service

RIO-8910: Fix for MP3 parser returning incorrect value for bitrate key
[android-x86/external-opencore.git] / android / thread_init.cpp
1 /* 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
25 #include "oscl_mem.h"
26 #include "oscl_error.h"
27
28 #include "OMX_Core.h"
29
30 #include "android_logger_config.h"
31
32 using namespace android;
33 static pthread_key_t ptkey=NULL;
34
35 static void keydestructor(void*)
36 {
37     // This thread is about to exit, so we can un-initialize
38     // PV for this thread.
39     UninitializeForThread();
40 }
41
42 static pthread_once_t create_tls_entry_once = PTHREAD_ONCE_INIT;
43
44 static void CreateTLSEntry() {
45     LOG_ALWAYS_FATAL_IF(
46             0 != pthread_key_create(&ptkey, keydestructor),
47             "Ran out of TLS entries");
48 }
49
50 template<class DestructClass>
51 class LogAppenderDestructDealloc : public OsclDestructDealloc 
52 {
53 public:
54     virtual void destruct_and_dealloc(OsclAny *ptr) 
55     { 
56         delete((DestructClass*)ptr); 
57     }
58 };
59
60 bool InitializeForThread()
61 {
62     pthread_once(&create_tls_entry_once, &CreateTLSEntry);
63
64     if (NULL == pthread_getspecific(ptkey)) {
65         // PV hasn't yet been initialized for this thread;
66         int error = OsclBase::Init();
67         if(error)
68         {
69             LOGE("OsclBase::Init error %d", error);
70             return false;
71         }
72         error = OsclErrorTrap::Init();
73         if(error)
74         {
75             LOGE("OsclErrorTrap::Init error %d", error);
76             return false;
77         }
78         OsclMem::Init();
79         PVLogger::Init();
80
81         void *data = &ptkey;
82         error = pthread_setspecific(ptkey,data);
83         if(error)
84         {
85             LOGE("pthread_setspecific error %d", error);
86             return false;
87         }
88         PVLoggerConfigFile obj;
89         obj.ReadAndParseLoggerConfigFile();
90     }
91     return true;
92 }
93
94
95 void UninitializeForThread() {
96     PVLogger::Cleanup();
97     OsclMem::Cleanup();
98     OsclErrorTrap::Cleanup();
99     OsclBase::Cleanup();
100     // In case this didn't get called from keydestructor(), set the key
101     // to NULL for this thread, which prevents the keydestructor() from
102     // running once the thread actually exits.
103     void *data = NULL;
104     pthread_setspecific(ptkey,data);
105 }
106
107