mt-test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007, Swedish Institute of Computer Science.
00003  * All rights reserved. 
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions 
00007  * are met: 
00008  * 1. Redistributions of source code must retain the above copyright 
00009  *    notice, this list of conditions and the following disclaimer. 
00010  * 2. Redistributions in binary form must reproduce the above copyright 
00011  *    notice, this list of conditions and the following disclaimer in the 
00012  *    documentation and/or other materials provided with the distribution. 
00013  * 3. Neither the name of the Institute nor the names of its contributors 
00014  *    may be used to endorse or promote products derived from this software 
00015  *    without specific prior written permission. 
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00027  * SUCH DAMAGE. 
00028  *
00029  * This file is part of the Contiki operating system.
00030  * 
00031  * Author: Oliver Schmidt <ol.sc@web.de>
00032  *
00033  * $Id: mt-test.c,v 1.3 2009/12/11 14:59:45 matsutsuka Exp $
00034  */
00035 
00036 /**
00037  * \file
00038  *         A very simple Contiki application showing how to use the Contiki
00039  *         Multi-threading library
00040  * \author
00041  *         Oliver Schmidt <ol.sc@web.de>
00042  * \author for PC-6001 version
00043  *         Takahide Matsutsuka <markn@markn.org>
00044  */
00045 
00046 #include <stdio.h>
00047 
00048 #include "contiki.h"
00049 #include "ctk.h"
00050 #include "sys/mt.h"
00051 #include "mtarch.h"
00052 
00053 #define WIN_XSIZE       10
00054 #define WIN_YSIZE       10
00055 
00056 static char log[WIN_XSIZE * WIN_YSIZE];
00057 static struct ctk_window window;
00058 static struct ctk_label loglabel = {CTK_LABEL(0, 0, WIN_XSIZE, WIN_YSIZE, log)};
00059 PROCESS(mt_process, "Multi-threading test");
00060 
00061 static char buf[20];
00062 
00063 void
00064 println(char *str1)
00065 {
00066   unsigned int i;
00067   
00068   for(i = 1; i < WIN_YSIZE; i++) {
00069     memcpy(&log[(i - 1) * WIN_XSIZE], &log[i * WIN_XSIZE], WIN_XSIZE);
00070   }
00071   memset(&log[(WIN_YSIZE - 1) * WIN_XSIZE], 0, WIN_XSIZE);
00072 
00073   strncpy(&log[(WIN_YSIZE - 1) * WIN_XSIZE], str1, WIN_XSIZE);
00074 
00075   CTK_WIDGET_REDRAW(&loglabel);
00076 }
00077 /*---------------------------------------------------------------------------*/
00078 static void
00079 thread_func(char *str, int len)
00080 {
00081   println((char *) (str + len));
00082   mt_yield();
00083 
00084   if(len) {
00085     thread_func(str, len - 1);
00086     mt_yield();
00087   }
00088 
00089   println((char *) (str + len));
00090 }
00091 /*---------------------------------------------------------------------------*/
00092 static void
00093 thread_main(void *data)
00094 {
00095   while(1) {
00096     thread_func((char *)data, 9);
00097   }
00098   mt_exit();
00099 }
00100 
00101 /*---------------------------------------------------------------------------*/
00102 PROCESS_THREAD(mt_process, ev, data)
00103 {
00104 
00105   static struct etimer timer;
00106   static int toggle = 0;
00107   static struct mt_thread th1;
00108   static struct mt_thread th2;
00109 
00110   PROCESS_BEGIN();
00111 
00112   ctk_window_new(&window, WIN_XSIZE, WIN_YSIZE, "Multithread");
00113   CTK_WIDGET_ADD(&window, &loglabel);
00114   memset(log, 0, sizeof(log));
00115   ctk_window_open(&window);
00116   mt_init();
00117   mt_start(&th1, thread_main, "JIHGFEDCBA");
00118   mt_start(&th2, thread_main, "9876543210");
00119 
00120   etimer_set(&timer, CLOCK_SECOND / 2);
00121   while(1) {
00122     PROCESS_WAIT_EVENT();
00123     if(ev == PROCESS_EVENT_TIMER) {
00124       if(toggle) {
00125         mt_exec(&th1);
00126         toggle--;
00127       } else {
00128         mt_exec(&th2);
00129         toggle++;
00130       }
00131       etimer_set(&timer, CLOCK_SECOND / 2);
00132     } else if(ev == ctk_signal_window_close || ev == PROCESS_EVENT_EXIT) {
00133       ctk_window_close(&window);
00134       process_exit(&mt_process);
00135       LOADER_UNLOAD();
00136     }
00137   }
00138   
00139   mt_stop(&th1);
00140   mt_stop(&th2);
00141   mt_remove();
00142 
00143   while(1) {
00144     PROCESS_WAIT_EVENT();
00145   }
00146   PROCESS_END();
00147 }
00148 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:44 2011 for Contiki 2.5 by  doxygen 1.6.1