Ejemplo n.º 1
0
void *iqueue_init()
{
    QUEUE *q = NULL;

    if((q = (QUEUE *)xmm_mnew(sizeof(QUEUE))))
    {
        MUTEX_INIT(q->mutex);
    }
    return q;
}
Ejemplo n.º 2
0
void xqueue_push(void *xqueue, int qid, void *ptr)
{
    XQNODE *node = NULL, *nodes = NULL;
    XQUEUE *q = (XQUEUE *)xqueue;
    XQROOT *root = NULL;
    int i = 0;

    if(q)
    {
        if(q && qid > 0 && qid < XQ_ROOTS_MAX && (root = &(q->roots[qid])))
        {
            MUTEX_LOCK(q->mutex);
            if((node = q->left))
            {
                q->left = node->next;
                q->nleft--;
            }
            else 
            {
                if((nodes = (XQNODE *)xmm_mnew(sizeof(XQNODE) * XQ_NODES_MAX)))
                {
                    q->list[q->nlist++] = nodes;
                    for(i = 1; i < XQ_NODES_MAX; i++)
                    {
                        nodes[i].next = q->left;
                        q->left = &nodes[i];
                    }
                    node = &(nodes[0]);
                }
                else
                {
                    //fprintf(stderr, xmm_new failed, %s\n", strerror(errno));
                }
            }
            if(node)
            {
                node->ptr = ptr;
                if(root->last)
                {
                    root->last->next = node;
                    root->last = node;
                }
                else
                {
                    root->first = root->last = node;
                }
                node->next = NULL;
                root->total++;
            }
            MUTEX_UNLOCK(q->mutex);
        }
    }
    return ;
}
Ejemplo n.º 3
0
/* initialize */
QINDEX *qindex_init()
{
    QINDEX *qindex = NULL;

    if((qindex = (QINDEX *)xmm_mnew(sizeof(QINDEX))))
    {
        MUTEX_INIT(qindex->mutex);
        qindex->queue                    = iqueue_init();
    }
    return qindex;
}
Ejemplo n.º 4
0
void iqueue_push(void *queue, int data)
{
    QNODE *node = NULL, *tmp = NULL;
    QUEUE *q = (QUEUE *)queue;
    int i = 0;

    if(q)
    {
        MUTEX_LOCK(q->mutex);
        if((node = q->left))
        {
            q->left = node->next;
        }
        else 
        {
            if((i = q->nlist) < QNODE_LINE_MAX 
                && (node = (QNODE *)xmm_mnew(QNODE_LINE_NUM * sizeof(QNODE))))
            {
                q->list[i] = node;
                q->nlist++;
                i = 1;
                while(i  < QNODE_LINE_NUM)
                {
                    tmp = &(node[i]); 
                    tmp->next = q->left;
                    q->left = tmp;
                    ++i;
                }
            }
        }
        if(node)
        {
            node->data = data;
            if(q->last)
            {
                q->last->next = node;
                q->last = node;
            }
            else
            {
                q->first = q->last = node;
            }
            node->next = NULL;
            q->total++;
        }
        MUTEX_UNLOCK(q->mutex);
    }
    return ;
}
Ejemplo n.º 5
0
void *xqueue_init()
{
    XQUEUE *q = NULL;
    int i = 0, j = 0;

    if((q = (XQUEUE *)xmm_mnew(sizeof(XQUEUE)))) 
    {
        //memset(q, 0, sizeof(XQUEUE));
        for(i = 1; i < XQ_ROOTS_MAX; i++)
        {
            q->waits[j++] = i;
            q->nwaits++;
        }
        MUTEX_INIT(q->mutex);
    }
    return q;
}
Ejemplo n.º 6
0
/* new mindex */
MINDEX *mindex_new(QINDEX *qindex)
{
    MINDEX *mindex = NULL;
    if(strlen(qindex->qtask_server_host) > 0 && qindex->qtask_server_port > 0
            && (qindex->qtask_commitid > 0 || qindex->qtask_queueid > 0)
            && (mindex = xmm_mnew(sizeof(MINDEX))))
    {
        /* set mtask */
        mtask_set(&(mindex->mtask), qindex->qtask_server_host, qindex->qtask_server_port,
                qindex->qtask_commitid, qindex->qtask_queueid);
        /* set db connection*/
        if(dbase_set(&(mindex->db), qindex->db_host, qindex->db_port) != 0
                || dbase_connect(&(mindex->db)) != 0)
        {
            FATAL_LOGGER(qindex->logger, "connect_source_db(%s:%d) failed, %s", qindex->db_host, qindex->db_port, strerror(errno));
        }
    }
    return mindex;
}
Ejemplo n.º 7
0
MMQUEUE *mmqueue_init(char *qfile)
{
    struct stat st = {0};
    MMQUEUE *mmq = NULL;

    if(qfile && (mmq = (MMQUEUE *)xmm_mnew(sizeof(MMQUEUE))))
    {
        MUTEX_INIT(mmq->mutex);
        if((mmq->fd = open(qfile, O_CREAT|O_RDWR, 0644)) > 0
            && fstat(mmq->fd, &st) == 0)
        {
            if(st.st_size == 0) 
            {
                mmq->end = sizeof(MMQSTATE);
                if(ftruncate(mmq->fd, mmq->end) != 0) _exit(-1);
            }
            else 
                mmq->end = st.st_size;
            mmq->size = sizeof(MMQNODE) * MMQ_NODE_MAX + sizeof(MMQSTATE);
            if(mmq->size < st.st_size) mmq->size = st.st_size;
            if((mmq->map = mmap(NULL, mmq->size, PROT_READ|PROT_WRITE, MAP_SHARED,
                        mmq->fd, 0)) == NULL || mmq->map == (void *)-1)
            {
                fprintf(stderr, "mmap qfile:%s failed, %s\n", qfile, strerror(errno));
                _exit(-1);
            }
            mmq->state = (MMQSTATE *)mmq->map;
            if(mmq->end == sizeof(MMQSTATE)) memset(mmq->state, 0, sizeof(MMQSTATE));
            mmq->nodes = (MMQNODE *)((char *)mmq->map + sizeof(MMQSTATE));
        }
        else
        {
            fprintf(stderr, "open qfile:%s failed, %s\n", qfile, strerror(errno));
            _exit(-1);
        }
    }
    return mmq;
}
Ejemplo n.º 8
0
/* Initialize procthread */
PROCTHREAD *procthread_init(int cond)
{
    PROCTHREAD *pth = NULL;

    if((pth = (PROCTHREAD *)xmm_mnew(sizeof(PROCTHREAD))))
    {
        if((pth->cond = pth->have_evbase = cond) > 0)
        {
            if((pth->evbase   = evbase_init(0)) == NULL)
            {
                fprintf(stderr, "Initialize evbase failed, %s\n", strerror(errno));
                _exit(-1);
            }
        }
        MUTEX_INIT(pth->mutex);
        pth->message_queue          = qmessage_init();
        pth->run                    = procthread_run;
        pth->set_acceptor           = procthread_set_acceptor;
        pth->pushconn               = procthread_pushconn;
        pth->newconn                = procthread_newconn;
        pth->addconn                = procthread_addconn;
        pth->add_connection         = procthread_add_connection;
        pth->newtask                = procthread_newtask;
        pth->newtransaction         = procthread_newtransaction;
        pth->shut_connection        = procthread_shut_connection;
        pth->over_connection        = procthread_over_connection;
        pth->terminate_connection   = procthread_terminate_connection;
        pth->stop                   = procthread_stop;
        pth->wakeup                 = procthread_wakeup;
        pth->terminate              = procthread_terminate;
        pth->state                  = procthread_state;
        pth->active_heartbeat       = procthread_active_heartbeat;
        pth->clean                  = procthread_clean;
    }
    return pth;
}
Ejemplo n.º 9
0
Archivo: chunk.c Proyecto: 5bruce/sbase
/* initialize chunk */
CHUNK *chunk_init()
{
    CHUNK *chunk = NULL;
    chunk = (CHUNK *)xmm_mnew(sizeof(CHUNK));
    return chunk;
}
Ejemplo n.º 10
0
/* 
 * initialize wtable 
 * */
