All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.questdb.network.Kqueue Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 *    ___                  _   ____  ____
 *   / _ \ _   _  ___  ___| |_|  _ \| __ )
 *  | | | | | | |/ _ \/ __| __| | | |  _ \
 *  | |_| | |_| |  __/\__ \ |_| |_| | |_) |
 *   \__\_\\__,_|\___||___/\__|____/|____/
 *
 * Copyright (C) 2014-2019 Appsicle
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 *
 ******************************************************************************/

package com.questdb.network;

import com.questdb.log.Log;
import com.questdb.log.LogFactory;
import com.questdb.std.Files;
import com.questdb.std.Unsafe;

import java.io.Closeable;

public final class Kqueue implements Closeable {
    private static final Log LOG = LogFactory.getLog(Kqueue.class);
    private final long changeList;
    private final long eventList;
    private final int kq;
    private final int capacity;
    private final KqueueFacade kqf;
    private final int bufferSize;
    private long writeAddress;
    private long readAddress;

    public Kqueue(int capacity) {
        this(KqueueFacadeImpl.INSTANCE, capacity);
    }

    public Kqueue(KqueueFacade kqf, int capacity) {
        this.kqf = kqf;
        this.capacity = capacity;
        this.bufferSize = KqueueAccessor.SIZEOF_KEVENT * capacity;
        this.changeList = this.writeAddress = Unsafe.calloc(bufferSize);
        this.eventList = this.readAddress = Unsafe.calloc(bufferSize);
        this.kq = kqf.kqueue();
        if (this.kq != -1) {
            Files.bumpFileCount();
        }
    }

    @Override
    public void close() {
        kqf.getNetworkFacade().close(kq, LOG);
        Unsafe.free(this.changeList, bufferSize);
        Unsafe.free(this.eventList, bufferSize);
    }

    public long getData() {
        return Unsafe.getUnsafe().getLong(readAddress + KqueueAccessor.DATA_OFFSET);
    }

    public int getFd() {
        return (int) Unsafe.getUnsafe().getLong(readAddress + KqueueAccessor.FD_OFFSET);
    }

    public int getFilter() {
        return Unsafe.getUnsafe().getShort(readAddress + KqueueAccessor.FILTER_OFFSET);
    }

    public int getFlags() {
        return Unsafe.getUnsafe().getShort(readAddress + KqueueAccessor.FLAGS_OFFSET);
    }

    public int listen(long sfd) {
        writeAddress = changeList;
        commonFd(sfd, 0);
        Unsafe.getUnsafe().putShort(writeAddress + KqueueAccessor.FILTER_OFFSET, KqueueAccessor.EVFILT_READ);
        Unsafe.getUnsafe().putShort(writeAddress + KqueueAccessor.FLAGS_OFFSET, KqueueAccessor.EV_ADD);
        return register(1);
    }

    public int poll() {
        return kqf.kevent(kq, 0, 0, eventList, capacity);
    }

    public void readFD(int fd, long data) {
        commonFd(fd, data);
        Unsafe.getUnsafe().putShort(writeAddress + KqueueAccessor.FILTER_OFFSET, KqueueAccessor.EVFILT_READ);
        Unsafe.getUnsafe().putShort(writeAddress + KqueueAccessor.FLAGS_OFFSET, (short) (KqueueAccessor.EV_ADD | KqueueAccessor.EV_ONESHOT));
    }

    public int register(int n) {
        return kqf.kevent(kq, changeList, n, 0, 0);
    }

    public void setReadOffset(int offset) {
        this.readAddress = eventList + offset;
    }

    public void setWriteOffset(int offset) {
        this.writeAddress = changeList + offset;
    }

    public void writeFD(int fd, long data) {
        commonFd(fd, data);
        Unsafe.getUnsafe().putShort(writeAddress + KqueueAccessor.FILTER_OFFSET, KqueueAccessor.EVFILT_WRITE);
        Unsafe.getUnsafe().putShort(writeAddress + KqueueAccessor.FLAGS_OFFSET, (short) (KqueueAccessor.EV_ADD | KqueueAccessor.EV_ONESHOT));
    }

    private void commonFd(long fd, long data) {
        Unsafe.getUnsafe().putLong(writeAddress + KqueueAccessor.FD_OFFSET, fd);
        Unsafe.getUnsafe().putLong(writeAddress + KqueueAccessor.DATA_OFFSET, data);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy