[BACK]Return to alarm.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / sample / alarm

Annotation of prex-old/usr/sample/alarm/alarm.c, Revision 1.1

1.1     ! nbrk        1: /*
        !             2:  * Copyright (c) 2005-2007, Kohsuke Ohtani
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms, with or without
        !             6:  * modification, are permitted provided that the following conditions
        !             7:  * are met:
        !             8:  * 1. Redistributions of source code must retain the above copyright
        !             9:  *    notice, this list of conditions and the following disclaimer.
        !            10:  * 2. Redistributions in binary form must reproduce the above copyright
        !            11:  *    notice, this list of conditions and the following disclaimer in the
        !            12:  *    documentation and/or other materials provided with the distribution.
        !            13:  * 3. Neither the name of the author nor the names of any co-contributors
        !            14:  *    may be used to endorse or promote products derived from this software
        !            15:  *    without specific prior written permission.
        !            16:  *
        !            17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
        !            18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
        !            21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            27:  * SUCH DAMAGE.
        !            28:  */
        !            29:
        !            30: /*
        !            31:  * alarm.c - sample alarm program.
        !            32:  */
        !            33:
        !            34: /*
        !            35:  * Expected output:
        !            36:  *
        !            37:  * Alarm sample program
        !            38:  * Start alarm timer
        !            39:  * Ring! count=1 time=1000 msec
        !            40:  * Ring! count=2 time=1200 msec
        !            41:  * Ring! count=3 time=1600 msec
        !            42:  * Ring! count=4 time=2200 msec
        !            43:  * Ring! count=5 time=3000 msec
        !            44:  * Ring! count=6 time=4000 msec
        !            45:  * Ring! count=7 time=5200 msec
        !            46:  * Ring! count=8 time=6600 msec
        !            47:  * Ring! count=9 time=8200 msec
        !            48:  * Ring! count=10 time=10000 msec
        !            49:  * End...
        !            50:  *
        !            51:  */
        !            52:
        !            53: #include <prex/prex.h>
        !            54: #include <sys/signal.h>
        !            55: #include <stdio.h>
        !            56:
        !            57: static int hz;
        !            58: static int count;
        !            59: static u_long start_tick;
        !            60:
        !            61: /*
        !            62:  * Re-program alarm timer as count * 200msec.
        !            63:  */
        !            64: static void
        !            65: alarm_handler(int code, void *regs)
        !            66: {
        !            67:        u_long tick;
        !            68:
        !            69:        if (code == SIGALRM) {
        !            70:                count++;
        !            71:                if (count > 10) {
        !            72:                        printf("End...\n");
        !            73:                        task_terminate(task_self());
        !            74:                }
        !            75:                timer_alarm(count * 200, 0);
        !            76:                sys_time(&tick);
        !            77:                printf("Ring! count=%d time=%d msec\n", count,
        !            78:                       (u_int)((tick - start_tick) * 1000 / hz));
        !            79:        }
        !            80:        exception_return(regs);
        !            81: }
        !            82:
        !            83: int
        !            84: main(int argc, char *argv[])
        !            85: {
        !            86:        struct info_timer info;
        !            87:
        !            88:        printf("Alarm sample program\n");
        !            89:
        !            90:        sys_info(INFO_TIMER, &info);
        !            91:        if (info.hz == 0)
        !            92:                panic("can not get timer tick rate");
        !            93:        hz = info.hz;
        !            94:
        !            95:        /*
        !            96:         * Install alarm handler
        !            97:         */
        !            98:        exception_setup(alarm_handler);
        !            99:        exception_raise(task_self(), 0xfff);
        !           100:        timer_sleep(2000, 0);
        !           101:
        !           102:        /*
        !           103:         * Kick first alarm timer.
        !           104:         */
        !           105:        printf("Start alarm timer\n");
        !           106:        count = 0;
        !           107:        sys_time(&start_tick);
        !           108:        timer_alarm(1000, 0);
        !           109:
        !           110:        /* Wait alarm */
        !           111:        for (;;);
        !           112:
        !           113:        return 0;
        !           114: }

CVSweb