From f1d9702b685c8b0d07814675c25968126474e9f2 Mon Sep 17 00:00:00 2001 From: oliverschmidt Date: Tue, 3 Apr 2007 20:13:27 +0000 Subject: [PATCH] Added an example application to show the usage of the Contiki Multi-threading library. --- examples/multi-threading/Makefile | 4 + examples/multi-threading/multi-threading.c | 132 +++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 examples/multi-threading/Makefile create mode 100644 examples/multi-threading/multi-threading.c diff --git a/examples/multi-threading/Makefile b/examples/multi-threading/Makefile new file mode 100644 index 000000000..e645bc725 --- /dev/null +++ b/examples/multi-threading/Makefile @@ -0,0 +1,4 @@ +CONTIKI = ../.. +all: multi-threading + +include $(CONTIKI)/Makefile.include diff --git a/examples/multi-threading/multi-threading.c b/examples/multi-threading/multi-threading.c new file mode 100644 index 000000000..b97f09c6d --- /dev/null +++ b/examples/multi-threading/multi-threading.c @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2007, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * Author: Oliver Schmidt + * + * $Id: multi-threading.c,v 1.1 2007/04/03 20:13:28 oliverschmidt Exp $ + */ + +/** + * \file + * A very simple Contiki application showing how to use the Contiki + * Multi-threading library + * \author + * Oliver Schmidt + */ + +#ifdef __CYGWIN__ + +#define WIN32_LEAN_AND_MEAN +#include + +/* The Contiki Multi-threading library is on Windows implemented using + * Win32 fibers. The Cygwin C-library implements Thread-Local-Storage + * in a way that is incompatible with fibers. Therefore most Cygwin + * C-library functions don't work when called from a Contiki thread. + * + * So the sample application calls the Win32 function wsprintf instead + * of the Cygwin function sprintf from its Contiki threads. */ +#define sprintf wsprintf + +#endif /* __CYGWIN__ */ + +#include + +#include "contiki.h" +#include "sys/mt.h" + +static char buf[10]; + +PROCESS(multi_threading_process, "Multi-threading process"); +AUTOSTART_PROCESSES(&multi_threading_process); + +/*---------------------------------------------------------------------------*/ +static void +thread_func(char *str, int len) +{ + sprintf(buf, "%s\n", str + len); + mt_yield(); + + if (len) { + thread_func(str, len - 1); + mt_yield(); + } + + sprintf(buf, "%s\n", str + len); +} +/*---------------------------------------------------------------------------*/ +static void +thread_main(void *data) +{ + while(1) { + thread_func((char *)data, 8); + } + mt_exit(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(multi_threading_process, ev, data) +{ + static struct mt_thread alpha_thread; + static struct mt_thread count_thread; + + static struct etimer timer; + static int toggle; + + PROCESS_BEGIN(); + + mt_init(); + mt_start(&alpha_thread, thread_main, "IHGFEDCBA"); + mt_start(&count_thread, thread_main, "987654321"); + + etimer_set(&timer, CLOCK_SECOND / 2); + + while(1) { + PROCESS_WAIT_EVENT(); + if(ev == PROCESS_EVENT_TIMER) { + if(toggle) { + mt_exec(&alpha_thread); + toggle--; + } else { + mt_exec(&count_thread); + toggle++; + } + printf(buf); + + etimer_set(&timer, CLOCK_SECOND / 2); + } + } + + mt_stop(&alpha_thread); + mt_stop(&count_thread); + mt_remove(); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/