fix progress bar deadlock

This commit is contained in:
ubuntu201711081 2020-12-08 16:49:50 +00:00
parent bd92bc47f2
commit b3ddb92786
4 changed files with 1036 additions and 2 deletions

View File

@ -419,6 +419,7 @@ int main(int argc, const char *argv[]){
init_bench_data(); init_bench_data();
if(parse_arg(argc,argv) < 0) return EXIT_FAILURE; if(parse_arg(argc,argv) < 0) return EXIT_FAILURE;
ready_progress_bar();
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,cmd_args.server_name,(struct sockaddr *)&addr); err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,cmd_args.server_name,(struct sockaddr *)&addr);
if (err != 0){ if (err != 0){
@ -480,6 +481,6 @@ int main(int argc, const char *argv[]){
fprintf(stdout,"operation: %lf us/op\n",avg / 1000.0); fprintf(stdout,"operation: %lf us/op\n",avg / 1000.0);
fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec); fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec);
} }
quit_progress_bar();
return retval; return retval;
} }

View File

@ -6,6 +6,9 @@
#include<unistd.h> #include<unistd.h>
#include<termio.h> #include<termio.h>
#include<pthread.h> #include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include "display_bar.h" #include "display_bar.h"
#include "timerhelper.h" #include "timerhelper.h"
@ -25,15 +28,34 @@ enum{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static raw_progress_bar_t scrolled = 1; static raw_progress_bar_t scrolled = 1;
static int tty_fd = STDIN_FILENO;
#ifdef DEBUG
static int deadlock = 0;
#endif
void ready_progress_bar(){ void ready_progress_bar(){
fflush(stdout); fflush(stdout);
if(!isatty(STDIN_FILENO)){
char * buf = ctermid(NULL);
tty_fd = open(buf,O_RDONLY);
}
} }
void lock_scrolled(){ void lock_scrolled(){
#ifdef DEBUG
deadlock++;
if (deadlock > 1)
{
fprintf(stderr,"deadlock occur\n");
exit(EXIT_FAILURE);
}
#endif
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
} }
void unlock_scrolled(){ void unlock_scrolled(){
#ifdef DEBUG
deadlock--;
#endif
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
} }
@ -75,7 +97,10 @@ void RawDisplayProgressBar(raw_progress_bar_t bar,size_t offset,size_t total,con
}//optimization. }//optimization.
//if ioctl failed? what should i do... //if ioctl failed? what should i do...
ioctl(STDIN_FILENO,TIOCGWINSZ,(char *)&wnd_size); ioctl(STDIN_FILENO,TIOCGWINSZ,(char *)&wnd_size);
if (wnd_size.ws_row < pos) return; if (wnd_size.ws_row < pos){
unlock_scrolled();
return;
}
fprintf(stdout,"\x1b[%dA[%s]: %.2f%% %s bytes: %ld/%ld \x1b[%dB\r",pos,buf,cur_progress,filename,total,offset,pos); fprintf(stdout,"\x1b[%dA[%s]: %.2f%% %s bytes: %ld/%ld \x1b[%dB\r",pos,buf,cur_progress,filename,total,offset,pos);
fflush(stdout); fflush(stdout);
unlock_scrolled(); unlock_scrolled();
@ -116,3 +141,9 @@ void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const ch
bar->last_update = ts; bar->last_update = ts;
RawDisplayProgressBar(bar->bar,offset,total,filename); RawDisplayProgressBar(bar->bar,offset,total,filename);
} }
void quit_progress_bar(){
if(tty_fd != STDIN_FILENO){
close(tty_fd);
}
}

View File

@ -13,6 +13,8 @@ void unlock_scrolled();
void ready_progress_bar(); void ready_progress_bar();
void quit_progress_bar();
void add_scrolled_unlocked(unsigned int i); void add_scrolled_unlocked(unsigned int i);
raw_progress_bar_t create_raw_progress_bar(); raw_progress_bar_t create_raw_progress_bar();

1000
testdata/dlist.txt vendored Normal file

File diff suppressed because it is too large Load Diff