Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

event_destroy.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2005 by Kevin L. Mitchell <klmitch@mit.edu>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 **
00018 ** @(#)$Id: event_destroy.c,v 1.11 2005/07/12 15:14:38 klmitch Exp $
00019 */
00027 #include "engines_int.h"
00028 
00029 #include <ltdl.h>
00030 
00031 RCSTAG("@(#)$Id: event_destroy.c,v 1.11 2005/07/12 15:14:38 klmitch Exp $");
00032 
00033 ev_err_t
00034 event_destroy(ev_ctx_t *ctx)
00035 {
00036   ev_gens_t *cursor, *next;
00037   ev_genhdr_t *gen, *lastgen = 0;
00038   ev_engine_t *eng, *nexteng;
00039   event_t *ev, *nextev;
00040   int i;
00041 
00042   ev_init(); /* make sure library is initialized... */
00043 
00044   /* sanity-check the argument... */
00045   if (!ec_verify(ctx))
00046     ev_return(EINVAL);
00047   if (ec_flags(ctx) & EV_CTX_RUNNING)
00048     ev_return(EBUSY);
00049 
00050   /* release all free events... */
00051   for (ev = ctx->ec_evs; ev; ev = nextev) {
00052     nextev = ev->ev_next;
00053     free(ev); /* release the memory... */
00054     ctx->ec_evcnt--; /* update the count... */
00055   }
00056 
00057   ctx->ec_evs = 0; /* zero the event list */
00058   ev_assert(!ctx->ec_evcnt);
00059 
00060   /* Now go through the list of registered generators and destroy them... */
00061   for (cursor = ctx->ec_gens; cursor; cursor = next) {
00062     next = cursor->gs_next;
00063     _ev_debug(EVAS_LOG, __FILE__, __LINE__, _gca_func,
00064               "Freeing generators %u-%u(%u); next is %u-%u(%u)",
00065               cursor->gs_first, cursor->gs_first + cursor->gs_count - 1,
00066               cursor->gs_count, next ? next->gs_first : 0,
00067               next ? next->gs_first + next->gs_count - 1 : 0,
00068               next ? next->gs_count : 0);
00069     for (i = 0; i < cursor->gs_count; i++) {
00070       /* release all active generators... */
00071       while ((gen = cursor->gs_gens[i].gd_active.gl_list)) {
00072         if (gen == lastgen) /* did we succeed in releasing the generator? */
00073           ev_return(EV_ERR_NOFREE);
00074         lastgen = gen; /* remember last generator we looked at... */
00075         if (cursor->gs_gens[i].gd_release) /* call release function */
00076           (cursor->gs_gens[i].gd_release)(ctx, (void *)gen);
00077         else { /* XXX pull this out into a macro or function */
00078           if (gen->eg_next) /* clip out of current list... */
00079             gen->eg_next->eg_prev = gen->eg_prev;
00080           if (gen->eg_prev)
00081             gen->eg_prev->eg_next = gen->eg_next;
00082           else
00083             cursor->gs_gens[i].gd_active.gl_list = gen->eg_next;
00084           cursor->gs_gens[i].gd_active.gl_count--;
00085           free(gen); /* now release the generator */
00086         }
00087       }
00088 
00089       ev_assert(!cursor->gs_gens[i].gd_active.gl_list);
00090       ev_assert(!cursor->gs_gens[i].gd_active.gl_count);
00091 
00092       /* now walk the free list (XXX put in macro or function?) */
00093       for (gen = cursor->gs_gens[i].gd_free.gl_list; gen; gen = lastgen) {
00094         lastgen = gen->eg_next; /* save pointer to next in list */
00095         free(gen); /* and release this one */
00096         cursor->gs_gens[i].gd_free.gl_count--;
00097       }
00098       cursor->gs_gens[i].gd_free.gl_list = 0;
00099       ev_assert(!cursor->gs_gens[i].gd_free.gl_count);
00100     }
00101     free(cursor); /* release the memory */
00102   }
00103 
00104   ctx->ec_maxgen = 0;
00105   ctx->ec_gens = 0;
00106 
00107   /* destroy the configuration context... */
00108   tc_destroy(ec_conf(ctx)); /* never mind the error code... */
00109 
00110   /* Walk through the list of engines... */
00111   for (eng = ctx->ec_engine; eng; eng = nexteng) {
00112     nexteng = eng->eng_list.el_next;
00113     if (eng->eng_fini && (eng->eng_runfl & EV_ENGINE_RUNNING))
00114       eng_fini(ctx, eng); /* call finalization function */
00115 
00116     /* no need to clip it from the list thanks to the nexteng pointer */
00117 
00118     if (eng->eng_handle) /* it was dynamically loaded; close it */
00119       lt_dlclose((lt_dlhandle)eng->eng_handle);
00120   }
00121 
00122   ctx->ec_engine = 0; /* zero out engine pointers */
00123   ctx->ec_socket.el_first = ctx->ec_socket.el_last = 0;
00124   ctx->ec_signal.el_first = ctx->ec_signal.el_last = 0;
00125   ctx->ec_timer.el_first = ctx->ec_timer.el_last = 0;
00126 
00127   /* If we initialized libltdl, we now need to clean it up... */
00128   if (ctx->ec_flags & EV_CTX_LTDLINIT)
00129     lt_dlexit();
00130 
00131   /* Finally, clear the flags and the magic number */
00132   ctx->ec_flags = 0;
00133   ctx->ec_magic = 0;
00134 
00135   ev_return(0); /* All done! */
00136 }

Generated on Wed Dec 28 23:36:56 2005 for event by  doxygen 1.4.4