1 /*
2  * Copyright (C) 2007-2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * Some OS specific dribs and drabs (locking etc).
19  */
20 
21 #if !defined(_WIN32)
22 #include <pthread.h>
23 #endif
24 
25 #include "logger.h"
26 
27 #if !defined(_WIN32)
28 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
29 #endif
30 
__android_log_lock()31 LIBLOG_HIDDEN void __android_log_lock() {
32 #if !defined(_WIN32)
33   /*
34    * If we trigger a signal handler in the middle of locked activity and the
35    * signal handler logs a message, we could get into a deadlock state.
36    */
37   pthread_mutex_lock(&log_init_lock);
38 #endif
39 }
40 
__android_log_trylock()41 LIBLOG_HIDDEN int __android_log_trylock() {
42 #if !defined(_WIN32)
43   return pthread_mutex_trylock(&log_init_lock);
44 #else
45   return 0;
46 #endif
47 }
48 
__android_log_unlock()49 LIBLOG_HIDDEN void __android_log_unlock() {
50 #if !defined(_WIN32)
51   pthread_mutex_unlock(&log_init_lock);
52 #endif
53 }
54