Skip to content

Commit

Permalink
Fixes CppMicroServices#840: removes manual reference counting (CppMic…
Browse files Browse the repository at this point in the history
…roServices#841)

* converted registration

* compiled, not passeD

* fixed weak_ptr error, now leaking mock objects

* fixed bug, need to decrement ref count

* updats from last week, test cases failing because of out of date expectations

* all tests are passing with shared and weak pointers to serviceRegistrationBasePrivate

* still passing tests, updated comments and cleaned up

* updating to share dependents

* new issue with dying service

* added coreInfo, maybe passing

* passing tests, removed manual ref counting from referenceBasePrivate, repeated all fast tests to ensure no sporadic failures

* updated comments

* updates before PR

* Removed manual reference counting

The manual reference counting in ServiceRegistrationBasePrivate and ServiceReferenceBasePrivate were removed. Additionally, some properties of ServiceRegistrationBasePrivate were offloaded to a new class ServiceRegistrationCoreInfo which both ServiceRegistrationBasePrivate and ServiceReferenceBasePrivate can access allowing ServiceReferenceBasePrivate to give up ownership of ServiceRegistrationBasePrivate.

Signed-off-by: Toby Cormack  <tcormack@mathworks.com>

* Removed manual reference counting and merged with upstream (CppMicroServices#840)

The manual reference counting in ServiceRegistrationBasePrivate and ServiceReferenceBasePrivate were removed. Additionally, some properties of ServiceRegistrationBasePrivate were offloaded to a new class ServiceRegistrationCoreInfo which both ServiceRegistrationBasePrivate and ServiceReferenceBasePrivate can access allowing ServiceReferenceBasePrivate to give up ownership of ServiceRegistrationBasePrivate.

Signed-off-by: Toby Cormack  <tcormack@mathworks.com>

* ServiceRegistrationCoreInfo now default destructor

* Updated based on Patty's comments CppMicroServices#840

* updated ServiceReferenceBase Constructors for clarity with shared_ptrs

* removed 'move' from serviceRegistry

* attempt at solving mac issue

* updates for lock type and removing unneccessary functions from reference

* lost lock

* changed to custom atomic load

* LockSet addition

* threading support in LockSet

* LockSet not threaded

* no names in func dec

* Incoorporated Jeff's Comments

* Assignment operator didn't fail on my computer, did in github

* updated for Jeff's 5/16 comments

* clang update

* mikes comments and fixes for multithreaded support

* remove ifdefs from BundleRegistry, abide by rule of (0,3,5), and add comments

---------

Signed-off-by: Toby Cormack  <tcormack@mathworks.com>

adaptations for C++14:
* move initialization statements out of if clauses

Signed-off-by: Ingmar Sittl <ingmar.sittl@elektrobit.com>
  • Loading branch information
tcormackMW authored and insi-eb committed Aug 7, 2023
1 parent 3c5de4f commit 30f6c71
Show file tree
Hide file tree
Showing 25 changed files with 587 additions and 369 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Cpp11BracedListStyle: false
IndentCaseLabels: true
IndentGotoLabels: false
IndentPPDirectives: AfterHash
InsertBraces: true
NamespaceIndentation: All
PackConstructorInitializers: CurrentLine
PointerAlignment: Left
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ doc/src/examples/makefile/main
*.pyc
/build*
.vscode/
.venv/
.venv/
10 changes: 7 additions & 3 deletions framework/include/cppmicroservices/ServiceReferenceBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define CPPMICROSERVICES_SERVICEREFERENCEBASE_H

#include "cppmicroservices/Any.h"
#include "cppmicroservices/detail/Threads.h"
#include <functional>

#include <atomic>
Expand Down Expand Up @@ -232,13 +233,16 @@ namespace cppmicroservices
*/
ServiceReferenceBase();

ServiceReferenceBase(ServiceRegistrationBasePrivate* reg);
ServiceReferenceBase(std::shared_ptr<ServiceRegistrationBasePrivate> reg);

void SetInterfaceId(std::string const& interfaceId);

// This class is not thread-safe, but we support thread-safe
// copying and assignment.
std::atomic<ServiceReferenceBasePrivate*> d;
// copying and assignment
// This was changed to a std::shared_ptr and is accessed through
// Load(), but when this repository uses c++20,
// this will be changed to std::atomic<shared_ptr>
cppmicroservices::detail::Atomic<std::shared_ptr<ServiceReferenceBasePrivate>> d;
};

/**
Expand Down
37 changes: 20 additions & 17 deletions framework/include/cppmicroservices/ServiceRegistrationBase.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*=============================================================================
/*=============================================================================
Library: CppMicroServices
Library: CppMicroServices
Copyright (c) The CppMicroServices developers. See the COPYRIGHT
file at the top-level directory of this distribution and at
https://github.com/CppMicroServices/CppMicroServices/COPYRIGHT .
Copyright (c) The CppMicroServices developers. See the COPYRIGHT
file at the top-level directory of this distribution and at
https://github.com/CppMicroServices/CppMicroServices/COPYRIGHT .
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
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
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.
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.
=============================================================================*/

