B.3 : THE 6 HOUR PROGRAM WITH 1 PPS SIGNAL THE MASTER TO BE CONNECTED TO 11 PIN.

Dependencies:   mbed

Committer:
manujose
Date:
Tue Dec 14 23:26:02 2010 +0000
Revision:
1:9efda6a3f83c
Parent:
0:e6964ff1bbcc
hello

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manujose 0:e6964ff1bbcc 1 /*
manujose 0:e6964ff1bbcc 2 * Copyright (c) 2004, Swedish Institute of Computer Science.
manujose 0:e6964ff1bbcc 3 * All rights reserved.
manujose 0:e6964ff1bbcc 4 *
manujose 0:e6964ff1bbcc 5 * Redistribution and use in source and binary forms, with or without
manujose 0:e6964ff1bbcc 6 * modification, are permitted provided that the following conditions
manujose 0:e6964ff1bbcc 7 * are met:
manujose 0:e6964ff1bbcc 8 * 1. Redistributions of source code must retain the above copyright
manujose 0:e6964ff1bbcc 9 * notice, this list of conditions and the following disclaimer.
manujose 0:e6964ff1bbcc 10 * 2. Redistributions in binary form must reproduce the above copyright
manujose 0:e6964ff1bbcc 11 * notice, this list of conditions and the following disclaimer in the
manujose 0:e6964ff1bbcc 12 * documentation and/or other materials provided with the distribution.
manujose 0:e6964ff1bbcc 13 * 3. Neither the name of the Institute nor the names of its contributors
manujose 0:e6964ff1bbcc 14 * may be used to endorse or promote products derived from this software
manujose 0:e6964ff1bbcc 15 * without specific prior written permission.
manujose 0:e6964ff1bbcc 16 *
manujose 0:e6964ff1bbcc 17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
manujose 0:e6964ff1bbcc 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
manujose 0:e6964ff1bbcc 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
manujose 0:e6964ff1bbcc 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
manujose 0:e6964ff1bbcc 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
manujose 0:e6964ff1bbcc 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
manujose 0:e6964ff1bbcc 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
manujose 0:e6964ff1bbcc 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
manujose 0:e6964ff1bbcc 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
manujose 0:e6964ff1bbcc 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
manujose 0:e6964ff1bbcc 27 * SUCH DAMAGE.
manujose 0:e6964ff1bbcc 28 *
manujose 0:e6964ff1bbcc 29 * This file is part of the protothreads library.
manujose 0:e6964ff1bbcc 30 *
manujose 0:e6964ff1bbcc 31 * Author: Adam Dunkels <adam@sics.se>
manujose 0:e6964ff1bbcc 32 *
manujose 0:e6964ff1bbcc 33 * $Id: pt-sem.h,v 1.2 2005/02/24 10:36:59 adam Exp $
manujose 0:e6964ff1bbcc 34 */
manujose 0:e6964ff1bbcc 35
manujose 0:e6964ff1bbcc 36 /**
manujose 0:e6964ff1bbcc 37 * \addtogroup pt
manujose 0:e6964ff1bbcc 38 * @{
manujose 0:e6964ff1bbcc 39 */
manujose 0:e6964ff1bbcc 40
manujose 0:e6964ff1bbcc 41 /**
manujose 0:e6964ff1bbcc 42 * \defgroup ptsem Protothread semaphores
manujose 0:e6964ff1bbcc 43 * @{
manujose 0:e6964ff1bbcc 44 *
manujose 0:e6964ff1bbcc 45 * This module implements counting semaphores on top of
manujose 0:e6964ff1bbcc 46 * protothreads. Semaphores are a synchronization primitive that
manujose 0:e6964ff1bbcc 47 * provide two operations: "wait" and "signal". The "wait" operation
manujose 0:e6964ff1bbcc 48 * checks the semaphore counter and blocks the thread if the counter
manujose 0:e6964ff1bbcc 49 * is zero. The "signal" operation increases the semaphore counter but
manujose 0:e6964ff1bbcc 50 * does not block. If another thread has blocked waiting for the
manujose 0:e6964ff1bbcc 51 * semaphore that is signalled, the blocked thread will become
manujose 0:e6964ff1bbcc 52 * runnable again.
manujose 0:e6964ff1bbcc 53 *
manujose 0:e6964ff1bbcc 54 * Semaphores can be used to implement other, more structured,
manujose 0:e6964ff1bbcc 55 * synchronization primitives such as monitors and message
manujose 0:e6964ff1bbcc 56 * queues/bounded buffers (see below).
manujose 0:e6964ff1bbcc 57 *
manujose 0:e6964ff1bbcc 58 * The following example shows how the producer-consumer problem, also
manujose 0:e6964ff1bbcc 59 * known as the bounded buffer problem, can be solved using
manujose 0:e6964ff1bbcc 60 * protothreads and semaphores. Notes on the program follow after the
manujose 0:e6964ff1bbcc 61 * example.
manujose 0:e6964ff1bbcc 62 *
manujose 0:e6964ff1bbcc 63 \code
manujose 0:e6964ff1bbcc 64 #include "pt-sem.h"
manujose 0:e6964ff1bbcc 65
manujose 0:e6964ff1bbcc 66 #define NUM_ITEMS 32
manujose 0:e6964ff1bbcc 67 #define BUFSIZE 8
manujose 0:e6964ff1bbcc 68
manujose 0:e6964ff1bbcc 69 static struct pt_sem mutex, full, empty;
manujose 0:e6964ff1bbcc 70
manujose 0:e6964ff1bbcc 71 PT_THREAD(producer(struct pt *pt))
manujose 0:e6964ff1bbcc 72 {
manujose 0:e6964ff1bbcc 73 static int produced;
manujose 0:e6964ff1bbcc 74
manujose 0:e6964ff1bbcc 75 PT_BEGIN(pt);
manujose 0:e6964ff1bbcc 76
manujose 0:e6964ff1bbcc 77 for(produced = 0; produced < NUM_ITEMS; ++produced) {
manujose 0:e6964ff1bbcc 78
manujose 0:e6964ff1bbcc 79 PT_SEM_WAIT(pt, &full);
manujose 0:e6964ff1bbcc 80
manujose 0:e6964ff1bbcc 81 PT_SEM_WAIT(pt, &mutex);
manujose 0:e6964ff1bbcc 82 add_to_buffer(produce_item());
manujose 0:e6964ff1bbcc 83 PT_SEM_SIGNAL(pt, &mutex);
manujose 0:e6964ff1bbcc 84
manujose 0:e6964ff1bbcc 85 PT_SEM_SIGNAL(pt, &empty);
manujose 0:e6964ff1bbcc 86 }
manujose 0:e6964ff1bbcc 87
manujose 0:e6964ff1bbcc 88 PT_END(pt);
manujose 0:e6964ff1bbcc 89 }
manujose 0:e6964ff1bbcc 90
manujose 0:e6964ff1bbcc 91 PT_THREAD(consumer(struct pt *pt))
manujose 0:e6964ff1bbcc 92 {
manujose 0:e6964ff1bbcc 93 static int consumed;
manujose 0:e6964ff1bbcc 94
manujose 0:e6964ff1bbcc 95 PT_BEGIN(pt);
manujose 0:e6964ff1bbcc 96
manujose 0:e6964ff1bbcc 97 for(consumed = 0; consumed < NUM_ITEMS; ++consumed) {
manujose 0:e6964ff1bbcc 98
manujose 0:e6964ff1bbcc 99 PT_SEM_WAIT(pt, &empty);
manujose 0:e6964ff1bbcc 100
manujose 0:e6964ff1bbcc 101 PT_SEM_WAIT(pt, &mutex);
manujose 0:e6964ff1bbcc 102 consume_item(get_from_buffer());
manujose 0:e6964ff1bbcc 103 PT_SEM_SIGNAL(pt, &mutex);
manujose 0:e6964ff1bbcc 104
manujose 0:e6964ff1bbcc 105 PT_SEM_SIGNAL(pt, &full);
manujose 0:e6964ff1bbcc 106 }
manujose 0:e6964ff1bbcc 107
manujose 0:e6964ff1bbcc 108 PT_END(pt);
manujose 0:e6964ff1bbcc 109 }
manujose 0:e6964ff1bbcc 110
manujose 0:e6964ff1bbcc 111 PT_THREAD(driver_thread(struct pt *pt))
manujose 0:e6964ff1bbcc 112 {
manujose 0:e6964ff1bbcc 113 static struct pt pt_producer, pt_consumer;
manujose 0:e6964ff1bbcc 114
manujose 0:e6964ff1bbcc 115 PT_BEGIN(pt);
manujose 0:e6964ff1bbcc 116
manujose 0:e6964ff1bbcc 117 PT_SEM_INIT(&empty, 0);
manujose 0:e6964ff1bbcc 118 PT_SEM_INIT(&full, BUFSIZE);
manujose 0:e6964ff1bbcc 119 PT_SEM_INIT(&mutex, 1);
manujose 0:e6964ff1bbcc 120
manujose 0:e6964ff1bbcc 121 PT_INIT(&pt_producer);
manujose 0:e6964ff1bbcc 122 PT_INIT(&pt_consumer);
manujose 0:e6964ff1bbcc 123
manujose 0:e6964ff1bbcc 124 PT_WAIT_THREAD(pt, producer(&pt_producer) &
manujose 0:e6964ff1bbcc 125 consumer(&pt_consumer));
manujose 0:e6964ff1bbcc 126
manujose 0:e6964ff1bbcc 127 PT_END(pt);
manujose 0:e6964ff1bbcc 128 }
manujose 0:e6964ff1bbcc 129 \endcode
manujose 0:e6964ff1bbcc 130 *
manujose 0:e6964ff1bbcc 131 * The program uses three protothreads: one protothread that
manujose 0:e6964ff1bbcc 132 * implements the consumer, one thread that implements the producer,
manujose 0:e6964ff1bbcc 133 * and one protothread that drives the two other protothreads. The
manujose 0:e6964ff1bbcc 134 * program uses three semaphores: "full", "empty" and "mutex". The
manujose 0:e6964ff1bbcc 135 * "mutex" semaphore is used to provide mutual exclusion for the
manujose 0:e6964ff1bbcc 136 * buffer, the "empty" semaphore is used to block the consumer is the
manujose 0:e6964ff1bbcc 137 * buffer is empty, and the "full" semaphore is used to block the
manujose 0:e6964ff1bbcc 138 * producer is the buffer is full.
manujose 0:e6964ff1bbcc 139 *
manujose 0:e6964ff1bbcc 140 * The "driver_thread" holds two protothread state variables,
manujose 0:e6964ff1bbcc 141 * "pt_producer" and "pt_consumer". It is important to note that both
manujose 0:e6964ff1bbcc 142 * these variables are declared as <i>static</i>. If the static
manujose 0:e6964ff1bbcc 143 * keyword is not used, both variables are stored on the stack. Since
manujose 0:e6964ff1bbcc 144 * protothreads do not store the stack, these variables may be
manujose 0:e6964ff1bbcc 145 * overwritten during a protothread wait operation. Similarly, both
manujose 0:e6964ff1bbcc 146 * the "consumer" and "producer" protothreads declare their local
manujose 0:e6964ff1bbcc 147 * variables as static, to avoid them being stored on the stack.
manujose 0:e6964ff1bbcc 148 *
manujose 0:e6964ff1bbcc 149 *
manujose 0:e6964ff1bbcc 150 */
manujose 0:e6964ff1bbcc 151
manujose 0:e6964ff1bbcc 152 /**
manujose 0:e6964ff1bbcc 153 * \file
manujose 0:e6964ff1bbcc 154 * Couting semaphores implemented on protothreads
manujose 0:e6964ff1bbcc 155 * \author
manujose 0:e6964ff1bbcc 156 * Adam Dunkels <adam@sics.se>
manujose 0:e6964ff1bbcc 157 *
manujose 0:e6964ff1bbcc 158 */
manujose 0:e6964ff1bbcc 159
manujose 0:e6964ff1bbcc 160 #ifndef __PT_SEM_H__
manujose 0:e6964ff1bbcc 161 #define __PT_SEM_H__
manujose 0:e6964ff1bbcc 162
manujose 0:e6964ff1bbcc 163 #include "pt.h"
manujose 0:e6964ff1bbcc 164
manujose 0:e6964ff1bbcc 165 struct pt_sem {
manujose 0:e6964ff1bbcc 166 unsigned int count;
manujose 0:e6964ff1bbcc 167 };
manujose 0:e6964ff1bbcc 168
manujose 0:e6964ff1bbcc 169 /**
manujose 0:e6964ff1bbcc 170 * Initialize a semaphore
manujose 0:e6964ff1bbcc 171 *
manujose 0:e6964ff1bbcc 172 * This macro initializes a semaphore with a value for the
manujose 0:e6964ff1bbcc 173 * counter. Internally, the semaphores use an "unsigned int" to
manujose 0:e6964ff1bbcc 174 * represent the counter, and therefore the "count" argument should be
manujose 0:e6964ff1bbcc 175 * within range of an unsigned int.
manujose 0:e6964ff1bbcc 176 *
manujose 0:e6964ff1bbcc 177 * \param s (struct pt_sem *) A pointer to the pt_sem struct
manujose 0:e6964ff1bbcc 178 * representing the semaphore
manujose 0:e6964ff1bbcc 179 *
manujose 0:e6964ff1bbcc 180 * \param c (unsigned int) The initial count of the semaphore.
manujose 0:e6964ff1bbcc 181 * \hideinitializer
manujose 0:e6964ff1bbcc 182 */
manujose 0:e6964ff1bbcc 183 #define PT_SEM_INIT(s, c) (s)->count = c
manujose 0:e6964ff1bbcc 184
manujose 0:e6964ff1bbcc 185 /**
manujose 0:e6964ff1bbcc 186 * Wait for a semaphore
manujose 0:e6964ff1bbcc 187 *
manujose 0:e6964ff1bbcc 188 * This macro carries out the "wait" operation on the semaphore. The
manujose 0:e6964ff1bbcc 189 * wait operation causes the protothread to block while the counter is
manujose 0:e6964ff1bbcc 190 * zero. When the counter reaches a value larger than zero, the
manujose 0:e6964ff1bbcc 191 * protothread will continue.
manujose 0:e6964ff1bbcc 192 *
manujose 0:e6964ff1bbcc 193 * \param pt (struct pt *) A pointer to the protothread (struct pt) in
manujose 0:e6964ff1bbcc 194 * which the operation is executed.
manujose 0:e6964ff1bbcc 195 *
manujose 0:e6964ff1bbcc 196 * \param s (struct pt_sem *) A pointer to the pt_sem struct
manujose 0:e6964ff1bbcc 197 * representing the semaphore
manujose 0:e6964ff1bbcc 198 *
manujose 0:e6964ff1bbcc 199 * \hideinitializer
manujose 0:e6964ff1bbcc 200 */
manujose 0:e6964ff1bbcc 201 #define PT_SEM_WAIT(pt, s) \
manujose 0:e6964ff1bbcc 202 do { \
manujose 0:e6964ff1bbcc 203 PT_WAIT_UNTIL(pt, (s)->count > 0); \
manujose 0:e6964ff1bbcc 204 --(s)->count; \
manujose 0:e6964ff1bbcc 205 } while(0)
manujose 0:e6964ff1bbcc 206
manujose 0:e6964ff1bbcc 207 /**
manujose 0:e6964ff1bbcc 208 * Signal a semaphore
manujose 0:e6964ff1bbcc 209 *
manujose 0:e6964ff1bbcc 210 * This macro carries out the "signal" operation on the semaphore. The
manujose 0:e6964ff1bbcc 211 * signal operation increments the counter inside the semaphore, which
manujose 0:e6964ff1bbcc 212 * eventually will cause waiting protothreads to continue executing.
manujose 0:e6964ff1bbcc 213 *
manujose 0:e6964ff1bbcc 214 * \param pt (struct pt *) A pointer to the protothread (struct pt) in
manujose 0:e6964ff1bbcc 215 * which the operation is executed.
manujose 0:e6964ff1bbcc 216 *
manujose 0:e6964ff1bbcc 217 * \param s (struct pt_sem *) A pointer to the pt_sem struct
manujose 0:e6964ff1bbcc 218 * representing the semaphore
manujose 0:e6964ff1bbcc 219 *
manujose 0:e6964ff1bbcc 220 * \hideinitializer
manujose 0:e6964ff1bbcc 221 */
manujose 0:e6964ff1bbcc 222 #define PT_SEM_SIGNAL(pt, s) ++(s)->count
manujose 0:e6964ff1bbcc 223
manujose 0:e6964ff1bbcc 224 #endif /* __PT_SEM_H__ */
manujose 0:e6964ff1bbcc 225
manujose 0:e6964ff1bbcc 226 /** @} */
manujose 0:e6964ff1bbcc 227 /** @} */
manujose 0:e6964ff1bbcc 228