Jelajahi Sumber

Shell: Store jobs as NonnullRefPtr<Job>

Andreas Kling 4 tahun lalu
induk
melakukan
b8440b12b7
2 mengubah file dengan 16 tambahan dan 14 penghapusan
  1. 1 1
      Shell/Shell.h
  2. 15 13
      Shell/main.cpp

+ 1 - 1
Shell/Shell.h

@@ -165,7 +165,7 @@ public:
     int last_return_code { 0 };
     int last_return_code { 0 };
     Vector<String> directory_stack;
     Vector<String> directory_stack;
     CircularQueue<String, 8> cd_history; // FIXME: have a configurable cd history length
     CircularQueue<String, 8> cd_history; // FIXME: have a configurable cd history length
-    HashMap<u64, RefPtr<Job>> jobs;
+    HashMap<u64, NonnullRefPtr<Job>> jobs;
     Vector<String, 256> cached_path;
     Vector<String, 256> cached_path;
 
 
     enum ShellEventType {
     enum ShellEventType {

+ 15 - 13
Shell/main.cpp

@@ -83,15 +83,17 @@ int main(int argc, char** argv)
     Core::EventLoop::register_signal(SIGCHLD, [](int) {
     Core::EventLoop::register_signal(SIGCHLD, [](int) {
         auto& jobs = s_shell->jobs;
         auto& jobs = s_shell->jobs;
         Vector<u64> disowned_jobs;
         Vector<u64> disowned_jobs;
-        for (auto& job : jobs) {
+        for (auto& it : jobs) {
+            auto job_id = it.key;
+            auto& job = *it.value;
             int wstatus = 0;
             int wstatus = 0;
-            auto child_pid = waitpid(job.value->pid(), &wstatus, WNOHANG | WUNTRACED);
+            auto child_pid = waitpid(job.pid(), &wstatus, WNOHANG | WUNTRACED);
             if (child_pid < 0) {
             if (child_pid < 0) {
                 if (errno == ECHILD) {
                 if (errno == ECHILD) {
                     // The child process went away before we could process its death, just assume it exited all ok.
                     // The child process went away before we could process its death, just assume it exited all ok.
                     // FIXME: This should never happen, the child should stay around until we do the waitpid above.
                     // FIXME: This should never happen, the child should stay around until we do the waitpid above.
-                    dbg() << "Child process gone, cannot get exit code for " << job.key;
-                    child_pid = job.value->pid();
+                    dbg() << "Child process gone, cannot get exit code for " << job_id;
+                    child_pid = job.pid();
                 } else {
                 } else {
                     ASSERT_NOT_REACHED();
                     ASSERT_NOT_REACHED();
                 }
                 }
@@ -102,21 +104,21 @@ int main(int argc, char** argv)
                 continue;
                 continue;
             }
             }
 #endif
 #endif
-            if (child_pid == job.value->pid()) {
+            if (child_pid == job.pid()) {
                 if (WIFEXITED(wstatus)) {
                 if (WIFEXITED(wstatus)) {
-                    job.value->set_has_exit(WEXITSTATUS(wstatus));
+                    job.set_has_exit(WEXITSTATUS(wstatus));
                 } else if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) {
                 } else if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) {
-                    job.value->set_has_exit(126);
+                    job.set_has_exit(126);
                 } else if (WIFSTOPPED(wstatus)) {
                 } else if (WIFSTOPPED(wstatus)) {
-                    job.value->unblock();
-                    job.value->set_is_suspended(true);
+                    job.unblock();
+                    job.set_is_suspended(true);
                 }
                 }
             }
             }
-            if (job.value->should_be_disowned())
-                disowned_jobs.append(job.key);
+            if (job.should_be_disowned())
+                disowned_jobs.append(job_id);
         }
         }
-        for (auto key : disowned_jobs)
-            jobs.remove(key);
+        for (auto job_id : disowned_jobs)
+            jobs.remove(job_id);
     });
     });
 
 
     Core::EventLoop::register_signal(SIGTSTP, [](auto) {
     Core::EventLoop::register_signal(SIGTSTP, [](auto) {