Commit 02002927 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

started cleaning up mutexes

parent 4e5f3cc3
......@@ -59,6 +59,7 @@ int client_write_n(struct client* client, size_t n);
void* serve_client(void *ctxt);
void server_quit(struct global_state* s);
void signal_to_bg_task();
int is_running(struct global_state* state);
/////////////// camera stuff
......@@ -67,9 +68,8 @@ void signal_to_bg_task();
struct client;
//TODO: move these into the global_state struct?
pthread_mutex_t global_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t global_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t global_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t global_cond = PTHREAD_COND_INITIALIZER;
#ifdef USE_CAMERA
......@@ -295,7 +295,7 @@ void* ui_task(void *ctxt)
{
struct global_state* s = ctxt;
while(s->running){
while(is_running(s)){
char c = getchar();
switch (c){
case 'q':
......@@ -320,14 +320,14 @@ void* bg_task(void *ctxt)
{
struct global_state* s = ctxt;
pthread_mutex_lock(&global_mutex);
while(s->running){
pthread_mutex_lock(&global_mutex);
pthread_cond_wait(&global_cond, &global_mutex);
#ifdef INFO
printf("bg_task: global_cond was signalled\n");
#endif
pthread_mutex_unlock(&global_mutex);
}
pthread_mutex_unlock(&global_mutex);
return 0;
}
......@@ -381,6 +381,7 @@ int try_accept(struct global_state* state, struct client* client)
static int create_threads(struct global_state* state)
{
pthread_mutex_lock(&global_mutex);
int result = 0;
if (pthread_create(&state->bg_thread, 0, bg_task, state)) {
printf("Error pthread_create()\n");
......@@ -407,6 +408,7 @@ static int create_threads(struct global_state* state)
failed_to_start_ui_thread:
failed_to_start_main_thread:
failed_to_start_bg_thread:
pthread_mutex_unlock(&global_mutex);
return result;
}
static void join_bg_thread(pthread_t* bg_thread, const char* msg)
......@@ -429,7 +431,7 @@ int serve_clients(struct global_state* state)
int result=0;
state->pfd.fd = state->listenfd;
state->pfd.events=POLLIN;
while(state->running) {
while(is_running(state)) {
int ret; // result of poll
struct client* client = malloc(sizeof(*client));
......@@ -448,13 +450,13 @@ int serve_clients(struct global_state* state)
#ifdef DEBUG_VERBOSE
printf("client poll returns %d\n", ret);
#endif
} while(ret==0 && state->running);
} while(ret==0 && is_running(state));
if(ret <0) {
perror("poll");
result = errno;
goto failed_poll;
} else if (state->running) {
} else if (is_running(state)) {
if(try_accept(state, client)) {
perror("try_accept");
result = errno;
......@@ -478,6 +480,14 @@ pthread_mutex_lock(&global_mutex);
pthread_mutex_unlock(&global_mutex);
}
int is_running(struct global_state* state)
{
int result=0;
pthread_mutex_lock(&global_mutex);
result = state->running;
pthread_mutex_unlock(&global_mutex);
return result;
}
int create_socket(struct global_state* state)
{
int reuse;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment