Skip to content

Commit

Permalink
fixed switch and self edge
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Stern committed Mar 15, 2021
1 parent 243340f commit 086bb95
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CXX=g++
MCS=$(MCS)

LDFLAGS= -lm -lpthread -L. -ltopologic -pthread #-lfl
CFLAGS=-Wall -Werror -g -fPIC #-O2
CFLAGS=-Wall -Werror -g -fPIC -O2 #-fsanitize=thread
OBJ=$(SRC:.c=.o)
AR=ar

Expand Down
3 changes: 2 additions & 1 deletion include/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#else
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#endif


#endif
#endif
1 change: 1 addition & 0 deletions include/topologic.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "./request.h"
#include "./graph.h"

#define PTHREAD_SLEEP_TIME 50 //milliseconds

#ifndef SWIGPYTHON
#ifndef _GNU_SOURCE
Expand Down
12 changes: 4 additions & 8 deletions src/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,13 @@ int destroy_graph(struct graph *graph)
return -1;
}
graph->state = TERMINATE;

if (graph->red_vertex_count >= 0)
{
if (graph->context != SINGLE)
{
graph->red_locked = 0;
while (graph->red_vertex_count > 0)
{
}
if (graph->red_vertex_count > 0)
pthread_cond_wait(&graph->red_fire, &graph->color_lock);
}
}

Expand All @@ -225,9 +223,8 @@ int destroy_graph(struct graph *graph)
if (graph->context != SINGLE)
{
graph->black_locked = 0;
while (graph->black_vertex_count > 0)
{
}
if (graph->black_vertex_count > 0)
pthread_cond_wait(&graph->black_fire, &graph->color_lock);
}
}

Expand All @@ -241,7 +238,6 @@ int destroy_graph(struct graph *graph)
graph->remove_edges = NULL;
destroy_graph_stack(graph->remove_vertices);
graph->remove_vertices = NULL;

pthread_mutex_destroy(&graph->lock);
pthread_cond_destroy(&graph->pause_cond);
graph->red_locked = 0;
Expand Down
92 changes: 53 additions & 39 deletions src/topologic.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@

#include "../include/topologic.h"

void sleep_ms(int milliseconds)
{
#ifdef WIN32
Sleep(milliseconds);
#else
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#endif
}

int start_set(struct graph *graph, int id[], int num_vertices)
{
topologic_debug("%s;id: %p;num_vertices: %d", "start_set", id, num_vertices);
Expand Down Expand Up @@ -409,45 +421,7 @@ int fire(struct graph *graph, struct vertex *vertex, struct vertex_result *args,
}
return -1;
}

if (graph->max_loop != -1 && iloop >= graph->max_loop)
{
topologic_debug("%s;%s;%d", "fire", "max loop hit", 0);
if (args->edge_argv)
{
free(args->edge_argv);
args->edge_argv = NULL;
}
if (args->vertex_argv)
{
free(args->vertex_argv);
args->vertex_argv = NULL;
}
if (args)
{
free(args);
args = NULL;
}
pthread_mutex_lock(&graph->lock);
if (color == RED)
{
--(graph->red_vertex_count);
--(graph->num_vertices);
if (graph->red_vertex_count <= 0)
pthread_cond_signal(&graph->red_fire);
}
else if (color == BLACK)
{
--(graph->black_vertex_count);
--(graph->num_vertices);
if (graph->black_vertex_count <= 0)
pthread_cond_signal(&graph->black_fire);
}
pthread_mutex_unlock(&graph->lock);
pthread_mutex_unlock(&vertex->lock);
return 0;
}


if (graph->state == TERMINATE)
{
topologic_debug("%s;%s;%d", "fire", "terminate", -1);
Expand Down Expand Up @@ -488,6 +462,44 @@ int fire(struct graph *graph, struct vertex *vertex, struct vertex_result *args,
return -1;
}

if (graph->max_loop != -1 && iloop >= graph->max_loop)
{
topologic_debug("%s;%s;%d", "fire", "max loop hit", 0);
if (args->edge_argv)
{
free(args->edge_argv);
args->edge_argv = NULL;
}
if (args->vertex_argv)
{
free(args->vertex_argv);
args->vertex_argv = NULL;
}
if (args)
{
free(args);
args = NULL;
}
pthread_mutex_lock(&graph->lock);
if (color == RED)
{
--(graph->red_vertex_count);
--(graph->num_vertices);
if (graph->red_vertex_count <= 0)
pthread_cond_signal(&graph->red_fire);
}
else if (color == BLACK)
{
--(graph->black_vertex_count);
--(graph->num_vertices);
if (graph->black_vertex_count <= 0)
pthread_cond_signal(&graph->black_fire);
}
pthread_mutex_unlock(&graph->lock);
pthread_mutex_unlock(&vertex->lock);
return 0;
}

(vertex->f)(graph, args, vertex->glbl, vertex->shared->vertex_data);

struct vertex *next_vertex = NULL;
Expand Down Expand Up @@ -604,6 +616,7 @@ int fire(struct graph *graph, struct vertex *vertex, struct vertex_result *args,
++(graph->black_vertex_count);
pthread_mutex_unlock(&graph->lock);
topologic_debug("%s;%s;%p", "fire", "firing next vertex", next_vertex);
sleep_ms(PTHREAD_SLEEP_TIME);
return fire(graph, next_vertex, args, flip_color, iloop_b);
}
else
Expand Down Expand Up @@ -645,6 +658,7 @@ void *fire_pthread(void *vargp)
int iloop = fireable->iloop;

free(vargp);
sleep_ms(PTHREAD_SLEEP_TIME);
int ret_val = fire(graph, v, args, color, iloop);
topologic_debug("%s;%s;%d", "fire_pthread", "finished", ret_val);
pthread_exit((void *)(intptr_t)ret_val);
Expand Down
2 changes: 1 addition & 1 deletion testing/generate_graph_json_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ int main() {
assert(graph == NULL);
fprintf(stderr, "JSON PARSING WORKS\n");
return 0;
}
}
4 changes: 2 additions & 2 deletions testing/switch_context_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main()
void cleanup(struct graph *graph)
{
assert(graph != NULL);
int i = 0;
/*int i = 0;
for (i = 0; i < MAXIMUM; i++)
{
Expand Down Expand Up @@ -81,7 +81,7 @@ void cleanup(struct graph *graph)
v->glbl = NULL;
}
remove_vertex(graph, v);
}
}*/
destroy_graph(graph);
graph = NULL;
}
Expand Down
Binary file removed testing/vgcore.20924
Binary file not shown.

0 comments on commit 086bb95

Please sign in to comment.