From: Tom Yu Date: Mon, 12 Dec 2011 20:44:14 +0000 (+0000) Subject: windows ccapiserver: replace Sleep with event wait X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=569d0874b655747abec8a8932276aa151e1eb76c;p=krb5.git windows ccapiserver: replace Sleep with event wait Signed-off-by: Kevin Wasserman ticket: 7050 git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@25538 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/ccapi/server/win/WorkItem.cpp b/src/ccapi/server/win/WorkItem.cpp index 22e209de9..79a348737 100644 --- a/src/ccapi/server/win/WorkItem.cpp +++ b/src/ccapi/server/win/WorkItem.cpp @@ -103,10 +103,26 @@ char* WorkItem::print(char* buf) { return buf; } +int WorkList::initialize() { + hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); + return 0; + } + +int WorkList::cleanup() { + CloseHandle(hEvent); + hEvent = INVALID_HANDLE_VALUE; + return 0; + } + +void WorkList::wait() { + WaitForSingleObject(hEvent, INFINITE); + } + int WorkList::add(WorkItem* item) { EnterCriticalSection(&cs); wl.push_front(item); LeaveCriticalSection(&cs); + SetEvent(hEvent); return 1; } diff --git a/src/ccapi/server/win/WorkQueue.cpp b/src/ccapi/server/win/WorkQueue.cpp index cc12054a1..fc5fa7e15 100644 --- a/src/ccapi/server/win/WorkQueue.cpp +++ b/src/ccapi/server/win/WorkQueue.cpp @@ -24,6 +24,7 @@ * or implied warranty. */ +#include "WorkQueue.h" extern "C" { #include "cci_debugging.h" } @@ -32,9 +33,21 @@ extern "C" { WorkList worklist; +EXTERN_C int worklist_initialize() { + return worklist.initialize(); + } + +EXTERN_C int worklist_cleanup() { + return worklist.cleanup(); + } + +EXTERN_C void worklist_wait() { + worklist.wait(); + } + /* C interfaces: */ -EXTERN_C bool worklist_isEmpty() { - return worklist.isEmpty(); +EXTERN_C BOOL worklist_isEmpty() { + return worklist.isEmpty() ? TRUE : FALSE; } EXTERN_C int worklist_add( const long rpcmsg, diff --git a/src/ccapi/server/win/WorkQueue.h b/src/ccapi/server/win/WorkQueue.h index 6b22651cc..68aa8b1ca 100644 --- a/src/ccapi/server/win/WorkQueue.h +++ b/src/ccapi/server/win/WorkQueue.h @@ -29,9 +29,16 @@ #include "windows.h" #include "ccs_pipe.h" +EXTERN_C int worklist_initialize(); + +EXTERN_C int worklist_cleanup(); + +/* Wait for work to be added to the list (via worklist_add) from another thread */ +EXTERN_C void worklist_wait(); + EXTERN_C BOOL worklist_isEmpty(); -EXTERN_C void worklist_add( const long rpcmsg, +EXTERN_C int worklist_add( const long rpcmsg, const ccs_pipe_t pipe, const k5_ipc_stream stream, const time_t serverStartTime); diff --git a/src/ccapi/server/win/ccs_os_server.cpp b/src/ccapi/server/win/ccs_os_server.cpp index b9c70a912..811090ba5 100644 --- a/src/ccapi/server/win/ccs_os_server.cpp +++ b/src/ccapi/server/win/ccs_os_server.cpp @@ -156,6 +156,10 @@ cc_int32 ccs_os_server_initialize (int argc, const char *argv[]) { // status = startup_server(opts); // } + if (!err) { + err = worklist_initialize(); + } + if (err) { Init::Cleanup(); fprintf( stderr, "An error occured while %s the server (%u)\n", @@ -174,6 +178,8 @@ cc_int32 ccs_os_server_cleanup (int argc, const char *argv[]) { cci_debug_printf("%s for user <%s> shutting down.", argv[0], argv[1]); + worklist_cleanup(); + return cci_check_error (err); } @@ -190,7 +196,6 @@ cc_int32 ccs_os_server_cleanup (int argc, const char *argv[]) { cc_int32 ccs_os_server_listen_loop (int argc, const char *argv[]) { cc_int32 err = 0; uintptr_t threadStatus; - unsigned int loopCounter = 0; ParseOpts::Opts opts = { 0 }; ParseOpts PO; @@ -221,15 +226,13 @@ cc_int32 ccs_os_server_listen_loop (int argc, const char *argv[]) { queue. */ rpcargs.sessID = (unsigned char*)sessID; rpcargs.opts = &opts; + /// TODO: check for NULL handle, error, etc. probably move to initialize func... threadStatus = _beginthread(receiveLoop, 0, (void*)&rpcargs); /* We handle the queue entries here. Work loop: */ while (TRUE) { - loopCounter++; - if (worklist_isEmpty() & 1) { - SleepEx(1000, TRUE); - } - else if (TRUE) { // Take next WorkItem from the queue: + worklist_wait(); + while (!worklist_isEmpty()) { k5_ipc_stream buf = NULL; long rpcmsg = CCMSG_INVALID; time_t serverStartTime = 0xDEADDEAD; @@ -303,7 +306,6 @@ cc_int32 ccs_os_server_listen_loop (int argc, const char *argv[]) { else {cci_debug_printf("Huh? Queue not empty but no item to remove.");} } } - return cci_check_error (err); } diff --git a/src/ccapi/server/win/workitem.h b/src/ccapi/server/win/workitem.h index 1d3df155c..fff56f326 100644 --- a/src/ccapi/server/win/workitem.h +++ b/src/ccapi/server/win/workitem.h @@ -36,9 +36,13 @@ class WorkList { private: std::list wl; CRITICAL_SECTION cs; + HANDLE hEvent; public: WorkList(); ~WorkList(); + int initialize(); + int cleanup(); + void wait(); int add(WorkItem*); int remove(WorkItem**); bool isEmpty() {return wl.empty();}