]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
We should only initialize a realtime queue when it is allocated, not every time we...
authorMark Michelson <mmichelson@digium.com>
Wed, 12 Sep 2007 14:24:53 +0000 (14:24 +0000)
committerMark Michelson <mmichelson@digium.com>
Wed, 12 Sep 2007 14:24:53 +0000 (14:24 +0000)
from being reallocated every time the queue is accessed.

I also removed a debug message I had accidentally left in on a previous commit.

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@82274 65c4cc65-6c06-0410-ace0-fbb531ad65f3

apps/app_queue.c
apps/app_voicemail.c

index 4d12245d46ca6c17130a3a2919335f1374d14d45..79a83f376a3596c5706587de5d5c84f7790cfc60 100644 (file)
@@ -1109,9 +1109,9 @@ static struct call_queue *find_queue_by_name_rt(const char *queuename, struct as
                ast_mutex_lock(&q->lock);
                clear_queue(q);
                q->realtime = 1;
+               init_queue(q);          /* Ensure defaults for all parameters not set explicitly. */
                AST_LIST_INSERT_HEAD(&queues, q, list);
        }
-       init_queue(q);          /* Ensure defaults for all parameters not set explicitly. */
 
        memset(tmpbuf, 0, sizeof(tmpbuf));
        for (v = queue_vars; v; v = v->next) {
@@ -4056,7 +4056,6 @@ static int __queues_show(struct mansession *s, int manager, int fd, int argc, ch
                                continue;
                        }
                }
-               ast_log(LOG_DEBUG, "MEMBERCOUNT is %d\n", q->membercount);
                max_buf[0] = '\0';
                max = max_buf;
                max_left = sizeof(max_buf);
index 4c41ce445c32a219f3a5631bb3b111ee2c815406..801f5426b9f226a1a4a9ea02dd88b05b15716952 100644 (file)
@@ -131,7 +131,7 @@ static void check_msgArray(struct vm_state *vms);
 static void copy_msgArray(struct vm_state *dst, struct vm_state *src);
 static int save_body(BODY *body, struct vm_state *vms, char *section, char *format);
 static int make_gsm_file(char *dest, char *imapuser, char *dir, int num);
-static void get_mailbox_delimiter(MAILSTREAM *stream);
+static void get_mailbox_delimiter(struct vm_state *vms);
 static void mm_parsequota (MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pquota);
 static void imap_mailbox_name(char *spec, struct vm_state *vms, int box, int target);
 static int imap_store_file(char *dir, char *mailboxuser, char *mailboxcontext, int msgnum, struct ast_channel *chan, struct ast_vm_user *vmu, char *fmt, int duration, struct vm_state *vms);
@@ -358,6 +358,7 @@ struct vm_state {
        int starting;
        int repeats;
 #ifdef IMAP_STORAGE
+       ast_mutex_t lock; /*Lock used around c-client mail_* calls so there are no races*/
        int updated; /* decremented on each mail check until 1 -allows delay */
        long msgArray[256];
        MAILSTREAM *mailstream;
@@ -890,7 +891,9 @@ static void vm_imap_delete(int msgnum, struct vm_state *vms)
                ast_log(LOG_DEBUG, "deleting msgnum %d, which is mailbox message %lu\n",msgnum,messageNum);
        /* delete message */
        sprintf (arg,"%lu",messageNum);
+       ast_mutex_lock(&vms->lock);
        mail_setflag (vms->mailstream,arg,"\\DELETED");
+       ast_mutex_unlock(&vms->lock);
 }
 
 #endif
@@ -2346,8 +2349,10 @@ static int imap_store_file(char *dir, char *mailboxuser, char *mailboxcontext, i
                INIT(&str, mail_string, buf, len);
                init_mailstream(vms, 0);
                imap_mailbox_name(mailbox, vms, 0, 1);
+               ast_mutex_lock(&vms->lock);
                if(!mail_append(vms->mailstream, mailbox, &str))
                        ast_log(LOG_ERROR, "Error while sending the message to %s\n", mailbox);
+               ast_mutex_unlock(&vms->lock);
                fclose(p);
                unlink(tmp);
                ast_free(buf);
@@ -2430,6 +2435,7 @@ static int messagecount(const char *context, const char *mailbox, const char *fo
                return -1;
        }
        if (ret == 0) {
+               ast_mutex_lock(&vms_p->lock);
                pgm = mail_newsearchpgm ();
                hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", (char *)mailbox);
                pgm->header = hdr;
@@ -2456,9 +2462,12 @@ static int messagecount(const char *context, const char *mailbox, const char *fo
                /*Freeing the searchpgm also frees the searchhdr*/
                mail_free_searchpgm(&pgm);
                vms_p->updated = 0;
+               ast_mutex_unlock(&vms_p->lock);
                return vms_p->vmArrayIndex;
        } else {  
+               ast_mutex_lock(&vms_p->lock);
                mail_ping(vms_p->mailstream);
+               ast_mutex_unlock(&vms_p->lock);
        }
        return 0;
 }
@@ -2557,8 +2566,12 @@ static int copy_message(struct ast_channel *chan, struct ast_vm_user *vmu, int i
        }
        imap_mailbox_name(dest, destvms, imbox, 1);
        snprintf(messagestring, sizeof(messagestring), "%ld", sendvms->msgArray[msgnum]);
-       if((mail_copy(sendvms->mailstream, messagestring, dest) == T))
+       ast_mutex_lock(&sendvms->lock);
+       if((mail_copy(sendvms->mailstream, messagestring, dest) == T)) {
+               ast_mutex_unlock(&sendvms->lock);
                return 0;
+       }
+       ast_mutex_unlock(&sendvms->lock);
        ast_log(LOG_WARNING, "Unable to copy message from mailbox %s to mailbox %s\n", vmu->mailbox, recip->mailbox);
        return -1;
 }
