Adjust a few things to avoid triggering warnings under MSVC
[folly.git] / folly / portability / PThread.h
1 /*
2  * Copyright 2017 Facebook, Inc.
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 #pragma once
18
19 #include <pthread.h>
20
21 #ifdef _WIN32
22 // We implement a sane comparison operand for
23 // pthread_t and an integer so that it may be
24 // compared against 0.
25
26 inline bool operator==(pthread_t ptA, unsigned int b) {
27   if (ptA.p == nullptr) {
28     return b == 0;
29   }
30   return pthread_getw32threadid_np(ptA) == b;
31 }
32
33 inline bool operator!=(pthread_t ptA, unsigned int b) {
34   if (ptA.p == nullptr) {
35     return b != 0;
36   }
37   return pthread_getw32threadid_np(ptA) != b;
38 }
39
40 inline bool operator==(pthread_t ptA, pthread_t ptB) {
41   return pthread_equal(ptA, ptB) != 0;
42 }
43
44 inline bool operator!=(pthread_t ptA, pthread_t ptB) {
45   return pthread_equal(ptA, ptB) == 0;
46 }
47
48 inline bool operator<(pthread_t ptA, pthread_t ptB) {
49   return ptA.p < ptB.p;
50 }
51
52 inline bool operator!(pthread_t ptA) {
53   return ptA == 0;
54 }
55
56 inline int pthread_attr_getstack(
57     pthread_attr_t* attr,
58     void** stackaddr,
59     size_t* stacksize) {
60   if (pthread_attr_getstackaddr(attr, stackaddr) != 0) {
61     return -1;
62   }
63   if (pthread_attr_getstacksize(attr, stacksize) != 0) {
64     return -1;
65   }
66   return 0;
67 }
68
69 inline int
70 pthread_attr_setstack(pthread_attr_t* attr, void* stackaddr, size_t stacksize) {
71   if (pthread_attr_setstackaddr(attr, stackaddr) != 0) {
72     return -1;
73   }
74   if (pthread_attr_setstacksize(attr, stacksize) != 0) {
75     return -1;
76   }
77   return 0;
78 }
79
80 inline int pthread_attr_getguardsize(
81     pthread_attr_t* /* attr */,
82     size_t* guardsize) {
83   *guardsize = 0;
84   return 0;
85 }
86
87 #include <xstddef>
88 namespace std {
89 template <>
90 struct hash<pthread_t> {
91   std::size_t operator()(const pthread_t& k) const {
92     return 0 ^ std::hash<decltype(k.p)>()(k.p) ^
93         std::hash<decltype(k.x)>()(k.x);
94   }
95 };
96 }
97 #endif