Contiki 2.5
mt-test.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science.
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 Institute nor the names of its 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 INSTITUTE 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 INSTITUTE 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  * This file is part of the Contiki operating system.
30  *
31  * Author: Oliver Schmidt <ol.sc@web.de>
32  *
33  * $Id: mt-test.c,v 1.3 2009/12/11 14:59:45 matsutsuka Exp $
34  */
35 
36 /**
37  * \file
38  * A very simple Contiki application showing how to use the Contiki
39  * Multi-threading library
40  * \author
41  * Oliver Schmidt <ol.sc@web.de>
42  * \author for PC-6001 version
43  * Takahide Matsutsuka <markn@markn.org>
44  */
45 
46 #include <stdio.h>
47 
48 #include "contiki.h"
49 #include "ctk.h"
50 #include "sys/mt.h"
51 #include "mtarch.h"
52 
53 #define WIN_XSIZE 10
54 #define WIN_YSIZE 10
55 
56 static char log[WIN_XSIZE * WIN_YSIZE];
57 static struct ctk_window window;
58 static struct ctk_label loglabel = {CTK_LABEL(0, 0, WIN_XSIZE, WIN_YSIZE, log)};
59 PROCESS(mt_process, "Multi-threading test");
60 
61 static char buf[20];
62 
63 void
64 println(char *str1)
65 {
66  unsigned int i;
67 
68  for(i = 1; i < WIN_YSIZE; i++) {
69  memcpy(&log[(i - 1) * WIN_XSIZE], &log[i * WIN_XSIZE], WIN_XSIZE);
70  }
71  memset(&log[(WIN_YSIZE - 1) * WIN_XSIZE], 0, WIN_XSIZE);
72 
73  strncpy(&log[(WIN_YSIZE - 1) * WIN_XSIZE], str1, WIN_XSIZE);
74 
75  CTK_WIDGET_REDRAW(&loglabel);
76 }
77 /*---------------------------------------------------------------------------*/
78 static void
79 thread_func(char *str, int len)
80 {
81  println((char *) (str + len));
82  mt_yield();
83 
84  if(len) {
85  thread_func(str, len - 1);
86  mt_yield();
87  }
88 
89  println((char *) (str + len));
90 }
91 /*---------------------------------------------------------------------------*/
92 static void
93 thread_main(void *data)
94 {
95  while(1) {
96  thread_func((char *)data, 9);
97  }
98  mt_exit();
99 }
100 
101 /*---------------------------------------------------------------------------*/
102 PROCESS_THREAD(mt_process, ev, data)
103 {
104 
105  static struct etimer timer;
106  static int toggle = 0;
107  static struct mt_thread th1;
108  static struct mt_thread th2;
109 
110  PROCESS_BEGIN();
111 
112  ctk_window_new(&window, WIN_XSIZE, WIN_YSIZE, "Multithread");
113  CTK_WIDGET_ADD(&window, &loglabel);
114  memset(log, 0, sizeof(log));
115  ctk_window_open(&window);
116  mt_init();
117  mt_start(&th1, thread_main, "JIHGFEDCBA");
118  mt_start(&th2, thread_main, "9876543210");
119 
121  while(1) {
123  if(ev == PROCESS_EVENT_TIMER) {
124  if(toggle) {
125  mt_exec(&th1);
126  toggle--;
127  } else {
128  mt_exec(&th2);
129  toggle++;
130  }
132  } else if(ev == ctk_signal_window_close || ev == PROCESS_EVENT_EXIT) {
133  ctk_window_close(&window);
134  process_exit(&mt_process);
135  LOADER_UNLOAD();
136  }
137  }
138 
139  mt_stop(&th1);
140  mt_stop(&th2);
141  mt_remove();
142 
143  while(1) {
145  }
146  PROCESS_END();
147 }
148 /*---------------------------------------------------------------------------*/