Try to rationalise the mutex lock/unlock sequence. Hunting for an
[citadel.git] / citadel / sysdep_decls.h
1 /* $Id$ */
2
3 #ifndef SYSDEP_DECLS_H
4 #define SYSDEP_DECLS_H
5
6 /*
7  * Uncomment this #define if you are a Citadel developer tracking
8  * down memory leaks in the server.  Do NOT do this on a production
9  * system because it definitely incurs a lot of additional overhead.
10 #define DEBUG_MEMORY_LEAKS
11  */
12
13
14 #include <pthread.h>
15 #include <stdarg.h>
16 #include "sysdep.h"
17 #include "server.h"
18
19 #if SIZEOF_SIZE_T == SIZEOF_INT 
20 #define SIZE_T_FMT "%d"
21 #else
22 #define SIZE_T_FMT "%ld"
23 #endif
24
25
26 /* Logging levels - correspond to syslog(3) */
27 enum LogLevel {
28         /* When about to exit the server for an unrecoverable error */
29          CTDL_EMERG,    /* system is unusable */
30         /* Manual intervention is required to avoid an abnormal exit */
31          CTDL_ALERT,    /* action must be taken immediately */
32         /* The server can continue to run with degraded functionality */
33          CTDL_CRIT,     /* critical conditions */
34         /* An error occurs but the server continues to run normally */
35          CTDL_ERR,      /* error conditions */
36         /* An abnormal condition was detected; server will continue normally */
37          CTDL_WARNING,  /* warning conditions */
38         /* Normal messages (login/out, activity, etc.) */
39          CTDL_NOTICE,   /* normal but significant condition */
40         /* Unimportant progress messages, etc. */
41          CTDL_INFO,     /* informational */
42         /* Debugging messages */
43          CTDL_DEBUG     /* debug-level messages */
44 };
45
46 #ifdef __GNUC__
47 void lprintf (enum LogLevel loglevel, const char *format, ...) __attribute__((__format__(__printf__,2,3)));
48 void cprintf (const char *format, ...) __attribute__((__format__(__printf__,1,2)));
49 #else
50 void lprintf (enum LogLevel loglevel, const char *format, ...);
51 void cprintf (const char *format, ...);
52 #endif
53
54 void vlprintf (enum LogLevel loglevel, const char *format, va_list arg_ptr);
55
56 extern pthread_key_t MyConKey;                  /* TSD key for MyContext() */
57
58 extern int enable_syslog;
59
60 void init_sysdep (void);
61 void begin_critical_section (int which_one);
62 void end_critical_section (int which_one);
63 int ig_tcp_server (char *ip_addr, int port_number, int queue_len,char **errormessage);
64 int ig_uds_server(char *sockpath, int queue_len, char **errormessage);
65 struct CitContext *MyContext (void);
66 struct CitContext *CreateNewContext (void);
67 void InitMyContext (struct CitContext *con);
68 void buffer_output(void);
69 void unbuffer_output(void);
70 void flush_output(void);
71 void client_write (char *buf, int nbytes);
72 int client_read_to (char *buf, int bytes, int timeout);
73 int client_read (char *buf, int bytes);
74 int client_getln (char *buf, int maxbytes);
75 void sysdep_master_cleanup (void);
76 void kill_session (int session_to_kill);
77 void *sd_context_loop (struct CitContext *con);
78 void start_daemon (int do_close_stdio);
79 void cmd_nset (char *cmdbuf);
80 int convert_login (char *NameToConvert);
81 void *worker_thread (void *arg);
82 void *context_cleanup_thread (void *arg);
83 void become_session(struct CitContext *which_con);
84 void InitializeMasterCC(void);
85 void init_master_fdset(void);
86 void create_worker(void);
87 void InitialiseSemaphores(void);
88
89
90 extern int num_sessions;
91 extern volatile int exit_signal;
92 extern volatile int shutdown_and_halt;
93 extern volatile int running_as_daemon;
94 extern volatile int restart_server;
95
96 extern int verbosity;
97 extern int rescan[];
98
99 extern struct worker_node {
100         pthread_t tid;
101         struct worker_node *next;
102 } *worker_list;
103
104
105
106 /*
107  * Thread stuff
108  */
109 #define CTDLTHREAD_BIGSTACK     0x0001
110 #define CTDLTHREAD_WORKER       0x0002
111
112 enum CtdlThreadState {
113         CTDL_THREAD_INVALID,
114         CTDL_THREAD_VALID,
115         CTDL_THREAD_CREATE,
116         CTDL_THREAD_CANCELLED,
117         CTDL_THREAD_EXITED,
118         CTDL_THREAD_STOPPING,
119         CTDL_THREAD_STOP_REQ,   /* Do NOT put any running states before this state */
120         CTDL_THREAD_SLEEPING,
121         CTDL_THREAD_BLOCKED,
122         CTDL_THREAD_RUNNING,
123         CTDL_THREAD_LAST_STATE
124 };
125
126 extern struct CtdlThreadNode {
127         pthread_t tid;                          /* id as returned by pthread_create() */
128         pid_t pid;                              /* pid, as best the OS will let us determine */
129         struct CitConext *Context;              /* The session context that this thread mught be working on or NULL if none */
130         long number;                            /* A unigue number for this thread (not implimented yet) */
131         int wakefd_recv;                        /* An fd that this thread can sleep on (not implimented yet) */
132         int wakefd_send;                        /* An fd that this thread can send out on (Not implimented yet) */
133         char *name;                             /* A name for this thread */
134         void *(*thread_func) (void *arg);       /* The actual function that does this threads work */
135         void *user_args;                        /* Arguments passed to this threads work function */
136         long flags;                             /* Flags that describe this thread */
137         enum CtdlThreadState state;             /* Flag to show state of this thread */
138         pthread_mutex_t ThreadMutex;            /* A mutex to sync this thread to others if this thread allows (also used for sleeping) */
139         pthread_cond_t ThreadCond;              /* A condition variable to sync this thread with others */
140         pthread_mutex_t SleepMutex;             /* A mutex for sleeping */
141         pthread_cond_t SleepCond;               /* A condition variable for sleeping */
142         pthread_attr_t attr;                    /* Attributes of this thread */
143         struct timeval start_time;              /* Time this thread was started */
144         struct timeval last_state_change;       /* Time when this thread last changed state */
145         double avg_sleeping;                    /* Average sleeping time */
146         double avg_running;                     /* Average running time */
147         double avg_blocked;                     /* Average blocked time */
148         double load_avg;                        /* Load average for this thread */
149         struct CtdlThreadNode *prev;            /* Previous thread in the thread table */
150         struct CtdlThreadNode *next;            /* Next thread in the thread table */
151 } *CtdlThreadList;
152
153
154 extern double CtdlThreadLoadAvg;
155 extern double CtdlThreadWorkerAvg;
156
157 void ctdl_internal_thread_gc (void);
158 void ctdl_thread_internal_init(void);
159 void ctdl_thread_internal_cleanup(void);
160 void ctdl_thread_internal_calc_loadavg(void);
161 struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args);
162
163
164
165 extern int SyslogFacility(char *name);
166 extern int syslog_facility;
167
168 #ifdef DEBUG_MEMORY_LEAKS
169 #define malloc(x) tracked_malloc(x, __FILE__, __LINE__)
170 #define realloc(x,y) tracked_realloc(x, y, __FILE__, __LINE__)
171 #undef strdup
172 #define strdup(x) tracked_strdup(x, __FILE__, __LINE__)
173 #define free(x) tracked_free(x)
174 void *tracked_malloc(size_t size, char *file, int line);
175 void *tracked_realloc(void *ptr, size_t size, char *file, int line);
176 void tracked_free(void *ptr);
177 char *tracked_strdup(const char *s, char *file, int line);
178 void dump_heap(void);
179 #endif
180
181 void create_maintenance_threads(void);
182
183 #endif /* SYSDEP_DECLS_H */