2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/PackedSyncPtr.h>
20 #include <gtest/gtest.h>
22 #include <unordered_map>
25 using folly::PackedSyncPtr;
29 // Compile time check for packability. This requires that
30 // PackedSyncPtr is a POD struct on gcc.
32 struct ignore { PackedSyncPtr<int> foo; char c; } FOLLY_PACK_ATTR;
34 static_assert(sizeof(ignore) == 9, "PackedSyncPtr wasn't packable");
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);
43 EXPECT_EQ(sp[0].first, 5);
45 EXPECT_EQ(sp[1].second, 7);
47 EXPECT_EQ(sp[1].second, 7);
49 EXPECT_EQ(sp->first, 9);
51 EXPECT_EQ((sp.get() + 1)->second, 7);
54 EXPECT_EQ(sp.extra(), 0);
56 EXPECT_EQ(sp.extra(), 0x13);
57 EXPECT_EQ((sp.get() + 1)->second, 7);
59 auto newP = new std::pair<int,int>();
61 EXPECT_EQ(sp.extra(), 0x13);
62 EXPECT_EQ(sp.get(), newP);
67 // Here we use the PackedSyncPtr to lock the whole SyncVec (base, *base, and sz)
70 PackedSyncPtr<T> base;
71 SyncVec() { base.init(); }
72 ~SyncVec() { free(base.get()); }
73 void push_back(const T& t) {
74 base.set((T*) realloc(base.get(),
75 (base.extra() + 1) * sizeof(T)));
76 base[base.extra()] = t;
77 base.setExtra(base.extra() + 1);
86 T* begin() const { return base.get(); }
87 T* end() const { return base.get() + base.extra(); }
89 typedef SyncVec<intptr_t> VecT;
90 typedef std::unordered_map<int64_t, VecT> Map;
91 const int mapCap = 1317;
92 const int nthrs = 297;
93 static Map map(mapCap);
95 // Each app thread inserts it's ID into every vec in map
96 // map is read only, so doesn't need any additional locking
97 void appThread(intptr_t id) {
98 for (auto& kv : map) {
100 kv.second.push_back(id);
105 TEST(PackedSyncPtr, Application) {
106 for (int64_t i = 0; i < mapCap / 2; ++i) {
107 map.insert(std::make_pair(i, VecT()));
109 std::vector<std::thread> thrs;
110 for (intptr_t i = 0; i < nthrs; i++) {
111 thrs.push_back(std::thread(appThread, i));
113 for (auto& t : thrs) {
117 for (auto& kv : map) {
118 // Make sure every thread successfully inserted it's ID into every vec
119 std::set<intptr_t> idsFound;
120 for (auto& elem : kv.second) {
121 EXPECT_TRUE(idsFound.insert(elem).second); // check for dups
123 EXPECT_EQ(idsFound.size(), nthrs); // check they are all there
127 TEST(PackedSyncPtr, extraData) {
128 PackedSyncPtr<int> p;
130 int* unaligned = reinterpret_cast<int*>(0xf003);
133 uintptr_t* bytes = reinterpret_cast<uintptr_t*>(&p);
134 LOG(INFO) << "Bytes integer is: 0x" << std::hex << *bytes;
135 EXPECT_EQ(p.get(), unaligned);