[libcamera-devel] [PATCH 1/1] libcamera: replaced Meyer's singleton
Rynn Wu (吳育恩)
Rynn.Wu at mediatek.com
Fri Nov 8 05:22:33 CET 2019
Hi there,
I found a Meyer’s singleton in IPAManager, basically Meyer’s singleton is supposed to be removed since it causes some unexpected exceptions in Android platforms while destroying the process.
I replaced it by std::call_once and std::unique_ptr, here is the patch, please kindly help to review it, thanks :-)
Rynn.
From 7efdb2e21263ff559cda8af3ec49721967ba8c60 Mon Sep 17 00:00:00 2001
From: Rynn Wu <rynn.wu at mediatek.com>
Date: Fri, 8 Nov 2019 12:05:55 +0800
Subject: [PATCH] libcamera: replaced Meyer's singleton
Since destroying order of Meyer's singleton is not guarantee and
there were some unexpected behaviors in Android platforms, it's better
to replace it by other ways.
Change-Id: Id2da20ea641f580cb3566f034cc9958bc391eac7
Signed-off-by: Rynn Wu <rynn.wu at mediatek.com>
---
src/libcamera/include/ipa_manager.h | 8 ++++++++
src/libcamera/ipa_manager.cpp | 12 ++++++++++--
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/src/libcamera/include/ipa_manager.h b/src/libcamera/include/ipa_manager.h
index 126f8ba..edaedf9 100644
--- a/src/libcamera/include/ipa_manager.h
+++ b/src/libcamera/include/ipa_manager.h
@@ -7,6 +7,9 @@
#ifndef __LIBCAMERA_IPA_MANAGER_H__
#define __LIBCAMERA_IPA_MANAGER_H__
+#include <functional>
+#include <memory>
+#include <mutex>
#include <vector>
#include <ipa/ipa_interface.h>
@@ -33,6 +36,11 @@ private:
~IPAManager();
int addDir(const char *libDir);
+
+private:
+ static std::unique_ptr<IPAManager, std::function<void(IPAManager*)>> singleton_;
+ static std::once_flag singletonFlag_;
+
};
} /* namespace libcamera */
diff --git a/src/libcamera/ipa_manager.cpp b/src/libcamera/ipa_manager.cpp
index f3180c0..8f60be4 100644
--- a/src/libcamera/ipa_manager.cpp
+++ b/src/libcamera/ipa_manager.cpp
@@ -9,6 +9,7 @@
#include <algorithm>
#include <dirent.h>
+#include <memory>
#include <string.h>
#include <sys/types.h>
@@ -79,6 +80,9 @@ IPAManager::~IPAManager()
delete module;
}
+std::unique_ptr<IPAManager, std::function<void(IPAManager*)>> IPAManager::singleton_;
+std::once_flag IPAManager:: singletonFlag_;
+
/**
* \brief Retrieve the IPA manager instance
*
@@ -90,8 +94,12 @@ IPAManager::~IPAManager()
*/
IPAManager *IPAManager::instance()
{
- static IPAManager ipaManager;
- return &ipaManager;
+ std::call_once(IPAManager::singletonFlag_, [](){
+ /* never release, give an empty customized deleter */
+ IPAManager::singleton_ = std::unique_ptr<IPAManager, std::function<void(IPAManager*)>>(
+ new IPAManager, [](IPAManager*){});
+ });
+ return IPAManager::singleton_.get();
}
/**
--
2.18.0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.libcamera.org/pipermail/libcamera-devel/attachments/20191108/a75fefb6/attachment.htm>
More information about the libcamera-devel
mailing list