Skip to content

Commit

Permalink
Adds SessionSharedPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjerryjohns committed May 18, 2022
1 parent 1e45e8a commit 63a2208
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/transport/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ static_library("transport") {
"SessionManager.h",
"SessionMessageCounter.h",
"SessionMessageDelegate.h",
"SessionSharedPtr.h",
"TransportMgr.h",
"TransportMgrBase.cpp",
"TransportMgrBase.h",
Expand Down
20 changes: 12 additions & 8 deletions src/transport/SessionHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#pragma once

#include <access/SubjectDescriptor.h>
#include <lib/core/Optional.h>
#include <lib/support/ReferenceCountedHandle.h>
#include <transport/SessionSharedPtr.h>

namespace chip {

Expand All @@ -29,27 +31,29 @@ class Session;
class SessionHolder;

/** @brief
* Non-copyable session reference. All SessionHandles are created within SessionManager. SessionHandle is not
* reference *counted, hence it is not allowed to store SessionHandle anywhere except for function arguments and
* return values. SessionHandle is short-lived as it is only available as stack variable, so it is never dangling. */
class SessionHandle
* A non-copyable version of SessionSharedPtr that is always guaranteed to point to a valid Session. This is always
* created within SessionManager.
*
* Since this is a strong reference, this should be short-lived and only used as a stack variable.
*/
class SessionHandle : protected SessionSharedPtr
{
public:
SessionHandle(Transport::Session & session) : mSession(session) {}
SessionHandle(Transport::Session & session) : SessionSharedPtr(session) {}
~SessionHandle() {}

SessionHandle(const SessionHandle &) = delete;
SessionHandle operator=(const SessionHandle &) = delete;
SessionHandle(SessionHandle &&) = default;
SessionHandle & operator=(SessionHandle &&) = delete;

bool operator==(const SessionHandle & that) const { return &mSession.Get() == &that.mSession.Get(); }
bool operator==(const SessionHandle & that) const { return Get() == that.Get(); }

Transport::Session * operator->() const { return mSession.operator->(); }
Transport::Session * operator->() const { return SessionSharedPtr::operator->(); }

private:
// TODO: Remove this once SessionHolder pivots to truly being a weak reference (#18399).
friend class SessionHolder;
ReferenceCountedHandle<Transport::Session> mSession;
};

} // namespace chip
2 changes: 1 addition & 1 deletion src/transport/SessionHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ SessionHolder & SessionHolder::operator=(SessionHolder && that)
void SessionHolder::Grab(const SessionHandle & session)
{
Release();
mSession.Emplace(session.mSession);
mSession.Emplace(session.mSession.Value());
session->AddHolder(*this);
}

Expand Down
3 changes: 2 additions & 1 deletion src/transport/SessionHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class SessionHolder : public SessionDelegate, public IntrusiveListNodeBase

bool Contains(const SessionHandle & session) const
{
return mSession.HasValue() && &mSession.Value().Get() == &session.mSession.Get();
auto ourSession = ToOptional();
return ourSession.HasValue() && ourSession.Value() == session;
}

void Grab(const SessionHandle & session);
Expand Down
105 changes: 105 additions & 0 deletions src/transport/SessionSharedPtr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <access/SubjectDescriptor.h>
#include <lib/core/Optional.h>
#include <lib/support/ReferenceCountedHandle.h>

namespace chip {

namespace Transport {
class Session;
} // namespace Transport

class SessionHolder;

/** @brief
* A shared_ptr like smart pointer to manage the lifetime of a Session object. Like a shared_ptr,
* this object can start out not tracking any Session and be attached to a Session there-after.
* The underlying Session is guaranteed to remain active and resident until all references to it from SessionSharedPtr
* instances in the system have gone away, at which point it invokes its custom destructor.
*
* Just because a Session is refcounted does not mean it actually gets destroyed upon reaching a count of 0.
* UnauthenticatedSession and SecureSession have different logic that gets invoked when the count hits 0.
*
* This should really only be used during session setup by the entity setting up the session.
* Once setup, the session should transfer ownership to the SessionManager at which point,
* all clients in the system should only be holding SessionWeakPtrs (SessionWeakPtr doesn't exist yet, but once
* #18399 is complete, SessionHolder will become SessionWeakPtr).
*
* This is copy-constructible.
*/
class SessionSharedPtr
{
public:
SessionSharedPtr() {}
SessionSharedPtr(Transport::Session & session) { mSession.Emplace(session); }

SessionSharedPtr(const SessionSharedPtr & session) { Grab(session); }

/*
* Add ourselves as a shared owner on the passed in session (if it points to
* a valid session). The passed in
* session object can now be free'ed safely without impacting the underlying
* Session.
*/
void operator=(const SessionSharedPtr & session) { Grab(session); }

/*
* If we're currently pointing to a valid session, remove ourselves
* as a shared owner of that session. If there are no more shared owners
* on that session, that session MAY be reclaimed.
*/
void Release() { mSession.ClearValue(); }

/*
* Assume shared ownership of the provided session.
*/
void Grab(Transport::Session & session) { mSession.Emplace(session); }

Transport::Session * operator->() const { return Get(); }

protected:
Transport::Session * Get() const
{
if (mSession.HasValue())
{
return &mSession.Value().Get();
}

return nullptr;
}

private:
void Grab(const SessionSharedPtr & session)
{
auto * underlyingSession = session.Get();
if (underlyingSession)
{
mSession.Emplace(*underlyingSession);
}
}

Optional<ReferenceCountedHandle<Transport::Session>> mSession;

// TODO: Remove this once SessionHolder pivots to truly being a weak reference (#18399).
friend class SessionHolder;
};

} // namespace chip

0 comments on commit 63a2208

Please sign in to comment.