Expand All @@ -30,6 +30,7 @@
namespace cppmicroservices
{

class ServiceRegistrationLocks;
class BundlePrivate;
class ServiceRegistrationBasePrivate;
class Properties;
Expand Down Expand Up @@ -196,11 +197,13 @@ namespace cppmicroservices
*/
ServiceRegistrationBase();

ServiceRegistrationBase(ServiceRegistrationBasePrivate* registrationPrivate);
ServiceRegistrationBase(std::shared_ptr<ServiceRegistrationBasePrivate> registrationPrivate);

ServiceRegistrationBase(BundlePrivate* bundle, InterfaceMapConstPtr const& service, Properties&& props);

ServiceRegistrationBasePrivate* d { nullptr };
std::shared_ptr<ServiceRegistrationLocks> LockServiceRegistration() const;

std::shared_ptr<ServiceRegistrationBasePrivate> d;
};

/**
Expand All @@ -224,7 +227,7 @@ namespace cppmicroservices
*/

US_HASH_FUNCTION_BEGIN(cppmicroservices::ServiceRegistrationBase)
return std::hash<cppmicroservices::ServiceRegistrationBasePrivate*>()(arg.d);
return std::hash<std::shared_ptr<cppmicroservices::ServiceRegistrationBasePrivate>>()(arg.d);
US_HASH_FUNCTION_END

