Fix copyright lines
[folly.git] / folly / io / async / EventBaseLocal.cpp
1 /*
2  * Copyright 2015-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/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   auto locked = eventBases_.rlock();
26   for (auto* evb : *locked) {
27     evb->runInEventBaseThread([ this, evb, key = key_ ] {
28       evb->localStorage_.erase(key);
29       evb->localStorageToDtor_.erase(this);
30     });
31   }
32 }
33
34 void* EventBaseLocalBase::getVoid(EventBase& evb) {
35   evb.dcheckIsInEventBaseThread();
36
37   return folly::get_default(evb.localStorage_, key_, {}).get();
38 }
39
40 void EventBaseLocalBase::erase(EventBase& evb) {
41   evb.dcheckIsInEventBaseThread();
42
43   evb.localStorage_.erase(key_);
44   evb.localStorageToDtor_.erase(this);
45
46   SYNCHRONIZED(eventBases_) {
47     eventBases_.erase(&evb);
48   }
49 }
50
51 void EventBaseLocalBase::onEventBaseDestruction(EventBase& evb) {
52   evb.dcheckIsInEventBaseThread();
53
54   SYNCHRONIZED(eventBases_) {
55     eventBases_.erase(&evb);
56   }
57 }
58
59 void EventBaseLocalBase::setVoid(EventBase& evb, std::shared_ptr<void>&& ptr) {
60   evb.dcheckIsInEventBaseThread();
61
62   auto alreadyExists =
63     evb.localStorage_.find(key_) != evb.localStorage_.end();
64
65   evb.localStorage_.emplace(key_, std::move(ptr));
66
67   if (!alreadyExists) {
68     eventBases_.wlock()->insert(&evb);
69     evb.localStorageToDtor_.insert(this);
70   }
71 }
72
73 std::atomic<uint64_t> EventBaseLocalBase::keyCounter_{0};
74 } // namespace detail
75 } // namespace folly