Add calling usleep() to avoid busy waiting 43/274343/1
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 27 Apr 2022 00:57:41 +0000 (09:57 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 27 Apr 2022 00:57:41 +0000 (09:57 +0900)
If the socket is non-blocking mode, while loop can make busy waiting.
To avoid busy waiting, this patch adds calling usleep() with 100 ms.

Change-Id: I41d25ee6f7c314c29920b48b00a8b94c67c6825f
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
aul/socket/socket.cc
src/aul_sock.c

index e61f637..46487f0 100644 (file)
@@ -61,6 +61,7 @@ int Socket::Recv(void* buf, unsigned int size) {
   if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK)
     is_blocking = false;
 
+  int retry_count = 20;
   unsigned char* buffer = static_cast<unsigned char*>(buf);
   unsigned int left = size;
   while (left) {
@@ -77,7 +78,11 @@ int Socket::Recv(void* buf, unsigned int size) {
           return -EAGAIN;
         }
 
-        continue;
+        if (retry_count > 0) {
+          usleep(100 * 1000);
+          retry_count--;
+          continue;
+        }
       }
 
       _E("recv() is failed. fd(%d), errno(%d)", fd_, errno);
index 02f61e5..5ad51b7 100644 (file)
@@ -61,6 +61,7 @@ static int __recv_raw(int fd, unsigned char *data, size_t data_size)
        ssize_t r;
        size_t size = data_size;
        bool is_blocking;
+       int retry_count = 20;
 
        if (fcntl(fd, F_GETFL, 0) & O_NONBLOCK)
                is_blocking = false;
@@ -79,7 +80,11 @@ static int __recv_raw(int fd, unsigned char *data, size_t data_size)
                                        return -EAGAIN;
                                }
 
-                               continue;
+                               if (retry_count > 0) {
+                                       usleep(100 * 1000);
+                                       retry_count--;
+                                       continue;
+                               }
                        }
 
                        _E("recv error. fd(%d), errno(%d)",