sas             Sat Nov 30 23:53:59 2002 EDT

  Modified files:              
    /php4/ext/ircg      ircg.c 
  Log:
  Make error subsystem work properly in a multi-process environment
  
  
Index: php4/ext/ircg/ircg.c
diff -u php4/ext/ircg/ircg.c:1.147 php4/ext/ircg/ircg.c:1.148
--- php4/ext/ircg/ircg.c:1.147  Sat Nov 30 23:10:34 2002
+++ php4/ext/ircg/ircg.c        Sat Nov 30 23:53:59 2002
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: ircg.c,v 1.147 2002/12/01 04:10:34 sas Exp $ */
+/* $Id: ircg.c,v 1.148 2002/12/01 04:53:59 sas Exp $ */
 
 /* {{{ includes */
 
@@ -91,7 +91,9 @@
 static struct {
        ircg_hash_table h_fmt_msgs;
 
+       IRCG_LOCK(fmt_msgs_lock);
 #define h_fmt_msgs php_ircg->h_fmt_msgs
+#define fmt_msgs_lock php_ircg->fmt_msgs_lock
 
        /* these just serve statistical/entertainment purposes */
        unsigned long irc_connects, irc_set_currents, irc_quit_handlers, 
@@ -103,9 +105,14 @@
 #define exec_fmt_msgs  php_ircg->exec_fmt_msgs
 #define exec_token_compiler  php_ircg->exec_token_compiler
 
+       time_t next_gc;
+#define next_gc php_ircg->next_gc
 
-       IRCG_LOCK(fmt_msgs_lock);
-#define fmt_msgs_lock php_ircg->fmt_msgs_lock
+       struct errormsg *error_msgs;
+#define error_msgs php_ircg->error_msgs
+
+       IRCG_LOCK(error_msgs_lock);
+#define error_msgs_lock php_ircg->error_msgs_lock
 } *php_ircg;
 
 /* }}} */
@@ -1146,9 +1153,6 @@
        struct errormsg *next;
 };
 
-static time_t next_gc;
-static struct errormsg *errormsgs;
-
 static void error_msg_dtor(struct errormsg *m)
 {
        smart_str_free_ex(&m->msg, 1);
@@ -1160,10 +1164,11 @@
        struct errormsg *m, *prev = NULL, *next;
        time_t lim;
 
+       IRCG_LOCK_GET(error_msgs_lock);
        lim = now - GC_INTVL;
        next_gc = now + GC_INTVL;
        
-       for (m = errormsgs; m; prev = m, m = m->next) {
+       for (m = error_msgs; m; prev = m, m = m->next) {
                if (m->when < lim) {
                        struct errormsg *to;
                        /* Check whether we have subsequent outdated records */
@@ -1179,19 +1184,21 @@
                        if (prev)
                                prev->next = to;
                        else
-                               errormsgs = to;
+                               error_msgs = to;
                        
                        if (!to) break;
                        m = to;
                }
        }
+       IRCG_LOCK_PUT(error_msgs_lock);
 }
 
 static void add_error_msg(smart_str *msg, int msgid, php_irconn_t *conn)
 {
        struct errormsg *m;
 
-       for (m = errormsgs; m; m = m->next) {
+       IRCG_LOCK_GET(error_msgs_lock);
+       for (m = error_msgs; m; m = m->next) {
                if (m->id == conn->irconn_id) break;
        }
 
@@ -1205,25 +1212,28 @@
        m->msg.len = 0;
        smart_str_append_ex(&m->msg, msg, 1);
        m->msgid = msgid;
-       m->next = errormsgs;
-       errormsgs = m;
+       m->next = error_msgs;
+       error_msgs = m;
+       IRCG_LOCK_PUT(error_msgs_lock);
 }
 
 static struct errormsg *lookup_and_remove_error_msg(int id)
 {
        struct errormsg *m, *prev = NULL;
 
-       for (m = errormsgs; m; prev = m, m = m->next) {
+       IRCG_LOCK_GET(error_msgs_lock);
+       for (m = error_msgs; m; prev = m, m = m->next) {
                if (m->id == id) {
                        if (prev)
                                prev->next = m->next;
                        else
-                               errormsgs = m->next;
+                               error_msgs = m->next;
 
-                       return m;
+                       break;
                }
        }
-       return NULL;
+       IRCG_LOCK_PUT(error_msgs_lock);
+       return m;
 }
 
 static void error_handler(irconn_t *ircc, int id, int fatal, smart_str *msg, void 
*conn_data)
@@ -2269,6 +2279,7 @@
 
        conn->login = php_ircg_now();
                
+       /* XXX: we take chances by assuming that wordsize read/writes are atomic */
        if (conn->login >= next_gc)
                error_msg_gc(conn->login);
 
@@ -2586,6 +2597,7 @@
                memset(php_ircg, 0, sizeof *php_ircg);
        
                IRCG_LOCK_INIT(fmt_msgs_lock);
+               IRCG_LOCK_INIT(error_msgs_lock);
 
                for (i = 0; i < NO_FMTS; i++) {
                        token_compiler(fmt_msgs_default[i], 
&fmt_msgs_default_compiled.fmt_msgs[i]);
@@ -2644,6 +2656,7 @@
 #endif
 
        IRCG_LOCK_DESTROY(fmt_msgs_lock);
+       IRCG_LOCK_DESTROY(error_msgs_lock);
 
        ircg_shutdown_global();
        



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to