Files
OpenCellular/include/consumer.h
Anton Staaf 1a0235e69f Streams: Define a lower level abstraction for queue access
This adds a new pair of interfaces called producer and consumer
which are simpler (in that they don't hold onto the underlying
queue) than the stream interfaces.  This makes it easier to
share a single queue between the endpoints that will manipulate
it.  It was not possible to share a queue between two objects
that implemented the in_stream and out_stream interfaces.

This also adds a pair of adaptors that can convert a producer
or consumer into a stream of the correct type.  These adaptors
will be used for existing code once the usb-stream and usart
drivers are converted over to use the producer/consumer
interfaces instead of the stream interfaces.

Signed-off-by: Anton Staaf <robotboy@chromium.org>

BRANCH=None
BUG=None
TEST=make buildall -j

Change-Id: I42b4b1ac15cca28e1adc6d3cea315f15e17a0b4d
Reviewed-on: https://chromium-review.googlesource.com/250941
Trybot-Ready: Anton Staaf <robotboy@chromium.org>
Tested-by: Anton Staaf <robotboy@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Commit-Queue: Anton Staaf <robotboy@chromium.org>
2015-02-24 00:27:50 +00:00

82 lines
2.4 KiB
C

/* Copyright (c) 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.
*
* Consumer interface
*
* The consumer abstraction allows for code that wants to be able to read from
* a queue, and be notified of new additions to the queue, or of requests to
* flush (empty) the queue.
*/
#ifndef INCLUDE_CONSUMER_H
#define INCLUDE_CONSUMER_H
#include "queue.h"
#include <stddef.h>
#include <stdint.h>
struct consumer;
struct producer;
struct consumer_ops {
/*
* Inform the consumer that count units were written to the queue.
* This gives it the oportunity to read additional units from the queue
* or to wake up a task or interrupt to do the same. If a consumer has
* no need for this information it can set this to NULL.
*/
void (*written)(struct consumer const *consumer, size_t count);
/*
* Flush (read) everything from the associated queue. This call blocks
* until the consumer has flushed the queue.
*/
void (*flush)(struct consumer const *consumer);
};
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.
*/
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 */