MUX(3)
π MUX(3): protocol multiplexor π
okay so bestie, this is the MUX(3) page from the Library Functions Manual, and itβs giving βgeneric protocol multiplexor realnessβ β¨. No cap, libmux handles the multiplexing/demultiplexing so you donβt have to.
πΈ NAME πΈ
the NAME crew is Mux, muxinit, muxrpc, and muxthreads, and together theyβre the protocol multiplexor π«Ά
β¨ SYNOPSIS β¨
the whole signature situation, verbatim:
#include <mux.h>
struct Mux
{
uint mintag;
uint maxtag;
int (*settag)(Mux *mux, void *msg, uint tag);
int (*gettag)(Mux *mux, void *msg);
int (*send)(Mux *mux, void *msg);
void *(*recv)(Mux *mux);
void *(*nbrecv)(Mux *mux);
void *aux;
... /* private fields follow */
};
void muxinit(Mux *mux);
void* muxrpc(Mux *mux, void *request);
void muxprocs(Mux *mux);
Muxrpc*muxrpcstart(Mux *mux, void *request);
void* muxrpccanfinish(Muxrpc *rpc);
π DESCRIPTION π
Libmux is a generic protocol multiplexor. A client program initializes a Mux structure with information about the protocol β mainly in the form of helper functions β and can then use muxrpc to execute individual RPCs without worrying about the details of multiplexing requests and demultiplexing responses. literally the whole point π
Libmux assumes that the protocol messages contain a tag (or message ID) field that exists for the sole purpose of demultiplexing messages. Libmux chooses the tags and then calls a helper function to put them in the outgoing messages. Libmux calls another helper function to retrieve tags from incoming messages. It also calls helper functions to send and receive packets. teamwork, but make it protocol β¨
A client should allocate a Mux structure and then call muxinit to initialize the libraryβs private elements. The client must initialize the following elements:
mintag,maxtagβ the range of valid tags.maxtagis the maximum valid tag plus one, so thatmaxtag-mintagis equal to the number of valid tags. If libmux runs out of tags β meaning all tags are being used for RPCs currently in progress β a new call tomuxrpcwill block until an executing call finishes. queue up, bestie π«Άsettag,gettagβ set or get the tag value in a message.send,recv,nbrecvβ send or receive protocol messages on the connection.recvshould block until a message is available and should return nil if the connection is closed.nbrecvshould not block; it returns nil if there is no message available to be read. Libmux will arrange that only one call torecvornbrecvis active at a time. one at a time, no chaos πauxβ an auxiliary pointer for use by the client. Once a client has initialized theMuxstructure, it can callmuxrpcto execute RPCs. The request is the message passed tosettagandsend. The return value is the response packet, as provided byrecv, or nil if an error occurred.Muxprocsallocates new procs (seethread(3)) in which to runsendandrecv. After a call tomuxprocs,muxrpcwill runsendandrecvin these procs instead of in the calling proc. This is useful if the implementation of either β particularlyrecvβ blocks an entire proc and there are other threads in the calling proc that need to remain active. very βlet the background besties handle itβ energy π
Libmux also provides a non-blocking interface, useful for programs forced to use a select(3)-based main loop. Muxrpcstart runs the first half of muxrpc: it assigns a tag and sends the request, but does not wait for the reply. Instead it returns a pointer to a Muxrpc structure that represents the in-progress call. Muxrpccanfinish checks whether the given call can finish. If no mux procs have been started, muxrpccanfinish may call nbrecv to poll for newly arrived responses. non-blocking girlie behavior, iconic β¨
π EXAMPLE π
See /src/lib9pclient/fs.c for an example of using libmux with 9P (see intro(9p)). go peek, itβs giving reference implementation π¦
π¦ SOURCE π¦
/src/libmux
π SEE ALSO π
thread(3)intro(9p)
π BUGS π
Libmux does not know how to free protocol messages, so message arriving with unexpected or invalid tags are leaked. not the glow-up we wanted, but there it is π
thatβs the whole MUX(3) page from the Library Functions Manual β the footer just says MUX(3), so no extra date/version tea to spill, bestie π
π«Ά
π¦ where this page comes from
- alpine 3.24: plan9port-doc 0_git20260208-r0 Β· LPL-1.02
- arch rolling: plan9port 20261021-2 Β· custom
- nixos 26.05: plan9port 0-unstable-2025-11-10
- nixos 26.05: plan9port-wayland 0-unstable-2025-11-26
- nixpkgs unstable: plan9port 0-unstable-2025-11-10
- nixpkgs unstable: plan9port-wayland 0-unstable-2025-11-26
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms