Browse Source

faux.async: Use faux_buf_t for async

Serj Kalichev 3 years ago
parent
commit
693f7b9d03
4 changed files with 33 additions and 52 deletions
  1. 12 50
      faux/async/async.c
  2. 2 1
      faux/buf.h
  3. 17 1
      faux/buf/buf.c
  4. 2 0
      faux/faux.map

+ 12 - 50
faux/async/async.c

@@ -266,33 +266,6 @@ ssize_t faux_async_write(faux_async_t *async, void *data, size_t len)
 }
 
 
-/** @brief Get amount of available data within first chunk.
- *
- * Inernal static function.
- *
- * @param [in] list Internal buffer (list of chunks) to inspect.
- * @param [in] rpos Current read position within chunk.
- * @param [in] wpos Current write position within chunk.
- * @return Available data length or < 0 on error.
- */
-static ssize_t data_avail(faux_list_t *list, size_t rpos, size_t wpos)
-{
-	size_t len = 0;
-
-	if (!list)
-		return -1;
-
-	len = faux_list_len(list);
-	if (len == 0)
-		return 0;
-	if (len > 1)
-		return (DATA_CHUNK - rpos);
-
-	// Single chunk
-	return (wpos - rpos);
-}
-
-
 /** @brief Write output buffer to fd in non-blocking mode.
  *
  * Previously data must be written to internal buffer by faux_async_write()
@@ -308,47 +281,40 @@ static ssize_t data_avail(faux_list_t *list, size_t rpos, size_t wpos)
 ssize_t faux_async_out(faux_async_t *async)
 {
 	ssize_t total_written = 0;
+	ssize_t avail = 0;
 
 	assert(async);
 	if (!async)
 		return -1;
 
-	while (async->o_size > 0) {
-		faux_list_node_t *node = NULL;
-		char *chunk_ptr = NULL;
+	while ((avail = faux_buf_len(async->obuf)) > 0) {
 		ssize_t data_to_write = 0;
 		ssize_t bytes_written = 0;
 		bool_t postpone = BOOL_FALSE;
+		void *data = NULL;
 
-		node = faux_list_head(async->o_list);
-		if (!node) // List is empty while o_size > 0
-			return -1;
-		chunk_ptr = faux_list_data(node);
-		data_to_write = data_avail(async->o_list,
-			async->o_rpos, async->o_wpos);
-		if (data_to_write <= 0) // Strange case
+		data_to_write = faux_buf_dread_lock_easy(async->obuf, &data);
+		if (data_to_write <= 0)
 			return -1;
 
-		bytes_written = write(async->fd, chunk_ptr + async->o_rpos,
-			data_to_write);
+		bytes_written = write(async->fd, data, data_to_write);
 		if (bytes_written > 0) {
-			async->o_size -= bytes_written;
 			total_written += bytes_written;
+			faux_buf_dread_unlock_easy(async->obuf, bytes_written);
+		} else {
+			faux_buf_dread_unlock_easy(async->obuf, 0);
 		}
-
 		if (bytes_written < 0) {
 			if ( // Something went wrong
 				(errno != EINTR) &&
 				(errno != EAGAIN) &&
 				(errno != EWOULDBLOCK)
-			)
+				)
 				return -1;
 			// Postpone next read
 			postpone = BOOL_TRUE;
-
 		// Not whole data block was written
 		} else if (bytes_written != data_to_write) {
-			async->o_rpos += bytes_written;
 			// Postpone next read
 			postpone = BOOL_TRUE;
 		}
@@ -357,15 +323,11 @@ ssize_t faux_async_out(faux_async_t *async)
 		if (postpone) {
 			// Execute callback
 			if (async->stall_cb)
-				async->stall_cb(async, async->o_size,
+				async->stall_cb(async,
+					faux_buf_len(async->obuf),
 					async->stall_udata);
 			break;
 		}
-
-		// Not postponed. Current chunk was fully written. So
-		// remove it from list.
-		async->o_rpos = 0;
-		faux_list_del(async->o_list, node);
 	}
 
 	return total_written;

+ 2 - 1
faux/buf.h

@@ -35,7 +35,8 @@ ssize_t faux_buf_dwrite_unlock(faux_buf_t *buf, size_t really_written,
 	struct iovec *iov);
 ssize_t faux_buf_dwrite_lock_easy(faux_buf_t *buf, void **data);
 ssize_t faux_buf_dwrite_unlock_easy(faux_buf_t *buf, size_t really_written);
-
+ssize_t faux_buf_dread_lock_easy(faux_buf_t *buf, void **data);
+ssize_t faux_buf_dread_unlock_easy(faux_buf_t *buf, size_t really_readed);
 
 C_DECL_END
 

+ 17 - 1
faux/buf/buf.c

@@ -553,6 +553,22 @@ unlock:
 }
 
 
+/** @brief Unlocks read data.
+ *
+ * It's a function complementary to faux_buf_dread_lock_easy().
+ * It has the same functionality as faux_dread_unlock() but doesn't free
+ * "struct iovec" array.
+ *
+ * @param [in] buf Allocated and initialized dynamic buffer object.
+ * @param [in] really_readed Length of data actually readed.
+ * @return Length of data actually unlocked or < 0 on error.
+ */
+ssize_t faux_buf_dread_unlock_easy(faux_buf_t *buf, size_t really_readed)
+{
+	return faux_buf_dread_unlock(buf, really_readed, NULL);
+}
+
+
 /** @brief Write data from linear buffer to dynamic buffer.
  *
  * @param [in] buf Allocated and initialized dynamic buffer object.
@@ -820,7 +836,7 @@ ssize_t faux_buf_dwrite_unlock(faux_buf_t *buf, size_t really_written,
 /** @brief Unlocks written data.
  *
  * It's a function complementary to faux_buf_dwrite_lock_easy().
- * It has the same functionality as faux_dwrite_lock() but doesn't free
+ * It has the same functionality as faux_dwrite_unlock() but doesn't free
  * "struct iovec" array.
  *
  * @param [in] buf Allocated and initialized dynamic buffer object.

+ 2 - 0
faux/faux.map

@@ -336,6 +336,8 @@ FAUX_2.0 {
 		faux_buf_dwrite_unlock;
 		faux_buf_dwrite_lock_easy;
 		faux_buf_dwrite_unlock_easy;
+		faux_buf_dread_lock_easy;
+		faux_buf_dread_unlock_easy;
 
 		testc_version_major;
 		testc_version_minor;