1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdlib.h>
6
7 #include <atomic>
8 #include <cassert>
9 #include <cstdarg>
10 #include <cstdint>
11 #include <cstdio>
12 #include <thread>
13
14 #include "cutils/log.h"
15 #include "cutils/properties.h"
16 #include "cutils/threads.h"
17
18 extern "C" {
19
property_get(const char * key,char * value,const char * default_value)20 int property_get(const char* key, char* value, const char* default_value) {
21 return 0;
22 }
23
__android_log_print(int priority,const char * tag,const char * format,...)24 int __android_log_print(int priority, const char* tag, const char* format,
25 ...) {
26 if (priority == ANDROID_LOG_VERBOSE || priority == ANDROID_LOG_DEBUG) {
27 return 1;
28 }
29 const char* local_tag = tag;
30 if (!local_tag) {
31 local_tag = "<NO_TAG>";
32 }
33 printf("%d %s ", priority, local_tag);
34 va_list ap;
35 va_start(ap, format);
36 vprintf(format, ap);
37 va_end(ap);
38 printf("\n");
39 return 1;
40 }
41
__android_log_assert(const char * condition,const char * tag,const char * format,...)42 void __android_log_assert(const char* condition, const char* tag,
43 const char* format, ...) {
44 const char* local_tag = tag;
45 if (!local_tag) {
46 local_tag = "<NO_TAG>";
47 }
48 printf("__android_log_assert: condition: %s tag: %s ", condition, local_tag);
49 if (format) {
50 va_list ap;
51 va_start(ap, format);
52 vprintf(format, ap);
53 va_end(ap);
54 }
55 printf("\n");
56
57 assert(0);
58 exit(-1);
59 }
60
sync_wait(int fd,int timeout)61 int sync_wait(int fd, int timeout) {
62 return -1;
63 }
64
thread_store_get(thread_store_t * store)65 void* thread_store_get(thread_store_t* store) {
66 return store->has_tls ? pthread_getspecific(store->tls) : nullptr;
67 }
68
thread_store_set(thread_store_t * store,void * value,thread_store_destruct_t destroy)69 void thread_store_set(thread_store_t* store,
70 void* value,
71 thread_store_destruct_t destroy) {
72 pthread_mutex_lock(&store->lock);
73 if (!store->has_tls) {
74 if (pthread_key_create(&store->tls, destroy) != 0) {
75 pthread_mutex_unlock(&store->lock);
76 return;
77 }
78 store->has_tls = 1;
79 }
80 pthread_mutex_unlock(&store->lock);
81 pthread_setspecific(store->tls, value);
82 }
83
gettid()84 pid_t gettid() {
85 static thread_local pid_t id = 0;
86 if (!id) {
87 static std::atomic<pid_t> next_thread_id{1};
88 id = next_thread_id++;
89 }
90 return id;
91 }
92
93 }
94