Explorar o código

change: make schedule() return int. status

greatbridf %!s(int64=2) %!d(string=hai) anos
pai
achega
6ab99e3b3d

+ 1 - 0
include/kernel/errno.h

@@ -26,5 +26,6 @@ extern uint32_t* _get_errno(void);
 #define EBADF (1 << 8)
 #define EPERM (1 << 9)
 #define ESRCH (1 << 10)
+#define EINTR (1 << 11)
 
 #endif

+ 2 - 1
include/kernel/process.hpp

@@ -519,7 +519,8 @@ public:
 };
 
 void NORETURN init_scheduler(void);
-void schedule(void);
+/// @return true if returned normally, false if being interrupted
+bool schedule(void);
 void NORETURN schedule_noreturn(void);
 
 constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)

+ 1 - 1
src/kernel/interrupt.cpp

@@ -251,8 +251,8 @@ extern "C" void irq0_handler(interrupt_stack*)
 {
     inc_tick();
     asm_outb(PORT_PIC1_COMMAND, PIC_EOI);
-    after_irq();
     schedule();
+    after_irq();
 }
 // keyboard interrupt
 extern "C" void irq1_handler(void)

+ 9 - 4
src/kernel/process.cpp

@@ -367,25 +367,30 @@ void NORETURN init_scheduler(void)
 }
 
 extern "C" void asm_ctx_switch(uint32_t** curr_esp, uint32_t* next_esp);
-void schedule()
+bool schedule()
 {
     auto thd = readythds->query();
+    process* proc = nullptr;
+    thread* curr_thd = nullptr;
 
     if (current_thread == thd)
-        return;
+        goto _end;
 
-    process* proc = thd->owner;
+    proc = thd->owner;
     if (current_process != proc) {
         asm_switch_pd(proc->mms.m_pd);
         current_process = proc;
     }
 
-    auto* curr_thd = current_thread;
+    curr_thd = current_thread;
 
     current_thread = thd;
     tss.esp0 = current_thread->pkstack;
 
     asm_ctx_switch(&curr_thd->esp, thd->esp);
+
+_end:
+    return current_process->signals.empty();
 }
 
 void NORETURN schedule_noreturn(void)

+ 4 - 1
src/kernel/syscall.cpp

@@ -213,7 +213,10 @@ int _syscall_wait(interrupt_stack* data)
         current_thread->attr.wait = 1;
         waitlst.subscribe(current_thread);
 
-        schedule();
+        if (!schedule()) {
+            waitlst.unsubscribe(current_thread);
+            return -EINTR;
+        }
 
         if (!waitlst.empty()) {
             waitlst.unsubscribe(current_thread);

+ 12 - 36
src/kernel/tty.cpp

@@ -6,10 +6,6 @@
 #include <stdint.h>
 #include <stdio.h>
 
-#define TTY_DATA (1 << 0)
-#define TTY_EOF (1 << 1)
-#define TTY_INT (1 << 2)
-
 tty::tty()
     : buf(BUFFER_SIZE)
 {
@@ -27,30 +23,14 @@ size_t tty::read(char* buf, size_t buf_size, size_t n)
 
     while (buf_size && n) {
         if (this->buf.empty()) {
-            while (this->blocklist.empty()) {
-                current_thread->attr.ready = 0;
-                current_thread->attr.wait = 1;
-                this->blocklist.subscribe(current_thread);
-                schedule();
-
-                if (!this->blocklist.empty()) {
-                    this->blocklist.unsubscribe(current_thread);
-                    break;
-                }
-            }
-
-            auto evt = this->blocklist.front();
-            switch ((int)evt.data1) {
-            // INTERRUPT
-            case TTY_INT:
-                return -1;
-            // DATA
-            case TTY_DATA:
-                break;
-            // EOF
-            case TTY_EOF:
-                return orig_n - n;
-            }
+            current_thread->attr.ready = 0;
+            current_thread->attr.wait = 1;
+            this->blocklist.subscribe(current_thread);
+
+            bool intr = !schedule();
+            this->blocklist.unsubscribe(current_thread);
+            if (intr || this->buf.empty())
+                goto _end;
         }
 
         *buf = this->buf.get();
@@ -61,6 +41,7 @@ size_t tty::read(char* buf, size_t buf_size, size_t n)
             break;
     }
 
+_end:
     return orig_n - n;
 }
 
@@ -102,7 +83,6 @@ void serial_tty::recvchar(char c)
             serial_send_data(PORT_SERIAL0, '\r');
             serial_send_data(PORT_SERIAL0, '\n');
         }
-        this->blocklist.push(kernel::evt { nullptr, (void*)TTY_DATA, nullptr, nullptr });
         this->blocklist.notify();
         break;
     // ^?: backspace
@@ -139,26 +119,22 @@ void serial_tty::recvchar(char c)
         break;
     // ^C: SIGINT
     case 0x03:
-        this->blocklist.push(kernel::evt { nullptr, (void*)TTY_INT, nullptr, nullptr });
-        this->blocklist.notify();
         procs->send_signal_grp(fg_pgroup, kernel::SIGINT);
+        this->blocklist.notify();
         break;
     // ^D: EOF
     case 0x04:
-        this->blocklist.push(kernel::evt { nullptr, (void*)TTY_EOF, nullptr, nullptr });
         this->blocklist.notify();
         break;
     // ^Z: SIGSTOP
     case 0x1a:
-        this->blocklist.push(kernel::evt { nullptr, (void*)TTY_INT, nullptr, nullptr });
-        this->blocklist.notify();
         procs->send_signal_grp(fg_pgroup, kernel::SIGSTOP);
+        this->blocklist.notify();
         break;
     // ^\: SIGQUIT
     case 0x1c:
-        this->blocklist.push(kernel::evt { nullptr, (void*)TTY_INT, nullptr, nullptr });
-        this->blocklist.notify();
         procs->send_signal_grp(fg_pgroup, kernel::SIGQUIT);
+        this->blocklist.notify();
         break;
     default:
         buf.put(c);