From 4df6aa909e1c0f8626cf1968a6e188779eef7629 Mon Sep 17 00:00:00 2001 From: jsorg71 Date: Mon, 31 Mar 2008 11:28:18 +0000 Subject: [PATCH] main loop reorganization --- xrdp/xrdp.c | 99 ++++++++++++++++++++++------------ xrdp/xrdp.h | 8 +-- xrdp/xrdp_listen.c | 126 ++++++++++++++++++++++++++------------------ xrdp/xrdp_process.c | 5 +- xrdp/xrdp_types.h | 7 ++- 5 files changed, 150 insertions(+), 95 deletions(-) diff --git a/xrdp/xrdp.c b/xrdp/xrdp.c index 6d1c3565..e4e0de58 100644 --- a/xrdp/xrdp.c +++ b/xrdp/xrdp.c @@ -32,10 +32,10 @@ static long g_threadid = 0; /* main threadid */ static SERVICE_STATUS_HANDLE g_ssh = 0; static SERVICE_STATUS g_service_status; #endif -static long g_term_mutex = 0; static long g_sync_mutex = 0; static long g_sync1_mutex = 0; -static int g_term = 0; +static tbus g_term_event = 0; +static tbus g_sync_event = 0; /* syncronize stuff */ static int g_sync_command = 0; static long g_sync_result = 0; @@ -51,23 +51,32 @@ g_xrdp_sync(long (*sync_func)(long param1, long param2), long sync_param1, long sync_result; int sync_command; - tc_mutex_lock(g_sync1_mutex); - tc_mutex_lock(g_sync_mutex); - g_sync_param1 = sync_param1; - g_sync_param2 = sync_param2; - g_sync_func = sync_func; - g_sync_command = 100; - tc_mutex_unlock(g_sync_mutex); - do + if (tc_get_threadid() == g_threadid) { - g_sleep(100); + /* this is the main thread, call the function directly */ + sync_result = sync_func(sync_param1, sync_param2); + } + else + { + tc_mutex_lock(g_sync1_mutex); tc_mutex_lock(g_sync_mutex); - sync_command = g_sync_command; - sync_result = g_sync_result; + g_sync_param1 = sync_param1; + g_sync_param2 = sync_param2; + g_sync_func = sync_func; + g_sync_command = 100; tc_mutex_unlock(g_sync_mutex); + g_set_wait_obj(g_sync_event); + do + { + g_sleep(100); + tc_mutex_lock(g_sync_mutex); + sync_command = g_sync_command; + sync_result = g_sync_result; + tc_mutex_unlock(g_sync_mutex); + } + while (sync_command != 0); + tc_mutex_unlock(g_sync1_mutex); } - while (sync_command != 0); - tc_mutex_unlock(g_sync1_mutex); return sync_result; } @@ -75,18 +84,18 @@ g_xrdp_sync(long (*sync_func)(long param1, long param2), long sync_param1, void DEFAULT_CC xrdp_shutdown(int sig) { - struct xrdp_listen* listen; + tbus threadid; - if (tc_get_threadid() != g_threadid) + threadid = tc_get_threadid(); + if (threadid != g_threadid) { return; } g_writeln("shutting down"); - g_writeln("signal %d threadid $%8.8x", sig, tc_get_threadid()); - listen = g_listen; - if (listen != 0) + g_writeln("signal %d threadid $%8.8x", sig, threadid); + if (!g_is_wait_obj_set(g_term_event)) { - g_set_term(1); + g_set_wait_obj(g_term_event); } } @@ -94,21 +103,35 @@ xrdp_shutdown(int sig) int APP_CC g_is_term(void) { - int rv; - - tc_mutex_lock(g_term_mutex); - rv = g_term; - tc_mutex_unlock(g_term_mutex); - return rv; + return g_is_wait_obj_set(g_term_event); } /*****************************************************************************/ void APP_CC g_set_term(int in_val) { - tc_mutex_lock(g_term_mutex); - g_term = in_val; - tc_mutex_unlock(g_term_mutex); + if (in_val) + { + g_set_wait_obj(g_term_event); + } + else + { + g_reset_wait_obj(g_term_event); + } +} + +/*****************************************************************************/ +tbus APP_CC +g_get_term_event(void) +{ + return g_term_event; +} + +/*****************************************************************************/ +tbus APP_CC +g_get_sync_event(void) +{ + return g_sync_event; } /*****************************************************************************/ @@ -197,9 +220,10 @@ MyServiceMain(DWORD dwArgc, LPTSTR* lpszArgv) g_set_current_dir("c:\\temp\\xrdp"); g_listen = 0; WSAStartup(2, &w); - g_term_mutex = tc_mutex_create(); g_sync_mutex = tc_mutex_create(); g_sync1_mutex = tc_mutex_create(); + g_term_event = g_create_wait_obj("xrdp_main_term"); + g_sync_event = g_create_wait_obj("xrdp_main_sync"); g_memset(&g_service_status, 0, sizeof(SERVICE_STATUS)); g_service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; g_service_status.dwCurrentState = SERVICE_RUNNING; @@ -230,9 +254,10 @@ MyServiceMain(DWORD dwArgc, LPTSTR* lpszArgv) //g_file_write(fd, text, g_strlen(text)); } xrdp_listen_delete(g_listen); - tc_mutex_delete(g_term_mutex); tc_mutex_delete(g_sync_mutex); tc_mutex_delete(g_sync1_mutex); + g_destroy_wait_obj(g_term_event); + g_destroy_wait_obj(g_sync_event); WSACleanup(); //CloseHandle(event_han); } @@ -512,14 +537,20 @@ main(int argc, char** argv) g_signal(9, xrdp_shutdown); /* SIGKILL */ g_signal(13, pipe_sig); /* sig pipe */ g_signal(15, xrdp_shutdown); /* SIGTERM */ - g_term_mutex = tc_mutex_create(); g_sync_mutex = tc_mutex_create(); g_sync1_mutex = tc_mutex_create(); + g_term_event = g_create_wait_obj("xrdp_main_term"); + g_sync_event = g_create_wait_obj("xrdp_main_sync"); + if (g_term_event == 0) + { + g_writeln("error creating g_term_event"); + } xrdp_listen_main_loop(g_listen); xrdp_listen_delete(g_listen); - tc_mutex_delete(g_term_mutex); tc_mutex_delete(g_sync_mutex); tc_mutex_delete(g_sync1_mutex); + g_destroy_wait_obj(g_term_event); + g_destroy_wait_obj(g_sync_event); #if defined(_WIN32) /* I don't think it ever gets here */ /* when running in win32 app mode, control c exits right away */ diff --git a/xrdp/xrdp.h b/xrdp/xrdp.h index 750332cd..ad3af894 100644 --- a/xrdp/xrdp.h +++ b/xrdp/xrdp.h @@ -42,6 +42,10 @@ int APP_CC g_is_term(void); void APP_CC g_set_term(int in_val); +tbus APP_CC +g_get_term_event(void); +tbus APP_CC +g_get_sync_event(void); void APP_CC g_loop(void); @@ -122,7 +126,7 @@ xrdp_wm_log_msg(struct xrdp_wm* self, char* msg); /* xrdp_process.c */ struct xrdp_process* APP_CC -xrdp_process_create(struct xrdp_listen* owner); +xrdp_process_create(struct xrdp_listen* owner, tbus done_event); void APP_CC xrdp_process_delete(struct xrdp_process* self); int APP_CC @@ -134,8 +138,6 @@ xrdp_listen_create(void); void APP_CC xrdp_listen_delete(struct xrdp_listen* self); int APP_CC -xrdp_listen_delete_pro(struct xrdp_listen* self, struct xrdp_process* pro); -int APP_CC xrdp_listen_main_loop(struct xrdp_listen* self); /* xrdp_region.c */ diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c index 406f6383..d7e63b7d 100644 --- a/xrdp/xrdp_listen.c +++ b/xrdp/xrdp_listen.c @@ -34,8 +34,9 @@ xrdp_listen_create(void) struct xrdp_listen* self; self = (struct xrdp_listen*)g_malloc(sizeof(struct xrdp_listen), 1); - self->process_list_max = 100; g_process_sem = tc_sem_create(0); + self->pro_done_event = g_create_wait_obj("xrdp_listen_pro_done_event"); + self->process_list = list_create(); return self; } @@ -44,6 +45,8 @@ void APP_CC xrdp_listen_delete(struct xrdp_listen* self) { tc_sem_delete(g_process_sem); + g_destroy_wait_obj(self->pro_done_event); + list_delete(self->process_list); g_free(self); } @@ -52,32 +55,37 @@ static int APP_CC xrdp_listen_term_processes(struct xrdp_listen* self) { int i; + struct xrdp_process* pro; /* tell all xrdp processes to end */ - for (i = 0; i < self->process_list_count; i++) + for (i = self->process_list->count - 1; i >= 0; i--) { - if (self->process_list[i] != 0) + pro = (struct xrdp_process*)list_get_item(self->process_list, i); + if (pro != 0) { - self->process_list[i]->term = 1; + pro->term = 1; } } /* make sure they are done */ - for (i = 0; i < self->process_list_count; i++) + for (i = self->process_list->count - 1; i >= 0; i--) { - if (self->process_list[i] != 0) + pro = (struct xrdp_process*)list_get_item(self->process_list, i); + if (pro != 0) { - while (self->process_list[i]->status > 0) + while (pro->status > 0) { g_sleep(10); } } } /* free them all */ - for (i = 0; i < self->process_list_count; i++) + for (i = self->process_list->count - 1; i >= 0; i--) { - if (self->process_list[i] != 0) + pro = (struct xrdp_process*)list_get_item(self->process_list, i); + if (pro != 0) { - xrdp_process_delete(self->process_list[i]); + xrdp_process_delete(pro); + list_remove_item(self->process_list, i); } } return 0; @@ -88,43 +96,27 @@ xrdp_listen_term_processes(struct xrdp_listen* self) static int APP_CC xrdp_listen_add_pro(struct xrdp_listen* self, struct xrdp_process* process) { - int i; - - for (i = 0; i < self->process_list_max; i++) - { - /* add process in new slot */ - if (self->process_list[i] == 0) - { - self->process_list[i] = process; - self->process_list_count++; - return 0; - } - /* add process in unused slot */ - /* this shouldn't happen */ - if (self->process_list[i]->status < 0) - { - xrdp_process_delete(self->process_list[i]); - self->process_list[i] = process; - return 0; - } - } - return 1; + list_add_item(self->process_list, (tbus)process); + return 0; } /*****************************************************************************/ -int APP_CC -xrdp_listen_delete_pro(struct xrdp_listen* self, struct xrdp_process* pro) +static int APP_CC +xrdp_listen_delete_done_pro(struct xrdp_listen* self) { int i; + struct xrdp_process* pro; - for (i = 0; i < self->process_list_max; i++) + for (i = self->process_list->count - 1; i >= 0; i--) { - if (self->process_list[i] == pro) + pro = (struct xrdp_process*)list_get_item(self->process_list, i); + if (pro != 0) { - DEBUG(("process deleted")); - xrdp_process_delete(pro); - self->process_list[i] = 0; - return 0; + if (pro->status < 0) + { + xrdp_process_delete(pro); + list_remove_item(self->process_list, i); + } } } return 0; @@ -200,9 +192,13 @@ int APP_CC xrdp_listen_main_loop(struct xrdp_listen* self) { int error; + int robjs_count; + int cont; char port[8]; + tbus robjs[4]; self->status = 1; + robjs_count = 0; xrdp_listen_get_port(port, sizeof(port)); self->sck = g_tcp_socket(); g_tcp_set_non_blocking(self->sck); @@ -217,33 +213,59 @@ xrdp_listen_main_loop(struct xrdp_listen* self) error = g_tcp_listen(self->sck); if (error == 0) { - while (!g_is_term() && !self->term) + robjs[0] = g_get_term_event(); + robjs[1] = g_get_sync_event(); + robjs[2] = g_create_wait_obj_from_socket(self->sck, 0); + robjs[3] = self->pro_done_event; + robjs_count = 4; + cont = 1; + while (cont) { - error = g_tcp_accept(self->sck); - if ((error == -1) && g_tcp_last_error_would_block(self->sck)) + if (g_obj_wait(robjs, robjs_count, 0, 0, -1) != 0) { g_sleep(100); - g_loop(); } - else if (error == -1) + if (g_is_wait_obj_set(robjs[0])) /* term */ { break; } - else + if (g_is_wait_obj_set(robjs[1])) /* sync */ { - g_process = xrdp_process_create(self); - if (xrdp_listen_add_pro(self, g_process) == 0) + g_reset_wait_obj(robjs[1]); + g_loop(); + } + if (g_is_wait_obj_set(robjs[2])) /* incomming connection */ + { + error = g_tcp_accept(self->sck); + if ((error == -1) && g_tcp_last_error_would_block(self->sck)) + { + g_sleep(100); + } + else if (error == -1) { - /* start thread */ - g_process->sck = error; - tc_thread_create(xrdp_process_run, 0); - tc_sem_dec(g_process_sem); /* this will wait */ + break; } else { - xrdp_process_delete(g_process); + g_process = xrdp_process_create(self, self->pro_done_event); + if (xrdp_listen_add_pro(self, g_process) == 0) + { + /* start thread */ + g_process->sck = error; + tc_thread_create(xrdp_process_run, 0); + tc_sem_dec(g_process_sem); /* this will wait */ + } + else + { + xrdp_process_delete(g_process); + } } } + if (g_is_wait_obj_set(robjs[3])) /* pro_done_event */ + { + g_reset_wait_obj(robjs[3]); + xrdp_listen_delete_done_pro(self); + } } } else diff --git a/xrdp/xrdp_process.c b/xrdp/xrdp_process.c index cf1debf4..d2e0aaeb 100644 --- a/xrdp/xrdp_process.c +++ b/xrdp/xrdp_process.c @@ -24,12 +24,13 @@ /*****************************************************************************/ struct xrdp_process* APP_CC -xrdp_process_create(struct xrdp_listen* owner) +xrdp_process_create(struct xrdp_listen* owner, tbus done_event) { struct xrdp_process* self; self = (struct xrdp_process*)g_malloc(sizeof(struct xrdp_process), 1); self->lis_layer = owner; + self->done_event = done_event; return self; } @@ -138,6 +139,6 @@ xrdp_process_main_loop(struct xrdp_process* self) self->session = 0; g_tcp_close(self->sck); self->status = -1; - xrdp_listen_delete_pro(self->lis_layer, self); + g_set_wait_obj(self->done_event); return 0; } diff --git a/xrdp/xrdp_types.h b/xrdp/xrdp_types.h index 3db33a9a..c1178366 100644 --- a/xrdp/xrdp_types.h +++ b/xrdp/xrdp_types.h @@ -249,6 +249,7 @@ struct xrdp_process /* create these when up and running */ struct xrdp_wm* wm; int app_sck; + tbus done_event; }; /* rdp listener */ @@ -256,10 +257,8 @@ struct xrdp_listen { int status; int sck; - int term; - struct xrdp_process* process_list[100]; /* 100 processes possible */ - int process_list_count; - int process_list_max; + struct list* process_list; + tbus pro_done_event; }; /* region */