src/server/daemon/event.c

Sat, 03 Dec 2022 12:27:00 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 03 Dec 2022 12:27:00 +0100
changeset 445
834351da593b
parent 443
ef3c8a0e1fee
child 490
d218607f5a7e
permissions
-rw-r--r--

improve acceptor error handling

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <cx/hash_map.h>
#include "../util/atomic.h"

#include "event.h"

CxMap *event_handler_map = NULL;
int numevhandlers = 0;

EVHandler *default_event_handler = NULL;

EVHandler *last_handler_c = NULL;

int create_event_handler(EventHandlerConfig *cfg) {
    if(event_handler_map == NULL) {
        event_handler_map = cxHashMapCreate(cxDefaultAllocator, 16);
    }
    
    CxHashKey key = cx_hash_key_bytes((const unsigned char*)cfg->name.ptr, cfg->name.length);
    
    // if the event handler already exists, we don't modify it
    if(cxMapGet(event_handler_map, key)) {
        /* TODO: log message */
        /* TODO: set reload status */
        log_ereport(LOG_DEBUG, "event handler %s already exists", cfg->name.ptr);
        return 0;
    }
    
    // create new handler
    EVHandler *e = evhandler_create(cfg);
    if(e == NULL) {
        log_ereport(LOG_FAILURE, "evhandler_create failed");
        return 1;
    }
    
    if(cfg->isdefault) {
        if(default_event_handler) {
            /* there can be only one default event handler  */
            /* TODO: log warning */
        }
        default_event_handler = e;
    }
    
    int ret = cxMapPut(event_handler_map, key, e);
    if(ret == 0) {
        last_handler_c = e;
        numevhandlers++;
    }
    
    return ret;
}

/*
 * checks if there is at least one event handler and a default handler
 * if necessary, check_event_handler_cfg() creates a default event handler
 */
int check_event_handler_cfg() {
    if(numevhandlers > 0 ) {
        if(default_event_handler) {
            return 0;
        } else {
            default_event_handler = last_handler_c;
            return 0;
        }
    }
    
    EventHandlerConfig cfg;
    cfg.name = cx_str("default");
    cfg.nthreads = 1;
    cfg.isdefault = 1;
    
    return create_event_handler(&cfg);
}


EVHandler* get_default_event_handler() {
    return default_event_handler;
}

EVHandler* get_event_handler(const char *name) {
    return cxMapGet(event_handler_map, cx_hash_key_str(name));
}

EventHandler* ev_instance(EVHandler *ev) {
    int nev = ev->numins;
    if(nev == 1) {
        return ev->instances[0];
    }
    
    int ins = ev->current & nev;
    ws_atomic_inc32(&ev->current);
    return ev->instances[ins];
}

mercurial