Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rlib
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package 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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Recolic
rlib
Commits
8b29cbbd
There was a problem fetching the pipeline stages.
Commit
8b29cbbd
authored
2 years ago
by
Recolic K
Browse files
Options
Downloads
Patches
Plain Diff
u
parent
cdf7ec5b
No related branches found
No related tags found
No related merge requests found
Pipeline
#898
failed with stage
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pool.hpp
+15
-19
15 additions, 19 deletions
pool.hpp
with
15 additions
and
19 deletions
pool.hpp
+
15
−
19
View file @
8b29cbbd
...
...
@@ -74,26 +74,21 @@ namespace rlib {
// }
// }
// `new` an object. Return nullptr if pool is full.
// "get" an object to use. Return nullptr if pool is full.
// try_borrow_one might fail and return nullptr, but it does not block.
// borrow_one might wait for a long time, but it always success.
obj_t
*
try_borrow_one
()
{
std
::
lock_guard
<
std
::
mutex
>
_l
(
buffer_mutex
);
return
do_try_borrow_one
();
}
obj_t
*
borrow_one
()
{
auto
result
=
try_borrow_one
();
if
(
result
)
return
result
;
// Not available. Wait for release_one.
std
::
unique_lock
<
std
::
mutex
>
lk
(
buffer_mutex
);
// wait for a release
borrow_cv
.
wait
(
lk
,
[
this
]{
return
this
->
new_obj_ready
;});
while
(
true
)
{
auto
result
=
try_borrow_one
();
if
(
result
)
return
result
;
result
=
do_try_borrow_one
();
lk
.
unlock
();
if
(
!
result
)
throw
std
::
logic_error
(
"unknown par error."
);
return
result
;
// wait for a release
borrow_cv
.
wait
(
lk
,
[
this
]{
return
this
->
new_obj_ready
;});
}
}
void
release_one
(
obj_t
*
which
)
{
{
...
...
@@ -107,6 +102,7 @@ namespace rlib {
borrow_cv
.
notify_one
();
}
// manually call constructor of this object again.
void
reconstruct_one
(
obj_t
*
which
)
{
reconstruct_impl
(
which
,
std
::
make_index_sequence
<
sizeof
...(
_bound_construct_args_t
)
>
());
}
...
...
@@ -122,7 +118,7 @@ namespace rlib {
protected
:
buffer_t
buffer
;
// list<obj_t obj, bool is_free>
private
:
std
::
list
<
obj_t
*>
free_list
;
std
::
list
<
obj_t
*>
free_list
;
// for speedup
std
::
mutex
buffer_mutex
;
// mutex for buffer and free_list
std
::
tuple
<
_bound_construct_args_t
...
>
_bound_args
;
...
...
@@ -130,8 +126,8 @@ namespace rlib {
size_t
inuse_objects
=
0
;
const
policy_t
policy
;
std
::
condition_variable
borrow_cv
;
// use buffer_mutex on notifying alloc event.
volatile
bool
new_obj_ready
=
false
;
void
notify_new_object_a
llocated
(
size_t
how_many
)
{
std
::
atomic
<
bool
>
new_obj_ready
=
false
;
void
notify_new_object_a
vailable
(
size_t
how_many
)
{
new_obj_ready
=
true
;
for
(
auto
cter
=
0
;
cter
<
how_many
;
++
cter
)
borrow_cv
.
notify_one
();
...
...
@@ -140,7 +136,7 @@ namespace rlib {
// try_borrow_one without lock.
obj_t
*
do_try_borrow_one
()
{
// Optimize here if is performance bottleneck (lockless list... etc...)
// NOT THREAD SAFE. USE
buffer_mutex
.
std
::
lock_guard
<
std
::
mutex
>
_l
(
buffer_mutex
);
if
(
policy
.
borrow_should_alloc
())
{
new_obj_to_buffer
();
free_list
.
push_back
(
&*--
buffer
.
end
());
...
...
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