From 1177e036608e44191abcb9d9182beced85acf915 Mon Sep 17 00:00:00 2001 From: ha7ilm Date: Thu, 12 Jan 2017 16:56:54 +0100 Subject: [PATCH] nmux: things start to work --- nmux.cpp | 16 +++++++++------- tsmpool.cpp | 9 ++++++++- tsmpool.h | 1 + 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/nmux.cpp b/nmux.cpp index e2e200e..77fd52d 100644 --- a/nmux.cpp +++ b/nmux.cpp @@ -174,15 +174,15 @@ int main(int argc, char* argv[]) for(;;) { - if(NMUX_DEBUG) fprintf(stderr, "mainfor: selecting..."); + // if(NMUX_DEBUG) fprintf(stderr, "mainfor: selecting..."); //Let's wait until there is any new data to read, or any new connection! select(highfd, &select_fds, NULL, NULL, NULL); - if(NMUX_DEBUG) fprintf(stderr, "selected.\n"); + // if(NMUX_DEBUG) fprintf(stderr, "selected.\n"); //Is there a new client connection? if( (new_socket = accept(listen_socket, (struct sockaddr*)&addr_cli, &addr_cli_len)) != -1) { - if(NMUX_DEBUG) fprintf(stderr, "mainfor: accepted (%d).\n", new_socket); + if(NMUX_DEBUG) fprintf(stderr, "mainfor: accepted (socket = %d).\n", new_socket); //Close all finished clients for(int i=0;iwait_mutex, NULL); new_client->wait_condition = wait_condition; new_client->sleeping = 0; - if(pthread_create(&new_client->thread, NULL, client_thread, (void*)&new_client)==0) + if(pthread_create(&new_client->thread, NULL, client_thread, (void*)new_client)==0) { clients.push_back(new_client); fprintf(stderr, MSG_START "pthread_create() done, clients now: %d\n", clients.size()); @@ -230,7 +230,7 @@ int main(int argc, char* argv[]) index_in_current_write_buffer = 0; } int retval = read(input_fd, current_write_buffer + index_in_current_write_buffer, bufsize - index_in_current_write_buffer); - if(NMUX_DEBUG) fprintf(stderr, "mainfor: read %d\n", retval); + // if(NMUX_DEBUG) fprintf(stderr, "mainfor: read %d\n", retval); if(retval>0) { index_in_current_write_buffer += retval; @@ -242,7 +242,8 @@ int main(int argc, char* argv[]) } else if(retval==-1) { - error_exit(MSG_START "(main thread/for) error in read(), exiting.\n"); + if(errno == EAGAIN) { if(NMUX_DEBUG) fprintf(stderr, "mainfor: read %d\n", retval); } + else error_exit(MSG_START "(main thread/for) error in read(), exiting.\n"); } } } @@ -286,7 +287,8 @@ void* client_thread (void* param) while(!pool_read_buffer || client_buffer_index >= lpool->size) { if(NMUX_DEBUG) fprintf(stderr, "client 0x%x: cond_waiting for more data\n", (unsigned)param); - char* pool_read_buffer = (char*)lpool->get_read_buffer(this_client->tsmthread); + pool_read_buffer = (char*)lpool->get_read_buffer(this_client->tsmthread); + if(pool_read_buffer) { client_buffer_index = 0; break; } pthread_mutex_lock(&this_client->wait_mutex); this_client->sleeping = 1; pthread_cond_wait(this_client->wait_condition, &this_client->wait_mutex); diff --git a/tsmpool.cpp b/tsmpool.cpp index 92d94cb..7334865 100644 --- a/tsmpool.cpp +++ b/tsmpool.cpp @@ -27,6 +27,7 @@ void* tsmpool::get_write_buffer() void* to_return = buffers[write_index]; write_index = index_next(write_index); pthread_mutex_unlock(&this->mutex); + fprintf(stderr, "gwb: write_index = %d\n", write_index); return to_return; } @@ -58,9 +59,15 @@ void* tsmpool::get_read_buffer(tsmthread_t* thread) { pthread_mutex_lock(&this->mutex); int* actual_read_index = (thread==NULL) ? &my_read_index : &thread->read_index; - if(*actual_read_index==index_before(write_index)) return NULL; + if(*actual_read_index==index_before(write_index)) + { + fprintf(stderr, "grb: fail," + "read_index %d is just before write_index\n", *actual_read_index); + return NULL; + } void* to_return = buffers[*actual_read_index]; *actual_read_index=index_next(*actual_read_index); pthread_mutex_unlock(&this->mutex); + fprintf(stderr, "grb: read_index = %d\n", *actual_read_index); return to_return; } diff --git a/tsmpool.h b/tsmpool.h index 26f3197..b825d8a 100644 --- a/tsmpool.h +++ b/tsmpool.h @@ -3,6 +3,7 @@ //It implements a big circular buffer that one thread writes into, and multiple threads read from. //The reader threads have lower priority than the writer thread (they can be left behind if the don't read fast enough). +#include #include #include