[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r10658: Yay, sa_evbufferevents work (in libevent-urz/trunk: . WIN32-Code sample)



Author: Urz
Date: 2007-06-22 03:28:28 -0400 (Fri, 22 Jun 2007)
New Revision: 10658

Modified:
   libevent-urz/trunk/WIN32-Code/win32.c
   libevent-urz/trunk/buffer.c
   libevent-urz/trunk/event-internal.h
   libevent-urz/trunk/event.h
   libevent-urz/trunk/sa_evbuffer.c
   libevent-urz/trunk/sample/sa_evbuffer-test.c
Log:
Yay, sa_evbufferevents work

Modified: libevent-urz/trunk/WIN32-Code/win32.c
===================================================================
--- libevent-urz/trunk/WIN32-Code/win32.c	2007-06-22 02:51:49 UTC (rev 10657)
+++ libevent-urz/trunk/WIN32-Code/win32.c	2007-06-22 07:28:28 UTC (rev 10658)
@@ -335,17 +335,21 @@
 		return (0);
 	}
 
+    /*
     dump_fd_set(win32op->readset_out);
     dump_fd_set(win32op->writeset_out);
     dump_fd_set(win32op->exset_out);
     event_debug(("%s: tv == %x\n", __func__, res));
+    */
     
 	res = select(fd_count,
 		     (struct fd_set*)win32op->readset_out,
 		     (struct fd_set*)win32op->writeset_out,
 		     (struct fd_set*)win32op->exset_out, tv);
 
+    /*
 	event_debug(("%s: select returned %d", __func__, res));
+    */
     if(res == SOCKET_ERROR) {
         event_debug(("Socket error found\n"));
     }

Modified: libevent-urz/trunk/buffer.c
===================================================================
--- libevent-urz/trunk/buffer.c	2007-06-22 02:51:49 UTC (rev 10657)
+++ libevent-urz/trunk/buffer.c	2007-06-22 07:28:28 UTC (rev 10658)
@@ -69,8 +69,7 @@
 #include "event.h"
 #include "event-internal.h"
 
