[erlang-questions] Non-blocking send in erlang: how to save memory when send one content to many people

Rapsey rapsey@REDACTED
Thu Dec 15 09:26:16 CET 2011

On Thu, Dec 15, 2011 at 9:00 AM, Zabrane Mickael <zabrane3@REDACTED> wrote:

> I have a NIF thread that listens on kqueue/epoll and communicates with a
> gen_server. Sockets are resources.
> 1 thread NIF per core?
All that the NIF thread does is accept connections and reads from sockets.
I see no need to have more than 1.

> Communication from NIFs to gen_server is simple since enif_send exists,
> communication from gen_server to the NIF thread is done by pipes (global
> library read pipe and write pipe).
> Could you please elaborae further on how communication is made from Erlang
> to NIF (the read/write pipe)?
On thread initialization the pipes get created and read pipe is placed in
epoll/kqueue. Write pipe is from erlang to NIF, and read pipe is for NIF to
read that data. I have a simple struct:
typedef struct kqmsg
    char what;
    int fd;
    ErlNifPid pid;
    void* data;
So I just fill up this struct with whatever info is required and do:
write(pipe_write,&msg,sizeof(struct kqmsg)

As for sockets, I do not use prim_inet:getfd, sockets are completely
separate from gen_tcp. The NIF thread keeps the socket FD until it reads
the first buffer from it. Once this happens it creates a socket resource,
then sends the binary and socket with enif_send to the Erlang process that
is in charge of deciding what to do with it.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111215/841e00fd/attachment.htm>

More information about the erlang-questions mailing list