Changeset 232 for branches

Show
Ignore:
Timestamp:
05/29/06 10:57:52 (18 years ago)
Author:
pieterpalmers
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/libfreebob-2.0/src/debugmodule/debugmodule.cpp

    r228 r232  
    7373    va_start( arg, format ); 
    7474 
    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 ); 
    7976     
    8077    va_end( arg ); 
     
    9693    va_start( arg, format ); 
    9794 
    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
    112101 
    113102const char* 
     
    135124DebugModuleManager::DebugModuleManager() 
    136125{ 
     126        freebob_messagebuffer_init(); 
     127 
    137128} 
    138129 
    139130DebugModuleManager::~DebugModuleManager() 
    140131{ 
     132        freebob_messagebuffer_exit(); 
     133 
    141134} 
    142135 
  • branches/libfreebob-2.0/src/debugmodule/debugmodule.h

    r221 r232  
    2323 
    2424#include "../fbtypes.h" 
     25 
     26#include "messagebuffer.h" 
    2527 
    2628#include <vector> 
  • branches/libfreebob-2.0/src/debugmodule/Makefile.am

    r222 r232  
    1919 
    2020noinst_HEADERS =                                \ 
    21                 debugmodule.h            
     21                debugmodule.h   messagebuffer.h         
    2222 
    2323libdebugmodule_la_SOURCES =             \ 
    24                 debugmodule.cpp         
     24                debugmodule.cpp messagebuffer.c 
    2525 
    2626 
  • branches/libfreebob-2.0/src/debugmodule/messagebuffer.c

    r199 r232  
    4444#define MB_BUFFERS      128 
    4545#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 */ 
    4747 
    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; 
     48static char fb_mb_buffers[MB_BUFFERS][MB_BUFFERSIZE]; 
     49static volatile unsigned int fb_mb_initialized = 0; 
     50static volatile unsigned int fb_mb_inbuffer = 0; 
     51static volatile unsigned int fb_mb_outbuffer = 0; 
     52// static volatile _Atomic_word fb_mb_overruns = 0; 
     53static volatile unsigned int fb_mb_overruns = 0; 
     54static pthread_t fb_mb_writer_thread; 
     55static pthread_mutex_t fb_mb_write_lock; 
     56static pthread_cond_t fb_mb_ready_cond; 
    5757 
    5858static void 
    59 mb_flush() 
     59fb_mb_flush() 
    6060{ 
    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); 
    6565        } 
    6666} 
    6767 
    6868static void * 
    69 mb_thread_func(void *arg) 
     69fb_mb_thread_func(void *arg) 
    7070{ 
    7171        /* The mutex is only to eliminate collisions between multiple 
    7272         * writer threads and protect the condition variable. */ 
    73        pthread_mutex_lock(&mb_write_lock); 
     73//     pthread_mutex_lock(&fb_mb_write_lock); 
    7474 
    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                mb_flush(); 
    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); 
    8282        } 
    8383 
    84        pthread_mutex_unlock(&mb_write_lock); 
     84//     pthread_mutex_unlock(&fb_mb_write_lock); 
    8585 
    8686        return NULL; 
     
    9090freebob_messagebuffer_init () 
    9191{ 
    92         if (mb_initialized) 
     92        if (fb_mb_initialized) 
    9393                return; 
     94                 
     95        fprintf(stderr, "Freebob message buffer init\n"); 
    9496 
    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); 
    9799 
    98        mb_overruns = 0; 
    99        mb_initialized = 1; 
     100       fb_mb_overruns = 0; 
     101       fb_mb_initialized = 1; 
    100102 
    101         if (pthread_create(&mb_writer_thread, NULL, &mb_thread_func, NULL) != 0) 
    102                mb_initialized = 0; 
     103        if (pthread_create(&fb_mb_writer_thread, NULL, &fb_mb_thread_func, NULL) != 0) 
     104               fb_mb_initialized = 0; 
    103105} 
    104106 
     
    106108freebob_messagebuffer_exit () 
    107109{ 
    108         if (!mb_initialized) 
     110        if (!fb_mb_initialized) 
    109111                return; 
    110112 
    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); 
    115117 
    116         pthread_join(mb_writer_thread, NULL); 
    117         mb_flush(); 
     118        pthread_join(fb_mb_writer_thread, NULL); 
     119        fb_mb_flush(); 
    118120 
    119         if (mb_overruns) 
     121        if (fb_mb_overruns) 
    120122                fprintf(stderr, "WARNING: %d message buffer overruns!\n", 
    121                         mb_overruns); 
     123                        fb_mb_overruns); 
    122124        else 
    123125                fprintf(stderr, "no message buffer overruns\n"); 
    124126 
    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); 
    127129} 
    128130 
     
    139141        va_end(ap); 
    140142 
    141         if (!mb_initialized) { 
     143        if (!fb_mb_initialized) { 
    142144                /* Unable to print message with realtime safety. 
    143145                 * Complain and print it anyway. */ 
     
    147149        } 
    148150 
    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); 
    154156        } else {                        /* lock collision */ 
    155 //              atomic_add(&mb_overruns, 1); 
     157//              atomic_add(&fb_mb_overruns, 1); 
    156158                // FIXME: atomicity 
    157                 mb_overruns++; // skip the atomicness for now 
     159                fb_mb_overruns++; // skip the atomicness for now 
    158160        } 
    159161} 
     162 
     163void  
     164freebob_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  
    3333 */ 
    3434 
     35#include <stdarg.h> 
     36 
    3537#ifndef __FREEBOB_MESSAGEBUFFER_H__ 
    3638#define __FREEBOB_MESSAGEBUFFER_H__ 
     
    4446 
    4547void freebob_messagebuffer_add(const char *fmt, ...); 
     48void freebob_messagebuffer_va_add (const char *fmt, va_list ap); 
    4649 
    4750#ifdef __cplusplus