@@ -3200,7 +3213,9 @@ static int save_to_folder(struct ast_vm_user *vmu, struct vm_state *vms, int msg
        imap_mailbox_name(dbox, vms, box, 1);
        if(option_debug > 2)
                ast_log(LOG_DEBUG, "Copying sequence %s to mailbox %s\n",sequence,dbox);
+       ast_mutex_lock(&vms->lock);
        res = mail_copy(vms->mailstream,sequence,dbox);
+       ast_mutex_unlock(&vms->lock);
        if (res == 1) return 0;
        return 1;
 #else
@@ -4080,7 +4095,9 @@ static int forward_message(struct ast_channel *chan, char *context, struct vm_st
                                }
 
                                /* This will only work for new messages... */
+                               ast_mutex_lock(&vms->lock);
                                header_content = mail_fetchheader (vms->mailstream, vms->msgArray[vms->curmsg]);
+                               ast_mutex_unlock(&vms->lock);
                                /* empty string means no valid header */
                                if (ast_strlen_zero(header_content)) {
                                        ast_log (LOG_ERROR,"Could not fetch header for message number %ld\n",vms->msgArray[vms->curmsg]);
@@ -4113,7 +4130,9 @@ static int forward_message(struct ast_channel *chan, char *context, struct vm_st
                                if(option_debug > 2)
                                        ast_log (LOG_DEBUG,"Before mail_fetchstructure, message number is %ld, filename is:%s\n",vms->msgArray[vms->curmsg], vms->fn);
                                /*mail_fetchstructure (mailstream, vmArray[0], &body); */
+                               ast_mutex_lock(&vms->lock);
                                mail_fetchstructure (vms->mailstream, vms->msgArray[vms->curmsg], &body);
+                               ast_mutex_unlock(&vms->lock);
                                save_body(body,vms,"3","gsm");
                                /* should not assume "fmt" here! */
                                save_body(body,vms,"2",fmt);
@@ -4409,16 +4428,19 @@ static int play_message(struct ast_channel *chan, struct ast_vm_user *vmu, struc
        }
 
        /* This will only work for new messages... */
+       ast_mutex_lock(&vms->lock);
        header_content = mail_fetchheader (vms->mailstream, vms->msgArray[vms->curmsg]);
        /* empty string means no valid header */
        if (ast_strlen_zero(header_content)) {
                ast_log (LOG_ERROR,"Could not fetch header for message number %ld\n",vms->msgArray[vms->curmsg]);
+               ast_mutex_unlock(&vms->lock);
                return -1;
        }
        snprintf(todir, sizeof(todir), "%s%s/%s/tmp", VM_SPOOL_DIR, vmu->context, vmu->mailbox);
        make_gsm_file(vms->fn, vms->imapuser, todir, vms->curmsg);
 
        mail_fetchstructure (vms->mailstream,vms->msgArray[vms->curmsg],&body);
+       ast_mutex_unlock(&vms->lock);
        
        /* We have the body, now we extract the file name of the first attachment. */
        if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
@@ -4677,12 +4699,14 @@ static int init_mailstream(struct vm_state *vms, int box)
 #include "linkage.c"
                /* Connect to INBOX first to get folders delimiter */
                imap_mailbox_name(tmp, vms, 0, 0);
+               ast_mutex_lock(&vms->lock);
                stream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
+               ast_mutex_unlock(&vms->lock);
                if (stream == NIL) {
                        ast_log (LOG_ERROR, "Can't connect to imap server %s\n", tmp);
                        return NIL;
                }
-               get_mailbox_delimiter(stream);
+               get_mailbox_delimiter(vms);
                /* update delimiter in imapfolder */
                for(cp = imapfolder; *cp; cp++)
                        if(*cp == '/')
@@ -4692,7 +4716,9 @@ static int init_mailstream(struct vm_state *vms, int box)
        imap_mailbox_name(tmp, vms, box, 1);
        if(option_debug > 2)
                ast_log (LOG_DEBUG,"Before mail_open, server: %s, box:%d\n", tmp, box);
+       ast_mutex_lock(&vms->lock);
        vms->mailstream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
+       ast_mutex_unlock(&vms->lock);
        if (vms->mailstream == NIL) {
                return -1;
        } else {
@@ -4717,10 +4743,13 @@ static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box)
        }
 
        /* Check Quota (here for now to test) */
+       ast_mutex_lock(&vms->lock);
        mail_parameters(NULL, SET_QUOTA, (void *) mm_parsequota);
+       ast_mutex_unlock(&vms->lock);
        imap_mailbox_name(dbox, vms, box, 1);
        imap_getquotaroot(vms->mailstream, dbox);
 
+       ast_mutex_lock(&vms->lock);
        pgm = mail_newsearchpgm();
 
        /* Check IMAP folder for Asterisk messages only... */
@@ -4747,6 +4776,7 @@ static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box)
        vms->lastmsg = vms->vmArrayIndex - 1;
 
        mail_free_searchpgm(&pgm);
+       ast_mutex_unlock(&vms->lock);
        return 0;
 }
 #else
