- Timestamp:
- 05/29/06 10:57:52 (18 years ago)
- Files:
-
- branches/libfreebob-2.0/src/debugmodule/debugmodule.cpp (modified) (3 diffs)
- branches/libfreebob-2.0/src/debugmodule/debugmodule.h (modified) (1 diff)
- branches/libfreebob-2.0/src/debugmodule/Makefile.am (modified) (1 diff)
- branches/libfreebob-2.0/src/debugmodule/messagebuffer.c (copied) (copied from branches/libfreebob-2.0/src/libstreaming/messagebuffer.c) (5 diffs)
- branches/libfreebob-2.0/src/debugmodule/messagebuffer.h (copied) (copied from branches/libfreebob-2.0/src/libstreaming/messagebuffer.h) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/libfreebob-2.0/src/debugmodule/debugmodule.cpp
r228 r232 73 73 va_start( arg, format ); 74 74 75 if ( vprintf( format, arg ) < 0 ) { 76 cerr << "Could not create debug string with printf" << endl; 77 return; 78 } 75 //freebob_messagebuffer_va_add( format, arg ); 79 76 80 77 va_end( arg ); … … 96 93 va_start( arg, format ); 97 94 98 if ( printf( "%s (%s)[%d] %s: ", getPreSequence( level ), 99 file, line, function ) < 0 ) { 100 cerr << "Could not create debug string with printf" << endl; 101 return; 102 } 103 if ( vprintf( format, arg ) < 0 ) { 104 cerr << "Could not create debug string with printf" << endl; 105 return; 106 } 107 if ( printf( "%s", getPostSequence( level ) ) < 0 ) { 108 cerr << "Could not create debug string with printf" << endl; 109 return; 110 } 111 va_end( arg );} 95 //freebob_messagebuffer_add( "%s (%s)[%d] %s: ", getPreSequence( level ), 96 // file, line, function ); 97 //freebob_messagebuffer_va_add( format, arg ); 98 //freebob_messagebuffer_add( "%s", getPostSequence( level ) ); 99 va_end( arg ); 100 } 112 101 113 102 const char* … … 135 124 DebugModuleManager::DebugModuleManager() 136 125 { 126 freebob_messagebuffer_init(); 127 137 128 } 138 129 139 130 DebugModuleManager::~DebugModuleManager() 140 131 { 132 freebob_messagebuffer_exit(); 133 141 134 } 142 135 branches/libfreebob-2.0/src/debugmodule/debugmodule.h
r221 r232 23 23 24 24 #include "../fbtypes.h" 25 26 #include "messagebuffer.h" 25 27 26 28 #include <vector> branches/libfreebob-2.0/src/debugmodule/Makefile.am
r222 r232 19 19 20 20 noinst_HEADERS = \ 21 debugmodule.h 21 debugmodule.h messagebuffer.h 22 22 23 23 libdebugmodule_la_SOURCES = \ 24 debugmodule.cpp 24 debugmodule.cpp messagebuffer.c 25 25 26 26 branches/libfreebob-2.0/src/debugmodule/messagebuffer.c
r199 r232 44 44 #define MB_BUFFERS 128 45 45 #define MB_NEXT(index) ((index+1) & (MB_BUFFERS-1)) 46 #define MB_BUFFERSIZE 256/* message length limit */46 #define MB_BUFFERSIZE 512 /* message length limit */ 47 47 48 static char mb_buffers[MB_BUFFERS][MB_BUFFERSIZE];49 static volatile unsigned int mb_initialized = 0;50 static volatile unsigned int mb_inbuffer = 0;51 static volatile unsigned int mb_outbuffer = 0;52 // static volatile _Atomic_word mb_overruns = 0;53 static volatile unsigned int mb_overruns = 0;54 static pthread_t mb_writer_thread;55 static pthread_mutex_t mb_write_lock;56 static pthread_cond_t mb_ready_cond;48 static char fb_mb_buffers[MB_BUFFERS][MB_BUFFERSIZE]; 49 static volatile unsigned int fb_mb_initialized = 0; 50 static volatile unsigned int fb_mb_inbuffer = 0; 51 static volatile unsigned int fb_mb_outbuffer = 0; 52 // static volatile _Atomic_word fb_mb_overruns = 0; 53 static volatile unsigned int fb_mb_overruns = 0; 54 static pthread_t fb_mb_writer_thread; 55 static pthread_mutex_t fb_mb_write_lock; 56 static pthread_cond_t fb_mb_ready_cond; 57 57 58 58 static void 59 mb_flush()59 fb_mb_flush() 60 60 { 61 /* called WITHOUT the mb_write_lock */62 while ( mb_outbuffer !=mb_inbuffer) {63 fputs( mb_buffers[mb_outbuffer], stderr);64 mb_outbuffer = MB_NEXT(mb_outbuffer);61 /* called WITHOUT the fb_mb_write_lock */ 62 while (fb_mb_outbuffer != fb_mb_inbuffer) { 63 fputs(fb_mb_buffers[fb_mb_outbuffer], stderr); 64 fb_mb_outbuffer = MB_NEXT(fb_mb_outbuffer); 65 65 } 66 66 } 67 67 68 68 static void * 69 mb_thread_func(void *arg)69 fb_mb_thread_func(void *arg) 70 70 { 71 71 /* The mutex is only to eliminate collisions between multiple 72 72 * writer threads and protect the condition variable. */ 73 pthread_mutex_lock(&mb_write_lock);73 // pthread_mutex_lock(&fb_mb_write_lock); 74 74 75 while ( mb_initialized) {76 pthread_cond_wait(&mb_ready_cond, &mb_write_lock);77 78 /* releasing the mutex reduces contention */79 pthread_mutex_unlock(&mb_write_lock);80 81 pthread_mutex_lock(&mb_write_lock);75 while (fb_mb_initialized) { 76 // pthread_cond_wait(&fb_mb_ready_cond, &fb_mb_write_lock); 77 // 78 // /* releasing the mutex reduces contention */ 79 // pthread_mutex_unlock(&fb_mb_write_lock); 80 // fb_mb_flush(); 81 // pthread_mutex_lock(&fb_mb_write_lock); 82 82 } 83 83 84 pthread_mutex_unlock(&mb_write_lock);84 // pthread_mutex_unlock(&fb_mb_write_lock); 85 85 86 86 return NULL; … … 90 90 freebob_messagebuffer_init () 91 91 { 92 if ( mb_initialized)92 if (fb_mb_initialized) 93 93 return; 94 95 fprintf(stderr, "Freebob message buffer init\n"); 94 96 95 pthread_mutex_init(& mb_write_lock, NULL);96 pthread_cond_init(& mb_ready_cond, NULL);97 pthread_mutex_init(&fb_mb_write_lock, NULL); 98 pthread_cond_init(&fb_mb_ready_cond, NULL); 97 99 98 99 100 fb_mb_overruns = 0; 101 fb_mb_initialized = 1; 100 102 101 if (pthread_create(& mb_writer_thread, NULL, &mb_thread_func, NULL) != 0)102 103 if (pthread_create(&fb_mb_writer_thread, NULL, &fb_mb_thread_func, NULL) != 0) 104 fb_mb_initialized = 0; 103 105 } 104 106 … … 106 108 freebob_messagebuffer_exit () 107 109 { 108 if (! mb_initialized)110 if (!fb_mb_initialized) 109 111 return; 110 112 111 pthread_mutex_lock(& mb_write_lock);112 mb_initialized = 0;113 pthread_cond_signal(& mb_ready_cond);114 pthread_mutex_unlock(& mb_write_lock);113 pthread_mutex_lock(&fb_mb_write_lock); 114 fb_mb_initialized = 0; 115 pthread_cond_signal(&fb_mb_ready_cond); 116 pthread_mutex_unlock(&fb_mb_write_lock); 115 117 116 pthread_join( mb_writer_thread, NULL);117 mb_flush();118 pthread_join(fb_mb_writer_thread, NULL); 119 fb_mb_flush(); 118 120 119 if ( mb_overruns)121 if (fb_mb_overruns) 120 122 fprintf(stderr, "WARNING: %d message buffer overruns!\n", 121 mb_overruns);123 fb_mb_overruns); 122 124 else 123 125 fprintf(stderr, "no message buffer overruns\n"); 124 126 125 pthread_mutex_destroy(& mb_write_lock);126 pthread_cond_destroy(& mb_ready_cond);127 pthread_mutex_destroy(&fb_mb_write_lock); 128 pthread_cond_destroy(&fb_mb_ready_cond); 127 129 } 128 130 … … 139 141 va_end(ap); 140 142 141 if (! mb_initialized) {143 if (!fb_mb_initialized) { 142 144 /* Unable to print message with realtime safety. 143 145 * Complain and print it anyway. */ … … 147 149 } 148 150 149 if (pthread_mutex_trylock(& mb_write_lock) == 0) {150 strncpy( mb_buffers[mb_inbuffer], msg, MB_BUFFERSIZE);151 mb_inbuffer = MB_NEXT(mb_inbuffer);152 pthread_cond_signal(& mb_ready_cond);153 pthread_mutex_unlock(& mb_write_lock);151 if (pthread_mutex_trylock(&fb_mb_write_lock) == 0) { 152 strncpy(fb_mb_buffers[fb_mb_inbuffer], msg, MB_BUFFERSIZE); 153 fb_mb_inbuffer = MB_NEXT(fb_mb_inbuffer); 154 pthread_cond_signal(&fb_mb_ready_cond); 155 pthread_mutex_unlock(&fb_mb_write_lock); 154 156 } else { /* lock collision */ 155 // atomic_add(& mb_overruns, 1);157 // atomic_add(&fb_mb_overruns, 1); 156 158 // FIXME: atomicity 157 mb_overruns++; // skip the atomicness for now159 fb_mb_overruns++; // skip the atomicness for now 158 160 } 159 161 } 162 163 void 164 freebob_messagebuffer_va_add (const char *fmt, va_list ap) 165 { 166 char msg[MB_BUFFERSIZE]; 167 168 /* format the message first, to reduce lock contention */ 169 vsnprintf(msg, MB_BUFFERSIZE, fmt, ap); 170 171 if (!fb_mb_initialized) { 172 /* Unable to print message with realtime safety. 173 * Complain and print it anyway. */ 174 fprintf(stderr, "ERROR: messagebuffer not initialized: %s", 175 msg); 176 return; 177 } 178 179 if (pthread_mutex_trylock(&fb_mb_write_lock) == 0) { 180 strncpy(fb_mb_buffers[fb_mb_inbuffer], msg, MB_BUFFERSIZE); 181 fb_mb_inbuffer = MB_NEXT(fb_mb_inbuffer); 182 pthread_cond_signal(&fb_mb_ready_cond); 183 pthread_mutex_unlock(&fb_mb_write_lock); 184 } else { /* lock collision */ 185 // atomic_add(&fb_mb_overruns, 1); 186 // FIXME: atomicity 187 fb_mb_overruns++; // skip the atomicness for now 188 } 189 } branches/libfreebob-2.0/src/debugmodule/messagebuffer.h
r201 r232 33 33 */ 34 34 35 #include <stdarg.h> 36 35 37 #ifndef __FREEBOB_MESSAGEBUFFER_H__ 36 38 #define __FREEBOB_MESSAGEBUFFER_H__ … … 44 46 45 47 void freebob_messagebuffer_add(const char *fmt, ...); 48 void freebob_messagebuffer_va_add (const char *fmt, va_list ap); 46 49 47 50 #ifdef __cplusplus