]> 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 ddc7e748db307edbfa597027af2e2016417f03ab..70b0570bf42022c9dac6a71a282c80d0b45153e2 100644 (file)
@@ -57,6 +57,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 a82cb3bdf97e8f138664b15d83150b5f8fa70d15..ff6680c4771e4b421f9f0f3120143db883c66da2 100644 (file)
@@ -106,6 +106,7 @@ enum {
     SYSCALL_SIGSUSPEND = 80,
     SYSCALL_READV     = 81,
     SYSCALL_WRITEV    = 82,
+    SYSCALL_ALARM     = 83,
 };
 
 #endif
index 1597f87f747f2a90ae971d49173ad9c28750bc96..f7cb152db0f96fccfcb51ed52fe3ec2fd2b725a3 100644 (file)
@@ -844,6 +844,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 0bb88fe09c6cb904e7f8c1c7ee710138fe3067e6..41b3dc263a362fc617ec9defd9100090120ac98d 100644 (file)
@@ -2263,6 +2263,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 333867ad9154d344d51bceed2796fc58332d631d..7501627cebf0f2e09a4d4bbeaf5cc9dfba130c11 100644 (file)
@@ -65,6 +65,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 841053bb67c8836c35491a46c09953c0a88d3657..e2bb132d8dc5816a6b3b4568e8c780334b9d9117 100644 (file)
@@ -42,6 +42,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 302319515b0c24ae884c2794b2d5072e570cdafa..609de9f9736dd4c7306869a1052e99bd1edaede6 100644 (file)
@@ -122,6 +122,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);
 }