Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/test/task/task.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test_thread
  2. main

   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 - test program for kernel task services.
  32  */
  33 
  34 #include <sys/prex.h>
  35 #include <stdio.h>
  36 #include <stdlib.h>
  37 
  38 #define NR_THREADS 16
  39 
  40 static char stack[NR_THREADS][1024];
  41 
  42 static void
  43 test_thread(void)
  44 {
  45         printf("New thread %x is started\n", (u_int)thread_self());
  46 
  47         for (;;) {
  48                 timer_sleep(100, 0);
  49                 printf("@");
  50         }
  51 }
  52 
  53 int
  54 main(int argc, char *argv[])
  55 {
  56         task_t task;
  57         thread_t t;
  58         int i, error;
  59 
  60         printf("Task test program\n");
  61         sys_log("Task test program\n");
  62 
  63         /*
  64          * Boost priority of this thread
  65          */
  66         thread_setpri(thread_self(), 90);
  67 
  68         /*
  69          * Create test task
  70          */
  71 #ifdef CONFIG_MMU
  72         error = task_create(task_self(), VM_COPY, &task);
  73 #else
  74         error = task_create(task_self(), VM_SHARE, &task);
  75 #endif
  76         if (error) {
  77                 printf("task_create failed. error=%d\n", error);
  78                 exit(1);
  79         }
  80 
  81         /*
  82          * Run threads
  83          */
  84         for (i = 0; i < NR_THREADS; i++) {
  85                 error = thread_create(task, &t);
  86                 printf("thread_create: error=%d\n", error);
  87                 error = thread_load(t, test_thread, stack[i]+1024);
  88                 printf("thread_load: error=%d\n", error);
  89                 thread_resume(t);
  90         }
  91 
  92         /*
  93          * Sleep for a while
  94          */
  95         timer_sleep(1000, 0);
  96 
  97         /*
  98          * Suspend test task
  99          */
 100         printf("\nSuspend test task.\n");
 101         error = task_suspend(task);
 102         if (error)
 103                 panic("task suspend failed");
 104 
 105         /*
 106          * Sleep for a while
 107          */
 108         printf("Sleep\n");
 109         timer_sleep(500, 0);
 110 
 111         printf("\nResume test task.\n");
 112         error = task_resume(task);
 113         if (error)
 114                 panic("task resume failed");
 115 
 116         /*
 117          * Sleep for a while
 118          */
 119         timer_sleep(3000, 0);
 120 
 121         printf("\nResume task, again.\n");
 122         error = task_resume(task);
 123         if (error)
 124                 printf("Error - OK!\n");
 125 
 126         timer_sleep(1000, 0);
 127 
 128         printf("\nTerminate task.\n");
 129         task_terminate(task);
 130 
 131         printf("\nTest OK!\n");
 132         return 0;
 133 }

/* [<][>][^][v][top][bottom][index][help] */