summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAnton Staaf <robotboy@chromium.org>2015-05-13 13:26:12 -0700
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2015-05-26 19:36:21 +0000
commit855646e36bebce29b929178cfb3c73b8f259c8b6 (patch)
treec0fcdc616da4ade170502a0cb842d70d1c44821b /include
parenta0ebf0a008670ece41529a2fce01882192331c6e (diff)
downloadchrome-ec-855646e36bebce29b929178cfb3c73b8f259c8b6.tar.gz
Producer/Consumer: Refactor to use Queue policies
Previously the Producer and Consumer interfaces tracked the Consumer and Producer respectively at the other end of the queue that they interacted with. This was done to avoid modifying the queue implementation, but resulted in a rougher interface that required additional initialization steps and prevented alternative configurations; many producers and one consumer for example. This commit uses the new queue policies to track this information. The new direct policy behaves as the old producer and consumers did. Now the producers and consumers are just named references to the queue that they work on and a convenient location for a notification callback when the queue is updated in a way that is relevent to the producer or consumer. All users of Producer and Consumer have been updated including the stream adaptors which are in use by the echo test code and the mcdp28x0 driver. Use of the stream adaptors has also been simplified. Signed-off-by: Anton Staaf <robotboy@chromium.org> BRANCH=None BUG=None TEST=make buildall -j Manual testing of Ryu (P5) and discovery board echo task Change-Id: I704be6378a31b4e20f5063295eff9943e4900409 Reviewed-on: https://chromium-review.googlesource.com/271792 Reviewed-by: Anton Staaf <robotboy@chromium.org> Commit-Queue: Anton Staaf <robotboy@chromium.org> Trybot-Ready: Anton Staaf <robotboy@chromium.org> Tested-by: Anton Staaf <robotboy@chromium.org>
Diffstat (limited to 'include')
-rw-r--r--include/consumer.h38
-rw-r--r--include/producer.h38
-rw-r--r--include/queue_policies.h43
-rw-r--r--include/stream_adaptor.h74
4 files changed, 98 insertions, 95 deletions
diff --git a/include/consumer.h b/include/consumer.h
index 4a456de512..d53d42a461 100644
--- a/include/consumer.h
+++ b/include/consumer.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015 The Chromium OS Authors. All rights reserved.
+/* Copyright 2015 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
@@ -37,45 +37,11 @@ struct consumer_ops {
struct consumer {
/*
- * A consumer references the producer at the other end of the queue.
- * This allows the consumer to notify the producer when units are
- * removed from the queue.
- */
- struct producer const *producer;
-
- /*
- * A consumer also references the queue that it is reading from. This
- * and the producer reference above could be more flexibly replaced by
- * a queue manager object that could handle multiple producer/consumers
- * or alternate notification mechanisms. But that complexity is not
- * yet warranted.
+ * A consumer references the queue that it is reading from.
*/
struct queue const *queue;
struct consumer_ops const *ops;
};
-/*
- * Notify the consumer by calling its written method directly, as opposed to
- * from a deferred callback or another task.
- */
-void consumer_notify_directly(struct consumer const *consumer, size_t count);
-
-/*
- * Read a single unit from the queue and notify the associated producer.
- * Return the number of units read.
- */
-size_t consumer_read_unit(struct consumer const *consumer, void *unit);
-
-/*
- * Read multiple units from the queue, using the provided memcpy like routine
- * and notify the producer. Return the number of units read.
- */
-size_t consumer_read_memcpy(struct consumer const *consumer,
- void *units,
- size_t count,
- void *(*memcpy)(void *dest,
- void const *src,
- size_t n));
-
#endif /* INCLUDE_CONSUMER_H */
diff --git a/include/producer.h b/include/producer.h
index d42e148ea6..d120fbc145 100644
--- a/include/producer.h
+++ b/include/producer.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015 The Chromium OS Authors. All rights reserved.
+/* Copyright 2015 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
@@ -31,45 +31,11 @@ struct producer_ops {
struct producer {
/*
- * A producer references the consumer at the other end of the queue.
- * This allows the producer to notify the consumer when new units are
- * added to the queue.
- */
- struct consumer const *consumer;
-
- /*
- * A producer also references the queue that it is writing into. This
- * and the consumer reference above could be more flexibly replaced by
- * a queue manager object that could handle multiple producer/consumers
- * or alternate notification mechanisms. But that complexity is not
- * yet warranted.
+ * A producer references the queue that it is writing into.
*/
struct queue const *queue;
struct producer_ops const *ops;
};
-/*
- * Notify the producer by calling its read method directly, as opposed to from
- * a deferred callback or another task.
- */
-void producer_notify_directly(struct producer const *producer, size_t count);
-
-/*
- * Write a single unit to the queue and notify the associated consumer. Return
- * the number of units written.
- */
-size_t producer_write_unit(struct producer const *producer, void const *unit);
-
-/*
- * Write multiple units to the queue, using the provided memcpy like routine
- * and notify the consumer. Return the number of units written.
- */
-size_t producer_write_memcpy(struct producer const *producer,
- void const *units,
- size_t count,
- void *(*memcpy)(void *dest,
- void const *src,
- size_t n));
-
#endif /* INCLUDE_PRODUCER_H */
diff --git a/include/queue_policies.h b/include/queue_policies.h
new file mode 100644
index 0000000000..ec6ba1a0ec
--- /dev/null
+++ b/include/queue_policies.h
@@ -0,0 +1,43 @@
+/* Copyright 2015 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Queue policies.
+ */
+#ifndef INCLUDE_QUEUE_POLICIES_H
+#define INCLUDE_QUEUE_POLICIES_H
+
+#include "queue.h"
+#include "consumer.h"
+#include "producer.h"
+
+/*
+ * The direct notification policy manages a 1-to-1 producer consumer model.
+ * When new units are added to the queue the consumer is notified directly, in
+ * whatever context (interrupt, deferred, task...) that the queue addition
+ * happened. Similarly, queue removals directly notify the producer.
+ */
+struct queue_policy_direct {
+ struct queue_policy policy;
+
+ struct producer const *producer;
+ struct consumer const *consumer;
+};
+
+void queue_add_direct(struct queue_policy const *policy, size_t count);
+void queue_remove_direct(struct queue_policy const *policy, size_t count);
+
+#define QUEUE_POLICY_DIRECT(PRODUCER, CONSUMER) \
+ ((struct queue_policy_direct const) { \
+ .policy = { \
+ .add = queue_add_direct, \
+ .remove = queue_remove_direct, \
+ }, \
+ .producer = &PRODUCER, \
+ .consumer = &CONSUMER, \
+ })
+
+#define QUEUE_DIRECT(SIZE, TYPE, PRODUCER, CONSUMER) \
+ QUEUE(SIZE, TYPE, QUEUE_POLICY_DIRECT(PRODUCER, CONSUMER).policy)
+
+#endif /* INCLUDE_QUEUE_POLICIES_H */
diff --git a/include/stream_adaptor.h b/include/stream_adaptor.h
index 916ab6e78d..93ce923f40 100644
--- a/include/stream_adaptor.h
+++ b/include/stream_adaptor.h
@@ -1,27 +1,27 @@
-/* Copyright (c) 2015 The Chromium OS Authors. All rights reserved.
+/* Copyright 2015 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef INCLUDE_STREAM_ADAPTOR_H
#define INCLUDE_STREAM_ADAPTOR_H
-/* STM32 USART driver for Chrome EC */
-
#include "common.h"
#include "in_stream.h"
#include "out_stream.h"
#include "consumer.h"
#include "producer.h"
+#include "queue.h"
+#include "queue_policies.h"
/*
* +..........+ +..........+------+...........+
* . .<------------->. | | .
- * . Producer . +---------+ . Consumer | ISFP | In Stream .
+ * . Producer . +---------+ . Consumer | ISFQ | In Stream .
* . .->| Queue |->. | | .
* +..........+ +---------+ +..........+------+...........+
*/
-struct in_stream_from_producer {
+struct in_stream_from_queue {
struct consumer consumer;
struct in_stream in;
};
@@ -29,31 +29,30 @@ struct in_stream_from_producer {
/*
*
*/
-extern struct in_stream_ops const in_stream_from_producer_in_stream_ops;
-extern struct consumer_ops const in_stream_from_producer_consumer_ops;
+extern struct in_stream_ops const in_stream_from_queue_in_stream_ops;
+extern struct consumer_ops const in_stream_from_queue_consumer_ops;
-#define IN_STREAM_FROM_PRODUCER(NAME, PRODUCER, QUEUE, READY) \
- struct in_stream_from_producer const NAME = { \
+#define IN_STREAM_FROM_QUEUE(NAME, QUEUE, READY) \
+ struct in_stream_from_queue const NAME = { \
.consumer = { \
- .producer = &PRODUCER, \
- .queue = &QUEUE, \
- .ops = &in_stream_from_producer_consumer_ops, \
+ .queue = &QUEUE, \
+ .ops = &in_stream_from_queue_consumer_ops, \
}, \
.in = { \
.ready = READY, \
- .ops = &in_stream_from_producer_in_stream_ops, \
+ .ops = &in_stream_from_queue_in_stream_ops, \
}, \
};
/*
* +..........+ +..........+------+............+
* . .<------------->. | | .
- * . Consumer . +---------+ . Producer | OSFC | Out Stream .
+ * . Consumer . +---------+ . Producer | OSFQ | Out Stream .
* . .<-| Queue |<-. | | .
* +..........+ +---------+ +..........+------+............+
*/
-struct out_stream_from_consumer {
+struct out_stream_from_queue {
struct producer producer;
struct out_stream out;
};
@@ -61,20 +60,49 @@ struct out_stream_from_consumer {
/*
*
*/
-extern struct out_stream_ops const out_stream_from_consumer_out_stream_ops;
-extern struct producer_ops const out_stream_from_consumer_producer_ops;
+extern struct out_stream_ops const out_stream_from_queue_out_stream_ops;
+extern struct producer_ops const out_stream_from_queue_producer_ops;
-#define OUT_STREAM_FROM_CONSUMER(NAME, CONSUMER, QUEUE, READY) \
- struct out_stream_from_consumer const NAME = { \
+#define OUT_STREAM_FROM_QUEUE(NAME, QUEUE, READY) \
+ struct out_stream_from_queue const NAME = { \
.producer = { \
- .consumer = &CONSUMER, \
- .queue = &QUEUE, \
- .ops = &out_stream_from_consumer_producer_ops, \
+ .queue = &QUEUE, \
+ .ops = &out_stream_from_queue_producer_ops, \
}, \
.out = { \
.ready = READY, \
- .ops = &out_stream_from_consumer_out_stream_ops, \
+ .ops = &out_stream_from_queue_out_stream_ops, \
}, \
};
+/*
+ * Given a forward declared device configuration called NAME that implements
+ * producer and consumer interfaces construct RX/TX queues and expose them as
+ * streams called <NAME>_in and <NAME>_out.
+ */
+#define IO_STREAM_CONFIG(NAME, RX_SIZE, TX_SIZE, IN_READY, OUT_READY) \
+ \
+ struct in_stream_from_queue const CONCAT2(NAME, _in); \
+ \
+ struct queue const CONCAT2(NAME, _rx_queue) = \
+ QUEUE_DIRECT(RX_SIZE, \
+ uint8_t, \
+ NAME.producer, \
+ CONCAT2(NAME, _in).consumer); \
+ IN_STREAM_FROM_QUEUE(CONCAT2(NAME, _in), \
+ CONCAT2(NAME, _rx_queue), \
+ IN_READY) \
+ \
+ \
+ struct out_stream_from_queue const CONCAT2(NAME, _out); \
+ \
+ struct queue const CONCAT2(NAME, _tx_queue) = \
+ QUEUE_DIRECT(TX_SIZE, \
+ uint8_t, \
+ CONCAT2(NAME, _out).producer, \
+ NAME.consumer); \
+ OUT_STREAM_FROM_QUEUE(CONCAT2(NAME, _out), \
+ CONCAT2(NAME, _tx_queue), \
+ OUT_READY)
+
#endif /* INCLUDE_STREAM_ADAPTOR_H */