Go to file
David du Colombier 798c8c6df8 fix warnings reported by Coverity
fd.c:230 negative_returns: fd is passed to a parameter that cannot be negative.
net.c:65 check_return: Calling setsockopt(fd, 1, 2, (char *)&n, 4U) without checking return value.
net.c:77 check_return: Calling fdnoblock(fd) without checking return value.
net.c:112 check_return: Calling fdnoblock(cfd) without checking return value.
net.c:114 check_return: Calling setsockopt(cfd, IPPROTO_TCP, 1, (char *)&one, 4U) without checking return value.
net.c:144 leaked_storage: Variable result going out of scope leaks the storage it points to.
net.c:168 check_return: Calling fdnoblock(fd) without checking return value.
net.c:173 check_return: Calling setsockopt(fd, 1, 6, &n, 4U) without checking return value.
net.c:206 check_return: Calling getsockopt(fd, 1, 4, (void *)&n, &sn) without checking return value.
task.c:59 negative_returns: fd is passed to a parameter that cannot be negative.
tcpload.c:50 Calling fdread without checking return value (as is done elsewhere 4 out of 5 times).
tcpproxy.c:51 check_return: Calling fdnoblock(fd) without checking return value.
2015-06-24 23:16:58 +02:00
386-ucontext.h libtask as of 2008/08/01 2009-10-10 13:04:03 -07:00
amd64-ucontext.h recent changes 2011-01-03 15:08:59 -05:00
asm.S use portable ucontext on linux/amd64 2014-08-03 19:12:47 +02:00
channel.c fix warnings reported by clang static analyzer 2015-06-15 22:11:58 +02:00
context.c fix clang warnings 2015-06-14 17:12:51 +02:00
COPYRIGHT libtask as of 2008/08/01 2009-10-10 13:04:03 -07:00
echo.c fix warnings reported by Coverity 2015-06-24 23:16:58 +02:00
fd.c fix warnings reported by Coverity 2015-06-24 23:16:58 +02:00
httpload.c fix warning 2015-04-11 16:41:34 +02:00
ip.c implement ipv6 2012-08-01 04:00:00 +02:00
ip.h implement ipv6 2012-08-01 04:00:00 +02:00
Makefile more examples 2014-08-03 18:59:15 +02:00
Makefile.sun implement ipv6 2012-08-01 04:00:00 +02:00
makesun libtask as of 2008/08/01 2009-10-10 13:04:03 -07:00
mips-ucontext.h recent changes 2011-01-03 15:08:59 -05:00
net.c fix warnings reported by Coverity 2015-06-24 23:16:58 +02:00
power-ucontext.h libtask as of 2008/08/01 2009-10-10 13:04:03 -07:00
primes.c libtask as of 2008/08/01 2009-10-10 13:04:03 -07:00
print.c remove trailing spaces 2014-08-03 18:28:37 +02:00
qlock.c remove trailing spaces 2014-08-03 18:28:37 +02:00
README implement fdreadn 2014-08-03 18:51:25 +02:00
rendez.c libtask as of 2008/08/01 2009-10-10 13:04:03 -07:00
task.c fix warnings reported by Coverity 2015-06-24 23:16:58 +02:00
task.h implement fdreadn 2014-08-03 18:51:25 +02:00
taskimpl.h fix clang warnings 2015-06-14 17:12:51 +02:00
tcpload.c fix warnings reported by Coverity 2015-06-24 23:16:58 +02:00
tcpproxy.c fix warnings reported by Coverity 2015-06-24 23:16:58 +02:00
testdelay1.c libtask as of 2008/08/01 2009-10-10 13:04:03 -07:00
testdelay.c remove trailing spaces 2014-08-03 18:28:37 +02:00

Libtask is a simple coroutine library.  It runs on Linux (ARM, MIPS, and x86),
FreeBSD (x86), OS X (PowerPC x86, and x86-64), and SunOS Solaris (Sparc),
and is easy to port to other systems.

Libtask gives the programmer the illusion of threads, but
the operating system sees only a single kernel thread.
For clarity, we refer to the coroutines as "tasks," not threads.

Scheduling is cooperative.  Only one task runs at a time,
and it cannot be rescheduled without explicitly giving up
the CPU.  Most of the functions provided in task.h do have
the possibility of going to sleep.  Programs using the task
functions should #include <task.h>.

--- Basic task manipulation

int taskcreate(void (*f)(void *arg), void *arg, unsigned int stacksize);

	Create a new task running f(arg) on a stack of size stacksize.

void tasksystem(void);

	Mark the current task as a "system" task.  These are ignored
	for the purposes of deciding the program is done running
	(see taskexit next).
	
void taskexit(int status);

	Exit the current task.  If this is the last non-system task,
	exit the entire program using the given exit status.

void taskexitall(int status);

	Exit the entire program, using the given exit status.

void taskmain(int argc, char *argv[]);

	Write this function instead of main.  Libtask provides its own main.

int taskyield(void);
	
	Explicitly give up the CPU.  The current task will be scheduled
	again once all the other currently-ready tasks have a chance
	to run.  Returns the number of other tasks that ran while the
	current task was waiting.  (Zero means there are no other tasks
	trying to run.)

int taskdelay(unsigned int ms)

	Explicitly give up the CPU for at least ms milliseconds.
	Other tasks continue to run during this time.

void** taskdata(void);

	Return a pointer to a single per-task void* pointer.
	You can use this as a per-task storage place.

