Skip to content

Commit

Permalink
BANG_close() now properly exits because I found the pthread_cancel()
Browse files Browse the repository at this point in the history
function.
  • Loading branch information
Nikhil Samith Bysani committed Dec 22, 2008
1 parent d39213b commit 2f6ce10
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -5,7 +5,7 @@
EXENAME=bang-machine

CC=gcc
COPTS=-Wall -Werror -g -D_REENTRANT -lpthread
COPTS=-Wall -Werror -g -D_REENTRANT -lpthread
GTKOPTS=`pkg-config --cflags --libs gtk+-2.0`

OBJS=bang-com.o bang-net.o bang-signals.o bang-module.o core.o main.o
Expand Down
5 changes: 3 additions & 2 deletions src/app/main.c
Expand Up @@ -61,16 +61,17 @@ static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)

/* Change TRUE to FALSE and the main window will be destroyed with
* a "delete_event". */
return TRUE;
return FALSE;
}

static void destroy(GtkWidget *widget, gpointer data){
///TODO: make bang_close actually close rather than wait forever.
BANG_close();
gtk_main_quit();
}

int main(int argc, char **argv) {
///Note: gtk expects that as a process, you do not need to free its memory
///So, it lets the operating system free all memory when the process closes.
gtk_init(&argc,&argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
Expand Down
23 changes: 19 additions & 4 deletions src/base/bang-net.c
Expand Up @@ -17,12 +17,18 @@
#include<sys/socket.h>
#include<unistd.h>

///TODO:currently only one server can be run a time, good or bad?
///The server thread.
pthread_t *server_thread;
char *port = DEFAULT_PORT;
int sock = -1;

void free_server_addrinfo(void *result) {
freeaddrinfo((struct addrinfo*)result);
}

void* BANG_server_thread(void *port) {
int sock; ///The main server socket
//int sock; ///The main server socket
struct addrinfo hints;
struct addrinfo *result, *rp;
BANG_sigargs args;
Expand All @@ -46,6 +52,12 @@ void* BANG_server_thread(void *port) {
return NULL;
}

///Let me rant about cleanup push. First of all, it is a macro. WTF!?!?
///Second of all it must have a matching pair pthread_cleanup_pop
///Third of all, it means that anything between those two lines is
///in a big do{}while(0). Seriously. god damn.
pthread_cleanup_push(free_server_addrinfo,result);

for (rp = result; rp != NULL; rp = rp->ai_next) {
sock = socket(rp->ai_family,rp->ai_socktype,rp->ai_protocol);

Expand Down Expand Up @@ -87,19 +99,20 @@ void* BANG_server_thread(void *port) {

//accepted client
args.length = sizeof(int);
int accptsock;
while (1) {
accptsock = accept(sock,rp->ai_addr,&rp->ai_addrlen);
args.args = calloc(1,sizeof(int));
*((int*)args.args) = accept(sock,rp->ai_addr,&rp->ai_addrlen);
*((int*)args.args) = accptsock;
BANG_send_signal(BANG_PEER_CONNECTED,args);
free(args.args);
}

freeaddrinfo(result);
pthread_cleanup_pop(1);
close(sock);
return NULL;
}


void* BANG_connect_thread(void *addr) {
struct addrinfo hints;
struct addrinfo *result, *rp;
Expand Down Expand Up @@ -164,6 +177,8 @@ void BANG_net_init(char *server_port,char start_server) {
}

void BANG_net_close() {
fprintf(stderr,"BANG net library closing.\n");
pthread_cancel(*server_thread);
pthread_join(*server_thread,NULL);
free(server_thread);
}
1 change: 1 addition & 0 deletions src/base/bang-signals.c
Expand Up @@ -39,6 +39,7 @@ void BANG_sig_init() {
}

void recursive_sig_free(signal_node *head) {
if (head == NULL) return;
if (head->next != NULL) {
recursive_sig_free(head->next);
}
Expand Down
1 change: 1 addition & 0 deletions src/base/core.c
Expand Up @@ -35,6 +35,7 @@ void BANG_init(int *argc, char **argv) {
}

void BANG_close() {
fprintf(stderr,"BANG library closing.\n");
BANG_com_close();
BANG_net_close();
BANG_sig_close();
Expand Down

0 comments on commit 2f6ce10

Please sign in to comment.