5d21101386d89b22b5476ff931f71f70a5856734
[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
25 /*
26  * Thread stuff
27  */
28
29 enum CtdlThreadState {
30         CTDL_THREAD_INVALID,
31         CTDL_THREAD_VALID,
32         CTDL_THREAD_CREATE,
33         CTDL_THREAD_CANCELLED,
34         CTDL_THREAD_EXITED,
35         CTDL_THREAD_STOPPING,
36         CTDL_THREAD_STOP_REQ,   /* Do NOT put any running states before this state */
37         CTDL_THREAD_SLEEPING,
38         CTDL_THREAD_BLOCKED,
39         CTDL_THREAD_RUNNING,
40         CTDL_THREAD_LAST_STATE
41 };
42
43 extern struct CtdlThreadNode {
44         pthread_t tid;                          /* id as returned by pthread_create() */
45         pid_t pid;                              /* pid, as best the OS will let us determine */
46         time_t when;                            /* When to start a scheduled thread */
47         struct CitConext *Context;              /* The session context that this thread mught be working on or NULL if none */
48         long number;                            /* A unigue number for this thread (not implimented yet) */
49         int wakefd_recv;                        /* An fd that this thread can sleep on (not implimented yet) */
50         int wakefd_send;                        /* An fd that this thread can send out on (Not implimented yet) */
51         int signal;                             /* A field to store a signal we caught. */
52         const char *name;                       /* A name for this thread */
53         void *(*thread_func) (void *arg);       /* The actual function that does this threads work */
54         void *user_args;                        /* Arguments passed to this threads work function */
55         long flags;                             /* Flags that describe this thread */
56         enum CtdlThreadState state;             /* Flag to show state of this thread */
57         pthread_mutex_t ThreadMutex;            /* A mutex to sync this thread to others if this thread allows (also used for sleeping) */
58         pthread_cond_t ThreadCond;              /* A condition variable to sync this thread with others */
59         pthread_mutex_t SleepMutex;             /* A mutex for sleeping */
60         pthread_cond_t SleepCond;               /* A condition variable for sleeping */
61         pthread_attr_t attr;                    /* Attributes of this thread */
62         struct timeval start_time;              /* Time this thread was started */
63         struct timeval last_state_change;       /* Time when this thread last changed state */
64         double avg_sleeping;                    /* Average sleeping time */
65         double avg_running;                     /* Average running time */
66         double avg_blocked;                     /* Average blocked time */
67         double load_avg;                        /* Load average for this thread */
68         struct CtdlThreadNode *prev;            /* Previous thread in the thread table */
69         struct CtdlThreadNode *next;            /* Next thread in the thread table */
70 } *CtdlThreadList;
71
72 typedef struct {
73         DB_TXN *tid;            /* Transaction handle */
74         DBC *cursors[MAXCDB];   /* Cursors, for traversals... */
75         struct CtdlThreadNode *self;    /* Pointer to this threads control structure */
76 }ThreadTSD ;
77
78 extern double CtdlThreadLoadAvg;
79 extern double CtdlThreadWorkerAvg;
80 extern pthread_key_t ThreadKey;
81
82 void ctdl_thread_internal_init_tsd(void);
83 void ctdl_internal_thread_gc (void);
84 void ctdl_thread_internal_init(void);
85 void ctdl_thread_internal_cleanup(void);
86 void ctdl_thread_internal_calc_loadavg(void);
87 void ctdl_thread_internal_free_tsd(void);
88 struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args);
89 void ctdl_thread_internal_check_scheduled(void);
90
91 void InitialiseSemaphores(void);
92 int try_critical_section (int which_one);
93 void begin_critical_section (int which_one);
94 void end_critical_section (int which_one);
95 void go_threading(void);
96
97 #endif // THREADS_H