From f1da02ea0671de6649cfb15652720b0657fb866c Mon Sep 17 00:00:00 2001
From: mahesh gondi <mashf13@gmail.com>
Date: Mon, 26 Mar 2012 03:12:16 +0000
Subject: [PATCH] scheduler : in  mk_sched_registeer_client, insertions are
 now O(1) instead of O(work_capacity)

---
 src/include/mk_scheduler.h |    2 +
 src/mk_scheduler.c         |   55 ++++++++++++++++++++++++++++----------------
 2 files changed, 37 insertions(+), 20 deletions(-)

diff --git a/src/include/mk_scheduler.h b/src/include/mk_scheduler.h
index fd2e442..1e990b4 100644
--- a/src/include/mk_scheduler.h
+++ b/src/include/mk_scheduler.h
@@ -37,6 +37,7 @@ struct sched_connection
 {
     int status;
     int socket;
+    int pos_in_queue;
 
     time_t arrive_time;
 };
@@ -46,6 +47,7 @@ struct sched_list_node
 {
     unsigned short int active_connections;
     struct sched_connection *queue;
+    int *free_in_queue;
 
     short int idx;
     pthread_t tid;
diff --git a/src/mk_scheduler.c b/src/mk_scheduler.c
index 11ee748..4a9bda9 100644
--- a/src/mk_scheduler.c
+++ b/src/mk_scheduler.c
@@ -112,28 +112,37 @@ int mk_sched_register_client(int remote_fd, struct sched_list_node *sched)
 {
     unsigned int i, ret;
 
-    for (i = 0; i < config->worker_capacity; i++) {
-        if (sched->queue[i].status == MK_SCHEDULER_CONN_AVAILABLE) {
-            MK_TRACE("[FD %i] Add in slot %i", remote_fd, i);
-
-            /* Before to continue, we need to run plugin stage 10 */
-            ret = mk_plugin_stage_run(MK_PLUGIN_STAGE_10,
-                                      remote_fd,
-                                      &sched->queue[i], NULL, NULL);
-
-            /* Close connection, otherwise continue */
-            if (ret == MK_PLUGIN_RET_CLOSE_CONX) {
-                mk_conn_close(remote_fd);
-                return MK_PLUGIN_RET_CLOSE_CONX;
-            }
+    if(sched->free_in_queue[0] == 0)
+        return -1;
+    i = sched->free_in_queue[sched->free_in_queue[0]];
+    MK_TRACE("Scheduler : free => %d ,  found location : %d", sched->free_in_queue[0], i);
 
-            /* Socket and status */
-            sched->queue[i].socket = remote_fd;
-            sched->queue[i].status = MK_SCHEDULER_CONN_PENDING;
-            sched->queue[i].arrive_time = log_current_utime;
-            return 0;
+    if (sched->queue[i].status == MK_SCHEDULER_CONN_AVAILABLE) {
+        MK_TRACE("[FD %i] Add in slot %i", remote_fd, i);
+
+        /* Before to continue, we need to run plugin stage 10 */
+        ret = mk_plugin_stage_run(MK_PLUGIN_STAGE_10,
+                                  remote_fd,
+                                  &sched->queue[i], NULL, NULL);
+
+        /* Close connection, otherwise continue */
+        if (ret == MK_PLUGIN_RET_CLOSE_CONX) {
+            mk_conn_close(remote_fd);
+            return MK_PLUGIN_RET_CLOSE_CONX;
         }
+
+        /* Socket and status */
+        sched->queue[i].socket = remote_fd;
+        sched->queue[i].status = MK_SCHEDULER_CONN_PENDING;
+        sched->queue[i].arrive_time = log_current_utime;
+        sched->free_in_queue[0] --;
+        return 0;
     }
+    else {
+        /*This should never happen*/
+        mk_err("worker : invalid free position in queue found");
+    }
+
 
     return -1;
 }
@@ -229,10 +238,14 @@ int mk_sched_register_thread(int efd)
     __sync_bool_compare_and_swap(&sl->epoll_fd, sl->epoll_fd, efd);
     sl->queue = mk_mem_malloc_z(sizeof(struct sched_connection) *
                                 config->worker_capacity);
+    sl->free_in_queue = mk_mem_malloc_z(sizeof(int) * (config->worker_capacity + 1));
+    sl->free_in_queue[0] = config->worker_capacity;
     sl->request_handler = NULL;
 
     for (i = 0; i < config->worker_capacity; i++) {
         sl->queue[i].status = MK_SCHEDULER_CONN_AVAILABLE;
+        sl->queue[i].pos_in_queue = i;
+        sl->free_in_queue[i + 1] = i;
     }
     return sl->idx;
 }
@@ -328,6 +341,8 @@ int mk_sched_remove_client(struct sched_list_node *sched, int remote_fd)
         __sync_fetch_and_sub(&sched->active_connections, 1);
         sc->status = MK_SCHEDULER_CONN_AVAILABLE;
         sc->socket = -1;
+        sched->free_in_queue[0] ++;
+        sched->free_in_queue[sched->free_in_queue[0]] = sc->pos_in_queue;
         return 0;
     }
     else {
-- 
1.7.5.4

