Make DestructorCheck::Safety no-copy, no-move
[folly.git] / folly / io / async / EventBaseLocal.cpp
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 #include <folly/io/async/EventBaseLocal.h>
18 #include <folly/MapUtil.h>
19 #include <atomic>
20 #include <thread>
21
22 namespace folly { namespace detail {
23
24 EventBaseLocalBase::~EventBaseLocalBase() {
25   for (auto* evb : *eventBases_.rlock()) {
26     evb->runInEventBaseThread([ this, evb, key = key_ ] {
27       evb->localStorage_.erase(key);
28       evb->localStorageToDtor_.erase(this);
29     });
30   }
31 }
32
33 void* EventBaseLocalBase::getVoid(EventBase& evb) {
34   DCHECK(evb.isInEventBaseThread());
35
36   return folly::get_default(evb.localStorage_, key_, {}).get();
37 }
38
39 void EventBaseLocalBase::erase(EventBase& evb) {
40   DCHECK(evb.isInEventBaseThread());
41
42   evb.localStorage_.erase(key_);
43   evb.localStorageToDtor_.erase(this);
44
45   SYNCHRONIZED(eventBases_) {
46     eventBases_.erase(&evb);
47   }
48 }
49
50 void EventBaseLocalBase::onEventBaseDestruction(EventBase& evb) {
51   DCHECK(evb.isInEventBaseThread());
52
53   SYNCHRONIZED(eventBases_) {
54     eventBases_.erase(&evb);
55   }
56 }
57
58 void EventBaseLocalBase::setVoid(EventBase& evb, std::shared_ptr<void>&& ptr) {
59   DCHECK(evb.isInEventBaseThread());
60
61   auto alreadyExists =
62     evb.localStorage_.find(key_) != evb.localStorage_.end();
63
64   evb.localStorage_.emplace(key_, std::move(ptr));
65
66   if (!alreadyExists) {
67     eventBases_.wlock()->insert(&evb);
68     evb.localStorageToDtor_.insert(this);
69   }
70 }
71
72 std::atomic<uint64_t> EventBaseLocalBase::keyCounter_{0};
73 }}