1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-01 09:24:10 +01:00

thread safe printf implementation now

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1066 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
corvus 2010-07-11 13:20:22 +00:00 committed by corvus
parent 5bb4fedc7f
commit 1dd60b54f6

View File

@ -2,37 +2,84 @@
* small etst program whether signals between threads work as they should
*/
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <signal.h>
static pthread_mutex_t Mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t pfMutex = PTHREAD_MUTEX_INITIALIZER;
#define printf(...) (void)pthread_mutex_lock(&pfMutex);printf(__VA_ARGS__);(void)pthread_mutex_unlock(&pfMutex)
/**
* printf is not thread safe - this little helper function allow debug output in thread safe context
*/
/* write a string to a filehandle */
void print_char(int fh, char *c) {
int t=0;
while (c[t]!=0) t++;
write(fh,c,t);
}
/* create a dezimal string from an integer */
int int2char(char* x,long i) {
if (i<0) {
x[0]='-';
return (int2char(&x[1],-i)+1);
}
if (i==0) {
x[0]=0;
return 0;
}
int k=int2char(x,i/10);
x[k]='0'+(i%10);
return k+1;
}
/* print a number*/
void print_number(int fh, long i) {
char buffer[39]; // 39 characters are enough to store a 128 bit integer in dezimal notation (~~3.4* 10^38)
char* number=buffer;
if (i==0) {
number="0";
} else {
int2char(number,i);
}
print_char(fh,number);
}
/**
* actual test program
*/
void sighandler(int sig) {
printf("signal handler called in thread %li - signal %i\n",(long)pthread_self(),sig);
print_char(2,"signal handler called in thread ");
print_number(2,(long)pthread_self());
print_char(2," - signal ");
print_number(2,sig);
print_char(2,"\n");
}
void* threadstart(void* arg) {
struct timespec timeout;
int t;
printf("thread %li started\n",(long)pthread_self());
print_char(2,"thread ");
print_number(2,(long)pthread_self());
print_char(2," started \n");
while (1) {
printf("getting mutex\n");
print_char(2,"getting mutex\n");
pthread_mutex_lock(&Mutex);
printf("got mutex\n");
print_char(2,"got mutex\n");
for (t=0;t<20;t++) {
timeout.tv_sec=1;
timeout.tv_nsec=0;
nanosleep(&timeout,0);
printf("thread %li still running...\n",(long)pthread_self());
print_char(2,"thread ");
print_number(2,(long)pthread_self());
print_char(2," still running...\n");
}
pthread_mutex_unlock(&Mutex);
sleep(1);
@ -45,25 +92,25 @@ int main(char** argc, int argv) {
pthread_t testthread1;
pthread_t testthread2;
struct sigaction action;
printf("thread test program\n");
printf("installing signal handler\n");
print_char(2,"thread test program\n");
print_char(2,"installing signal handler\n");
action.sa_handler=sighandler;
action.sa_flags=0;
sigfillset( &action.sa_mask );
sigaction(SIGUSR1,&action,NULL);
sleep(5);
printf("starting thread 1\n");
print_char(2,"starting thread 1\n");
pthread_create(&testthread1,NULL,threadstart,NULL);
sleep(5);
printf("starting thread 2\n");
print_char(2,"starting thread 2\n");
pthread_create(&testthread2,NULL,threadstart,NULL);
while (1) {
sleep(5);
printf("sending SIG_USR1 to thread 1\n");
print_char(2,"sending SIG_USR1 to thread 1\n");
pthread_kill(testthread1,SIGUSR1);
sleep(5);
printf("sending SIG_USR1 to thread 2\n");
print_char(2,"sending SIG_USR1 to thread 2\n");
pthread_kill(testthread2,SIGUSR1);
}
}