Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
otb
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julien Cabieces
otb
Commits
27bb5792
Commit
27bb5792
authored
9 years ago
by
Guillaume Pasero
Browse files
Options
Downloads
Patches
Plain Diff
ENH: add callback to unregister deleted applications, and method to release unused handles
parent
b8db0841
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Modules/Wrappers/ApplicationEngine/src/otbWrapperApplicationRegistry.cxx
+77
-9
77 additions, 9 deletions
...s/ApplicationEngine/src/otbWrapperApplicationRegistry.cxx
with
77 additions
and
9 deletions
Modules/Wrappers/ApplicationEngine/src/otbWrapperApplicationRegistry.cxx
+
77
−
9
View file @
27bb5792
...
...
@@ -37,6 +37,58 @@ public:
typedef
std
::
pair
<
Application
*
,
void
*
>
AppHandlePairType
;
typedef
std
::
list
<
AppHandlePairType
>
AppHandleContainerType
;
/** Add a pair (application, library handle) in the private registry */
bool
AddPair
(
Application
*
app
,
void
*
handle
)
{
AppHandlePairType
pair
;
if
(
app
&&
handle
)
{
// mutex lock to ensure thread safety
itk
::
MutexLockHolder
<
itk
::
SimpleMutexLock
>
mutexHolder
(
m_Mutex
);
pair
.
first
=
app
;
pair
.
second
=
handle
;
m_Container
.
push_back
(
pair
);
return
true
;
}
return
false
;
}
/** When an application is deleted, unregister its pointer from private registry */
void
UnregisterApp
(
const
Application
*
app
)
{
if
(
app
)
{
// mutex lock to ensure thread safety
itk
::
MutexLockHolder
<
itk
::
SimpleMutexLock
>
mutexHolder
(
m_Mutex
);
AppHandleContainerType
::
iterator
it
=
m_Container
.
begin
();
while
(
it
!=
m_Container
.
end
())
{
if
((
*
it
).
first
==
app
)
{
(
*
it
).
first
=
NULL
;
}
++
it
;
}
}
}
/** Release the library handles from applications already deleted */
void
ReleaseUnusedHandle
()
{
itk
::
MutexLockHolder
<
itk
::
SimpleMutexLock
>
mutexHolder
(
m_Mutex
);
AppHandleContainerType
::
iterator
it
;
for
(
it
=
m_Container
.
begin
()
;
it
!=
m_Container
.
end
()
;
++
it
)
{
if
((
*
it
).
first
==
NULL
)
{
itk
::
DynamicLoader
::
CloseLibrary
(
(
*
it
).
second
);
(
*
it
).
second
=
NULL
;
}
}
m_Container
.
remove
(
AppHandlePairType
(
NULL
,
NULL
));
}
/** close all handles at program exit */
~
ApplicationPrivateRegistry
()
{
AppHandleContainerType
::
iterator
it
;
...
...
@@ -47,11 +99,25 @@ public:
m_Container
.
clear
();
}
private
:
AppHandleContainerType
m_Container
;
itk
::
SimpleMutexLock
m_Mutex
;
};
// static finalizer to close opened libraries
static
ApplicationPrivateRegistry
m_ApplicationPrivateRegistryGlobal
;
// Define callbacks to unregister applications in ApplicationPrivateRegistry
void
DeleteAppCallback
(
itk
::
Object
*
obj
,
const
itk
::
EventObject
&
,
void
*
)
{
Application
*
appPtr
=
dynamic_cast
<
Application
*>
(
obj
);
m_ApplicationPrivateRegistryGlobal
.
UnregisterApp
(
appPtr
);
}
void
DeleteAppConstCallback
(
const
itk
::
Object
*
obj
,
const
itk
::
EventObject
&
,
void
*
)
{
const
Application
*
appPtr
=
dynamic_cast
<
const
Application
*>
(
obj
);
m_ApplicationPrivateRegistryGlobal
.
UnregisterApp
(
appPtr
);
}
ApplicationRegistry
::
ApplicationRegistry
()
{
...
...
@@ -69,6 +135,8 @@ ApplicationRegistry::SetApplicationPath(std::string newpath)
// do NOT use putenv() directly, since the string memory must be managed carefully
itksys
::
SystemTools
::
PutEnv
(
putEnvPath
.
str
().
c_str
());
// TODO : releaseUnusedHandle() ?
}
void
...
...
@@ -186,6 +254,8 @@ ApplicationRegistry::CreateApplicationFaster(const std::string& name)
std
::
vector
<
std
::
string
>
ApplicationRegistry
::
GetAvailableApplications
(
bool
useFactory
)
{
// TODO : releaseUnusedHandle() ?
ApplicationPointer
appli
;
std
::
set
<
std
::
string
>
appSet
;
...
...
@@ -279,8 +349,6 @@ ApplicationRegistry::LoadApplicationFromPath(std::string path,std::string name)
{
Application
::
Pointer
appli
;
static
itk
::
SimpleMutexLock
mutex
;
if
(
itksys
::
SystemTools
::
FileExists
(
path
.
c_str
(),
true
))
{
itk
::
LibHandle
lib
=
itk
::
DynamicLoader
::
OpenLibrary
(
path
.
c_str
());
...
...
@@ -307,13 +375,13 @@ ApplicationRegistry::LoadApplicationFromPath(std::string path,std::string name)
if
(
appli
.
IsNotNull
())
{
appli
->
Init
();
//
mu
te
x
l
ock to ensure thread safety
itk
::
MutexLockHolder
<
itk
::
SimpleMutexLock
>
mutexHolder
(
mutex
);
ApplicationPrivateRegistry
::
AppHandlePairType
curPair
;
c
urPair
.
first
=
appli
.
GetPointer
(
);
c
urPair
.
second
=
(
void
*
)
lib
;
m_A
ppli
cationPrivateRegistryGlobal
.
m_Container
.
push_back
(
curPair
);
//
regis
te
r
l
ibrary handle
m_ApplicationPrivateRegistryGlobal
.
AddPair
(
appli
.
GetPointer
(),
(
void
*
)
lib
);
// set a callback on DeleteEvent
itk
::
CStyleCommand
::
Pointer
command
=
itk
::
CStyleCommand
::
New
()
;
c
ommand
->
SetCallback
(
&
DeleteAppCallback
);
c
ommand
->
SetConstCallback
(
&
DeleteAppConstCallback
)
;
a
ppli
->
AddObserver
(
itk
::
DeleteEvent
(),
command
);
return
appli
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment