Clean up g_fork_count

Make it static and atomic
This commit is contained in:
ridiculousfish 2019-06-03 12:58:35 -07:00
parent bc103c2ea6
commit b478f877ee
3 changed files with 11 additions and 11 deletions

View file

@ -308,7 +308,6 @@ class env_stack_t final : public environment_t {
static env_stack_t &globals(); static env_stack_t &globals();
}; };
extern int g_fork_count;
extern bool g_use_posix_spawn; extern bool g_use_posix_spawn;
extern bool term_has_xn; // does the terminal have the "eat_newline_glitch" extern bool term_has_xn; // does the terminal have the "eat_newline_glitch"

View file

@ -57,6 +57,9 @@
/// Base open mode to pass to calls to open. /// Base open mode to pass to calls to open.
#define OPEN_MASK 0666 #define OPEN_MASK 0666
/// Number of calls to fork() or posix_spawn().
static relaxed_atomic_t<int> s_fork_count{0};
void exec_close(int fd) { void exec_close(int fd) {
ASSERT_IS_MAIN_THREAD(); ASSERT_IS_MAIN_THREAD();
@ -455,7 +458,9 @@ static bool fork_child_for_process(const std::shared_ptr<job_t> &job, process_t
// This is the parent process. Store away information on the child, and // This is the parent process. Store away information on the child, and
// possibly give it control over the terminal. // possibly give it control over the terminal.
FLOGF(exec_fork, L"Fork #%d, pid %d: %s for '%ls'", g_fork_count, pid, fork_type, p->argv0()); s_fork_count++;
FLOGF(exec_fork, L"Fork #%d, pid %d: %s for '%ls'", int(s_fork_count), pid, fork_type,
p->argv0());
p->pid = pid; p->pid = pid;
on_process_created(job, p->pid); on_process_created(job, p->pid);
@ -688,7 +693,7 @@ static bool exec_external_command(parser_t &parser, const std::shared_ptr<job_t>
// Prefer to use posix_spawn, since it's faster on some systems like OS X. // Prefer to use posix_spawn, since it's faster on some systems like OS X.
bool use_posix_spawn = g_use_posix_spawn && can_use_posix_spawn_for_job(j); bool use_posix_spawn = g_use_posix_spawn && can_use_posix_spawn_for_job(j);
if (use_posix_spawn) { if (use_posix_spawn) {
g_fork_count++; // spawn counts as a fork+exec s_fork_count++; // spawn counts as a fork+exec
// Create posix spawn attributes and actions. // Create posix spawn attributes and actions.
pid_t pid = 0; pid_t pid = 0;
posix_spawnattr_t attr = posix_spawnattr_t(); posix_spawnattr_t attr = posix_spawnattr_t();
@ -718,8 +723,8 @@ static bool exec_external_command(parser_t &parser, const std::shared_ptr<job_t>
// A 0 pid means we failed to posix_spawn. Since we have no pid, we'll never get // A 0 pid means we failed to posix_spawn. Since we have no pid, we'll never get
// told when it's exited, so we have to mark the process as failed. // told when it's exited, so we have to mark the process as failed.
FLOGF(exec_fork, L"Fork #%d, pid %d: spawn external command '%s' from '%ls'", g_fork_count, FLOGF(exec_fork, L"Fork #%d, pid %d: spawn external command '%s' from '%ls'",
pid, actual_cmd, file ? file : L"<no file>"); int(s_fork_count), pid, actual_cmd, file ? file : L"<no file>");
if (pid == 0) { if (pid == 0) {
job_mark_process_as_failed(j, p); job_mark_process_as_failed(j, p);
return false; return false;
@ -1100,8 +1105,8 @@ bool exec_job(parser_t &parser, shared_ptr<job_t> j) {
} }
} }
FLOGF(exec_job_exec, L"Executed job %d from command '%ls' with pgrp %d", j->job_id, j->command_wcstr(), FLOGF(exec_job_exec, L"Executed job %d from command '%ls' with pgrp %d", j->job_id,
j->pgid); j->command_wcstr(), j->pgid);
j->set_flag(job_flag_t::CONSTRUCTED, true); j->set_flag(job_flag_t::CONSTRUCTED, true);
if (!j->is_foreground()) { if (!j->is_foreground()) {

View file

@ -171,8 +171,6 @@ int setup_child_process(process_t *p, const dup2_list_t &dup2s) {
return 0; return 0;
} }
int g_fork_count = 0;
/// This function is a wrapper around fork. If the fork calls fails with EAGAIN, it is retried /// This function is a wrapper around fork. If the fork calls fails with EAGAIN, it is retried
/// FORK_LAPS times, with a very slight delay between each lap. If fork fails even then, the process /// FORK_LAPS times, with a very slight delay between each lap. If fork fails even then, the process
/// will exit with an error message. /// will exit with an error message.
@ -191,8 +189,6 @@ pid_t execute_fork(bool wait_for_threads_to_die) {
struct timespec pollint; struct timespec pollint;
int i; int i;
g_fork_count++;
for (i = 0; i < FORK_LAPS; i++) { for (i = 0; i < FORK_LAPS; i++) {
pid = fork(); pid = fork();
if (pid >= 0) { if (pid >= 0) {