4103466f5b861d3222b8b2d620c451f705e12024
[citadel.git] / citadel / threads.h
1 /* $Id$ */
2
3 #ifndef THREADS_H
4 #define THREADS_H
5
6 #include "sysdep.h"
7
8 #ifdef HAVE_PTHREAD_H
9 #include <pthread.h>
10 #endif
11
12 #include <sys/time.h>
13 #include <string.h>
14
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
22
23 #include "server.h"
24 #include "sysdep_decls.h"
25
26 #ifndef timerclear
27 #define timerclear(tvp)         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
28 #endif
29
30 #ifndef timerisset
31 #define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
32 #endif
33
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
40
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
52
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
64  
65
66 // #define THREADS_USESIGNALS
67
68 /*
69  * Thread stuff
70  */
71
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
84 };
85 typedef struct CtdlThreadNode CtdlThreadNode;
86
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         long reltid;                            /* counting from start... */
91         time_t when;                            /* When to start a scheduled thread */
92         struct CitContext *Context;             /* The session context that this thread mught be working on or NULL if none */
93         long number;                            /* A unigue number for this thread (not implimented yet) */
94         int wakefd_recv;                        /* An fd that this thread can sleep on (not implimented yet) */
95         int wakefd_send;                        /* An fd that this thread can send out on (Not implimented yet) */
96         int signal;                             /* A field to store a signal we caught. */
97         const char *name;                       /* A name for this thread */
98         void *(*thread_func) (void *arg);       /* The actual function that does this threads work */
99         void *user_args;                        /* Arguments passed to this threads work function */
100         long flags;                             /* Flags that describe this thread */
101         enum CtdlThreadState state;             /* Flag to show state of this thread */
102         time_t stop_ticker;                     /* A counter to determine how long it has taken for this thread to exit */
103         citthread_mutex_t ThreadMutex;          /* A mutex to sync this thread to others if this thread allows (also used for sleeping) */
104         citthread_cond_t ThreadCond;            /* A condition variable to sync this thread with others */
105         citthread_mutex_t SleepMutex;           /* A mutex for sleeping */
106         citthread_cond_t SleepCond;             /* A condition variable for sleeping */
107         citthread_attr_t attr;                  /* Attributes of this thread */
108         struct timeval start_time;              /* Time this thread was started */
109         struct timeval last_state_change;       /* Time when this thread last changed state */
110         double avg_sleeping;                    /* Average sleeping time */
111         double avg_running;                     /* Average running time */
112         double avg_blocked;                     /* Average blocked time */
113         double load_avg;                        /* Load average for this thread */
114         CtdlThreadNode *prev;           /* Previous thread in the thread table */
115         CtdlThreadNode *next;           /* Next thread in the thread table */
116 } ;
117  
118 extern CtdlThreadNode *CtdlThreadList;
119
120 typedef struct ThreadTSD ThreadTSD;
121
122 struct ThreadTSD {
123         DB_TXN *tid;            /* Transaction handle */
124         DBC *cursors[MAXCDB];   /* Cursors, for traversals... */
125         CtdlThreadNode *self;   /* Pointer to this threads control structure */
126 } ;
127
128 extern double CtdlThreadLoadAvg;
129 extern double CtdlThreadWorkerAvg;
130 extern long statcount;          /* are we doing a stats check? */
131 extern citthread_key_t ThreadKey;
132
133 void ctdl_thread_internal_init_tsd(void);
134 void ctdl_internal_thread_gc (void);
135 void ctdl_thread_internal_init(void);
136 void ctdl_thread_internal_cleanup(void);
137 void ctdl_thread_internal_calc_loadavg(void);
138 void ctdl_thread_internal_free_tsd(void);
139 CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args);
140 void ctdl_thread_internal_check_scheduled(void);
141 void ctdl_thread_internal_change_state (CtdlThreadNode *this_thread, enum CtdlThreadState new_state);
142
143 void InitialiseSemaphores(void);
144 int try_critical_section (int which_one);
145 void begin_critical_section (int which_one);
146 void end_critical_section (int which_one);
147 void go_threading(void);
148
149 #endif // THREADS_H