[MAJOR] replaced all timeouts with struct timeval

The timeout functions were difficult to manipulate because they were
rounding results to the millisecond. Thus, it was difficult to compare
and to check what expired and what did not. Also, the comparison
functions were heavy with multiplies and divides by 1000. Now, all
timeouts are stored in timevals, reducing the number of operations
for updates and leading to cleaner and more efficient code.
diff --git a/include/common/appsession.h b/include/common/appsession.h
index eb9ba2d..8fa681c 100644
--- a/include/common/appsession.h
+++ b/include/common/appsession.h
@@ -44,7 +44,7 @@
 static void print_table(const CHTbl *htbl);
 #endif
 
-int appsession_refresh(struct task *t);
+void appsession_refresh(struct task *t, struct timeval *next);
 int appsession_task_init(void);
 int appsession_init(void);
 void appsession_cleanup(void);
diff --git a/include/proto/checks.h b/include/proto/checks.h
index 6ac32a1..839af55 100644
--- a/include/proto/checks.h
+++ b/include/proto/checks.h
@@ -2,7 +2,7 @@
   include/proto/checks.h
   Functions prototypes for the checks.
 
-  Copyright (C) 2000-2006 Willy Tarreau - w@1wt.eu
+  Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -25,7 +25,7 @@
 #include <types/task.h>
 #include <common/config.h>
 
-int process_chk(struct task *t);
+void process_chk(struct task *t, struct timeval *next);
 
 #endif /* _PROTO_CHECKS_H */
 
diff --git a/include/proto/proto_http.h b/include/proto/proto_http.h
index 298d30d..da4ea0e 100644
--- a/include/proto/proto_http.h
+++ b/include/proto/proto_http.h
@@ -58,7 +58,7 @@
 #define HTTP_IS_VER_TOKEN(x) (http_is_ver_token[(unsigned char)(x)])
 
 int event_accept(int fd);
-int process_session(struct task *t);
+void process_session(struct task *t, struct timeval *next);
 int process_cli(struct session *t);
 int process_srv(struct session *t);
 
diff --git a/include/proto/proxy.h b/include/proto/proxy.h
index 3fa541a..38d7325 100644
--- a/include/proto/proxy.h
+++ b/include/proto/proxy.h
@@ -2,7 +2,7 @@
   include/proto/proxy.h
   This file defines function prototypes for proxy management.
 
-  Copyright (C) 2000-2006 Willy Tarreau - w@1wt.eu
+  Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -23,10 +23,11 @@
 #define _PROTO_PROXY_H
 
 #include <common/config.h>
+#include <common/time.h>
 #include <types/proxy.h>
 
 int start_proxies(int verbose);
-int maintain_proxies(void);
+void maintain_proxies(struct timeval *next);
 void soft_stop(void);
 void pause_proxy(struct proxy *p);
 void pause_proxies(void);
diff --git a/include/proto/queue.h b/include/proto/queue.h
index c113f03..4370cb3 100644
--- a/include/proto/queue.h
+++ b/include/proto/queue.h
@@ -2,7 +2,7 @@
   include/proto/queue.h
   This file defines everything related to queues.
 
-  Copyright (C) 2000-2006 Willy Tarreau - w@1wt.eu
+  Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -35,7 +35,7 @@
 struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px);
 struct pendconn *pendconn_add(struct session *sess);
 void pendconn_free(struct pendconn *p);
-int process_srv_queue(struct task *t);
+void process_srv_queue(struct task *t, struct timeval *next);
 unsigned int srv_dynamic_maxconn(const struct server *s);
 
 
diff --git a/include/proto/task.h b/include/proto/task.h
index 2c724ad..c594d52 100644
--- a/include/proto/task.h
+++ b/include/proto/task.h
@@ -111,12 +111,10 @@
  *   - wake up all expired tasks
  *   - call all runnable tasks
  *   - call maintain_proxies() to enable/disable the listeners
- *   - return the delay till next event in ms, -1 = wait indefinitely
- * Note: this part should be rewritten with the O(ln(n)) scheduler.
- *
+ *   - return the date of next event in <next> or eternity.
  */
 
-int process_runnable_tasks();
+void process_runnable_tasks(struct timeval *next);
 
 
 #endif /* _PROTO_TASK_H */
diff --git a/include/types/buffers.h b/include/types/buffers.h
index 4afc293..9b781b8 100644
--- a/include/types/buffers.h
+++ b/include/types/buffers.h
@@ -63,9 +63,9 @@
 	struct timeval rex;             /* expiration date for a read  */
 	struct timeval wex;             /* expiration date for a write */
 	struct timeval cex;             /* expiration date for a connect */
