Eliminate yet more calls to principal_parser()

In particular, remove job_t::from_job_id
This commit is contained in:
ridiculousfish 2020-02-08 12:47:13 -08:00
parent f1f97b6476
commit fba3c83ba5
8 changed files with 21 additions and 23 deletions

View File

@ -130,7 +130,7 @@ static int parse_cmd_opts(functions_cmd_opts_t &opts, int *optind, //!OCLINT(hi
}
/// Return a definition of the specified function. Used by the functions builtin.
static wcstring functions_def(const wcstring &name) {
static wcstring functions_def(const parser_t &parser, const wcstring &name) {
assert(!name.empty() && "Empty name");
wcstring out;
wcstring desc, def;
@ -184,7 +184,7 @@ static wcstring functions_def(const wcstring &name) {
break;
}
case event_type_t::job_exit: {
const job_t *j = job_t::from_job_id(d.param1.job_id);
const job_t *j = parser.job_get(d.param1.job_id);
if (j) append_format(out, L" --on-job-exit %d", j->pgid);
break;
}
@ -435,7 +435,7 @@ int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
if (i != optind) streams.out.append(L"\n");
const wchar_t *funcname = argv[optind];
report_function_metadata(funcname, opts.verbose, streams, parser, true);
wcstring def = functions_def(funcname);
wcstring def = functions_def(parser, funcname);
if (!streams.out_is_redirected && isatty(STDOUT_FILENO)) {
std::vector<highlight_spec_t> colors;

View File

@ -188,14 +188,13 @@ int builtin_jobs(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
const job_t *j = nullptr;
if (argv[i][0] == L'%') {
int jobId = -1;
jobId = fish_wcstoi(argv[i] + 1);
if (errno || jobId < -1) {
int job_id = fish_wcstoi(argv[i] + 1);
if (errno || job_id < -1) {
streams.err.append_format(_(L"%ls: '%ls' is not a valid job id"), cmd,
argv[i]);
return STATUS_INVALID_ARGS;
}
j = job_t::from_job_id(jobId);
j = parser.job_get(job_id);
} else {
int pid = fish_wcstoi(argv[i]);
if (errno || pid < 0) {

View File

@ -76,9 +76,9 @@ static int wait_for_backgrounds(parser_t &parser, bool any_flag) {
return 0;
}
static bool all_specified_jobs_finished(const std::vector<job_id_t> &ids) {
static bool all_specified_jobs_finished(const parser_t &parser, const std::vector<job_id_t> &ids) {
for (auto id : ids) {
if (job_t *j = job_t::from_job_id(id)) {
if (const job_t *j = parser.job_get(id)) {
// If any specified job is not completed, return false.
// If there are stopped jobs, they are ignored.
if (j->is_constructed() && !j->is_completed() && !j->is_stopped()) {
@ -89,9 +89,9 @@ static bool all_specified_jobs_finished(const std::vector<job_id_t> &ids) {
return true;
}
static bool any_specified_jobs_finished(const std::vector<job_id_t> &ids) {
static bool any_specified_jobs_finished(const parser_t &parser, const std::vector<job_id_t> &ids) {
for (auto id : ids) {
if (job_t *j = job_t::from_job_id(id)) {
if (const job_t *j = parser.job_get(id)) {
// If any specified job is completed, return true.
if (j->is_constructed() && (j->is_completed() || j->is_stopped())) {
return true;
@ -107,8 +107,8 @@ static bool any_specified_jobs_finished(const std::vector<job_id_t> &ids) {
static int wait_for_backgrounds_specified(parser_t &parser, const std::vector<job_id_t> &ids,
bool any_flag) {
sigint_checker_t sigint;
while ((!any_flag && !all_specified_jobs_finished(ids)) ||
(any_flag && !any_specified_jobs_finished(ids))) {
while ((!any_flag && !all_specified_jobs_finished(parser, ids)) ||
(any_flag && !any_specified_jobs_finished(parser, ids))) {
if (sigint.check()) {
return 128 + SIGINT;
}

View File

@ -168,7 +168,7 @@ wcstring event_get_desc(const parser_t &parser, const event_t &evt) {
}
case event_type_t::job_exit: {
job_t *j = job_t::from_job_id(ed.param1.job_id);
const job_t *j = parser.job_get(ed.param1.job_id);
if (j) {
return format_string(_(L"exit handler for job %d, '%ls'"), j->job_id(),
j->command_wcstr());

View File

@ -586,6 +586,13 @@ job_t *parser_t::job_get(job_id_t id) {
return nullptr;
}
const job_t *parser_t::job_get(job_id_t id) const {
for (const auto &job : job_list) {
if (id <= 0 || job->job_id() == id) return job.get();
}
return nullptr;
}
job_t *parser_t::job_get_from_pid(pid_t pid) const {
pid_t pgid = getpgid(pid);

View File

@ -364,6 +364,7 @@ class parser_t : public std::enable_shared_from_this<parser_t> {
/// Return the job with the specified job id. If id is 0 or less, return the last job used.
job_t *job_get(job_id_t job_id);
const job_t *job_get(job_id_t job_id) const;
/// Returns the job with the given pid.
job_t *job_get_from_pid(pid_t pid) const;

View File

@ -118,11 +118,6 @@ void release_job_id(job_id_t jid) {
consumed_job_ids->erase(where);
}
job_t *job_t::from_job_id(job_id_t id) {
ASSERT_IS_MAIN_THREAD();
return parser_t::principal_parser().job_get(id);
}
/// Return true if all processes in the job have stopped or completed.
bool job_t::is_stopped() const {
for (const process_ptr_t &p : processes) {

View File

@ -513,10 +513,6 @@ class job_t {
/// \returns the statuses for this job.
statuses_t get_statuses() const;
/// Return the job instance matching this unique job id.
/// If id is 0 or less, return the last job used.
static job_t *from_job_id(job_id_t id);
};
/// Whether this shell is attached to the keyboard at all.