#endif // CPPMICROSERVICES_SERVICEREGISTRATIONBASE_H
1 change: 1 addition & 0 deletions framework/include/cppmicroservices/detail/Threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace cppmicroservices
UniqueLock&
operator=(UniqueLock&&)
{
return *this;
}
explicit UniqueLock(MutexLockingStrategy const&) {}
explicit UniqueLock(MutexLockingStrategy const*) {}
Expand Down
4 changes: 4 additions & 0 deletions framework/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(_srcs
util/SharedLibrary.cpp
util/SharedLibraryException.cpp
util/Utils.cpp
util/ServiceRegistrationLocks.cpp

service/ListenerToken.cpp
service/ServiceException.cpp
Expand All @@ -34,6 +35,7 @@ set(_srcs
service/ServiceReferenceBasePrivate.cpp
service/ServiceRegistrationBase.cpp
service/ServiceRegistrationBasePrivate.cpp
service/ServiceRegistrationCoreInfo.cpp
service/ServiceRegistry.cpp

bundle/Bundle.cpp
Expand Down Expand Up @@ -67,13 +69,15 @@ set(_private_headers
util/Properties.h
util/PropsCheck.h
util/Utils.h
util/ServiceRegistrationLocks.h

service/ServiceHooks.h
service/ServiceListenerEntry.h
service/ServiceListenerHookPrivate.h
service/ServiceListeners.h
service/ServiceReferenceBasePrivate.h
service/ServiceRegistrationBasePrivate.h
service/ServiceRegistrationCoreInfo.h
service/ServiceRegistry.h

bundle/BundleArchive.h
Expand Down
35 changes: 17 additions & 18 deletions framework/src/bundle/BundleContext.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*=============================================================================
/*=============================================================================
Library: CppMicroServices
Library: CppMicroServices
Copyright (c) The CppMicroServices developers. See the COPYRIGHT
file at the top-level directory of this distribution and at
https://github.com/CppMicroServices/CppMicroServices/COPYRIGHT .
Copyright (c) The CppMicroServices developers. See the COPYRIGHT
file at the top-level directory of this distribution and at
https://github.com/CppMicroServices/CppMicroServices/COPYRIGHT .
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
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
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.
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.
=============================================================================*/

Expand Down Expand Up @@ -274,8 +274,7 @@ namespace cppmicroservices
{
try
{
sref.d.load()->UngetService(b.lock(), true);
//sref.d.load()->UngetService(b.lock(), true);
sref.d.Load()->UngetService(b.lock(), true);
}
catch (...)
{
Expand Down Expand Up @@ -312,7 +311,7 @@ namespace cppmicroservices
auto b = GetAndCheckBundlePrivate(d);

std::shared_ptr<ServiceHolder<void>> h(
new ServiceHolder<void>(b, reference, reference.d.load()->GetService(b.get())));
new ServiceHolder<void>(b, reference, reference.d.Load()->GetService(b.get())));
return std::shared_ptr<void>(h, h->service.get());
}

Expand All @@ -333,7 +332,7 @@ namespace cppmicroservices
d->CheckValid();
auto b = GetAndCheckBundlePrivate(d);

auto serviceInterfaceMap = reference.d.load()->GetServiceInterfaceMap(b.get());
auto serviceInterfaceMap = reference.d.Load()->GetServiceInterfaceMap(b.get());
std::shared_ptr<ServiceHolder<InterfaceMap const>> h(
new ServiceHolder<InterfaceMap const>(b, reference, serviceInterfaceMap));
return InterfaceMapConstPtr(h, h->service.get());
Expand Down
4 changes: 2 additions & 2 deletions framework/src/bundle/BundleHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace cppmicroservices
{
ServiceReference<BundleFindHook> sr = srBaseIter->GetReference();
std::shared_ptr<BundleFindHook> fh
= std::static_pointer_cast<BundleFindHook>(sr.d.load()->GetService(GetPrivate(selfBundle).get()));
= std::static_pointer_cast<BundleFindHook>(sr.d.Load()->GetService(GetPrivate(selfBundle).get()));
if (fh)
{
try
Expand Down Expand Up @@ -139,7 +139,7 @@ namespace cppmicroservices
}

std::shared_ptr<BundleEventHook> eh = std::static_pointer_cast<BundleEventHook>(
sr.d.load()->GetService(GetPrivate(GetBundleContext().GetBundle()).get()));
sr.d.Load()->GetService(GetPrivate(GetBundleContext().GetBundle()).get()));
if (eh)
{
try
Expand Down
5 changes: 4 additions & 1 deletion framework/src/bundle/BundlePrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ namespace cppmicroservices
try
{
if (util::Exists(bundleDir))
{
util::RemoveDirectoryRecursive(bundleDir);
}
}
catch (...)
{
Expand Down Expand Up @@ -823,7 +825,8 @@ namespace cppmicroservices
coreCtx->services.GetUsedByBundle(this, srs);
for (std::vector<ServiceRegistrationBase>::const_iterator i = srs.begin(); i != srs.end(); ++i)
{
i->GetReference(std::string()).d.load()->UngetService(this->shared_from_this(), false);
auto ref = i->GetReference(std::string());
ref.d.Load()->UngetService(this->shared_from_this(), false);
}
}

Expand Down
4 changes: 2 additions & 2 deletions framework/src/service/ServiceHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace cppmicroservices
{
ServiceReference<ServiceFindHook> sr = fhrIter->GetReference();
auto fh
= std::static_pointer_cast<ServiceFindHook>(sr.d.load()->GetService(GetPrivate(selfBundle).get()));
= std::static_pointer_cast<ServiceFindHook>(sr.d.Load()->GetService(GetPrivate(selfBundle).get()));
if (fh)
{
try
Expand Down Expand Up @@ -177,7 +177,7 @@ namespace cppmicroservices
{
ServiceReference<ServiceEventListenerHook> sr = sriIter->GetReference();
auto elh = std::static_pointer_cast<ServiceEventListenerHook>(
sr.d.load()->GetService(GetPrivate(selfBundle).get()));
sr.d.Load()->GetService(GetPrivate(selfBundle).get()));
if (elh)
{
try
Expand Down
4 changes: 3 additions & 1 deletion framework/src/service/ServiceListeners.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ namespace cppmicroservices
// Get a copy of the service reference and keep it until we are
// done with its properties.
auto ref = evt.GetServiceReference();
auto props = ref.d.load()->GetProperties();
auto props = ref.d.Load()->GetProperties();

{
auto l = this->Lock();
Expand All @@ -469,7 +469,9 @@ namespace cppmicroservices
for (auto& sse : complicatedListeners)
{
if (receivers.count(sse) == 0)
{
continue;
}
LDAPExpr const& ldapExpr = sse.GetLDAPExpr();
if (ldapExpr.IsNull() || ldapExpr.Evaluate(props, false))
{
Expand Down
8 changes: 4 additions & 4 deletions framework/src/service/ServiceObjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ namespace cppmicroservices

if (isPrototypeScope)
{
result = m_reference.d.load()->GetPrototypeService(MakeBundleContext(m_context).GetBundle());
result = m_reference.d.Load()->GetPrototypeService(MakeBundleContext(m_context).GetBundle());
}
else
{
result = m_reference.d.load()->GetServiceInterfaceMap(
result = m_reference.d.Load()->GetServiceInterfaceMap(
GetPrivate(MakeBundleContext(m_context).GetBundle()).get());
}

Expand Down Expand Up @@ -115,11 +115,11 @@ namespace cppmicroservices

if (isPrototypeScope)
{
sref.d.load()->UngetPrototypeService(bundle, interfaceMap);
sref.d.Load()->UngetPrototypeService(bundle, interfaceMap);
}
else
{
sref.d.load()->UngetService(bundle, true);
sref.d.Load()->UngetService(bundle, true);
}
}
}
Expand Down
Loading

0 comments on commit 30f6c71

Please sign in to comment.