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