src/server/daemon/event_solaris.c

Wed, 25 Jan 2017 19:19:47 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 25 Jan 2017 19:19:47 +0100
branch
aio
changeset 159
9ba9f8befa80
parent 153
85320d8b5d5c
child 172
5580517faafc
permissions
-rw-r--r--

makes EventHandler public

/*
 * 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 <stdio.h>
#include <stdlib.h>
#include <atomic.h>

#include "event_solaris.h"

EVHandler* evhandler_create(EventHandlerConfig *cfg) {
    EVHandler *ev = malloc(sizeof(EVHandler));
    ev->current = 0;
    ev->instances = calloc(cfg->nthreads, sizeof(void*));
    ev->numins = cfg->nthreads;
    
    for(int i=0;i<cfg->nthreads;i++) {
        EventHandler *handler = malloc(sizeof(EventHandler));
        ev->instances[i] = handler;
        
        handler->port = port_create();
        if(handler->port == 0) {
            // TODO: error
            return NULL;
        }
        
        SYS_THREAD t = systhread_start(
                0,
                0,
                (thrstartfunc)ev_handle_events,
                handler);
        systhread_detach(t);
    }
    
    return ev;
}

void ev_handle_events(EventHandler *ev) {   
    port_event_t events[64];
    struct timespec timeout;
    timeout.tv_nsec = 0;
    timeout.tv_sec = 600;
    
    for(;;) {
        // wait for events
        uint_t nev = 1;
        int ret = port_getn(ev->port, events, 64, &nev, &timeout);
        if(ret == -1) {
            // TODO: check for error
            perror("port_getn");
            continue;
        }
        
        for(int i=0;i<nev;i++) {
            Event *event = events[i].portev_user;
            if(event->fn) {
                if(event->fn(ev, event)) {
                    /*
                     * on solaris we have to reassociate the fd after
                     * each event
                     * we do this if the event function returns 1
                     */
                    if(port_associate(
                            ev->port,
                            PORT_SOURCE_FD,
                            (uintptr_t)event->object,
                            ev_convert2sys_events(event->events),
                            event))
                    {
                        perror("port_associate");
                    }                 
                } else if(event->finish) {
                    event->finish(ev, event);
                }
            }
        }
    }
}

int ev_convert2sys_events(int events) {
    int e = 0;
    if((events & EVENT_POLLIN) == EVENT_POLLIN) {
        e |= POLLIN;
    }
    if((events & EVENT_POLLOUT) == EVENT_POLLOUT) {
        e |= POLLOUT;
    }
    return e;
}


int ev_pollin(EventHandler *h, int fd, Event *event) {
    event->object = (intptr_t)fd;
    event->events = EVENT_POLLIN;
    return port_associate(
            h->port,
            PORT_SOURCE_FD,
            (uintptr_t)fd,
            POLLIN,
            event);
}

int ev_pollout(EventHandler *h, int fd, Event *event) {
    event->object = (intptr_t)fd;
    event->events = EVENT_POLLOUT;
    return port_associate(
            h->port,
            PORT_SOURCE_FD,
            (uintptr_t)fd,
            POLLOUT,
            event);
}

int evt_send(EventHandler *h, Event *event) {
    event->object = 0;
    event->events = 0;
    return port_send(h->port, 0, event);
}

mercurial