* Removed some temporary diagnostics that had been left in serv_sieve.c
[citadel.git] / citadel / threads.h
blobd3df82b69f220dcfcdda5f09b6b1478870def800
1 /* $Id:$ */
3 #ifndef THREADS_H
4 #define THREADS_H
6 #include "sysdep.h"
8 #ifdef HAVE_PTHREAD_H
9 #include <pthread.h>
10 #endif
12 #include <sys/time.h>
13 #include <string.h>
15 #ifdef HAVE_DB_H
16 #include <db.h>
17 #elif defined(HAVE_DB4_DB_H)
18 #include <db4/db.h>
19 #else
20 #error Neither <db.h> nor <db4/db.h> was found by configure. Install db4-devel.
21 #endif
23 #include "server.h"
24 #include "sysdep_decls.h"
26 #ifndef timerclear
27 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
28 #endif
30 #ifndef timerisset
31 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
32 #endif
34 #ifndef timercmp
35 #define timercmp(tvp, uvp, cmp) \
36 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
37 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
38 ((tvp)->tv_sec cmp (uvp)->tv_sec))
39 #endif
41 #ifndef timeradd
42 #define timeradd(tvp, uvp, vvp) \
43 do { \
44 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
45 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
46 if ((vvp)->tv_usec >= 1000000) { \
47 (vvp)->tv_sec++; \
48 (vvp)->tv_usec -= 1000000; \
49 } \
50 } while (0)
51 #endif
53 #ifndef timersub
54 #define timersub(tvp, uvp, vvp) \
55 do { \
56 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
57 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
58 if ((vvp)->tv_usec < 0) { \
59 (vvp)->tv_sec--; \
60 (vvp)->tv_usec += 1000000; \
61 } \
62 } while (0)
63 #endif
66 // #define THREADS_USESIGNALS
69 * Thread stuff
72 enum CtdlThreadState {
73 CTDL_THREAD_INVALID,
74 CTDL_THREAD_VALID,
75 CTDL_THREAD_CREATE,
76 CTDL_THREAD_CANCELLED,
77 CTDL_THREAD_EXITED,
78 CTDL_THREAD_STOPPING,
79 CTDL_THREAD_STOP_REQ, /* Do NOT put any running states before this state */
80 CTDL_THREAD_SLEEPING,
81 CTDL_THREAD_BLOCKED,
82 CTDL_THREAD_RUNNING,
83 CTDL_THREAD_LAST_STATE
85 typedef struct CtdlThreadNode CtdlThreadNode;
87 struct CtdlThreadNode{
88 citthread_t tid; /* id as returned by citthread_create() */
89 pid_t pid; /* pid, as best the OS will let us determine */
90 time_t when; /* When to start a scheduled thread */
91 struct CitContext *Context; /* The session context that this thread mught be working on or NULL if none */
92 long number; /* A unigue number for this thread (not implimented yet) */
93 int wakefd_recv; /* An fd that this thread can sleep on (not implimented yet) */
94 int wakefd_send; /* An fd that this thread can send out on (Not implimented yet) */
95 int signal; /* A field to store a signal we caught. */
96 const char *name; /* A name for this thread */
97 void *(*thread_func) (void *arg); /* The actual function that does this threads work */
98 void *user_args; /* Arguments passed to this threads work function */
99 long flags; /* Flags that describe this thread */
100 enum CtdlThreadState state; /* Flag to show state of this thread */
101 time_t stop_ticker; /* A counter to determine how long it has taken for this thread to exit */
102 citthread_mutex_t ThreadMutex; /* A mutex to sync this thread to others if this thread allows (also used for sleeping) */
103 citthread_cond_t ThreadCond; /* A condition variable to sync this thread with others */
104 citthread_mutex_t SleepMutex; /* A mutex for sleeping */
105 citthread_cond_t SleepCond; /* A condition variable for sleeping */
106 citthread_attr_t attr; /* Attributes of this thread */
107 struct timeval start_time; /* Time this thread was started */
108 struct timeval last_state_change; /* Time when this thread last changed state */
109 double avg_sleeping; /* Average sleeping time */
110 double avg_running; /* Average running time */
111 double avg_blocked; /* Average blocked time */
112 double load_avg; /* Load average for this thread */
113 CtdlThreadNode *prev; /* Previous thread in the thread table */
114 CtdlThreadNode *next; /* Next thread in the thread table */
117 extern CtdlThreadNode *CtdlThreadList;
119 typedef struct ThreadTSD ThreadTSD;
121 struct ThreadTSD {
122 DB_TXN *tid; /* Transaction handle */
123 DBC *cursors[MAXCDB]; /* Cursors, for traversals... */
124 CtdlThreadNode *self; /* Pointer to this threads control structure */
127 extern double CtdlThreadLoadAvg;
128 extern double CtdlThreadWorkerAvg;
129 extern citthread_key_t ThreadKey;
131 void ctdl_thread_internal_init_tsd(void);
132 void ctdl_internal_thread_gc (void);
133 void ctdl_thread_internal_init(void);
134 void ctdl_thread_internal_cleanup(void);
135 void ctdl_thread_internal_calc_loadavg(void);
136 void ctdl_thread_internal_free_tsd(void);
137 CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args);
138 void ctdl_thread_internal_check_scheduled(void);
140 void InitialiseSemaphores(void);
141 int try_critical_section (int which_one);
142 void begin_critical_section (int which_one);
143 void end_critical_section (int which_one);
144 void go_threading(void);
146 #endif // THREADS_H