Skip to content

Commit

Permalink
Loading libkqueue on linux as dynamic library
Browse files Browse the repository at this point in the history
  • Loading branch information
trikko committed Dec 25, 2024
1 parent ebed90a commit 8bd60db
Showing 1 changed file with 63 additions and 12 deletions.
75 changes: 63 additions & 12 deletions source/serverino/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,33 @@ static if (Backend == BackendType.KQUEUE)
enum EVFILT_SYSCOUNT = 11;


int kqueue();

pragma(mangle, "kevent")
int kevent_f(
int kq,
const kevent* changelist,
int nchanges,
kevent* eventlist,
int nevents,
const timespec* timeout
);
version(linux)
{
int function() kqueue;

int function(
int kq,
const kevent* changelist,
int nchanges,
kevent* eventlist,
int nevents,
const timespec* timeout
) kevent_f;
}
else
{
int kqueue();

pragma(mangle, "kevent")
int kevent_f(
int kq,
const kevent* changelist,
int nchanges,
kevent* eventlist,
int nevents,
const timespec* timeout
);
}
}
}
else static assert(false, "kqueue backend is only available on Linux and BSD");
Expand Down Expand Up @@ -545,4 +561,39 @@ version(Posix) package void setProcessName(string[] names)
pthread_setname_np(pthread_self(), names[0].toStringz);
}

package immutable static DEFAULT_BUFFER_SIZE = 32*1024;
package immutable static DEFAULT_BUFFER_SIZE = 32*1024;


version(Posix)
{
static if (Backend == BackendType.KQUEUE)
{

void* kqueue_handle = null;

shared static this()
{
import core.sys.posix.dlfcn;

extern (C) int dll();

kqueue_handle = dlopen("libkqueue.so", RTLD_LAZY);
if (!kqueue_handle)
{
assert(false, "Failed to load libkqueue.so");
}

kqueue = cast(typeof(kqueue))dlsym(kqueue_handle, "kqueue");
kevent_f = cast(typeof(kevent_f))dlsym(kqueue_handle, "kevent");

assert(kqueue && kevent_f, "Failed to load kqueue or kevent functions");
}

shared static ~this()
{
import core.sys.posix.dlfcn;
dlclose(kqueue_handle);
}
}
}

0 comments on commit 8bd60db

Please sign in to comment.