NAME
report, reportdata, reporting, reportcheck — print debugging/monitoring information

SYNOPSIS
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <worker.h>
#include <error.h>
#define report if(!Reporting){}else _report
#define reportcheck(x) (Reporting?_reportcheck(x):0)
#define reportdata if(!Reporting){}else _reportdata
struct Request {
void    (*func)(Worker*, void*);
void    *arg;
};
struct Worker {
char    name[64];
Request      *r;     /* Pointer to work to do */
ulong version;     /* Incremented when accepting new work */
Channel      *chan;/* for allocating work */
Channel *event;    /* for signalling worker */
void    *arg;        /* set to func's arg */
void    *aux;        /* unused by library */
};
void    workerdispatch(void (*f)(Worker*,void*), void *arg)
Timer*       timerdispatch(void (*f)(Worker*, void*), void *arg, vlong when)
int     timerrecall(Timer*)
int     recvt(Channel *c, void **v, vlong when)
int     sendt(Channel *c, void *v, vlong when)

DESCRIPTION
Typical Plan 9 servers create threads to handle incoming requests. The worker library allows these threads to be recycled so they can be reused for a next request.

Workerdispatch finds a waiting worker thread, or creates one if there are none and causes the thread to execute the function f in that thread. This function is called with two arguments, a pointer to the Worker structure and the argument arg provided by the caller of workerdispatch.

When the function returns, the worker thread makes itself available for the next request. Up to Nworker worker threads can be queued.

Timerdispatch calls workerdispatch in the future, at time when, which is given in nanoseconds (see nsec(2)). If when is in the past, the function is called immediately. It returns an address for use in timerrecall .

Timerrecall finds an outstanding timerdispatch event by the address returned by timerdisplatch and attempts to cancel it. If cancellation succeeds, it returns 1; if there is a race condition, or the function has already been called, it returns 0.

recvt and sendt execute send and receive operations with timeouts. The calls return 1 if the operation succeeded normally, 0 if it timed out and –1 if it was interrupted.

FILES
/sys/lib/acid/worker   useful acid(1) functions for debugging worker programs.

SOURCE
/sys/src/libworker

SEE ALSO
thread(2) error(2) nsec(2)

DIAGNOSTICS
Uncaught errors (see error(2)) in the function f cause the function to be exited and the worker thread to requeue itself.

BUGS
The Stack for worker threads is only Wstack bytes

AUTHOR
Sape Mullender
Copyright © 2025 Plan 9 Foundation