2 * Copyright 2017 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/io/async/EventBaseLocal.h>
18 #include <folly/MapUtil.h>
22 namespace folly { namespace detail {
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);
34 void* EventBaseLocalBase::getVoid(EventBase& evb) {
35 evb.dcheckIsInEventBaseThread();
37 return folly::get_default(evb.localStorage_, key_, {}).get();
40 void EventBaseLocalBase::erase(EventBase& evb) {
41 evb.dcheckIsInEventBaseThread();
43 evb.localStorage_.erase(key_);
44 evb.localStorageToDtor_.erase(this);
46 SYNCHRONIZED(eventBases_) {
47 eventBases_.erase(&evb);
51 void EventBaseLocalBase::onEventBaseDestruction(EventBase& evb) {
52 evb.dcheckIsInEventBaseThread();
54 SYNCHRONIZED(eventBases_) {
55 eventBases_.erase(&evb);
59 void EventBaseLocalBase::setVoid(EventBase& evb, std::shared_ptr<void>&& ptr) {
60 evb.dcheckIsInEventBaseThread();
63 evb.localStorage_.find(key_) != evb.localStorage_.end();
65 evb.localStorage_.emplace(key_, std::move(ptr));
68 eventBases_.wlock()->insert(&evb);
69 evb.localStorageToDtor_.insert(this);
73 std::atomic<uint64_t> EventBaseLocalBase::keyCounter_{0};