Hi Daixiwen,
Chapter 10 of the Nios II developer's handbook has a section about Newlib telling that programs based on MicroC/OS-II can call ANSI C standard library functions. I interpret this as that they are thread safe...
By the way, I am not using printf from different threads. I have an interrupt routine which post some messages to another task. From that task I decide to send debug info to the Nios II terminal window with printf().
I want to explain why i came to this question on this forum:
One of the Altera provided sample projects shows another way to override the default printf() function. It looks like this:
int printf( const char * format, ... )
{
OS_SEM_DATA semdata;
INT8U err;
char* string;
int length;
int ret_code;
// If the OSPrintfSem semaphore has not yet been initialized,
// wait until it has before we proceed.
while( OSSemQuery( OSPrintfSem, &semdata ) != OS_NO_ERR )
{
OSTimeDlyHMSM( 0, 0, 0, 10 );
}
// Grab the printf semaphore before we try any printing.
OSSemPend( OSPrintfSem, 0, &err );
if( err == OS_NO_ERR )
{
string = malloc( 256 );
va_list ap;
va_start( ap ,format );
ret_code = vsprintf( string, format, ap );
va_end( ap );
length = strlen( string );
fwrite ( string, 1, length, stdout );
free( string );
err = OSSemPost( OSPrintfSem );
}
if( err == OS_NO_ERR )
{
return( ret_code );
}
else
{
return( -1 );
}
}
I had no problems at all when I used the default Nios II internal IIC. But for some reasons I needed to switch to the external vectored interrupt controller and then all problems began. It took me headache to understand what was going on and why my processor seemed to stall sometimes in alt_tick() ( i posted another message which did not result in responses on this forum).
Today I discovered that I could solve this issue by using another implementation of the printf by using something like this:
int printf(const char *format, ...)
{
alt_u8 err;
OSSemPend(OSPrintfSem, 0, &err);
va_list vl;
va_start(vl, format);
vprintf(format, vl);
va_end(vl);
OSSemPost(OSPrintfSem);
}
That solved unexpected behaviour of my Nios hangup behaviour. Therefore I was triggered again about all this printf() behaviour and found the text in the Nios manual which indicates that printf() should be thread safe. Or I missinterpret something there....
Anyway... when using the default printf() I get buggy behaviour and using the second example above of replacing the default printf() seems to solve my problem.
But i still do not understand why I did not got this faulty behaviour with the IIC....(where I used the first example above of the replaced printf()...
regards