diff --git a/applications/test/dynamicLibrary/Make/files b/applications/test/dynamicLibrary/Make/files
new file mode 100644
index 0000000000..54ed5e99bc
--- /dev/null
+++ b/applications/test/dynamicLibrary/Make/files
@@ -0,0 +1,3 @@
+Test-dynamicLibrary.C
+
+EXE = $(FOAM_USER_APPBIN)/Test-dynamicLibrary
diff --git a/applications/test/dynamicLibrary/Make/options b/applications/test/dynamicLibrary/Make/options
new file mode 100644
index 0000000000..75c7356f1c
--- /dev/null
+++ b/applications/test/dynamicLibrary/Make/options
@@ -0,0 +1,2 @@
+EXE_INC =
+EXE_LIBS =
diff --git a/applications/test/dynamicLibrary/Test-dynamicLibrary.C b/applications/test/dynamicLibrary/Test-dynamicLibrary.C
new file mode 100644
index 0000000000..ee7009b961
--- /dev/null
+++ b/applications/test/dynamicLibrary/Test-dynamicLibrary.C
@@ -0,0 +1,143 @@
+/*---------------------------------------------------------------------------*\
+ ========= |
+ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
+ \\ / O peration |
+ \\ / A nd | www.openfoam.com
+ \\/ M anipulation |
+-------------------------------------------------------------------------------
+ Copyright (C) 2020 OpenCFD Ltd.
+-------------------------------------------------------------------------------
+License
+ This file is part of OpenFOAM.
+
+ OpenFOAM is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with OpenFOAM. If not, see .
+
+Application
+ Test-dynamicLibrary
+
+Description
+ Test loading/unloading of libraries
+
+\*---------------------------------------------------------------------------*/
+
+#include "argList.H"
+#include "profiling.H"
+#include "DynamicList.H"
+
+using namespace Foam;
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+int main(int argc, char *argv[])
+{
+ argList::addNote("Low-level test of library load/unload");
+
+ profiling::disable(); // No profiling output
+ argList::noBanner();
+ argList::noParallel();
+ argList::removeOption("case");
+ argList::removeOption("noFunctionObjects");
+ argList::addBoolOption("no-close", "Skip dlclose");
+ argList::addBoolOption("quiet", "Disable verbosity");
+
+ argList::addArgument("lib...");
+ argList::noMandatoryArgs(); // Arguments are optional
+
+ argList args(argc, argv, false, true);
+
+ const bool noClose = args.found("no-close");
+ const bool verbose = !args.found("quiet");
+
+ //- Pointers to the loaded libraries
+ DynamicList libPtrs_;
+
+ //- Names of loaded libraries, or of libraries to be loaded
+ DynamicList libNames_;
+
+ label nbad = 0;
+ wordHashSet loaded;
+
+ for (int argi = 1; argi < args.size(); ++argi)
+ {
+ const fileName libName(fileName::validate(args[argi]));
+
+ if (libName.empty())
+ {
+ continue;
+ }
+
+ void* ptr = Foam::dlOpen(libName, false);
+
+ if (!ptr)
+ {
+ ++nbad;
+ }
+ else
+ {
+ libPtrs_.append(ptr);
+ libNames_.append(libName);
+
+ if (verbose)
+ {
+ const word addr(Foam::name(ptr));
+
+ if (loaded.insert(addr))
+ {
+ InfoErr << "Can load " << libName << nl;
+ }
+ else
+ {
+ InfoErr << "Already loaded " << libName << nl;
+ }
+ }
+ }
+ }
+
+ if (!noClose)
+ {
+ forAllReverse(libPtrs_, i)
+ {
+ void* ptr = libPtrs_[i];
+
+ if (ptr == nullptr)
+ {
+ libNames_[i].clear();
+ continue;
+ }
+
+ const bool ok = Foam::dlClose(ptr);
+
+ if (verbose)
+ {
+ if (ok)
+ {
+ InfoErr << "Closed ";
+ }
+ else
+ {
+ InfoErr << "Failed closing ";
+ }
+
+ InfoErr
+ << libNames_[i]
+ << " with handle " << Foam::name(ptr) << nl;
+ }
+ }
+ }
+
+ return 0;
+}
+
+
+// ************************************************************************* //
diff --git a/src/OpenFOAM/db/Time/Time.C b/src/OpenFOAM/db/Time/Time.C
index cccda67da8..dbc1c8214c 100644
--- a/src/OpenFOAM/db/Time/Time.C
+++ b/src/OpenFOAM/db/Time/Time.C
@@ -471,7 +471,7 @@ Foam::Time::Time
if (enableLibs)
{
- libs_.open(controlDict_, "libs");
+ libs_.open("libs", controlDict_);
}
// Explicitly set read flags on objectRegistry so anything constructed
@@ -553,12 +553,9 @@ Foam::Time::Time
// Libraries
//
// * enable by default unless '-no-libs' option was used
- if (!args.found("no-libs"))
+ if (enableLibs && !args.found("no-libs"))
{
- if (enableLibs)
- {
- libs_.open(controlDict_, "libs");
- }
+ libs_.open("libs", controlDict_);
}
// Explicitly set read flags on objectRegistry so anything constructed
@@ -634,7 +631,7 @@ Foam::Time::Time
if (enableLibs)
{
- libs_.open(controlDict_, "libs");
+ libs_.open("libs", controlDict_);
}
@@ -708,7 +705,7 @@ Foam::Time::Time
if (enableLibs)
{
- libs_.open(controlDict_, "libs");
+ libs_.open("libs", controlDict_);
}
setMonitoring(); // for profiling etc
diff --git a/src/OpenFOAM/db/Time/Time.H b/src/OpenFOAM/db/Time/Time.H
index 4d080a48cd..75f0a01eae 100644
--- a/src/OpenFOAM/db/Time/Time.H
+++ b/src/OpenFOAM/db/Time/Time.H
@@ -130,14 +130,14 @@ public:
private:
- // Private data
+ // Private Data
//- Profiling trigger for time-loop (for run, loop)
mutable std::unique_ptr loopProfiling_;
- //- Any loaded dynamic libraries. Make sure to construct before
- // reading controlDict.
- dlLibraryTable libs_;
+ //- Any loaded dynamic libraries
+ // Construct before reading controlDict
+ mutable dlLibraryTable libs_;
//- The controlDict
unwatchedIOdictionary controlDict_;
@@ -145,7 +145,7 @@ private:
protected:
- // Protected data
+ // Protected Data
label startTimeIndex_;
@@ -502,14 +502,8 @@ public:
return functionObjects_;
}
- //- External access to the loaded libraries
- const dlLibraryTable& libs() const
- {
- return libs_;
- }
-
- //- External access to the loaded libraries
- dlLibraryTable& libs()
+ //- Mutable access to the loaded dynamic libraries
+ dlLibraryTable& libs() const
{
return libs_;
}
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
index 72a1f92778..0d877cb5f6 100644
--- a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
+++ b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
- Copyright (C) 2018-2019 OpenCFD Ltd.
+ Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@@ -73,7 +73,7 @@ Foam::dlLibraryTable& Foam::functionEntries::codeStream::libs
(
dict.topDict()
);
- return const_cast