Add client references
[fio.git] / client.c
index a65cbcba9d505795f76eb72ce2ad958c87569c97..b0ccad0481699fa1fc54178ebff6672618cf9e52 100644 (file)
--- a/client.c
+++ b/client.c
@@ -93,8 +93,10 @@ static struct fio_client *find_client_by_fd(int fd)
        flist_for_each(entry, &client_hash[bucket]) {
                client = flist_entry(entry, struct fio_client, hash_list);
 
-               if (client->fd == fd)
+               if (client->fd == fd) {
+                       client->refs++;
                        return client;
+               }
        }
 
        return NULL;
@@ -102,6 +104,11 @@ static struct fio_client *find_client_by_fd(int fd)
 
 static void remove_client(struct fio_client *client)
 {
+       assert(client->refs);
+
+       if (--client->refs)
+               return;
+
        dprint(FD_NET, "client: removed <%s>\n", client->hostname);
        flist_del(&client->list);
 
@@ -123,6 +130,11 @@ static void remove_client(struct fio_client *client)
        sum_stat_clients--;
 }
 
+static void put_client(struct fio_client *client)
+{
+       remove_client(client);
+}
+
 static void __fio_client_add_cmd_option(struct fio_client *client,
                                        const char *opt)
 {
@@ -187,6 +199,7 @@ struct fio_client *fio_client_add_explicit(struct client_ops *ops,
 
        client->fd = -1;
        client->ops = ops;
+       client->refs = 1;
 
        __fio_client_add_cmd_option(client, "fio");
 
@@ -235,6 +248,7 @@ int fio_client_add(struct client_ops *ops, const char *hostname, void **cookie)
 
        client->fd = -1;
        client->ops = ops;
+       client->refs = 1;
 
        __fio_client_add_cmd_option(client, "fio");
 
@@ -1116,6 +1130,7 @@ int fio_handle_clients(struct client_ops *ops)
                                retval = 1;
                        } else if (client->error)
                                retval = 1;
+                       put_client(client);
                }
        }