![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A Small Cooperative Multitasking Kernel
timer-unix-c.h@0:73b89fc74e9f, 2011-07-24 (annotated)
- Committer:
- Ivop
- Date:
- Sun Jul 24 17:15:42 2011 +0000
- Revision:
- 0:73b89fc74e9f
first release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ivop | 0:73b89fc74e9f | 1 | /* Opus Una - A Small Cooperative Multitasking Kernel in C |
Ivop | 0:73b89fc74e9f | 2 | * |
Ivop | 0:73b89fc74e9f | 3 | * Copyright (C) 2011 by Ivo van Poorten <ivop@euronet.nl> |
Ivop | 0:73b89fc74e9f | 4 | * This file is licensed under the terms of the GNU Lesser |
Ivop | 0:73b89fc74e9f | 5 | * General Public License, version 3. |
Ivop | 0:73b89fc74e9f | 6 | */ |
Ivop | 0:73b89fc74e9f | 7 | |
Ivop | 0:73b89fc74e9f | 8 | /* Run the kernel as a process on Unix */ |
Ivop | 0:73b89fc74e9f | 9 | |
Ivop | 0:73b89fc74e9f | 10 | #include "timer.h" |
Ivop | 0:73b89fc74e9f | 11 | #include "kernel.h" |
Ivop | 0:73b89fc74e9f | 12 | |
Ivop | 0:73b89fc74e9f | 13 | #include <sys/time.h> |
Ivop | 0:73b89fc74e9f | 14 | #include <signal.h> |
Ivop | 0:73b89fc74e9f | 15 | #include <string.h> |
Ivop | 0:73b89fc74e9f | 16 | #include <unistd.h> |
Ivop | 0:73b89fc74e9f | 17 | |
Ivop | 0:73b89fc74e9f | 18 | static void timer_sr(int x) { |
Ivop | 0:73b89fc74e9f | 19 | ou_ticks++; |
Ivop | 0:73b89fc74e9f | 20 | ou_update_tasks(); |
Ivop | 0:73b89fc74e9f | 21 | } |
Ivop | 0:73b89fc74e9f | 22 | |
Ivop | 0:73b89fc74e9f | 23 | void ou_start_timer(void) { |
Ivop | 0:73b89fc74e9f | 24 | struct itimerval val; |
Ivop | 0:73b89fc74e9f | 25 | struct sigaction act; |
Ivop | 0:73b89fc74e9f | 26 | |
Ivop | 0:73b89fc74e9f | 27 | memset(&act, 0, sizeof(act)); |
Ivop | 0:73b89fc74e9f | 28 | |
Ivop | 0:73b89fc74e9f | 29 | act.sa_handler = timer_sr; |
Ivop | 0:73b89fc74e9f | 30 | sigaction(SIGALRM, &act, NULL); // signal() is not portable |
Ivop | 0:73b89fc74e9f | 31 | |
Ivop | 0:73b89fc74e9f | 32 | val.it_interval.tv_usec = 10000; // 10ms |
Ivop | 0:73b89fc74e9f | 33 | val.it_interval.tv_sec = 0; |
Ivop | 0:73b89fc74e9f | 34 | val.it_value.tv_usec = 10000; // 10ms |
Ivop | 0:73b89fc74e9f | 35 | val.it_value.tv_sec = 0; |
Ivop | 0:73b89fc74e9f | 36 | setitimer(ITIMER_REAL, &val, NULL); |
Ivop | 0:73b89fc74e9f | 37 | } |
Ivop | 0:73b89fc74e9f | 38 | |
Ivop | 0:73b89fc74e9f | 39 | void ou_idle(void) { |
Ivop | 0:73b89fc74e9f | 40 | usleep(5); // save power/cpu time/et cetera... |
Ivop | 0:73b89fc74e9f | 41 | } |