void needstack(int n);

	Tell the task library that you need at least n bytes left
	on the stack.  If you don't have it, the task library will call abort.
	(It's hard to figure out how big stacks should be.  I usually make
	them really big (say 32768) and then don't worry about it.)

void taskname(char*, ...);

	Takes an argument list like printf.  Sets the current task's name.
	
char* taskgetname(void);

	Returns the current task's name.  Is the actual buffer; do not free.
	
void taskstate(char*, ...);
char* taskgetstate(void);

	Like taskname and taskgetname but for the task state.
	
	When you send a tasked program a SIGQUIT (or SIGINFO, on BSD)
	it will print a list of all its tasks and their names and states.
	This is useful for debugging why your program isn't doing anything!

unsigned int taskid(void);

	Return the unique task id for the current task.

--- Non-blocking I/O

There is a small amount of runtime support for non-blocking I/O
on file descriptors.  

int fdnoblock(int fd);

	Sets I/O on the given fd to be non-blocking.  Should be
	called before any of the other fd routines.

int fdread(int, void*, int);

	Like regular read(), but puts task to sleep while waiting for
	data instead of blocking the whole program.

int fdreadn(int, void*, int);

	Like fdread(), but does successive read calls until n bytes have
	been read, or a read system call returns a non-positive count.

int fdwrite(int, void*, int);

	Like regular write(), but puts task to sleep while waiting to
	write data instead of blocking the whole program.

void fdwait(int fd, int rw);

	Low-level call sitting underneath fdread and fdwrite.
	Puts task to sleep while waiting for I/O to be possible on fd.
	Rw specifies type of I/O: 'r' means read, 'w' means write,
	anything else means just exceptional conditions (hang up, etc.)
	The 'r' and 'w' also wake up for exceptional conditions.

--- Network I/O

These are convenient packaging of the ugly Unix socket routines.
They can all put the current task to sleep during the call.  

int netannounce(int proto, char *address, int port)

	Start a network listener running on address and port of protocol.
	Proto is either TCP or UDP.  Port is a port number.  Address is a
	string version of a host name or IP address.  If address is null,
	then announce binds to the given port on all available interfaces.
	Returns a fd to use with netaccept.
	Examples: netannounce(TCP, "localhost", 80) or 
		netannounce(TCP, "127.0.0.1", 80) or netannounce(TCP, 0, 80).

int netaccept(int fd, char *server, int *port)

	Get the next connection that comes in to the listener fd.
	Returns a fd to use to talk to the guy who just connected.
	If server is not null, it must point at a buffer of at least
	46 bytes that is filled in with the remote IP address.
	If port is not null, it is filled in with the report port.
	Example:
		char server[46];
		int port;
		
		if(netaccept(fd, server, &port) >= 0)
			printf("connect from %s:%d", server, port);

int netdial(int proto, char *name, int port)

	Create a new (outgoing) connection to a particular host.
	Name can be an ip address or a domain name.  If it's a domain name,
	the entire program will block while the name is resolved
	(the DNS library does not provide a nice non-blocking interface).
	Example: netdial(TCP, "www.google.com", 80)
		or netdial(TCP, "18.26.4.9", 80)

--- Time

unsigned int taskdelay(unsigned int ms)

	Put the current task to sleep for approximately ms milliseconds.
	Return the actual amount of time slept, in milliseconds.

--- Example programs

In this directory, tcpproxy.c is a simple TCP proxy that illustrates
most of the above.  You can run

	tcpproxy 1234 www.google.com 80

and then you should be able to visit http://localhost:1234/ and see Google.

Other examples are:
	primes.c - simple prime sieve
	httpload.c - simple HTTP load generator
	testdelay.c - test taskdelay()

--- Building

To build, run make.  You can run make install to copy task.h and
libtask.a to the appropriate places in /usr/local.  Then you
should be able to just link with -ltask in your programs
that use it.  

On SunOS Solaris machines, run makesun instead of just make.

--- Contact Info

Please email me with questions or problems.

Russ Cox
rsc@swtch.com


--- Stuff you probably won't use at first ---
--- but might want to know about eventually ---

void tasksleep(Rendez*);
int taskwakeup(Rendez*);
int taskwakeupall(Rendez*);

	A Rendez is a condition variable. You can declare a new one by
	just allocating memory for it (or putting it in another structure)
	and then zeroing the memory.  Tasksleep(r) 'sleeps on r', giving
	up the CPU.  Multiple tasks can sleep on a single Rendez.
	When another task comes along and calls taskwakeup(r), 
	the first task sleeping on r (if any) will be woken up.
	Taskwakeupall(r) wakes up all the tasks sleeping on r.
	They both return the actual number of tasks awakened.



void qlock(QLock*);
int canqlock(QLock*);
void qunlock(QLock*);

	You probably won't need locks because of the cooperative 
	scheduling, but if you do, here are some.  You can make a new
	QLock by just declaring it and zeroing the memory.
	Calling qlock will give up the CPU if the lock is held by someone else.
	Calling qunlock will not give up the CPU.
	Calling canqlock tries to lock the lock, but will not give up the CPU.
	It returns 1 if the lock was acquired, 0 if it cannot be at this time.

void rlock(RWLock*);
int canrlock(RWLock*);
void runlock(RWLock*);

void wlock(RWLock*);
int canwlock(RWLock*);
void wunlock(RWLock*);

	RWLocks are reader-writer locks.  Any number of readers
	can lock them at once, but only one writer at a time.
	If a writer is holding it, there can't be any readers.
	

Channel *chancreate(int, int);
etc.

	Channels are buffered communication pipes you can
	use to send messages between tasks.  Some people like
	doing most of the inter-task communication using channels.
	
	For details on channels see the description of channels in
	http://swtch.com/usr/local/plan9/man/man3/thread.html and
	http://swtch.com/~rsc/thread/
	and also the example program primes.c, which implements
	a concurrent prime sieve.