@@ -6868,12 +6898,14 @@ out:
        if(option_debug > 2)
                ast_log(LOG_DEBUG, "*** Checking if we can expunge, deleted set to %d, expungeonhangup set to %d\n",deleted,expungeonhangup);
        if (vmu && deleted == 1 && expungeonhangup == 1) {
+               ast_mutex_lock(&vms.lock);
 #ifdef HAVE_IMAP_TK2006
                if (LEVELUIDPLUS (vms.mailstream)) {
                        mail_expunge_full(vms.mailstream,NIL,EX_UID);
                } else 
 #endif
                        mail_expunge(vms.mailstream);
+               ast_mutex_unlock(&vms.lock);
        }
        /*  before we delete the state, we should copy pertinent info
         *  back to the persistent model */
@@ -7922,7 +7954,9 @@ static int advanced_options(struct ast_channel *chan, struct ast_vm_user *vmu, s
        }
 
        /* This will only work for new messages... */
+       ast_mutex_lock(&vms->lock);
        header_content = mail_fetchheader (vms->mailstream, vms->msgArray[vms->curmsg]);
+       ast_mutex_unlock(&vms->lock);
        /* empty string means no valid header */
        if (ast_strlen_zero(header_content)) {
                ast_log (LOG_ERROR,"Could not fetch header for message number %ld\n",vms->msgArray[vms->curmsg]);
@@ -8958,6 +8992,7 @@ static void vmstate_delete(struct vm_state *vms)
        if (!vf) {
                ast_log(LOG_ERROR, "No vmstate found for user:%s, mailbox %s\n",vms->imapuser,vms->username);
        } else {
+               ast_mutex_destroy(&vf->vms->lock);
                free(vf);
        }
        ast_mutex_unlock(&vmstate_lock);
@@ -8989,6 +9024,7 @@ static void init_vm_state(struct vm_state *vms)
        for (x = 0; x < 256; x++) {
                vms->msgArray[x] = 0;
        }
+       ast_mutex_init(&vms->lock);
 }
 
 static void check_msgArray(struct vm_state *vms) 
@@ -9021,7 +9057,9 @@ static int save_body(BODY *body, struct vm_state *vms, char *section, char *form
        if (!body || body == NIL)
                return -1;
        display_body (body, NIL, (long) 0);
+       ast_mutex_lock(&vms->lock);
        body_content = mail_fetchbody (vms->mailstream, vms->msgArray[vms->curmsg], section, &len);
+       ast_mutex_unlock(&vms->lock);
        if (body_content != NIL) {
                sprintf(filename,"%s.%s", vms->fn, format);
                /* ast_log (LOG_DEBUG,body_content); */
@@ -9032,10 +9070,12 @@ static int save_body(BODY *body, struct vm_state *vms, char *section, char *form
 }
 
 /* get delimiter via mm_list callback */
-static void get_mailbox_delimiter(MAILSTREAM *stream) {
+static void get_mailbox_delimiter(struct vm_state *vms) {
        char tmp[50];
        sprintf(tmp, "{%s}", imapserver);
-       mail_list(stream, tmp, "*");
+       ast_mutex_lock(&vms->lock);
+       mail_list(vms->mailstream, tmp, "*");
+       ast_mutex_unlock(&vms->lock);
 }
 
 #endif /* IMAP_STORAGE */