src/server/daemon/event_linux.h

Sat, 24 Aug 2024 18:34:13 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 24 Aug 2024 18:34:13 +0200
changeset 555
66b0accda0a8
parent 552
4ed0e46aa9dc
permissions
-rw-r--r--

move first request poll to the event handler

/*
 * 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.
 */

#ifndef EVENT_LINUX_H
#define	EVENT_LINUX_H

#include "event.h"
#include <inttypes.h>

#include <cx/list.h>

#ifdef	__cplusplus
extern "C" {
#endif
    
#define EV_QUEUE_RESERVE 8
    
typedef struct EventQueue EventQueue;
    
typedef struct EventHandlerLinux {
    /*
     * base eventhandler elements (fnreturn, watchlist)
     */
    EventHandler base;
    
    /*
     * eventhandler thread
     */
    pthread_t thr;
    
    /*
     * epoll fd
     */
    int ep;
    
    /*
     * eventfd for notifying that new elements
     * in the event_queue are available
     */
    int event_fd;
    
    /*
     * queue for custom events (event_send)
     * each block can contain up to EV_MAX_EVENTS events
     * the event loop should not retrieve more than one block
     * 
     * first queue block
     */
    EventQueue *queue_begin;
    
    /*
     * last queue block
     */
    EventQueue *queue_end;
    
    /*
     * allocated unused blocks
     */
    EventQueue *reserve_block[EV_QUEUE_RESERVE];
    
    /*
     * number of unused blocks
     */
    size_t num_reserve;
    
    /*
     * mutex for event_queue
     */
    pthread_mutex_t queue_lock;
} EventHandlerLinux;

struct EventQueue {
    /*
     * array of events
     */
    Event *events[EV_MAX_EVENTS];
    /*
     * number of events
     */
    size_t numevents;
    /*
     * next event block
     */
    EventQueue *next;
};

void ev_handle_events(EventHandlerLinux *ev);

int ev_convert2sys_events(int events);

void ev_queue_free(EventQueue *queue);

#ifdef	__cplusplus
}
#endif

#endif	/* EVENT_LINUX_H */

mercurial