Show
Ignore:
Timestamp:
05/20/10 16:05:25 (2 years ago)
Author:
mwhitworth
Message:

Add to TCP/IP stack.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • Whitix/branches/netchannel/kernel/timer.c

    r2084 r2085  
    2323#include <error.h> 
    2424#include <task.h> 
     25#include <sys.h> 
    2526#include <malloc.h> 
     27#include <event.h> 
     28#include <print.h> 
    2629 
    2730LIST_HEAD(timerList); 
     
    185188SYMBOL_EXPORT(Sleep); 
    186189 
     190#define KE_TIMER_ALLOC_STEP             10 
     191 
     192void TimerWakeup(void* data) 
     193{ 
     194        struct KeTimer* timer = (struct KeTimer*)data; 
     195 
     196        EventFire(timer->thread, timer->uInfo.func, timer->uInfo.data); 
     197} 
     198 
    187199/* Kernel system calls for timers */ 
    188 int SysTimerCreate() 
    189 { 
    190         return 0; 
    191 } 
    192  
    193 int SysTimerDestroy() 
    194 { 
    195         return 0; 
    196 } 
     200int SysTimerCreate(struct UserTimer* timerDesc) 
     201{ 
     202        int i; 
     203        struct KeTimer* curr; 
     204 
     205        SpinLock(&current->timerListLock); 
     206 
     207        if (!current->timers) 
     208        { 
     209                current->numTimers = KE_TIMER_ALLOC_STEP; 
     210                current->timers = (struct KeTimer**)MemAlloc(sizeof(struct KeTimer) * KE_TIMER_ALLOC_STEP); 
     211        } 
     212 
     213        for (i = 0; i < current->numTimers; i++) 
     214                if (!current->timers[i]) 
     215                        break; 
     216 
     217        if (i == current->numTimers) 
     218        { 
     219                KePrint("TODO: Extend array\n"); 
     220        } 
     221 
     222        current->timers[i] = (struct KeTimer*)MemAlloc(sizeof(struct KeTimer)); 
     223        curr = current->timers[i]; 
     224 
     225        curr->thread = currThread; 
     226        memcpy(&curr->uInfo, timerDesc, sizeof(struct UserTimer)); 
     227        curr->timer.func = TimerWakeup; 
     228        curr->timer.data = curr; 
     229 
     230        SpinUnlock(&current->timerListLock); 
     231 
     232        return i; 
     233} 
     234 
     235struct KeTimer* TimerGet(struct Process* process, int timer) 
     236{ 
     237        if (timer < 0 || timer > process->numTimers) 
     238                return NULL; 
     239 
     240        return process->timers[timer]; 
     241} 
     242 
     243int SysTimerModify(int tid, struct UserTimerSpec* newValue, struct UserTimerSpec* oldValue) 
     244{ 
     245        struct KeTimer* timer; 
     246 
     247        timer = TimerGet(current, tid); 
     248 
     249        if (oldValue) 
     250        { 
     251                KePrint("oldValue != NULL\n"); 
     252                return -ENOTIMPL; 
     253        } 
     254 
     255        if (!newValue) 
     256                return -EFAULT; 
     257 
     258        /* TODO: Check if timer is already on list */ 
     259 
     260        timer->timer.expires = newValue->secs * 1000; 
     261        /* useconds */ 
     262        TimerAdd(&timer->timer); 
     263 
     264        return 0; 
     265} 
     266 
     267int SysTimerDestroy(int timer) 
     268{ 
     269        KePrint("SysTimerDestroy(%d)\n", timer); 
     270        return 0; 
     271} 
     272 
     273int TimerProcessRemove(struct Process* process) 
     274{ 
     275        if (process->timers) 
     276        { 
     277                KePrint("Free timers\n"); 
     278        } 
     279 
     280        return 0; 
     281} 
     282 
     283static struct SysCall timerSysCallTable[]= 
     284{ 
     285        SysEntry(SysTimerCreate, 4), 
     286        SysEntry(SysTimerModify, 12), 
     287        SysEntry(SysTimerDestroy, 4), 
     288        SysEntryEnd() 
     289}; 
     290 
     291int TimerInit() 
     292{ 
     293        SysRegisterRange(SYS_TIMER_BASE, timerSysCallTable); 
     294        return 0; 
     295}