-void evbuffer_lock(struct evbuffer *buffer, const char *from)
-{
+void ev_lock(mutex lock) {
 #ifdef WIN32
     /* Code borrowed and changed from Tor's compat.c */
     /*
@@ -90,33 +89,42 @@
      */
     DWORD ret;
     
-    printf("LOCK %x %s\n", (int) buffer, from);
     do {
-        ret = WaitForSingleObject(buffer->lock, INFINITE);
+        ret = WaitForSingleObject(lock, INFINITE);
         if(ret != WAIT_ABANDONED && ret != WAIT_OBJECT_0) {
-            printf("Failed to acquire mutex: %d",(int) GetLastError());
+            printf("Failed to acquire mutex: %d\n",(int) GetLastError());
         }
     } while(ret != WAIT_ABANDONED && ret != WAIT_OBJECT_0);
 #endif
 }
 
-void evbuffer_unlock(struct evbuffer *buffer, const char *from)
+void evbuffer_lock(struct evbuffer *buffer, const char *from)
 {
+    /* printf("LOCK %x %s\n", (int) buffer, from); */
+    ev_lock(buffer->lock);
+}
+
+void ev_unlock(mutex lock) {
 #ifdef WIN32
     /* Code borrowed and changed from Tor's compat.c */
     BOOL ret;
     
-    printf("UNLOCK %x %s\n", (int) buffer, from);
     ret = 0;
     do {
-        ret = ReleaseMutex(buffer->lock);
+        ret = ReleaseMutex(lock);
         if(!ret) {
-            printf("Failed to release mutex: %d", (int) GetLastError());
+            printf("Failed to release mutex: %d\n", (int) GetLastError());
         }
     } while(!ret);
 #endif
 }
 
+void evbuffer_unlock(struct evbuffer *buffer, const char *from)
+{
+    /* printf("UNLOCK %x %s\n", (int) buffer, from); */
+    ev_unlock(buffer->lock);
+}
+
 struct evbuffer *
 evbuffer_new(void)
 {

Modified: libevent-urz/trunk/event-internal.h
===================================================================
--- libevent-urz/trunk/event-internal.h	2007-06-22 02:51:49 UTC (rev 10657)
+++ libevent-urz/trunk/event-internal.h	2007-06-22 07:28:28 UTC (rev 10658)
@@ -34,6 +34,9 @@
 extern "C" {
 #endif
 
+void ev_lock(mutex lock);
+void ev_unlock(mutex lock);
+
 struct event_base {
 	const struct eventop *evsel;
 	void *evbase;

Modified: libevent-urz/trunk/event.h
===================================================================
--- libevent-urz/trunk/event.h	2007-06-22 02:51:49 UTC (rev 10657)
+++ libevent-urz/trunk/event.h	2007-06-22 07:28:28 UTC (rev 10658)
@@ -213,6 +213,13 @@
 
 /* These functions deal with buffering input and output */
 
+#ifdef WIN32
+typedef HANDLE mutex;
+#else
+/* When this is implemented for non-Win32, change to appropriate type */
+typedef char mutex;
+#endif
+
 struct evbuffer {
 	u_char *buffer;
 	u_char *orig_buffer;
@@ -225,7 +232,7 @@
 	void *cbarg;
     
     #ifdef WIN32
-    HANDLE lock;
+    mutex lock;
     #endif
   
 };

Modified: libevent-urz/trunk/sa_evbuffer.c
===================================================================
--- libevent-urz/trunk/sa_evbuffer.c	2007-06-22 02:51:49 UTC (rev 10657)
+++ libevent-urz/trunk/sa_evbuffer.c	2007-06-22 07:28:28 UTC (rev 10658)
@@ -73,17 +73,21 @@
 void sa_bufferevent_setwatermark(struct sa_bufferevent *, short, size_t, size_t);
 void sa_bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t, void *);
 
+mutex status_mutex;
 u_char del_notifier_status = NOTIFIER_UNINIT;
 int del_notifier[2];
 static struct event evbuffer_del_event;
 
 static void notify() {
-    printf("Notify Called\n");
-    if(del_notifier_status == NOTIFIER_READY) {
-        printf("Notifying\n");
-        printf("Notify: write returned %d\n", pipe_write(del_notifier[EVBUFFER_END], ".", 1));
+    /* printf("Notify Called (Status: %d)\n", (int) del_notifier_status); */
+    ev_lock(status_mutex);
+    if(del_notifier_status == NOTIFIER_READY) { 
+        /* printf("Notifying\n"); */
+        /* printf("Notify: write returned %d\n", pipe_write(del_notifier[EVBUFFER_END], ".", 1)); */
+        pipe_write(del_notifier[EVBUFFER_END], ".", 1);
         del_notifier_status = NOTIFIER_PENDING;
     }
+    ev_unlock(status_mutex);
 }
 
 static void del_cb(int fd, short what, void *arg) 
@@ -92,13 +96,15 @@
     char readdata;
     ssize_t readlen;
     
-    printf("Delayed Callback Called\n");
+    /* printf("Delayed Callback Called\n"); */
     
     /* Read data out of socketpair */
     readlen = pipe_read(fd, &readdata, 1);
     
     /* Set notifier status back to ready */
+    ev_lock(status_mutex);
     del_notifier_status = NOTIFIER_READY;
+    ev_unlock(status_mutex);
     
     /*
      * Traverse bufev_list.
@@ -126,11 +132,14 @@
     u_long ioarg = 1;
     #endif
     
-    printf("sa_evbuffer initializing\n");
+    /* printf("sa_evbuffer initializing\n"); */
     
     if(del_notifier_status != NOTIFIER_UNINIT) {
         printf("Initialized already, returning\n");
         return;
+    } else {
+        status_mutex = CreateMutex(NULL, FALSE, NULL);
+        ev_lock(status_mutex);
     }
     
     /* perform list initialization as required by queue.h */
@@ -167,6 +176,7 @@
     }
     
     del_notifier_status = NOTIFIER_READY;
+    ev_unlock(status_mutex);
 }
 
 /*
@@ -277,7 +287,7 @@
 {
 	struct sa_bufferevent *bufev;
     
-    printf("sa_bufferevent_new called\n");
+    /* printf("sa_bufferevent_new called\n"); */
     /* Ensure delayed callbacks are set up. */
     sa_bufferevent_init();
 

Modified: libevent-urz/trunk/sample/sa_evbuffer-test.c
===================================================================
--- libevent-urz/trunk/sample/sa_evbuffer-test.c	2007-06-22 02:51:49 UTC (rev 10657)
+++ libevent-urz/trunk/sample/sa_evbuffer-test.c	2007-06-22 07:28:28 UTC (rev 10658)
@@ -117,7 +117,7 @@
     char output[1000];
     char *template, *module, *func, *file;
     
-    printf("Worker thread begun\n");
+    /* printf("Worker thread begun\n"); */
     
     while(1) {
         Sleep(1000);
@@ -130,9 +130,8 @@
         sprintf(output, template, module, func, file, rand() % 5000, rand());
         log_data(output);
         
-        printf("BufPrint:");
-        printf(logpipe->input->buffer);
-        printf("W");
+        /*printf("BufPrint:");
+        printf(logpipe->input->buffer);*/
     }
     return 0;
 }
@@ -140,7 +139,7 @@
 DWORD WINAPI logmgr_thread(LPVOID nodata) 
 {
     int error;
-    printf("Logmgr thread begun\n");
+    /* printf("Logmgr thread begun\n"); */
 
     error = event_dispatch();
     printf("Event dispach returned %d - error?\n", error);
@@ -154,7 +153,7 @@
     static char *indata = NULL;
     static char *floating_ptr = NULL;
     
-    printf("OnRead Called\n");
+    /* printf("OnRead Called\n"); */
     
     if(indata == NULL) {
         indata = malloc(indata_size);
@@ -170,7 +169,6 @@
     floating_ptr += len_read;
     
     while(1) {
-        printf("L");
         
         str_term = indata;
         while(*str_term != '\0' && str_term < floating_ptr) {
@@ -179,7 +177,6 @@
         
         if(str_term == floating_ptr) {
             /* Did not read entire string */
-            printf("&");
             return;
         }
         /* We now have a string starting at indata and ending at str_term */
@@ -192,7 +189,6 @@
         memmove(indata, str_term+1, (size_t) (floating_ptr - (str_term+1)));
         /* Reset the floating pointer to the new end of memory */
         floating_ptr = indata + (floating_ptr - (str_term+1));
-        /* printf("@"); */
     }
 }
 
@@ -201,11 +197,10 @@
     char data[1001];
     char *upto;
     
-    printf("printer thread begun\n");
+    /* printf("printer thread begun\n"); */
     
     while(1) {
         Sleep(1000);
-        printf("P");
         
         ret = sa_bufferevent_unload(logpipe, data, 1000);
         
@@ -218,7 +213,7 @@
         
         while(upto <= (data+ret)) {
             printf(upto);
-            upto += strlen(upto);
+            upto += strlen(upto)+1;
         }