diff mbox

[OpenWrt-Devel,5/5] ustream: call notify_write() when bytes are written.

Message ID 1415703092-46458-5-git-send-email-yszhou4tech@gmail.com
State Superseded
Headers show

Commit Message

Yousong Zhou Nov. 11, 2014, 10:51 a.m. UTC
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
---
 ustream.c |   20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

Comments

Felix Fietkau Dec. 11, 2014, 4:36 p.m. UTC | #1
On 2014-11-11 11:51, Yousong Zhou wrote:
> 
> Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
The idea behind the notify_write callback was to notify callers of
buffers being drained. What's the intended use case for this patch?

- Felix
Yousong Zhou Dec. 12, 2014, 3:48 a.m. UTC | #2
On 12 December 2014 at 00:36, Felix Fietkau <nbd@openwrt.org> wrote:
> On 2014-11-11 11:51, Yousong Zhou wrote:
>>
>> Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
> The idea behind the notify_write callback was to notify callers of
> buffers being drained. What's the intended use case for this patch?

Thank you, I see the design point now.  Am I right that it's only
intended for writes from the buffer?  I added noitfy_write() call for
each successful s->write() mainly for bookkeeping purposes.  This
patch can be ignored.


               yousong
diff mbox

Patch

diff --git a/ustream.c b/ustream.c
index 64cdc6a..1d0c38e 100644
--- a/ustream.c
+++ b/ustream.c
@@ -364,6 +364,17 @@  static void ustream_write_error(struct ustream *s)
 	s->write_error = true;
 }
 
+static int ustream_call_write_cb(struct ustream *s, const char *data, int len, bool more)
+{
+	int bytes;
+
+	bytes = s->write(s, data, len, more);
+	if (s->notify_write && bytes > 0)
+		s->notify_write(s, bytes);
+
+	return bytes;
+}
+
 bool ustream_write_pending(struct ustream *s)
 {
 	struct ustream_buf *buf = s->w.head;
@@ -376,7 +387,7 @@  bool ustream_write_pending(struct ustream *s)
 		struct ustream_buf *next = buf->next;
 		int maxlen = buf->tail - buf->data;
 
-		len = s->write(s, buf->data, maxlen, !!buf->next);
+		len = ustream_call_write_cb(s, buf->data, maxlen, !!buf->next);
 		if (len < 0) {
 			ustream_write_error(s);
 			break;
@@ -396,9 +407,6 @@  bool ustream_write_pending(struct ustream *s)
 		buf = next;
 	}
 
-	if (s->notify_write)
-		s->notify_write(s, wr);
-
 	if (s->eof && wr && !s->w.data_bytes)
 		ustream_state_change(s);
 
@@ -441,7 +449,7 @@  int ustream_write(struct ustream *s, const char *data, int len, bool more)
 		return 0;
 
 	if (!l->data_bytes) {
-		wr = s->write(s, data, len, more);
+		wr = ustream_call_write_cb(s, data, len, more);
 		if (wr == len)
 			return wr;
 
@@ -475,7 +483,7 @@  int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
 		maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
 		va_end(arg2);
 		if (maxlen < MAX_STACK_BUFLEN) {
-			wr = s->write(s, buf, maxlen, false);
+			wr = ustream_call_write_cb(s, buf, maxlen, false);
 			if (wr < 0) {
 				ustream_write_error(s);
 				return wr;