1 // Copyright 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "ThreadInfo.h"
15 
16 #include "android/base/memory/LazyInstance.h"
17 #include "android/base/threads/Thread.h"
18 #include "android/base/threads/ThreadStore.h"
19 
20 using android::base::LazyInstance;
21 using android::base::ThreadStoreBase;
22 
sDefaultTlsDestructorCallback(void * ptr)23 static bool sDefaultTlsDestructorCallback(__attribute__((__unused__)) void* ptr) {
24   return true;
25 }
26 static bool (*sTlsDestructorCallback)(void*) = sDefaultTlsDestructorCallback;
27 
setTlsDestructor(tlsDtorCallback func)28 void setTlsDestructor(tlsDtorCallback func) {
29     sTlsDestructorCallback = func;
30 }
31 
doTlsDestruct(void * obj)32 static void doTlsDestruct(void* obj) {
33     sTlsDestructorCallback(obj);
34 }
35 
36 class ThreadInfoStore : public ThreadStoreBase {
37 public:
ThreadInfoStore()38     ThreadInfoStore() : ThreadStoreBase(NULL) { }
39     ~ThreadInfoStore();
40 };
41 
42 static LazyInstance<ThreadInfoStore> sTls = LAZY_INSTANCE_INIT;
43 
~ThreadInfoStore()44 ThreadInfoStore::~ThreadInfoStore() {
45     doTlsDestruct(sTls->get());
46 }
47 
goldfish_get_egl_tls()48 EGLThreadInfo *goldfish_get_egl_tls()
49 {
50     EGLThreadInfo* ti = (EGLThreadInfo*)sTls->get();
51 
52     if (ti) return ti;
53 
54     ti = new EGLThreadInfo();
55     sTls->set(ti);
56 
57     return ti;
58 }
59 
getEGLThreadInfo()60 EGLThreadInfo* getEGLThreadInfo() {
61     return goldfish_get_egl_tls();
62 }
63 
getCurrentThreadId()64 int32_t getCurrentThreadId() {
65     return (int32_t)android::base::getCurrentThreadId();
66 }
67