WTABLE *wtable_init(char *dir)
{
    char path[WT_PATH_MAX], *p = NULL;
    struct stat st = {0};
    WTABLE *w = NULL;
    int n = 0;

    if(dir && (w = (WTABLE *)xmm_mnew(sizeof(WTABLE))))
    {
        n = sprintf(path, "%s/%s", dir, WT_LOG_NAME);
        force_mkdir(path);
        p = path;
        LOGGER_INIT(w->logger, p);
        /* state */
        n = sprintf(path, "%s/%s", dir, WT_STATE_NAME);
        if((w->statefd = open(path, O_CREAT|O_RDWR, 0644)) <= 0
                || fstat(w->statefd, &st) != 0)
        {
            if(w->statefd > 0) close(w->statefd);
            FATAL_LOGGER(w->logger, "open state file[%s] failed, %s",
                    path, strerror(errno));
            _exit(-1);
        }
        else
        {
            if(st.st_size < sizeof(WSTATE)
                    && ftruncate(w->statefd, sizeof(WSTATE)) != 0)
            {
                _exit(-1);
            }
            if((w->state = (WSTATE *)mmap(NULL, sizeof(WSTATE),
                            PROT_READ|PROT_WRITE, MAP_SHARED, w->statefd, 0)) == NULL
                    || w->state == (void *)-1)
            {
                FATAL_LOGGER(w->logger, "mmap state failed, %s", strerror(errno));
                _exit(-1);
            }
            if(st.st_size < sizeof(WSTATE))
                memset(((char *)w->state + st.st_size), 0, sizeof(WSTATE) - st.st_size);
        }
        w->workers = w->state->workers;
        memset(w->workers, 0, sizeof(WORKER) * W_WORKER_MAX);
        w->state->nworkers = 0;
        w->state->conn_total = 0;
        n = sprintf(path, "%s/%s", dir, WT_MDB_DIR);
        w->mdb = db_init(path, DB_USE_MMAP);
        /* mmtrie */
        n = sprintf(path, "%s/%s", dir, WT_MAP_NAME);
        if((w->map = mmtrie_init(path)) == NULL) _exit(-1);
        /* appmap */
        n = sprintf(path, "%s/%s", dir, WT_APPS_NAME);
        if((w->appmap = mmtree64_init(path)) == NULL) _exit(-1);
        mmtree64_use_all(w->appmap);
        /* logger & mutex & mtree & mqueue */
        if((w->queue = mqueue_init()) == NULL) _exit(-1);
        if((w->mtree = mtree_init()) == NULL) _exit(-1);
        w->whitelist = mtree_new_tree(w->mtree);
        //MQ(w->queue)->logger = w->logger;
        MUTEX_INIT(w->mutex);
    }
    return w;
}
Ejemplo n.º 11
0
/* initialize XMAP */
XMAP *xmap_init(char *basedir)
{
    char path[XM_PATH_MAX];
    struct stat st = {0};
    XMAP *xmap = NULL;
    int i = 0, k = 0, v = 0;
    unsigned int id  = 0;

    if(basedir && (xmap = (XMAP *)xmm_mnew(sizeof(XMAP))))
    {
        //xmap->mtrie = mtrie_init();
        MUTEX_INIT(xmap->mutex);
        /* logger */
        sprintf(path, "%s/%s", basedir, "xmap.log");
        xmap_mkdir(path);
        LOGGER_INIT(xmap->logger, path);
        /* tree */
        sprintf(path, "%s/%s", basedir, "xmap.tree");
        xmap->tree = mmtree_init(path);
        sprintf(path, "%s/%s", basedir, "xmap.tree64");
        xmap->tree64 = mmtree64_init(path);
        /* kmap */
        sprintf(path, "%s/%s", basedir, "xmap.kmap");
        xmap->kmap = mmtrie_init(path);
        /* queue */
        sprintf(path, "%s/%s", basedir, "xmap.queue");
        xmap->queue = mmqueue_init(path);
        /* db */
        sprintf(path, "%s/%s", basedir, "cache/");
        xmap->db = cdb_init(path, CDB_USE_MMAP);
        /* state */
        sprintf(path, "%s/%s", basedir, "xmap.state");
        if((xmap->stateio.fd = open(path, O_CREAT|O_RDWR, 0644)) > 0
                && fstat(xmap->stateio.fd, &st) == 0)
        {
            if((xmap->stateio.map = mmap(NULL, sizeof(XMSTATE), PROT_READ|PROT_WRITE,
                                         MAP_SHARED, xmap->stateio.fd, 0)) == NULL
                    || xmap->stateio.map == (void *)-1)
            {
                FATAL_LOGGER(xmap->logger, "mmap state:%s failed, %s", path, strerror(errno));
                _exit(-1);
            }
            xmap->state = (XMSTATE *)(xmap->stateio.map);
            xmap->stateio.end = st.st_size;
            if(st.st_size == 0)
            {
                xmap->stateio.end = xmap->stateio.size = sizeof(XMSTATE);
                if(ftruncate(xmap->stateio.fd, xmap->stateio.end) != 0)
                {
                    FATAL_LOGGER(xmap->logger, "ftruncate state %s failed, %s", path, strerror(errno));
                    _exit(-1);
                }
                memset(xmap->state, 0, sizeof(XMSTATE));
            }
            for(i = 0; i < DBASE_MASK_MAX; i++)
            {
                if(xmap->state->masks[i].root == 0)
                    xmap->state->masks[i].root = mmtree64_new_tree(xmap->tree64);
            }
        }
        /* disk */
        sprintf(path, "%s/%s", basedir, "xmap.disk");
        if((xmap->diskio.fd = open(path, O_CREAT|O_RDWR, 0644)) > 0
                && fstat(xmap->diskio.fd, &st) == 0)
        {
            if((xmap->diskio.map = mmap(NULL, sizeof(MDISK) * XM_DISK_MAX, PROT_READ|PROT_WRITE,
                                        MAP_SHARED, xmap->diskio.fd, 0)) == NULL
                    || xmap->diskio.map == (void *)-1)
            {
                FATAL_LOGGER(xmap->logger, "mmap disk:%s failed, %s", path, strerror(errno));
                _exit(-1);
            }
            xmap->disks = (MDISK *)(xmap->diskio.map);
            xmap->diskio.end = st.st_size;
        }
        /* meta */
        sprintf(path, "%s/%s", basedir, "xmap.meta");
        if((xmap->metaio.fd = open(path, O_CREAT|O_RDWR, 0644)) > 0
                && fstat(xmap->metaio.fd, &st) == 0)
        {
            if((xmap->metaio.map = mmap(NULL, sizeof(XMMETA) * XM_META_MAX, PROT_READ|PROT_WRITE,
                                        MAP_SHARED, xmap->metaio.fd, 0)) == NULL
                    || xmap->metaio.map == (void *)-1)
            {
                FATAL_LOGGER(xmap->logger, "mmap meta:%s failed, %s", path, strerror(errno));
                _exit(-1);
            }
            xmap->metas = (XMMETA *)(xmap->metaio.map);
            xmap->metaio.end = st.st_size;
            xmap->start_time = time(NULL);
        }
        if(!xmap->state->qwait) xmap->state->qwait = mmtree_new_tree(xmap->tree);
        if(!xmap->state->qleft) xmap->state->qleft = mmqueue_new(xmap->queue);
        xmap->state->id_wait = 0;
        while((id = mmtree_min(xmap->tree, xmap->state->qwait, &k, &v)))
        {
            mmtree_remove(xmap->tree, xmap->state->qwait, id, NULL, NULL);
        }
        while(mmqueue_pop(xmap->queue, xmap->state->qleft, (int *)&id) > 0);
    }
    return xmap;
}
Ejemplo n.º 12
0
/* Initialize evbase */
EVBASE *evbase_init()
{
    EVBASE *evbase = NULL;

    if((evbase = (EVBASE *)xmm_mnew(sizeof(EVBASE))))
    {
        MUTEX_RESET(evbase->mutex);
#ifdef HAVE_EVPORT
        evops_default = EOP_PORT;
        evops[EOP_PORT].name = "PORT";
        evops[EOP_PORT].init      = evport_init;
        evops[EOP_PORT].add       = evport_add;
        evops[EOP_PORT].update    = evport_update;
        evops[EOP_PORT].del       = evport_del;
        evops[EOP_PORT].loop      = evport_loop;
        evops[EOP_PORT].reset     = evport_reset;
        evops[EOP_PORT].clean     = evport_clean;
#endif
#ifdef HAVE_EVSELECT
        evops_default = EOP_SELECT;
        evops[EOP_SELECT].name    = "SELECT";
        evops[EOP_SELECT].init    = evselect_init;
        evops[EOP_SELECT].add     = evselect_add;
        evops[EOP_SELECT].update  = evselect_update;
        evops[EOP_SELECT].del     = evselect_del;
        evops[EOP_SELECT].loop    = evselect_loop;
        evops[EOP_SELECT].reset   = evselect_reset;
        evops[EOP_SELECT].clean   = evselect_clean;
#endif
#ifdef HAVE_EVPOLL
        evops_default = EOP_POLL;
        evops[EOP_POLL].name      = "POLL";
        evops[EOP_POLL].init      = evpoll_init;
        evops[EOP_POLL].add       = evpoll_add;
        evops[EOP_POLL].update    = evpoll_update;
        evops[EOP_POLL].del       = evpoll_del;
        evops[EOP_POLL].loop      = evpoll_loop;
        evops[EOP_POLL].reset     = evpoll_reset;
        evops[EOP_POLL].clean     = evpoll_clean;
#endif
#ifdef HAVE_EVRTSIG
        evops_default = EOP_RTSIG;
        evops[EOP_RTSIG].name     = "RTSIG" ;
        evops[EOP_RTSIG].init     = evrtsig_init;
        evops[EOP_RTSIG].add      = evrtsig_add;
        evops[EOP_RTSIG].update   = evrtsig_update;
        evops[EOP_RTSIG].del      = evrtsig_del;
        evops[EOP_RTSIG].loop     = evrtsig_loop;
        evops[EOP_RTSIG].reset    = evrtsig_reset;
        evops[EOP_RTSIG].clean    = evrtsig_clean;
#endif
#ifdef HAVE_EVEPOLL
        evops_default = EOP_EPOLL;
        evops[EOP_EPOLL].name     = "EPOLL";
        evops[EOP_EPOLL].init     = evepoll_init;
        evops[EOP_EPOLL].add      = evepoll_add;
        evops[EOP_EPOLL].update   = evepoll_update;
        evops[EOP_EPOLL].del      = evepoll_del;
        evops[EOP_EPOLL].loop     = evepoll_loop;
        evops[EOP_EPOLL].reset    = evepoll_reset;
        evops[EOP_EPOLL].clean    = evepoll_clean;
#endif
#ifdef HAVE_EVKQUEUE
        evops_default = EOP_KQUEUE;
        evops[EOP_KQUEUE].name    = "KQUEUE";
        evops[EOP_KQUEUE].init    = evkqueue_init;
        evops[EOP_KQUEUE].add     = evkqueue_add;
        evops[EOP_KQUEUE].update  = evkqueue_update;
        evops[EOP_KQUEUE].del     = evkqueue_del;
        evops[EOP_KQUEUE].loop    = evkqueue_loop;
        evops[EOP_KQUEUE].reset   = evkqueue_reset;
        evops[EOP_KQUEUE].clean   = evkqueue_clean;
#endif
#ifdef HAVE_EVDEVPOLL
        evops_default = EOP_DEVPOLL;
        evops[EOP_DEVPOLL].name   = "/dev/poll";
        evops[EOP_DEVPOLL].init   = evdevpoll_init;
        evops[EOP_DEVPOLL].add    = evdevpoll_add;
        evops[EOP_DEVPOLL].update = evdevpoll_update;
        evops[EOP_DEVPOLL].del    = evdevpoll_del;
        evops[EOP_DEVPOLL].loop   = evdevpoll_loop;
        evops[EOP_DEVPOLL].reset  = evdevpoll_reset;
        evops[EOP_DEVPOLL].clean  = evdevpoll_clean;
#endif
#ifdef WIN32
        evops_default = EOP_WIN32;
        evops[EOP_WIN32].name     = "WIN32";
        evops[EOP_WIN32].init     = evwin32_init;
        evops[EOP_WIN32].add      = evwin32_add;
        evops[EOP_WIN32].update   = evwin32_update;
        evops[EOP_WIN32].del      = evwin32_del;
        evops[EOP_WIN32].loop     = evwin32_loop;
        evops[EOP_WIN32].reset    = evwin32_reset;
        evops[EOP_WIN32].clean    = evwin32_clean;
#endif
        evbase->set_logfile = evbase_set_logfile;
        evbase->set_log_level = evbase_set_log_level;
        evbase->set_evops   = evbase_set_evops;
        //evbase->clean 	=  evbase_clean;
        if(evops_default == -1 || evbase->set_evops(evbase, evops_default) == -1)
        {
            xmm_free(evbase, sizeof(EVBASE)); 
            fprintf(stderr, "Initialize evbase to default[%d] failed, %s\n", 
                    evops_default, strerror(errno));
            evbase = NULL;
        }
    }
    return evbase;
}