From 7ed52495d0a5eb4884b8ba5b5eab489433b0a55e Mon Sep 17 00:00:00 2001 From: jsorg71 Date: Sun, 4 Feb 2007 07:53:15 +0000 Subject: [PATCH] thread_calls --- xrdp/xrdp.c | 48 ++++++++++++++++++++++----------------------- xrdp/xrdp.h | 2 -- xrdp/xrdp_listen.c | 19 ++++++++++++++---- xrdp/xrdp_process.c | 2 +- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/xrdp/xrdp.c b/xrdp/xrdp.c index 7e95bada..f3ecb38c 100644 --- a/xrdp/xrdp.c +++ b/xrdp/xrdp.c @@ -51,23 +51,23 @@ g_xrdp_sync(long (*sync_func)(long param1, long param2), long sync_param1, long sync_result; int sync_command; - tc_lock_mutex(g_sync1_mutex); - tc_lock_mutex(g_sync_mutex); + 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_unlock_mutex(g_sync_mutex); + tc_mutex_unlock(g_sync_mutex); do { g_sleep(100); - tc_lock_mutex(g_sync_mutex); + tc_mutex_lock(g_sync_mutex); sync_command = g_sync_command; sync_result = g_sync_result; - tc_unlock_mutex(g_sync_mutex); + tc_mutex_unlock(g_sync_mutex); } while (sync_command != 0); - tc_unlock_mutex(g_sync1_mutex); + tc_mutex_unlock(g_sync1_mutex); return sync_result; } @@ -101,9 +101,9 @@ g_is_term(void) { int rv; - tc_lock_mutex(g_term_mutex); + tc_mutex_lock(g_term_mutex); rv = g_term; - tc_unlock_mutex(g_term_mutex); + tc_mutex_unlock(g_term_mutex); return rv; } @@ -111,9 +111,9 @@ g_is_term(void) void APP_CC g_set_term(int in_val) { - tc_lock_mutex(g_term_mutex); + tc_mutex_lock(g_term_mutex); g_term = in_val; - tc_unlock_mutex(g_term_mutex); + tc_mutex_unlock(g_term_mutex); } /*****************************************************************************/ @@ -128,7 +128,7 @@ pipe_sig(int sig_num) void APP_CC g_loop(void) { - tc_lock_mutex(g_sync_mutex); + tc_mutex_lock(g_sync_mutex); if (g_sync_command != 0) { if (g_sync_func != 0) @@ -140,7 +140,7 @@ g_loop(void) } g_sync_command = 0; } - tc_unlock_mutex(g_sync_mutex); + tc_mutex_unlock(g_sync_mutex); } /* win32 service control functions */ @@ -202,9 +202,9 @@ MyServiceMain(DWORD dwArgc, LPTSTR* lpszArgv) g_set_current_dir("c:\\temp\\xrdp"); g_listen = 0; WSAStartup(2, &w); - g_term_mutex = tc_create_mutex(); - g_sync_mutex = tc_create_mutex(); - g_sync1_mutex = tc_create_mutex(); + g_term_mutex = tc_mutex_create(); + g_sync_mutex = tc_mutex_create(); + g_sync1_mutex = tc_mutex_create(); g_memset(&g_service_status, 0, sizeof(SERVICE_STATUS)); g_service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; g_service_status.dwCurrentState = SERVICE_RUNNING; @@ -235,9 +235,9 @@ MyServiceMain(DWORD dwArgc, LPTSTR* lpszArgv) //g_file_write(fd, text, g_strlen(text)); } WSACleanup(); - tc_delete_mutex(g_term_mutex); - tc_delete_mutex(g_sync_mutex); - tc_delete_mutex(g_sync1_mutex); + tc_mutex_delete(g_term_mutex); + tc_mutex_delete(g_sync_mutex); + tc_mutex_delete(g_sync1_mutex); xrdp_listen_delete(g_listen); //CloseHandle(event_han); } @@ -516,13 +516,13 @@ 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_create_mutex(); - g_sync_mutex = tc_create_mutex(); - g_sync1_mutex = tc_create_mutex(); + g_term_mutex = tc_mutex_create(); + g_sync_mutex = tc_mutex_create(); + g_sync1_mutex = tc_mutex_create(); xrdp_listen_main_loop(g_listen); - tc_delete_mutex(g_term_mutex); - tc_delete_mutex(g_sync_mutex); - tc_delete_mutex(g_sync1_mutex); + tc_mutex_delete(g_term_mutex); + tc_mutex_delete(g_sync_mutex); + tc_mutex_delete(g_sync1_mutex); #if defined(_WIN32) /* I don't think it ever gets here */ WSACleanup(); diff --git a/xrdp/xrdp.h b/xrdp/xrdp.h index 7e597503..de782657 100644 --- a/xrdp/xrdp.h +++ b/xrdp/xrdp.h @@ -117,8 +117,6 @@ xrdp_process_create(struct xrdp_listen* owner); void APP_CC xrdp_process_delete(struct xrdp_process* self); int APP_CC -xrdp_process_loop(struct xrdp_process* self); -int APP_CC xrdp_process_main_loop(struct xrdp_process* self); /* xrdp_listen.c */ diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c index d8b3b54f..3399e427 100644 --- a/xrdp/xrdp_listen.c +++ b/xrdp/xrdp_listen.c @@ -22,6 +22,9 @@ #include "xrdp.h" +/* 'g_process' is protected by the semaphore 'g_process_sem'. One thread sets + g_process and waits for the other to process it */ +static long g_process_sem = 0; static struct xrdp_process* g_process = 0; /*****************************************************************************/ @@ -130,8 +133,13 @@ xrdp_listen_delete_pro(struct xrdp_listen* self, struct xrdp_process* pro) THREAD_RV THREAD_CC xrdp_process_run(void* in_val) { + struct xrdp_process* process; + DEBUG(("process started")); - xrdp_process_main_loop(g_process); + process = g_process; + g_process = 0; + tc_sem_inc(g_process_sem); + xrdp_process_main_loop(process); DEBUG(("process done")); return 0; } @@ -149,6 +157,8 @@ xrdp_listen_main_loop(struct xrdp_listen* self) struct list* names; struct list* values; + self->status = 1; + g_process_sem = tc_sem_create(0); /* default to port 3389 */ g_strncpy(port, "3389", 7); /* see if port is in xrdp.ini file */ @@ -183,7 +193,6 @@ xrdp_listen_main_loop(struct xrdp_listen* self) list_delete(values); g_file_close(fd); } - self->status = 1; self->sck = g_tcp_socket(); g_tcp_set_non_blocking(self->sck); error = g_tcp_bind(self->sck, port); @@ -200,7 +209,7 @@ xrdp_listen_main_loop(struct xrdp_listen* self) while (!g_is_term() && !self->term) { error = g_tcp_accept(self->sck); - if (error == -1 && g_tcp_last_error_would_block(self->sck)) + if ((error == -1) && g_tcp_last_error_would_block(self->sck)) { g_sleep(100); g_loop(); @@ -217,7 +226,8 @@ xrdp_listen_main_loop(struct xrdp_listen* self) /* start thread */ g_process->sck = error; tc_thread_create(xrdp_process_run, 0); - g_sleep(100); + tc_sem_dec(g_process_sem); /* this will wait */ + g_sleep(250); /* just for safety */ } else { @@ -232,6 +242,7 @@ xrdp_listen_main_loop(struct xrdp_listen* self) } xrdp_listen_term_processes(self); g_tcp_close(self->sck); + tc_sem_delete(g_process_sem); self->status = -1; return 0; } diff --git a/xrdp/xrdp_process.c b/xrdp/xrdp_process.c index dd217faf..2ff99377 100644 --- a/xrdp/xrdp_process.c +++ b/xrdp/xrdp_process.c @@ -47,7 +47,7 @@ xrdp_process_delete(struct xrdp_process* self) } /*****************************************************************************/ -int APP_CC +static int APP_CC xrdp_process_loop(struct xrdp_process* self) { int rv;