Fixed the SMTP sending as a thread. Now the thread doesn't think its
authorDave West <davew@uncensored.citadel.org>
Sat, 14 Nov 2009 16:17:06 +0000 (16:17 +0000)
committerDave West <davew@uncensored.citadel.org>
Sat, 14 Nov 2009 16:17:06 +0000 (16:17 +0000)
running when it isn't.

citadel/modules/smtp/serv_smtp.c

index 3eb235b300351176389b03598a5879ae744308d3..917ea5579758e78a44f143ad2c62fe9346093203 100644 (file)
@@ -122,7 +122,7 @@ enum {                              /* Command states for login authentication */
 
 int run_queue_now = 0; /* Set to 1 to ignore SMTP send retry times */
 
-/* citthread_mutex_t smtp_send_lock; */
+citthread_mutex_t smtp_send_lock;
 
 
 /*****************************************************************************/
@@ -1723,14 +1723,14 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
  * 
  * Run through the queue sending out messages.
  */
-void *smtp_do_queue(void /* *arg */) {
+void *smtp_do_queue(void *arg) {
        int num_processed = 0;
-       /* struct CitContext smtp_queue_CC; */
+       struct CitContext smtp_queue_CC;
 
        CtdlLogPrintf(CTDL_INFO, "SMTP client: processing outbound queue\n");
 
-       /* CtdlFillSystemContext(&smtp_queue_CC, "SMTP Send");
-       citthread_setspecific(MyConKey, (void *)&smtp_queue_CC ); */
+       CtdlFillSystemContext(&smtp_queue_CC, "SMTP Send");
+       citthread_setspecific(MyConKey, (void *)&smtp_queue_CC );
 
        if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
                CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", SMTP_SPOOLOUT_ROOM);
@@ -1739,8 +1739,7 @@ void *smtp_do_queue(void /* *arg */) {
                num_processed = CtdlForEachMessage(MSGS_ALL, 0L, NULL, SPOOLMIME, NULL, smtp_do_procmsg, NULL);
        }
 
-       run_queue_now = 0;
-       /* citthread_mutex_unlock (&smtp_send_lock); */
+       citthread_mutex_unlock (&smtp_send_lock);
        CtdlLogPrintf(CTDL_INFO, "SMTP client: queue run completed; %d messages processed\n", num_processed);
        return(NULL);
 }
@@ -1760,25 +1759,25 @@ void *smtp_do_queue(void /* *arg */) {
  */
 void smtp_queue_thread (void)
 {
-       /*
-        * if (citthread_mutex_trylock (&smtp_send_lock)) {
-        *      CtdlLogPrintf(CTDL_DEBUG, "SMTP queue run already in progress\n");
-        * }
-        * else {
-        *      CtdlThreadCreate("SMTP Send", CTDLTHREAD_BIGSTACK, smtp_do_queue, NULL);
-        * }
-        */
-
-       static int queue_running = 0;   /* The mutex locking isn't working.  Gotta do this. */
-       if (!queue_running) {
-               queue_running = 1;
-               smtp_do_queue();
-               queue_running = 0;
+       if (citthread_mutex_trylock (&smtp_send_lock)) {
+               CtdlLogPrintf(CTDL_DEBUG, "SMTP queue run already in progress\n");
+       }
+       else {
+               CtdlThreadCreate("SMTP Send", CTDLTHREAD_BIGSTACK, smtp_do_queue, NULL);
        }
 }
 
 
 
+void smtp_server_going_down (void)
+{
+       CtdlLogPrintf(CTDL_DEBUG, "SMTP module clean up for shutdown.\n");
+
+       citthread_mutex_destroy (&smtp_send_lock);
+}
+
+
+
 /*****************************************************************************/
 /*                          SMTP UTILITY COMMANDS                            */
 /*****************************************************************************/
@@ -1859,7 +1858,6 @@ void smtp_cleanup_function(void) {
 
        CtdlLogPrintf(CTDL_DEBUG, "Performing SMTP cleanup hook\n");
        free(SMTP);
-       /* citthread_mutex_destroy (&smtp_send_lock); */
 }
 
 
@@ -1915,7 +1913,8 @@ CTDL_MODULE_INIT(smtp)
                CtdlRegisterSessionHook(smtp_queue_thread, EVT_TIMER);
                CtdlRegisterSessionHook(smtp_cleanup_function, EVT_STOP);
                CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
-               /* citthread_mutex_init (&smtp_send_lock, NULL); */
+               CtdlRegisterCleanupHook (smtp_server_going_down);
+               citthread_mutex_init (&smtp_send_lock, NULL);
        }
        
        /* return our Subversion id for the Log */