aba122a78e1a76364d8d64d60b33b94aa154056b
[folly.git] / folly / portability / PThread.cpp
1 /*\r
2  * Copyright 2017 Facebook, Inc.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *   http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 #include <folly/portability/PThread.h>\r
18 \r
19 #if !FOLLY_HAVE_PTHREAD && _WIN32\r
20 #include <unordered_map>\r
21 #include <utility>\r
22 \r
23 namespace folly {\r
24 namespace portability {\r
25 namespace pthread {\r
26 static thread_local struct PThreadLocalMap {\r
27   PThreadLocalMap() = default;\r
28   ~PThreadLocalMap() {\r
29     for (auto kv : keyMap) {\r
30       // Call destruction callbacks if they exist.\r
31       if (kv.second.second != nullptr) {\r
32         kv.second.second(kv.second.first);\r
33       }\r
34     }\r
35   }\r
36 \r
37   int createKey(pthread_key_t* key, void (*destructor)(void*)) {\r
38     auto ret = TlsAlloc();\r
39     if (ret == TLS_OUT_OF_INDEXES) {\r
40       return -1;\r
41     }\r
42     *key = ret;\r
43     keyMap.emplace(*key, std::make_pair(nullptr, destructor));\r
44     return 0;\r
45   }\r
46 \r
47   int deleteKey(pthread_key_t key) {\r
48     if (!TlsFree(key)) {\r
49       return -1;\r
50     }\r
51     keyMap.erase(key);\r
52     return 0;\r
53   }\r
54 \r
55   void* getKey(pthread_key_t key) {\r
56     return TlsGetValue(key);\r
57   }\r
58 \r
59   int setKey(pthread_key_t key, void* value) {\r
60     if (!TlsSetValue(key, value)) {\r
61       return -1;\r
62     }\r
63     keyMap[key].first = value;\r
64     return 0;\r
65   }\r
66 \r
67   std::unordered_map<pthread_key_t, std::pair<void*, void (*)(void*)>> keyMap{};\r
68 } s_tls_key_map;\r
69 \r
70 int pthread_key_create(pthread_key_t* key, void (*destructor)(void*)) {\r
71   return s_tls_key_map.createKey(key, destructor);\r
72 }\r
73 \r
74 int pthread_key_delete(pthread_key_t key) {\r
75   return s_tls_key_map.deleteKey(key);\r
76 }\r
77 \r
78 void* pthread_getspecific(pthread_key_t key) {\r
79   return s_tls_key_map.getKey(key);\r
80 }\r
81 \r
82 int pthread_setspecific(pthread_key_t key, const void* value) {\r
83   // Yes, the PThread API really is this bad -_-...\r
84   return s_tls_key_map.setKey(key, const_cast<void*>(value));\r
85 }\r
86 }\r
87 }\r
88 }\r
89 #endif\r