proxygen
PThreadTest.cpp File Reference
#include <folly/portability/PThread.h>
#include <folly/portability/GTest.h>
#include <atomic>

Go to the source code of this file.

Functions

 TEST (PThreadTest, pthread_create_and_join)
 
 TEST (PThreadTest, pthread_join_return_value)
 
 TEST (PThreadTest, pthread_equal)
 
 TEST (PThreadTest, pthread_self_on_pthread_thread)
 

Function Documentation

TEST ( PThreadTest  ,
pthread_create_and_join   
)

Definition at line 23 of file PThreadTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, and int32_t.

23  {
24  static std::atomic<bool> hasRun{false};
25  static std::atomic<int32_t> argPassedIn{0};
26  auto mainFunc = [](void* arg) -> void* {
27  hasRun = true;
28  argPassedIn = (int32_t) reinterpret_cast<uintptr_t>(arg);
29  return nullptr;
30  };
31 
32  pthread_t thread;
33  EXPECT_EQ(pthread_create(&thread, nullptr, mainFunc, (void*)53), 0);
34  EXPECT_EQ(pthread_join(thread, nullptr), 0);
35  EXPECT_TRUE(hasRun);
36  EXPECT_EQ(argPassedIn, 53);
37 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( PThreadTest  ,
pthread_join_return_value   
)

Definition at line 39 of file PThreadTest.cpp.

References EXPECT_EQ, and EXPECT_TRUE.

39  {
40  static std::atomic<bool> hasRun{false};
41  auto mainFunc = [](void*) -> void* {
42  hasRun = true;
43  return (void*)5;
44  };
45 
46  pthread_t thread;
47  EXPECT_EQ(pthread_create(&thread, nullptr, mainFunc, nullptr), 0);
48  void* exitCode = nullptr;
49  EXPECT_EQ(pthread_join(thread, &exitCode), 0);
50  EXPECT_EQ(exitCode, (void*)5);
51  EXPECT_TRUE(hasRun);
52 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( PThreadTest  ,
pthread_equal   
)

Definition at line 54 of file PThreadTest.cpp.

References EXPECT_EQ, and EXPECT_NE.

54  {
55  auto self = pthread_self();
56  EXPECT_NE(pthread_equal(self, self), 0);
57 
58  auto mainFunc = [](void*) -> void* { return nullptr; };
59  pthread_t thread;
60  EXPECT_EQ(pthread_create(&thread, nullptr, mainFunc, nullptr), 0);
61  EXPECT_EQ(pthread_equal(thread, self), 0);
62 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( PThreadTest  ,
pthread_self_on_pthread_thread   
)

Definition at line 64 of file PThreadTest.cpp.

References EXPECT_EQ, EXPECT_NE, and EXPECT_TRUE.

64  {
65  static std::atomic<bool> hasRun{false};
66  static pthread_t otherSelf;
67  auto mainFunc = [](void*) -> void* {
68  hasRun = true;
69  otherSelf = pthread_self();
70  return nullptr;
71  };
72 
73  pthread_t thread;
74  EXPECT_EQ(pthread_create(&thread, nullptr, mainFunc, nullptr), 0);
75  EXPECT_EQ(pthread_join(thread, nullptr), 0);
76  EXPECT_NE(pthread_equal(otherSelf, thread), 0);
77  EXPECT_TRUE(hasRun);
78 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926