[Cbe-oss-dev] [PATCH 3/3] libspe2: Add behavior flag implementation to spe_out_intr_mbox_read

Kazunori Asayama asayama at sm.sony.co.jp
Fri Apr 13 14:23:16 EST 2007


The patch below adds missing "behavior flag" implementation to
spe_out_intr_mbox_read API in libspe2.

Signed-off-by: Kazunori Asayama <asayama at sm.sony.co.jp>

Index: libspe2-public/spebase/create.c
===================================================================
--- libspe2-public.orig/spebase/create.c
+++ libspe2-public/spebase/create.c
@@ -43,7 +43,7 @@ struct fd_attr
 static const struct fd_attr spe_fd_attr[NUM_MBOX_FDS] = {
 	[FD_MBOX]	= { .name = "mbox",      .mode = O_RDONLY, },
 	[FD_MBOX_STAT]	= { .name = "mbox_stat", .mode = O_RDONLY, },
-	[FD_IBOX]	= { .name = "ibox",      .mode = O_RDONLY, },
+	[FD_IBOX]	= { .name = "ibox",      .mode = O_RDONLY | O_NONBLOCK, },
 	[FD_IBOX_STAT]	= { .name = "ibox_stat", .mode = O_RDONLY, },
 	[FD_WBOX]	= { .name = "wbox",      .mode = O_WRONLY | O_NONBLOCK, },
 	[FD_WBOX_STAT]	= { .name = "wbox_stat", .mode = O_RDONLY, },
Index: libspe2-public/spebase/mbox.c
===================================================================
--- libspe2-public.orig/spebase/mbox.c
+++ libspe2-public/spebase/mbox.c
@@ -97,6 +97,70 @@ static int write_all(int fd, const void 
 }
 
 
+static int read_immediate(int fd, void *buf, size_t count)
+{
+	size_t total = 0;
+
+	while (total < count) {
+		/* Non-blocking mode is asssumed. */
+		int rc = read(fd, (char *)buf + total, count - total);
+		if (rc == -1) {
+			if (errno == EAGAIN) {
+				break;
+			}
+			else {
+				return -1;
+			}
+		}
+		else if (rc == 0) {
+			break;
+		}
+		total += rc;
+	}
+
+	return total;
+}
+
+static int read_any(int fd, void *buf, size_t count)
+{
+	while (count > 0) {
+		int rc;
+		fd_set fds;
+
+		FD_ZERO(&fds);
+		FD_SET(fd, &fds);
+
+		rc = select(fd + 1, &fds, NULL, NULL, NULL);
+		if (rc == -1) {
+			return -1;
+		}
+		else if (rc > 0) {
+			rc = read_immediate(fd, buf, count);
+			if (rc) return rc;
+		}
+	}
+
+	return 0;
+}
+
+static int read_all(int fd, void *buf, size_t count)
+{
+	size_t total = 0;
+
+	while (total < count) {
+		int rc = read_any(fd, (char *)buf + total, count - total);
+		if (rc == -1) {
+			return -1;
+		}
+		else if (rc == 0) {
+			break;
+		}
+		total += rc;
+	}
+
+	return total;
+}
+
 int _base_spe_out_mbox_read(spe_context_ptr_t spectx, 
                         unsigned int mbox_data[], 
                         int count)
@@ -200,11 +264,30 @@ int _base_spe_out_intr_mbox_read(spe_con
 		return -1;
 	}
 
-	rc = read(open_if_closed(spectx,FD_IBOX, 0), mbox_data, count*4);
-	if (rc != -1)
-		rc /= 4;
+	switch (behavior_flag) {
+	case SPE_MBOX_ALL_BLOCKING: // read all, even if blocking
+		rc = read_all(open_if_closed(spectx,FD_IBOX, 0), mbox_data, 4*count);
+		break;
 
-	return rc;
+	case  SPE_MBOX_ANY_BLOCKING: // read at least one, even if blocking
+		rc = read_any(open_if_closed(spectx,FD_IBOX, 0), mbox_data, 4*count);
+		break;
+
+	case  SPE_MBOX_ANY_NONBLOCKING: // only reaad, if non blocking
+		rc = read_immediate(open_if_closed(spectx,FD_IBOX, 0), mbox_data, 4*count);
+		break;
+
+	default:
+		errno = EINVAL;
+		return -1;
+	}
+
+	if (rc == -1) {
+		errno = EIO;
+		return -1;
+	}
+
+	return rc / 4;
 }
 
  int _base_spe_signal_write(spe_context_ptr_t spectx, 



More information about the cbe-oss-dev mailing list