Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
Nix
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
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
Nora Puchreiner
Nix
Commits
b2ce6fde
Commit
b2ce6fde
authored
8 years ago
by
Eelco Dolstra
Browse files
Options
Downloads
Patches
Plain Diff
ThreadPool: Start doing work as soon as work items are enqueued
parent
58c84cda
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/libutil/thread-pool.cc
+75
-55
75 additions, 55 deletions
src/libutil/thread-pool.cc
src/libutil/thread-pool.hh
+9
-4
9 additions, 4 deletions
src/libutil/thread-pool.hh
with
84 additions
and
59 deletions
src/libutil/thread-pool.cc
+
75
−
55
View file @
b2ce6fde
#include
"thread-pool.hh"
#include
"affinity.hh"
namespace
nix
{
ThreadPool
::
ThreadPool
(
size_t
_
nr
Threads
)
:
nr
Threads
(
_
nr
Threads
)
ThreadPool
::
ThreadPool
(
size_t
_
max
Threads
)
:
max
Threads
(
_
max
Threads
)
{
if
(
!
nrThreads
)
{
nrThreads
=
std
::
thread
::
hardware_concurrency
();
if
(
!
nrThreads
)
nrThreads
=
1
;
restoreAffinity
();
// FIXME
if
(
!
maxThreads
)
{
maxThreads
=
std
::
thread
::
hardware_concurrency
();
if
(
!
maxThreads
)
maxThreads
=
1
;
}
debug
(
format
(
"starting pool of %d threads"
)
%
maxThreads
);
}
ThreadPool
::~
ThreadPool
()
{
std
::
vector
<
std
::
thread
>
workers
;
{
auto
state
(
state_
.
lock
());
state
->
quit
=
true
;
std
::
swap
(
workers
,
state
->
workers
);
}
debug
(
format
(
"reaping %d worker threads"
)
%
workers
.
size
());
work
.
notify_all
();
for
(
auto
&
thr
:
workers
)
thr
.
join
();
}
void
ThreadPool
::
enqueue
(
const
work_t
&
t
)
{
auto
state_
(
state
.
lock
());
state_
->
left
.
push
(
t
);
wakeup
.
notify_one
();
auto
state
(
state_
.
lock
());
assert
(
!
state
->
quit
);
state
->
left
.
push
(
t
);
if
(
state
->
left
.
size
()
>
state
->
workers
.
size
()
&&
state
->
workers
.
size
()
<
maxThreads
)
state
->
workers
.
emplace_back
(
&
ThreadPool
::
workerEntry
,
this
);
work
.
notify_one
();
}
void
ThreadPool
::
process
()
{
printMsg
(
lvlDebug
,
format
(
"starting pool of %d threads"
)
%
nrThreads
);
std
::
vector
<
std
::
thread
>
workers
;
while
(
true
)
{
auto
state
(
state_
.
lock
());
if
(
state
->
exception
)
std
::
rethrow_exception
(
state
->
exception
);
if
(
state
->
left
.
empty
()
&&
!
state
->
pending
)
break
;
state
.
wait
(
done
);
}
}
for
(
size_t
n
=
0
;
n
<
nrThreads
;
n
++
)
workers
.
push_back
(
std
::
thread
([
&
]()
{
bool
first
=
tru
e
;
void
ThreadPool
::
workerEntry
(
)
{
bool
didWork
=
fals
e
;
while
(
true
)
{
work_t
w
;
{
auto
state
(
state_
.
lock
());
while
(
true
)
{
work_t
work
;
{
auto
state_
(
state
.
lock
());
if
(
state_
->
exception
)
return
;
if
(
!
first
)
{
assert
(
state_
->
pending
);
state_
->
pending
--
;
}
first
=
false
;
while
(
state_
->
left
.
empty
())
{
if
(
!
state_
->
pending
)
{
wakeup
.
notify_all
();
return
;
}
if
(
state_
->
exception
)
return
;
state_
.
wait
(
wakeup
);
}
work
=
state_
->
left
.
front
();
state_
->
left
.
pop
();
state_
->
pending
++
;
}
try
{
work
();
}
catch
(
std
::
exception
&
e
)
{
auto
state_
(
state
.
lock
());
if
(
state_
->
exception
)
{
if
(
!
dynamic_cast
<
Interrupted
*>
(
&
e
))
printMsg
(
lvlError
,
format
(
"error: %s"
)
%
e
.
what
());
}
else
{
state_
->
exception
=
std
::
current_exception
();
wakeup
.
notify_all
();
}
if
(
state
->
quit
||
state
->
exception
)
return
;
if
(
didWork
)
{
assert
(
state
->
pending
);
state
->
pending
--
;
didWork
=
false
;
}
if
(
!
state
->
left
.
empty
())
break
;
if
(
!
state
->
pending
)
done
.
notify_all
();
state
.
wait
(
work
);
}
w
=
state
->
left
.
front
();
state
->
left
.
pop
();
state
->
pending
++
;
}
}));
for
(
auto
&
thr
:
workers
)
thr
.
join
();
try
{
w
();
}
catch
(
std
::
exception
&
e
)
{
auto
state
(
state_
.
lock
());
if
(
state
->
exception
)
{
if
(
!
dynamic_cast
<
Interrupted
*>
(
&
e
))
printMsg
(
lvlError
,
format
(
"error: %s"
)
%
e
.
what
());
}
else
{
state
->
exception
=
std
::
current_exception
();
work
.
notify_all
();
done
.
notify_all
();
}
}
{
auto
state_
(
state
.
lock
());
if
(
state_
->
exception
)
std
::
rethrow_exception
(
state_
->
exception
);
didWork
=
true
;
}
}
...
...
This diff is collapsed.
Click to expand it.
src/libutil/thread-pool.hh
+
9
−
4
View file @
b2ce6fde
...
...
@@ -15,7 +15,9 @@ class ThreadPool
{
public:
ThreadPool
(
size_t
nrThreads
=
0
);
ThreadPool
(
size_t
maxThreads
=
0
);
~
ThreadPool
();
// FIXME: use std::packaged_task?
typedef
std
::
function
<
void
()
>
work_t
;
...
...
@@ -34,19 +36,22 @@ public:
private:
size_t
nr
Threads
;
size_t
max
Threads
;
struct
State
{
std
::
queue
<
work_t
>
left
;
size_t
pending
=
0
;
std
::
exception_ptr
exception
;
std
::
vector
<
std
::
thread
>
workers
;
bool
quit
=
false
;
};
Sync
<
State
>
state
;
Sync
<
State
>
state
_
;
std
::
condition_variable
w
akeup
;
std
::
condition_variable
w
ork
,
done
;
void
workerEntry
();
};
}
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