8cf58285553d9ef8887d458e7e1e475c1faed4b6
[goodguy/history.git] / cinelerra-5.1 / guicast / bctrace.h
1 #ifndef __BC_TRACE_H__
2 #define __BC_TRACE_H__
3
4 #include "arraylist.h"
5 #include "linklist.h"
6 #include "bctrace.inc"
7 #include "bcwindowbase.inc"
8 #include "cstrdup.h"
9 #include <pthread.h>
10
11
12 class BC_Trace
13 {
14 public:
15         BC_Trace();
16         ~BC_Trace();
17
18         static BC_Trace *global_trace;
19         static void reset_locks();
20
21         static void delete_temps();
22         static void set_temp(char *string);
23         static void unset_temp(char *string);
24
25         static void enable_locks();
26         static void disable_locks();
27         static int set_lock(const char *title, const char *location, trace_info *info);
28         static void set_lock2(int table_id, trace_info *info);
29         static void unset_lock2(int table_id, trace_info *info);
30         static void unset_lock(trace_info *info);
31 // Used in lock destructors so takes away all references
32         static void unset_all_locks(trace_info *info);
33         static void clear_locks_tid(pthread_t tid);
34
35         static void new_trace(const char *text);
36         static void new_trace(const char *file, const char *function, int line);
37         static void delete_traces();
38
39         static void enable_memory();
40         static void disable_memory();
41         static void set_buffer(int size, void *ptr, const char* location);
42 // This one returns 1 if the buffer wasn't found.
43         static int unset_buffer(void *ptr);
44         static void lock_locks(const char *s);
45         static void unlock_locks();
46
47         static void dump_traces(FILE *fp=stdout);
48         static void dump_locks(FILE *fp=stdout);
49         static void dump_buffers(FILE *fp=stdout);
50         static void dump_threads(FILE *fp=stdout);
51
52         static void dump_shm_stat(const char *fn, FILE *fp=stdout);
53         static void dump_shm_stats(FILE *fp=stdout);
54 };
55
56 class bc_trace_list : public List<trace_item> {
57 public:
58         void clear() { while( last ) remove(last); }
59         bc_trace_list() {}
60         ~bc_trace_list() { clear(); }
61 };
62
63 class bc_trace_t : public bc_trace_list {
64 public:
65         int size;
66         bc_trace_t() : size(0) {}
67         ~bc_trace_t() {}
68 };
69
70 class bc_trace_spin : public bc_trace_t {
71         pthread_spinlock_t spin;
72 public:
73         void *operator new(size_t n) { return (void*) malloc(n); }
74         void operator delete(void *t, size_t n) { free(t); }
75
76         void lock() { pthread_spin_lock(&spin); }
77         void unlock() { pthread_spin_unlock(&spin); }
78         bc_trace_spin() { pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE); }
79         ~bc_trace_spin() { pthread_spin_destroy(&spin); }
80 };
81
82 class bc_trace_mutex : public bc_trace_t {
83         pthread_mutex_t mutex;
84 public:
85         void *operator new(size_t n) { return (void*) malloc(n); }
86         void operator delete(void *t, size_t n) { free(t); }
87
88         void lock() { pthread_mutex_lock(&mutex); }
89         void unlock() { pthread_mutex_unlock(&mutex); }
90         bc_trace_mutex() { pthread_mutex_init(&mutex, 0); }
91         ~bc_trace_mutex() { pthread_mutex_destroy(&mutex); }
92 };
93
94 extern bc_trace_mutex execution_table;
95 extern bc_trace_mutex memory_table;
96 extern bc_trace_mutex lock_table;
97 extern bc_trace_mutex file_table;
98 extern "C" void dump();
99
100 class trace_item : public ListItem<trace_item> {
101 public:
102         bc_trace_t &table;
103         trace_item(bc_trace_t &t);
104         ~trace_item();
105 };
106
107 class execution_item : public trace_item {
108 public:
109         void *operator new(size_t n) { return (void*) malloc(n); }
110         void operator delete(void *t, size_t n) { free(t); }
111
112         const char *value;
113         void clear() { delete [] value;  value = 0; }
114         void set(const char *v) { delete [] value;  value = cstrdup(v); }
115
116         execution_item() : trace_item(execution_table) { value = 0; }
117         ~execution_item() { clear(); }
118 };
119
120 class lock_item : public trace_item {
121         static int table_id;
122 public:
123         void *operator new(size_t n) { return (void*) malloc(n); }
124         void operator delete(void *t, size_t n) { free(t); }
125
126         trace_info *info;
127         const char *title;
128         const char *loc;
129         int is_owner;
130         int id;
131         pthread_t tid;
132         void set(trace_info *info, const char *title, const char *loc) {
133                 this->info = info;  this->title = title;
134                 this->loc = loc;  this->is_owner = 0;
135                 this->id = table_id++;  this->tid = pthread_self();
136         }
137         void clear() {
138                 this->info = 0;  this->title = 0; this->loc = 0;
139                 this->is_owner = 0;  this->id = -1;  this->tid = 0;
140         }
141
142         lock_item() : trace_item(lock_table) { clear(); }
143         ~lock_item() {}
144 };
145
146 class memory_item : public trace_item {
147 public:
148         void *operator new(size_t n) { return (void*) malloc(n); }
149         void operator delete(void *t, size_t n) { free(t); }
150
151         int size;
152         void *ptr;
153         const char *loc;
154
155         memory_item(int size, void *ptr, const char *loc)
156          : trace_item(memory_table) {
157                 this->size = size; this->ptr = ptr; this->loc = loc;
158         }
159         ~memory_item() {}
160 };
161
162 class file_item : public trace_item {
163 public:
164         void *operator new(size_t n) { return (void*) malloc(n); }
165         void operator delete(void *t, size_t n) { free(t); }
166
167         const char *value;
168         void clear() { delete [] value;  value = 0; }
169         void set(const char *v) { delete [] value;  value = cstrdup(v); }
170
171         file_item() : trace_item(file_table) { value = 0; }
172         ~file_item() { clear(); }
173 };
174
175 // track unjoined threads at termination
176 #ifdef TRACE_THREADS
177
178 class TheLock {
179 public:
180         pthread_mutex_t the_lock;
181
182         void lock() { pthread_mutex_lock(&the_lock); }
183         void unlock() { pthread_mutex_unlock(&the_lock); }
184
185         void init() {
186                 pthread_mutexattr_t attr;
187                 pthread_mutexattr_init(&attr);
188                 pthread_mutex_init(&the_lock, &attr);
189         }
190         void finit() {
191                 pthread_mutex_destroy(&the_lock);
192         }
193         void reset() { finit();  init(); }
194
195         TheLock()  { init();  }
196         ~TheLock() { finit(); }
197 };
198
199 class TheLocker {
200 public:
201         static TheLock the_lock;
202         static void reset() { the_lock.reset(); }
203
204         TheLocker() { the_lock.lock(); }
205         ~TheLocker() { the_lock.unlock(); }
206 };
207
208 class TheDbg {
209 public:
210         pthread_t tid, owner;  const char *name;
211         TheDbg(pthread_t t, pthread_t o, const char *nm) { tid = t; owner = o; name = nm; }
212         ~TheDbg() {}
213 };
214
215
216 class TheList : public ArrayList<TheDbg *> {
217 public:
218         static TheList the_list;
219         static void dump_threads(FILE *fp);
220         static void dbg_add(pthread_t tid, pthread_t owner, const char *nm);
221         static void dbg_del(pthread_t tid);
222         static void reset() { the_list.remove_all_objects(); TheLocker::reset(); }
223         void check() {
224                 int i = the_list.size();
225                 if( !i ) return;
226                 printf("unjoined tids / owner %d\n", i);
227                 while( --i >= 0 ) printf("  %016lx / %016lx %s\n",
228                         (unsigned long)the_list[i]->tid,
229                         (unsigned long)the_list[i]->owner,
230                         the_list[i]->name);
231         }
232          TheList() {}
233         ~TheList() { check(); reset(); }
234 };
235
236 #endif
237
238 #endif