1 /*
2  * Copyright (C) 2007 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 #ifndef _LIBS_CUTILS_THREADS_H
18 #define _LIBS_CUTILS_THREADS_H
19 
20 #include  <sys/types.h>
21 
22 #if !defined(_WIN32)
23 #include <pthread.h>
24 #else
25 #include <windows.h>
26 #endif
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 //
33 // Deprecated: use android::base::GetThreadId instead, which doesn't truncate on Mac/Windows.
34 //
35 
36 extern pid_t gettid();
37 
38 //
39 // Deprecated: use `_Thread_local` in C or `thread_local` in C++.
40 //
41 
42 #if !defined(_WIN32)
43 
44 typedef struct {
45     pthread_mutex_t   lock;
46     int               has_tls;
47     pthread_key_t     tls;
48 } thread_store_t;
49 
50 #define  THREAD_STORE_INITIALIZER  { PTHREAD_MUTEX_INITIALIZER, 0, 0 }
51 
52 #else // !defined(_WIN32)
53 
54 typedef struct {
55     int               lock_init;
56     int               has_tls;
57     DWORD             tls;
58     CRITICAL_SECTION  lock;
59 } thread_store_t;
60 
61 #define  THREAD_STORE_INITIALIZER  { 0, 0, 0, {0, 0, 0, 0, 0, 0} }
62 
63 #endif // !defined(_WIN32)
64 
65 typedef void  (*thread_store_destruct_t)(void*  value);
66 
67 extern void*  thread_store_get(thread_store_t*  store);
68 
69 extern void   thread_store_set(thread_store_t*          store,
70                                void*                    value,
71                                thread_store_destruct_t  destroy);
72 
73 #ifdef __cplusplus
74 }
75 #endif
76 
77 #endif /* _LIBS_CUTILS_THREADS_H */
78