Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
fe41df87
Commit
fe41df87
authored
Oct 20, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pthread_cond_signal might wake multiple threads
Close #4759
parent
8276a077
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
14 deletions
+17
-14
thread.cpp
native/jni/core/thread.cpp
+17
-14
No files found.
native/jni/core/thread.cpp
View file @
fe41df87
...
@@ -18,8 +18,8 @@ static pthread_cond_t send_task = PTHREAD_COND_INITIALIZER_MONOTONIC_NP;
...
@@ -18,8 +18,8 @@ static pthread_cond_t send_task = PTHREAD_COND_INITIALIZER_MONOTONIC_NP;
static
pthread_cond_t
recv_task
=
PTHREAD_COND_INITIALIZER_MONOTONIC_NP
;
static
pthread_cond_t
recv_task
=
PTHREAD_COND_INITIALIZER_MONOTONIC_NP
;
// The following variables should be guarded by lock
// The following variables should be guarded by lock
static
int
availab
le_threads
=
0
;
static
int
id
le_threads
=
0
;
static
int
active
_threads
=
0
;
static
int
total
_threads
=
0
;
static
function
<
void
()
>
pending_task
;
static
function
<
void
()
>
pending_task
;
static
void
operator
+=
(
timespec
&
a
,
const
timespec
&
b
)
{
static
void
operator
+=
(
timespec
&
a
,
const
timespec
&
b
)
{
...
@@ -40,8 +40,8 @@ static void reset_pool() {
...
@@ -40,8 +40,8 @@ static void reset_pool() {
send_task
=
PTHREAD_COND_INITIALIZER_MONOTONIC_NP
;
send_task
=
PTHREAD_COND_INITIALIZER_MONOTONIC_NP
;
pthread_cond_destroy
(
&
recv_task
);
pthread_cond_destroy
(
&
recv_task
);
recv_task
=
PTHREAD_COND_INITIALIZER_MONOTONIC_NP
;
recv_task
=
PTHREAD_COND_INITIALIZER_MONOTONIC_NP
;
availab
le_threads
=
0
;
id
le_threads
=
0
;
active
_threads
=
0
;
total
_threads
=
0
;
pending_task
=
nullptr
;
pending_task
=
nullptr
;
}
}
...
@@ -58,7 +58,7 @@ static void *thread_pool_loop(void * const is_core_pool) {
...
@@ -58,7 +58,7 @@ static void *thread_pool_loop(void * const is_core_pool) {
function
<
void
()
>
local_task
;
function
<
void
()
>
local_task
;
{
{
mutex_guard
g
(
lock
);
mutex_guard
g
(
lock
);
++
availab
le_threads
;
++
id
le_threads
;
if
(
!
pending_task
)
{
if
(
!
pending_task
)
{
if
(
is_core_pool
)
{
if
(
is_core_pool
)
{
pthread_cond_wait
(
&
send_task
,
&
lock
);
pthread_cond_wait
(
&
send_task
,
&
lock
);
...
@@ -68,17 +68,20 @@ static void *thread_pool_loop(void * const is_core_pool) {
...
@@ -68,17 +68,20 @@ static void *thread_pool_loop(void * const is_core_pool) {
ts
+=
{
THREAD_IDLE_MAX_SEC
,
0
};
ts
+=
{
THREAD_IDLE_MAX_SEC
,
0
};
if
(
pthread_cond_timedwait
(
&
send_task
,
&
lock
,
&
ts
)
==
ETIMEDOUT
)
{
if
(
pthread_cond_timedwait
(
&
send_task
,
&
lock
,
&
ts
)
==
ETIMEDOUT
)
{
// Terminate thread after max idle time
// Terminate thread after max idle time
--
availab
le_threads
;
--
id
le_threads
;
--
active
_threads
;
--
total
_threads
;
return
nullptr
;
return
nullptr
;
}
}
}
}
}
}
local_task
.
swap
(
pending_task
);
if
(
pending_task
)
{
pthread_cond_signal
(
&
recv_task
);
local_task
.
swap
(
pending_task
);
--
available_threads
;
pthread_cond_signal
(
&
recv_task
);
}
--
idle_threads
;
}
}
local_task
();
if
(
local_task
)
local_task
();
if
(
getpid
()
==
gettid
())
if
(
getpid
()
==
gettid
())
exit
(
0
);
exit
(
0
);
}
}
...
@@ -87,9 +90,9 @@ static void *thread_pool_loop(void * const is_core_pool) {
...
@@ -87,9 +90,9 @@ static void *thread_pool_loop(void * const is_core_pool) {
void
exec_task
(
function
<
void
()
>
&&
task
)
{
void
exec_task
(
function
<
void
()
>
&&
task
)
{
mutex_guard
g
(
lock
);
mutex_guard
g
(
lock
);
pending_task
.
swap
(
task
);
pending_task
.
swap
(
task
);
if
(
availab
le_threads
==
0
)
{
if
(
id
le_threads
==
0
)
{
++
active
_threads
;
++
total
_threads
;
long
is_core_pool
=
active
_threads
<=
CORE_POOL_SIZE
;
long
is_core_pool
=
total
_threads
<=
CORE_POOL_SIZE
;
new_daemon_thread
(
thread_pool_loop
,
(
void
*
)
is_core_pool
);
new_daemon_thread
(
thread_pool_loop
,
(
void
*
)
is_core_pool
);
}
else
{
}
else
{
pthread_cond_signal
(
&
send_task
);
pthread_cond_signal
(
&
send_task
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment