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