Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
ftl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Nicolas Pope
ftl
Commits
2993e5f5
Commit
2993e5f5
authored
4 years ago
by
Nicolas Pope
Browse files
Options
Downloads
Patches
Plain Diff
Add a few comments
parent
562e1269
No related branches found
No related tags found
No related merge requests found
Pipeline
#27275
passed
4 years ago
Stage: all
Stage: deploy
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
components/common/cpp/include/ftl/handle.hpp
+20
-2
20 additions, 2 deletions
components/common/cpp/include/ftl/handle.hpp
with
20 additions
and
2 deletions
components/common/cpp/include/ftl/handle.hpp
+
20
−
2
View file @
2993e5f5
...
...
@@ -26,8 +26,7 @@ struct Handle {
friend
struct
BaseHandler
;
/**
* Cancel the timer job. If currently executing it will block and wait for
* the job to complete.
* Cancel the callback and invalidate the handle.
*/
inline
void
cancel
()
{
if
(
handler_
)
handler_
->
remove
(
*
this
);
handler_
=
nullptr
;
}
...
...
@@ -62,8 +61,17 @@ struct Handle {
Handle
(
BaseHandler
*
h
,
int
id
)
:
handler_
(
h
),
id_
(
id
)
{}
};
/**
* This class is used to manage callbacks. The template parameters are the
* arguments to be passed to the callback when triggered. This class is already
* thread-safe.
*/
template
<
typename
...
ARGS
>
struct
Handler
:
BaseHandler
{
/**
* Add a new callback function. It returns a `Handle` object that must
* remain in scope, the destructor of the `Handle` will remove the callback.
*/
Handle
on
(
const
std
::
function
<
bool
(
ARGS
...)
>
&
f
)
{
std
::
unique_lock
<
std
::
mutex
>
lk
(
mutex_
);
int
id
=
id_
++
;
...
...
@@ -71,6 +79,12 @@ struct Handler : BaseHandler {
return
make_handle
(
this
,
id
);
}
/**
* Safely trigger all callbacks. Note that `Handler` is locked when
* triggering so callbacks cannot make modifications to it or they will
* lock up. To remove a callback, return false from the callback, else
* return true.
*/
void
trigger
(
ARGS
...
args
)
{
std
::
unique_lock
<
std
::
mutex
>
lk
(
mutex_
);
try
{
...
...
@@ -82,6 +96,10 @@ struct Handler : BaseHandler {
}
}
/**
* Remove a callback using its `Handle`. This is equivalent to allowing the
* `Handle` to be destroyed or cancelled.
*/
void
remove
(
const
Handle
&
h
)
override
{
std
::
unique_lock
<
std
::
mutex
>
lk
(
mutex_
);
callbacks_
.
erase
(
h
.
id
());
...
...
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