]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
feat: alarm() syscall (83) — per-process SIGALRM timer via scheduler tick check
authorTulio A M Mendes <[email protected]>
Thu, 12 Feb 2026 03:25:31 +0000 (00:25 -0300)
committerTulio A M Mendes <[email protected]>
Fri, 13 Feb 2026 02:20:50 +0000 (23:20 -0300)
include/process.h
include/syscall.h
src/kernel/scheduler.c
src/kernel/syscall.c
user/ulibc/include/syscall.h
user/ulibc/include/unistd.h
user/ulibc/src/unistd.c

index 0be6a6900004a5bd48e29e504f99b9b499e17022..f2d32b70ee21d8db3e319248a634ae2c67081be4 100644 (file)
@@ -66,6 +66,7 @@ struct process {
     int8_t  nice;               // -20 to +19 (maps to priority)
     process_state_t state;
     uint32_t wake_at_tick;
+    uint32_t alarm_tick;
     int exit_status;
 
     int has_user_regs;
index adee948697efaa6a2556809be2c9c7b157f24ba5..2cdb5a1c0a6fe2f93557b9c8514b39b536e30d2e 100644 (file)
@@ -115,6 +115,7 @@ enum {
     SYSCALL_SIGSUSPEND = 80,
     SYSCALL_READV     = 81,
     SYSCALL_WRITEV    = 82,
+    SYSCALL_ALARM     = 83,
 };
 
 #endif
index 9bf571d09af4b06c767d453597328677a8c27e3d..147e7d8df86509fe113971b9ee757441fbc99263 100644 (file)
@@ -853,6 +853,10 @@ void process_wake_check(uint32_t current_tick) {
                 rq_enqueue(rq_active, iter);
             }
         }
+        if (iter->alarm_tick != 0 && current_tick >= iter->alarm_tick) {
+            iter->alarm_tick = 0;
+            iter->sig_pending_mask |= (1U << 14); /* SIGALRM */
+        }
         iter = iter->next;
     } while (iter != start);
 
index 6f647c10104bc87b11b3168d82216304a4c4cc07..cfc6b94183d9514652007034a334b7d061f5ed7c 100644 (file)
@@ -2272,6 +2272,23 @@ void syscall_handler(struct registers* regs) {
         return;
     }
 
+    if (syscall_no == SYSCALL_ALARM) {
+        if (!current_process) { regs->eax = 0; return; }
+        uint32_t seconds = regs->ebx;
+        uint32_t now = get_tick_count();
+        uint32_t old_remaining = 0;
+        if (current_process->alarm_tick > now) {
+            old_remaining = (current_process->alarm_tick - now) / 50 + 1;
+        }
+        if (seconds == 0) {
+            current_process->alarm_tick = 0;
+        } else {
+            current_process->alarm_tick = now + seconds * 50;
+        }
+        regs->eax = old_remaining;
+        return;
+    }
+
     if (syscall_no == SYSCALL_SIGSUSPEND) {
         if (!current_process) { regs->eax = (uint32_t)-EINVAL; return; }
         uint32_t new_mask = 0;
index f43ab0275129eb1a8bb90ab1395f4359403ecd7d..4598efc33b3bd035cfdcbe133913f64c7f64d442 100644 (file)
@@ -74,6 +74,7 @@ enum {
     SYS_SIGSUSPEND = 80,
     SYS_READV = 81,
     SYS_WRITEV = 82,
+    SYS_ALARM = 83,
 };
 
 /* Raw syscall wrappers — up to 5 args via INT 0x80 */
index af7f24dff72974e80cffdf432d303ec41d3dffb2..6b480f8456695b4b65407eddc20fd41a27d85744 100644 (file)
@@ -51,6 +51,7 @@ int     setuid(int uid);
 int     setgid(int gid);
 int     truncate(const char* path, int length);
 int     ftruncate(int fd, int length);
+unsigned int alarm(unsigned int seconds);
 void*   brk(void* addr);
 
 void    _exit(int status) __attribute__((noreturn));
index f1c044839f8987f783c162162e774e4b2c0b2505..7ffeb054af137b1c0b857e7836f0ed1063f0f376 100644 (file)
@@ -131,6 +131,10 @@ int ftruncate(int fd, int length) {
     return __syscall_ret(_syscall2(SYS_FTRUNCATE, fd, length));
 }
 
+unsigned int alarm(unsigned int seconds) {
+    return (unsigned int)_syscall1(SYS_ALARM, (int)seconds);
+}
+
 void* brk(void* addr) {
     return (void*)_syscall1(SYS_BRK, (int)addr);
 }