thread: changed from pthread_mutex_t to QemuMutex
authorJinhyung Choi <jinhyung2.choi@samsung.com>
Mon, 16 Jun 2014 08:43:19 +0000 (17:43 +0900)
committerJinhyung Choi <jinhyung2.choi@samsung.com>
Tue, 17 Jun 2014 03:04:05 +0000 (12:04 +0900)
Also, changed pthread_create to qemu_thread_create for ecs

Change-Id: I30ea53f2dec168528937ec3361e5a848038d96a9
Signed-off-by: Jinhyung Choi <jinhyung2.choi@samsung.com>
tizen/src/ecs/ecs.c
tizen/src/ecs/ecs_msg.c
tizen/src/guest_server.c

index 758ae1555595fe91c531b27e9f072ee4a32b9673..cdd658d9eefc029d03852c41047cadbd8a50f1bd 100644 (file)
@@ -29,7 +29,6 @@
  */
 
 #include <stdbool.h>
-#include <pthread.h>
 #include <stdlib.h>
 
 #include "hw/qdev.h"
@@ -72,7 +71,10 @@ static int payloadsize;
 
 static int g_client_id = 1;
 
-static pthread_mutex_t mutex_clilist = PTHREAD_MUTEX_INITIALIZER;
+static QemuMutex mutex_clilist;
+QemuMutex mutex_guest_connection;
+
+static QemuThread ecs_thread_id;
 
 static int suspend_state = 1;
 
@@ -99,7 +101,7 @@ void ecs_client_close(ECS_Client* clii) {
     if (clii == NULL)
         return;
 
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clilist);
 
     if (clii->client_fd > 0) {
         INFO("ecs client closed with fd: %d\n", clii->client_fd);
@@ -115,12 +117,12 @@ void ecs_client_close(ECS_Client* clii) {
     g_free(clii);
     clii = NULL;
 
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clilist);
 }
 
 bool send_to_all_client(const char* data, const int len) {
     TRACE("data len: %d, data: %s\n", len, data);
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clilist);
 
     ECS_Client *clii,*next;
 
@@ -128,16 +130,16 @@ bool send_to_all_client(const char* data, const int len) {
     {
         send_to_client(clii->client_fd, data, len);
     }
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clilist);
 
     return true;
 }
 
 void send_to_single_client(ECS_Client *clii, const char* data, const int len)
 {
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clilist);
     send_to_client(clii->client_fd, data, len);
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clilist);
 }
 
 void send_to_client(int fd, const char* data, const int len)
@@ -231,6 +233,9 @@ static void ecs_close(ECS_State *cs) {
     g_free(cs);
     cs = NULL;
     current_ecs = NULL;
+
+    qemu_mutex_destroy(&mutex_clilist);
+    qemu_mutex_destroy(&mutex_guest_connection);
 }
 
 #ifndef _WIN32
@@ -414,13 +419,13 @@ static int ecs_add_client(ECS_State *cs, int fd) {
     FD_SET(fd, &cs->reads);
 #endif
 
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clilist);
 
     QTAILQ_INSERT_TAIL(&clients, clii, next);
 
     TRACE("Add an ecs client. fd: %d\n", fd);
 
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clilist);
 
 //    send_ecs_version_check(clii);
 
@@ -709,38 +714,43 @@ static void* ecs_initialize(void* args) {
     current_ecs = cs;
     cs->ecs_running = 1;
 
+    qemu_mutex_init(&mutex_clilist);
+    qemu_mutex_init(&mutex_guest_connection);
+
     TRACE("ecs_loop entered.\n");
     while (cs->ecs_running) {
         ret = ecs_loop(cs);
         if (0 > ret) {
-            ecs_close(cs);
             break;
         }
     }
     TRACE("ecs_loop exited.\n");
 
+    ecs_close(cs);
+
     return NULL;
 }
 
 int stop_ecs(void) {
+    void *ret = NULL;
+
     INFO("ecs is closing.\n");
     if (NULL != current_ecs) {
         current_ecs->ecs_running = 0;
-    //    ecs_close(current_ecs);
     }
 
-    pthread_mutex_destroy(&mutex_clilist);
+    ret = qemu_thread_join(&ecs_thread_id);
+    if (ret) {
+        ERR("ecs is failed to join thread.\n");
+    }
 
     return 0;
 }
 
 int start_ecs(void) {
-    pthread_t thread_id;
 
-    if (0 != pthread_create(&thread_id, NULL, ecs_initialize, NULL)) {
-        ERR("pthread creation failed.\n");
-        return -1;
-    }
+    qemu_thread_create(&ecs_thread_id, "ecs", ecs_initialize, NULL, QEMU_THREAD_JOINABLE);
+
     return 0;
 }
 
