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
6f54c576
Commit
6f54c576
authored
Oct 17, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow fork in thread pool
parent
e8ae103d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
11 deletions
+43
-11
daemon.cpp
native/jni/core/daemon.cpp
+12
-0
thread.cpp
native/jni/core/thread.cpp
+30
-11
daemon.hpp
native/jni/include/daemon.hpp
+1
-0
No files found.
native/jni/core/daemon.cpp
View file @
6f54c576
...
...
@@ -68,6 +68,18 @@ void unregister_poll(int fd, bool auto_close) {
}
}
void
clear_poll
()
{
if
(
poll_fds
)
{
for
(
auto
&
poll_fd
:
*
poll_fds
)
{
close
(
poll_fd
.
fd
);
}
}
delete
poll_fds
;
delete
poll_map
;
poll_fds
=
nullptr
;
poll_map
=
nullptr
;
}
static
void
poll_ctrl_handler
(
pollfd
*
pfd
)
{
int
code
=
read_int
(
pfd
->
fd
);
switch
(
code
)
{
...
...
native/jni/core/thread.cpp
View file @
6f54c576
...
...
@@ -6,30 +6,48 @@
using
namespace
std
;
#ifndef PTHREAD_COND_INITIALIZER_MONOTONIC_NP
#define PTHREAD_COND_INITIALIZER_MONOTONIC_NP { { 1 << 1 } }
#endif
#define THREAD_IDLE_MAX_SEC 60
#define CORE_POOL_SIZE 3
static
pthread_mutex_t
lock
=
PTHREAD_MUTEX_INITIALIZER
;
static
pthread_cond_t
send_task
=
PTHREAD_COND_INITIALIZER
;
static
pthread_cond_t
recv_task
=
PTHREAD_COND_INITIALIZER
;
static
pthread_cond_t
send_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
static
int
available_threads
=
0
;
static
int
active_threads
=
0
;
static
function
<
void
()
>
pending_task
;
static
void
operator
+=
(
time
val
&
a
,
const
timeval
&
b
)
{
static
void
operator
+=
(
time
spec
&
a
,
const
timespec
&
b
)
{
a
.
tv_sec
+=
b
.
tv_sec
;
a
.
tv_
usec
+=
b
.
tv_u
sec
;
if
(
a
.
tv_
usec
>=
1000000
)
{
a
.
tv_
nsec
+=
b
.
tv_n
sec
;
if
(
a
.
tv_
nsec
>=
1000000000L
)
{
a
.
tv_sec
++
;
a
.
tv_
usec
-=
1000000
;
a
.
tv_
nsec
-=
1000000000L
;
}
}
static
timespec
to_ts
(
const
timeval
&
tv
)
{
return
{
tv
.
tv_sec
,
tv
.
tv_usec
*
1000
};
}
static
void
reset_pool
()
{
clear_poll
();
pthread_mutex_unlock
(
&
lock
);
pthread_mutex_destroy
(
&
lock
);
pthread_mutex_init
(
&
lock
,
nullptr
);
pthread_cond_destroy
(
&
send_task
);
send_task
=
PTHREAD_COND_INITIALIZER_MONOTONIC_NP
;
pthread_cond_destroy
(
&
recv_task
);
recv_task
=
PTHREAD_COND_INITIALIZER_MONOTONIC_NP
;
available_threads
=
0
;
active_threads
=
0
;
pending_task
=
nullptr
;
}
static
void
*
thread_pool_loop
(
void
*
const
is_core_pool
)
{
pthread_atfork
(
nullptr
,
nullptr
,
&
reset_pool
);
// Block all signals
sigset_t
mask
;
sigfillset
(
&
mask
);
...
...
@@ -45,10 +63,9 @@ static void *thread_pool_loop(void * const is_core_pool) {
if
(
is_core_pool
)
{
pthread_cond_wait
(
&
send_task
,
&
lock
);
}
else
{
timeval
tv
;
gettimeofday
(
&
tv
,
nullptr
);
tv
+=
{
THREAD_IDLE_MAX_SEC
,
0
};
auto
ts
=
to_ts
(
tv
);
timespec
ts
;
clock_gettime
(
CLOCK_MONOTONIC
,
&
ts
);
ts
+=
{
THREAD_IDLE_MAX_SEC
,
0
};
if
(
pthread_cond_timedwait
(
&
send_task
,
&
lock
,
&
ts
)
==
ETIMEDOUT
)
{
// Terminate thread after max idle time
--
available_threads
;
...
...
@@ -62,6 +79,8 @@ static void *thread_pool_loop(void * const is_core_pool) {
--
available_threads
;
}
local_task
();
if
(
getpid
()
==
gettid
())
exit
(
0
);
}
}
...
...
native/jni/include/daemon.hpp
View file @
6f54c576
...
...
@@ -49,6 +49,7 @@ int connect_daemon(bool create = false);
using
poll_callback
=
void
(
*
)(
pollfd
*
);
void
register_poll
(
const
pollfd
*
pfd
,
poll_callback
callback
);
void
unregister_poll
(
int
fd
,
bool
auto_close
);
void
clear_poll
();
// Thread pool
void
exec_task
(
std
::
function
<
void
()
>
&&
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