0406b56d1f1ab2a4d8b4f6dca550e4f37db33abb
[folly.git] / folly / test / PackedSyncPtrTest.cpp
1 /*
2  * Copyright 2014 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 #include "folly/PackedSyncPtr.h"
18
19 #include <cinttypes>
20 #include <gtest/gtest.h>
21 #include <thread>
22 #include <unordered_map>
23 #include <utility>
24
25 using folly::PackedSyncPtr;
26
27 namespace {
28
29 // Compile time check for packability.  This requires that
30 // PackedSyncPtr is a POD struct on gcc.
31 FOLLY_PACK_PUSH
32 struct ignore { PackedSyncPtr<int> foo; char c; } FOLLY_PACK_ATTR;
33 FOLLY_PACK_POP
34 static_assert(sizeof(ignore) == 9, "PackedSyncPtr wasn't packable");
35
36 }
37
38 TEST(PackedSyncPtr, Basic) {
39   PackedSyncPtr<std::pair<int,int>> sp;
40   sp.init(new std::pair<int,int>[2]);
41   EXPECT_EQ(sizeof(sp), 8);
42   sp->first = 5;
43   EXPECT_EQ(sp[0].first, 5);
44   sp[1].second = 7;
45   EXPECT_EQ(sp[1].second, 7);
46   sp.lock();
47   EXPECT_EQ(sp[1].second, 7);
48   sp[0].first = 9;
49   EXPECT_EQ(sp->first, 9);
50   sp.unlock();
51   EXPECT_EQ((sp.get() + 1)->second, 7);
52
53   sp.lock();
54   EXPECT_EQ(sp.extra(), 0);
55   sp.setExtra(0x13);
56   EXPECT_EQ(sp.extra(), 0x13);
57   EXPECT_EQ((sp.get() + 1)->second, 7);
58   delete[] sp.get();
59   auto newP = new std::pair<int,int>();
60   sp.set(newP);
61   EXPECT_EQ(sp.extra(), 0x13);
62   EXPECT_EQ(sp.get(), newP);
63   sp.unlock();
64 }
65
66 // Here we use the PackedSyncPtr to lock the whole SyncVec (base, *base, and sz)
67 template<typename T>
68 struct SyncVec {
69   PackedSyncPtr<T> base;
70   SyncVec() { base.init(); }
71   void push_back(const T& t) {
72     base.set((T*) realloc(base.get(),
73       (base.extra() + 1) * sizeof(T)));
74     base[base.extra()] = t;
75     base.setExtra(base.extra() + 1);
76   }
77   void lock() {
78     base.lock();
79   }
80   void unlock() {
81     base.unlock();
82   }
83
84   T* begin() const { return base.get(); }
85   T* end() const { return base.get() + base.extra(); }
86 };
87 typedef SyncVec<intptr_t> VecT;
88 typedef std::unordered_map<int64_t, VecT> Map;
89 const int mapCap = 1317;
90 const int nthrs = 297;
91 static Map map(mapCap);
92
93 // Each app thread inserts it's ID into every vec in map
94 // map is read only, so doesn't need any additional locking
95 void appThread(intptr_t id) {
96   for (auto& kv : map) {
97     kv.second.lock();
98     kv.second.push_back(id);
99     kv.second.unlock();
100   }
101 }
102
103 TEST(PackedSyncPtr, Application) {
104   for (int64_t i = 0; i < mapCap / 2; ++i) {
105     map.insert(std::make_pair(i, VecT()));
106   }
107   std::vector<std::thread> thrs;
108   for (intptr_t i = 0; i < nthrs; i++) {
109     thrs.push_back(std::thread(appThread, i));
110   }
111   for (auto& t : thrs) {
112     t.join();
113   }
114
115   for (auto& kv : map) {
116     // Make sure every thread successfully inserted it's ID into every vec
117     std::set<intptr_t> idsFound;
118     for (auto& elem : kv.second) {
119       EXPECT_TRUE(idsFound.insert(elem).second);  // check for dups
120     }
121     EXPECT_EQ(idsFound.size(), nthrs); // check they are all there
122   }
123 }
124
125 TEST(PackedSyncPtr, extraData) {
126   PackedSyncPtr<int> p;
127   p.init();
128   int* unaligned = reinterpret_cast<int*>(0xf003);
129   p.lock();
130   p.set(unaligned);
131   uintptr_t* bytes = reinterpret_cast<uintptr_t*>(&p);
132   LOG(INFO) << "Bytes integer is: 0x" << std::hex << *bytes;
133   EXPECT_EQ(p.get(), unaligned);
134   p.unlock();
135 }