added QServer for external cache access
[NeonServV5.git] / src / main.c
index 60b7ec6ac4b37fb9e1f5f08f95ea29a0c02b176b..e8d366aa0d7141696c51b302cc900f2b961fa3c2 100644 (file)
 #include "commands.h"
 #include "ConfigParser.h"
 #include "ssl.h"
+#include "QServer.h"
 
 time_t start_time;
 static int running, hard_restart;
 static int statistics_requested_lusers = 0;
 int statistics_enabled;
 TIMEQ_CALLBACK(main_statistics);
+#ifdef HAVE_THREADS
+int running_threads;
+pthread_mutex_t cache_sync;
+pthread_mutex_t whohandler_sync, whohandler_mass_sync;
+#endif
 
 void cleanup() {
     free_sockets();
+    qserver_free();
     free_parser();
     free_UserNode();
     free_ChanNode();
@@ -100,12 +107,10 @@ void * thread_main(void *arg) {
         do {
             socket_loop(SOCKET_SELECT_TIME);
         } while(time(0) < socket_wait);
-        timeq_tick();
-        loop_bots();
         clearTempUsers();
         destroyEvents();
-        queue_loop();
     }
+    running_threads--;
     return NULL;
 }
 #endif
@@ -131,7 +136,14 @@ main:
     
     statistics_enabled = get_int_field("statistics.enable");
     
+    #ifdef HAVE_THREADS
+    THREAD_MUTEX_INIT(cache_sync);
+    THREAD_MUTEX_INIT(whohandler_sync);
+    THREAD_MUTEX_INIT(whohandler_mass_sync);
+    #endif
+    
     queue_init();
+    init_sockets();
     init_timeq();
     init_lang();
     ssl_init();
@@ -146,6 +158,7 @@ main:
     register_commands();
     init_bots();
     init_DBHelper();
+    qserver_init();
     
     load_languages();
     int update_minutes = get_int_field("statistics.frequency");
@@ -160,11 +173,21 @@ main:
     pthread_t tid[worker_threads];
     int tid_id = 0;
     for(tid_id = 0; tid_id < worker_threads; tid_id++) {
+        running_threads++;
         pthread_create(&tid[tid_id], NULL, thread_main, NULL);
     }
+    int usleep_delay = 1000000 / TICKS_PER_SECOND;
+    while(running) {
+        timeq_tick();
+        loop_bots();
+        qserver_loop();
+        queue_loop();
+        usleep(usleep_delay);
+    }
     for(tid_id = 0; tid_id < worker_threads; tid_id++) {
         pthread_join(tid[tid_id], NULL);
     }
+    running_threads = 0;
     #else
     time_t socket_wait;
     while(running) {
@@ -176,6 +199,7 @@ main:
         loop_bots();
         clearTempUsers();
         destroyEvents();
+        qserver_loop();
         queue_loop();
     }
     #endif