@@ -778,7 +788,7 @@ bool handle_protobuf_msg(ECS_Client* cli, char* data, int len)
         if (!msg)
             goto fail;
 
-        pthread_mutex_lock(&mutex_clilist);
+        qemu_mutex_lock(&mutex_clilist);
         if(cli->client_type == TYPE_NONE) {
             if (!strncmp(msg->category, MSG_TYPE_NFC, 3)) {
                 QTAILQ_REMOVE(&clients, cli, next);
@@ -801,11 +811,11 @@ bool handle_protobuf_msg(ECS_Client* cli, char* data, int len)
             }
             else {
                 ERR("unsupported category is found: %s\n", msg->category);
-                pthread_mutex_unlock(&mutex_clilist);
+                qemu_mutex_unlock(&mutex_clilist);
                 goto fail;
             }
         }
-        pthread_mutex_unlock(&mutex_clilist);
+        qemu_mutex_unlock(&mutex_clilist);
 
         msgproc_nfc_req(cli, msg);
     }
index bebb5d8c1bc59f9b5d64013f0816f61cf7b8aa38..2929e5d65ee7ef045172745e1d9c8f7eff994e26 100644 (file)
@@ -76,7 +76,7 @@ MULTI_DEBUG_CHANNEL(qemu, ecs-msg);
 
 // utility functions
 static int guest_connection = 0;
-static pthread_mutex_t mutex_guest = PTHREAD_MUTEX_INITIALIZER;
+extern QemuMutex mutex_guest_connection;
 
 /*static function define*/
 static void handle_sdcard(char* dataBuf, size_t dataLen);
@@ -318,9 +318,9 @@ bool msgproc_injector_req(ECS_Client* ccli, ECS__InjectorReq* msg)
             }
         }
     } else if (!strncmp(cmd, MSG_TYPE_GUEST, 5)) {
-        pthread_mutex_lock(&mutex_guest);
+        qemu_mutex_lock(&mutex_guest_connection);
         value = guest_connection;
-        pthread_mutex_unlock(&mutex_guest);
+        qemu_mutex_unlock(&mutex_guest_connection);
         send_status_injector_ntf(MSG_TYPE_GUEST, 5, value, NULL);
         return true;
     }
@@ -715,9 +715,9 @@ static bool injector_req_handle(const char* cat, type_action action)
         return true;
     } else if (!strncmp(cat, MSG_TYPE_GUEST, 5)) {
         INFO("emuld connection is %d\n", action);
-        pthread_mutex_lock(&mutex_guest);
+        qemu_mutex_lock(&mutex_guest_connection);
         guest_connection = action;
-        pthread_mutex_unlock(&mutex_guest);
+        qemu_mutex_unlock(&mutex_guest_connection);
         return false;
     }
 
index ff004bc38e5e448033ef4985ff39b42c4b566c49..893228410c88a871a989345790089b67895fb555 100644 (file)
@@ -77,7 +77,9 @@ typedef struct GS_Client {
 static QTAILQ_HEAD(GS_ClientHead, GS_Client)
 clients = QTAILQ_HEAD_INITIALIZER(clients);
 
-static pthread_mutex_t mutex_clilist = PTHREAD_MUTEX_INITIALIZER;
+static QemuThread guest_thread_id;
+static QemuMutex mutex_clients;
+static int running = 0;
 
 static void remove_sdb_client(GS_Client* client)
 {
@@ -85,12 +87,13 @@ static void remove_sdb_client(GS_Client* client)
         return;
     }
 
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clients);
 
     QTAILQ_REMOVE(&clients, client, next);