-	int rto;                        /* read timeout */
-	int wto;                        /* write timeout */
-	int cto;                        /* connect timeout */
+	struct timeval rto;             /* read timeout */
+	struct timeval wto;             /* write timeout */
+	struct timeval cto;             /* connect timeout */
 	unsigned int l;                 /* data length */
 	char *r, *w, *lr;               /* read ptr, write ptr, last read */
 	char *rlim;                     /* read limit, used for header rewriting */
diff --git a/include/types/fd.h b/include/types/fd.h
index 392c94e..a1b6e9d 100644
--- a/include/types/fd.h
+++ b/include/types/fd.h
@@ -81,7 +81,7 @@
  *    it returns 0. It may be the same as clr().
  *  - clo() should be used to do indicate the poller that fd will be closed. It
  *    may be the same as rem() on some pollers.
- *  - poll() calls the poller, waiting at most wait_time ms.
+ *  - poll() calls the poller, expiring at <exp>
  */
 struct poller {
 	void   *private;                                     /* any private data for the poller */
@@ -92,7 +92,7 @@
 	int  REGPRM2 (*cond_c)(const int fd, int dir);       /* clear polling on <fd> for <dir> if set */
 	void REGPRM1    (*rem)(const int fd);                /* remove any polling on <fd> */
 	void REGPRM1    (*clo)(const int fd);                /* mark <fd> as closed */
-    	void REGPRM2   (*poll)(struct poller *p, int wait_time); /* the poller itself */
+    	void REGPRM2   (*poll)(struct poller *p, struct timeval *exp); /* the poller itself */
 	int  REGPRM1   (*init)(struct poller *p);            /* poller initialization */
 	void REGPRM1   (*term)(struct poller *p);            /* termination of this poller */
 	int  REGPRM1   (*test)(struct poller *p);            /* pre-init check of the poller */
diff --git a/include/types/proxy.h b/include/types/proxy.h
index c403e59..532348e 100644
--- a/include/types/proxy.h
+++ b/include/types/proxy.h
@@ -96,7 +96,6 @@
 	char *appsession_name;			/* name of the cookie to look for */
 	int  appsession_name_len;		/* strlen(appsession_name), computed only once */
 	int  appsession_len;			/* length of the appsession cookie value to be used */
-	int  appsession_timeout;
 	CHTbl htbl_proxy;			/* Per Proxy hashtable */
 	char *capture_name;			/* beginning of the name of the cookie to capture */
 	int  capture_namelen;			/* length of the cookie name to match */
@@ -104,9 +103,10 @@
 	struct uri_auth *uri_auth;		/* if non-NULL, the (list of) per-URI authentications */
 	char *monitor_uri;			/* a special URI to which we respond with HTTP/200 OK */
 	int monitor_uri_len;			/* length of the string above. 0 if unused */
-	int clitimeout;				/* client I/O timeout (in milliseconds) */
-	int srvtimeout;				/* server I/O timeout (in milliseconds) */
-	int contimeout;				/* connect timeout (in milliseconds) */
+	struct timeval clitimeout;		/* client I/O timeout (in milliseconds) */
+	struct timeval srvtimeout;		/* server I/O timeout (in milliseconds) */
+	struct timeval contimeout;		/* connect timeout (in milliseconds) */
+	struct timeval appsession_timeout;
 	char *id;				/* proxy id */
 	struct list pendconns;			/* pending connections with no server assigned yet */
 	int nbpend, nbpend_max;			/* number of pending connections with no server assigned yet */
diff --git a/include/types/task.h b/include/types/task.h
index 1fd78be..42699db 100644
--- a/include/types/task.h
+++ b/include/types/task.h
@@ -2,7 +2,7 @@
   include/types/task.h
   Macros, variables and structures for task management.
 
-  Copyright (C) 2000-2006 Willy Tarreau - w@1wt.eu
+  Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -38,7 +38,7 @@
 	struct ultree *wq;		/* NULL if unqueued, or back ref to the carrier node in the WQ */
 	int state;			/* task state : IDLE or RUNNING */
 	struct timeval expire;		/* next expiration time for this task, use only for fast sorting */
-	int (*process)(struct task *t);	/* the function which processes the task */
+	void (*process)(struct task *t, struct timeval *next);	/* the function which processes the task */
 	void *context;			/* the task's context */
 };