Annotation of prex-old/usr/sample/task/task.c, Revision 1.1
1.1 ! nbrk 1: /*
! 2: * Copyright (c) 2005-2006, 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: * task.c - a sample program to run tasks.
! 32: */
! 33:
! 34: #include <prex/prex.h>
! 35: #include <stdio.h>
! 36:
! 37: #define NR_TASKS 10
! 38:
! 39: /*
! 40: * Create new task
! 41: */
! 42: static task_t
! 43: task_run(void (*func)(void), char *stack)
! 44: {
! 45: task_t task;
! 46: thread_t th;
! 47:
! 48: if (task_create(task_self(), VM_COPY, &task) != 0)
! 49: return 0;
! 50:
! 51: if (thread_create(task, &th) != 0)
! 52: return 0;
! 53:
! 54: if (thread_load(th, func, stack) != 0)
! 55: return 0;
! 56:
! 57: if (thread_resume(th) != 0)
! 58: return 0;
! 59:
! 60: return task;
! 61: }
! 62:
! 63: /*
! 64: * Function to be called in new task.
! 65: */
! 66: static void
! 67: hey_yo(void)
! 68: {
! 69: task_t self;
! 70:
! 71: /*
! 72: * Display string
! 73: */
! 74: self = task_self();
! 75: printf("Task %x: Hey, Yo!\n", (u_int)self);
! 76:
! 77: /*
! 78: * Wait 5 sec
! 79: */
! 80: timer_sleep(5000, 0);
! 81:
! 82: /*
! 83: * Terminate current task.
! 84: */
! 85: printf("Task %x: Bye!\n", (u_int)self);
! 86: task_terminate(task_self());
! 87: }
! 88:
! 89: int
! 90: main(int argc, char *argv[])
! 91: {
! 92: static char stack[1024];
! 93: int i;
! 94:
! 95: printf("Task sample program\n");
! 96:
! 97: /* Create new tasks */
! 98: for (i = 0; i < NR_TASKS; i++) {
! 99: if (task_run(hey_yo, stack + 1024) == 0)
! 100: panic("failed to create task");
! 101: }
! 102:
! 103: /* Wait here... */
! 104: task_suspend(task_self());
! 105: return 0;
! 106: }
CVSweb