Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/sample/sem/sem.c

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

DEFINITIONS

This source file includes following definitions.
  1. thread_run
  2. new_thread
  3. main

   1 /*
   2  * Copyright (c) 2005, 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  * sem.c - sample program for semaphore.
  32  */
  33 
  34 #include <sys/prex.h>
  35 #include <stdio.h>
  36 
  37 static sem_t sem;
  38 
  39 void
  40 thread_run(void (*start)(void), void *stack)
  41 {
  42         thread_t t;
  43 
  44         if (thread_create(task_self(), &t) != 0)
  45                 panic("thread_create is failed");
  46 
  47         if (thread_load(t, start, stack) != 0)
  48                 panic("thread_load is failed");
  49 
  50         if (thread_resume(t) != 0)
  51                 panic("thread_resume failed");
  52 
  53         return;
  54 }
  55 
  56 /*
  57  * The main routine create 10 copy of this thread. But, since the
  58  * initial semaphore value is 3, only 3 threads can run at same time.
  59  */
  60 static void
  61 new_thread(void)
  62 {
  63         thread_t t;
  64 
  65         t = thread_self();
  66         printf("Start thread=%x\n", (u_int)t);
  67         thread_yield();
  68 
  69         /*
  70          * Acquire semaphore
  71          */
  72         sem_wait(&sem, 0);
  73 
  74         /*
  75          * Sleep 2000 ms
  76          */
  77         printf("Running thread=%x\n", (u_int)t);
  78         timer_sleep(2000, 0);
  79 
  80         /*
  81          * Release semaphore
  82          */
  83         sem_post(&sem); 
  84 
  85         printf("End thread=%x\n", (u_int)t);
  86         thread_terminate(t);
  87 }
  88 
  89 int
  90 main(int argc, char *argv[])
  91 {
  92         static char stack[10][1024];
  93         int i;
  94 
  95         printf("Semaphore sample program\n");
  96 
  97         /*
  98          * Initialize semaphore with initial count 3
  99          */
 100         sem_init(&sem, 3);
 101 
 102         /*
 103          * Boost the priority of this thread
 104          */
 105         thread_setpri(thread_self(), 100);
 106 
 107         /*
 108          * Create 10 threads
 109          */
 110         for (i = 0; i < 10; i++)
 111                 thread_run(new_thread, stack[i]+1024);
 112 
 113         /*
 114          * Wait...
 115          */
 116         thread_suspend(thread_self());
 117 
 118         return 0;
 119 }

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