Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
windows ccapiserver: replace Sleep with event wait
Signed-off-by: Kevin Wasserman <kevin.wasserman@painless-security.com>

ticket: 7050

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@25538 dc483132-0cff-0310-8789-dd5450dbe970
  • Loading branch information
tlyu committed Dec 12, 2011
1 parent 873dd95 commit 569d087
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
16 changes: 16 additions & 0 deletions src/ccapi/server/win/WorkItem.cpp
Expand Up @@ -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;
}

Expand Down
17 changes: 15 additions & 2 deletions src/ccapi/server/win/WorkQueue.cpp
Expand Up @@ -24,6 +24,7 @@
* or implied warranty.
*/

#include "WorkQueue.h"
extern "C" {
#include "cci_debugging.h"
}
Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion src/ccapi/server/win/WorkQueue.h
Expand Up @@ -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);
Expand Down
16 changes: 9 additions & 7 deletions src/ccapi/server/win/ccs_os_server.cpp
Expand Up @@ -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",
Expand All @@ -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);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions src/ccapi/server/win/workitem.h
Expand Up @@ -36,9 +36,13 @@ class WorkList {
private:
std::list <WorkItem*> 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();}
Expand Down

0 comments on commit 569d087

Please sign in to comment.