f6fa812bf1c176ae2db0649640ed85b12d8e76ad
[citadel.git] / citadel / threads.h
1
2 #ifndef THREADS_H
3 #define THREADS_H
4
5 #include "sysdep.h"
6
7 #ifdef HAVE_PTHREAD_H
8 #include <pthread.h>
9 #endif
10
11 #include <sys/time.h>
12 #include <string.h>
13
14 #ifdef HAVE_DB_H
15 #include <db.h>
16 #elif defined(HAVE_DB4_DB_H)
17 #include <db4/db.h>
18 #else
19 #error Neither <db.h> nor <db4/db.h> was found by configure. Install db4-devel.
20 #endif
21
22 #include "server.h"
23 #include "sysdep_decls.h"
24
25 #ifndef timerclear
26 #define timerclear(tvp)         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
27 #endif
28
29 #ifndef timerisset
30 #define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
31 #endif
32
33 #ifndef timercmp
34 #define timercmp(tvp, uvp, cmp) \
35  (((tvp)->tv_sec == (uvp)->tv_sec) ? \
36      ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
37      ((tvp)->tv_sec cmp (uvp)->tv_sec))
38 #endif
39
40 #ifndef timeradd
41 #define timeradd(tvp, uvp, vvp) \
42  do { \
43   (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
44   (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
45   if ((vvp)->tv_usec >= 1000000) { \
46    (vvp)->tv_sec++; \
47    (vvp)->tv_usec -= 1000000; \
48   } \
49  } while (0)
50 #endif
51
52 #ifndef timersub
53 #define timersub(tvp, uvp, vvp) \
54  do { \
55   (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
56   (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
57   if ((vvp)->tv_usec < 0) { \
58    (vvp)->tv_sec--; \
59    (vvp)->tv_usec += 1000000; \
60   } \
61  } while (0)
62 #endif
63  
64
65 // #define THREADS_USESIGNALS
66
67 /*
68  * Thread stuff
69  */
70
71 enum CtdlThreadState {
72         CTDL_THREAD_INVALID,
73         CTDL_THREAD_VALID,
74         CTDL_THREAD_CREATE,
75         CTDL_THREAD_CANCELLED,
76         CTDL_THREAD_EXITED,
77         CTDL_THREAD_STOPPING,
78         CTDL_THREAD_STOP_REQ,   /* Do NOT put any running states before this state */
79         CTDL_THREAD_SLEEPING,
80         CTDL_THREAD_BLOCKED,
81         CTDL_THREAD_RUNNING,
82         CTDL_THREAD_LAST_STATE
83 };
84 typedef struct CtdlThreadNode CtdlThreadNode;
85
86 struct CtdlThreadNode{
87         citthread_t tid;                                /* id as returned by citthread_create() */
88         pid_t pid;                              /* pid, as best the OS will let us determine */
89         long reltid;                            /* counting from start... */
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 */
115 } ;
116  
117 extern CtdlThreadNode *CtdlThreadList;
118
119 typedef struct ThreadTSD ThreadTSD;
120
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 */
125 } ;
126
127 extern double CtdlThreadLoadAvg;
128 extern double CtdlThreadWorkerAvg;
129 extern long statcount;          /* are we doing a stats check? */
130 extern citthread_key_t ThreadKey;
131
132 void ctdl_thread_internal_init_tsd(void);
133 void ctdl_internal_thread_gc (void);
134 void ctdl_thread_internal_init(void);
135 void ctdl_thread_internal_cleanup(void);
136 void ctdl_thread_internal_calc_loadavg(void);
137 void ctdl_thread_internal_free_tsd(void);
138 CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args);
139 void ctdl_thread_internal_check_scheduled(void);
140 void ctdl_thread_internal_change_state (CtdlThreadNode *this_thread, enum CtdlThreadState new_state);
141
142 void InitialiseSemaphores(void);
143 int try_critical_section (int which_one);
144 void begin_critical_section (int which_one);
145 void end_critical_section (int which_one);
146 void go_threading(void);
147
148 #endif // THREADS_H