-    g_free(client);
 
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clients);
+
+    g_free(client);
 }
 
 static void send_to_sdb_client(GS_Client* client, int state)
@@ -139,14 +142,14 @@ static void send_to_sdb_client(GS_Client* client, int state)
 
 void notify_all_sdb_clients(int state)
 {
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clients);
     GS_Client *client, *next;
 
     QTAILQ_FOREACH_SAFE(client, &clients, next, next)
     {
         send_to_sdb_client(client, state);
     }
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clients);
 
 }
 
@@ -166,6 +169,7 @@ static void add_sdb_client(struct sockaddr_in* addr, int port, const char* seria
         return;
     }
 
+    qemu_mutex_lock(&mutex_clients);
     QTAILQ_FOREACH_SAFE(cli, &clients, next, next)
     {
         if (!strcmp(serial, cli->serial) && !strcmp(inet_ntoa(addr->sin_addr), inet_ntoa((cli->addr).sin_addr))) {
@@ -173,6 +177,7 @@ static void add_sdb_client(struct sockaddr_in* addr, int port, const char* seria
             return;
         }
     }
+    qemu_mutex_unlock(&mutex_clients);
 
     client = g_malloc0(sizeof(GS_Client));
     if (NULL == client) {
@@ -184,11 +189,11 @@ static void add_sdb_client(struct sockaddr_in* addr, int port, const char* seria
     client->port = port;
     strcpy(client->serial, serial);
 
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clients);
 
     QTAILQ_INSERT_TAIL(&clients, client, next);
 
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clients);
 
     INFO("Added new sdb client. ip: %s, port: %d, serial: %s\n", inet_ntoa((client->addr).sin_addr), client->port, client->serial);
 
@@ -504,14 +509,16 @@ static void server_process(void)
 
     client_len = sizeof(client_addr);
 
-    while (1) {
+    running = 1;
+
+    while (running) {
         memset(&readbuf, 0, RECV_BUF_SIZE);
 
         if (server_sock == 0) {
             INFO("server_sock is closed\n");
             return;
         }
-        read_cnt = recvfrom(server_sock, readbuf, RECV_BUF_SIZE, 0,
+        read_cnt = recvfrom(server_sock, readbuf, RECV_BUF_SIZE, MSG_DONTWAIT,
                             (struct sockaddr*) &client_addr, &client_len);
 
         if (read_cnt < 0) {
@@ -536,7 +543,7 @@ static void server_process(void)
 
 static void close_clients(void)
 {
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clients);
     GS_Client * client, *next;
 
     QTAILQ_FOREACH_SAFE(client, &clients, next, next)
@@ -549,7 +556,7 @@ static void close_clients(void)
         }
     }
 
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clients);
 }
 
 static void close_server(void)
@@ -565,6 +572,8 @@ static void close_server(void)
     }
 #endif
     server_sock = 0;
+
+    qemu_mutex_destroy(&mutex_clients);
 }
 
 static void* run_guest_server(void* args)
@@ -604,6 +613,8 @@ static void* run_guest_server(void* args)
 
     INFO("guest server start...port:%d\n", port);
 
+    qemu_mutex_init(&mutex_clients);
+
     server_process();
 
     close_server();
@@ -613,15 +624,19 @@ static void* run_guest_server(void* args)
 
 void start_guest_server(int server_port)
 {
-    QemuThread thread_id;
     svr_port = server_port;
-    qemu_thread_create(&thread_id, "guest_server", run_guest_server, NULL, QEMU_THREAD_DETACHED);
+    qemu_thread_create(&guest_thread_id, "guest_server", run_guest_server, NULL, QEMU_THREAD_JOINABLE);
     INFO("created guest server thread\n");
 }
 
 void shutdown_guest_server(void)
 {
+    void *ret = NULL;
     INFO("shutdown_guest_server.\n");
 
-    close_server();
+    running = 0;
+
+    ret = qemu_thread_join(&guest_thread_id);
+    if (ret)
+        ERR("guest_thread_id join failed.\n");
 }