Skip to content

Commit 086bb95

Browse files
author
Matthew Stern
committed
fixed switch and self edge
1 parent 243340f commit 086bb95

File tree

8 files changed

+64
-52
lines changed

8 files changed

+64
-52
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CXX=g++
66
MCS=$(MCS)
77

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

include/header.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#else
2020
#include <pthread.h>
2121
#include <unistd.h>
22+
#include <time.h>
2223
#endif
2324

2425

25-
#endif
26+
#endif

include/topologic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "./request.h"
1313
#include "./graph.h"
1414

15+
#define PTHREAD_SLEEP_TIME 50 //milliseconds
1516

1617
#ifndef SWIGPYTHON
1718
#ifndef _GNU_SOURCE

src/graph.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,13 @@ int destroy_graph(struct graph *graph)
208208
return -1;
209209
}
210210
graph->state = TERMINATE;
211-
212211
if (graph->red_vertex_count >= 0)
213212
{
214213
if (graph->context != SINGLE)
215214
{
216215
graph->red_locked = 0;
217-
while (graph->red_vertex_count > 0)
218-
{
219-
}
216+
if (graph->red_vertex_count > 0)
217+
pthread_cond_wait(&graph->red_fire, &graph->color_lock);
220218
}
221219
}
222220

@@ -225,9 +223,8 @@ int destroy_graph(struct graph *graph)
225223
if (graph->context != SINGLE)
226224
{
227225
graph->black_locked = 0;
228-
while (graph->black_vertex_count > 0)
229-
{
230-
}
226+
if (graph->black_vertex_count > 0)
227+
pthread_cond_wait(&graph->black_fire, &graph->color_lock);
231228
}
232229
}
233230

@@ -241,7 +238,6 @@ int destroy_graph(struct graph *graph)
241238
graph->remove_edges = NULL;
242239
destroy_graph_stack(graph->remove_vertices);
243240
graph->remove_vertices = NULL;
244-
245241
pthread_mutex_destroy(&graph->lock);
246242
pthread_cond_destroy(&graph->pause_cond);
247243
graph->red_locked = 0;

src/topologic.c

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33

44
#include "../include/topologic.h"
55

6+
void sleep_ms(int milliseconds)
7+
{
8+
#ifdef WIN32
9+
Sleep(milliseconds);
10+
#else
11+
struct timespec ts;
12+
ts.tv_sec = milliseconds / 1000;
13+
ts.tv_nsec = (milliseconds % 1000) * 1000000;
14+
nanosleep(&ts, NULL);
15+
#endif
16+
}
17+
618
int start_set(struct graph *graph, int id[], int num_vertices)
719
{
820
topologic_debug("%s;id: %p;num_vertices: %d", "start_set", id, num_vertices);
@@ -409,45 +421,7 @@ int fire(struct graph *graph, struct vertex *vertex, struct vertex_result *args,
409421
}
410422
return -1;
411423
}
412-
413-
if (graph->max_loop != -1 && iloop >= graph->max_loop)
414-
{
415-
topologic_debug("%s;%s;%d", "fire", "max loop hit", 0);
416-
if (args->edge_argv)
417-
{
418-
free(args->edge_argv);
419-
args->edge_argv = NULL;
420-
}
421-
if (args->vertex_argv)
422-
{
423-
free(args->vertex_argv);
424-
args->vertex_argv = NULL;
425-
}
426-
if (args)
427-
{
428-
free(args);
429-
args = NULL;
430-
}
431-
pthread_mutex_lock(&graph->lock);
432-
if (color == RED)
433-
{
434-
--(graph->red_vertex_count);
435-
--(graph->num_vertices);
436-
if (graph->red_vertex_count <= 0)
437-
pthread_cond_signal(&graph->red_fire);
438-
}
439-
else if (color == BLACK)
440-
{
441-
--(graph->black_vertex_count);
442-
--(graph->num_vertices);
443-
if (graph->black_vertex_count <= 0)
444-
pthread_cond_signal(&graph->black_fire);
445-
}
446-
pthread_mutex_unlock(&graph->lock);
447-
pthread_mutex_unlock(&vertex->lock);
448-
return 0;
449-
}
450-
424+
451425
if (graph->state == TERMINATE)
452426
{
453427
topologic_debug("%s;%s;%d", "fire", "terminate", -1);
@@ -488,6 +462,44 @@ int fire(struct graph *graph, struct vertex *vertex, struct vertex_result *args,
488462
return -1;
489463
}
490464

465+
if (graph->max_loop != -1 && iloop >= graph->max_loop)
466+
{
467+
topologic_debug("%s;%s;%d", "fire", "max loop hit", 0);
468+
if (args->edge_argv)
469+
{
470+
free(args->edge_argv);
471+
args->edge_argv = NULL;
472+
}
473+
if (args->vertex_argv)
474+
{
475+
free(args->vertex_argv);
476+
args->vertex_argv = NULL;
477+
}
478+
if (args)
479+
{
480+
free(args);
481+
args = NULL;
482+
}
483+
pthread_mutex_lock(&graph->lock);
484+
if (color == RED)
485+
{
486+
--(graph->red_vertex_count);
487+
--(graph->num_vertices);
488+
if (graph->red_vertex_count <= 0)
489+
pthread_cond_signal(&graph->red_fire);
490+
}
491+
else if (color == BLACK)
492+
{
493+
--(graph->black_vertex_count);
494+
--(graph->num_vertices);
495+
if (graph->black_vertex_count <= 0)
496+
pthread_cond_signal(&graph->black_fire);
497+
}
498+
pthread_mutex_unlock(&graph->lock);
499+
pthread_mutex_unlock(&vertex->lock);
500+
return 0;
501+
}
502+
491503
(vertex->f)(graph, args, vertex->glbl, vertex->shared->vertex_data);
492504

493505
struct vertex *next_vertex = NULL;
@@ -604,6 +616,7 @@ int fire(struct graph *graph, struct vertex *vertex, struct vertex_result *args,
604616
++(graph->black_vertex_count);
605617
pthread_mutex_unlock(&graph->lock);
606618
topologic_debug("%s;%s;%p", "fire", "firing next vertex", next_vertex);
619+
sleep_ms(PTHREAD_SLEEP_TIME);
607620
return fire(graph, next_vertex, args, flip_color, iloop_b);
608621
}
609622
else
@@ -645,6 +658,7 @@ void *fire_pthread(void *vargp)
645658
int iloop = fireable->iloop;
646659

647660
free(vargp);
661+
sleep_ms(PTHREAD_SLEEP_TIME);
648662
int ret_val = fire(graph, v, args, color, iloop);
649663
topologic_debug("%s;%s;%d", "fire_pthread", "finished", ret_val);
650664
pthread_exit((void *)(intptr_t)ret_val);

testing/generate_graph_json_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ int main() {
2121
assert(graph == NULL);
2222
fprintf(stderr, "JSON PARSING WORKS\n");
2323
return 0;
24-
}
24+
}

testing/switch_context_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int main()
5353
void cleanup(struct graph *graph)
5454
{
5555
assert(graph != NULL);
56-
int i = 0;
56+
/*int i = 0;
5757
5858
for (i = 0; i < MAXIMUM; i++)
5959
{
@@ -81,7 +81,7 @@ void cleanup(struct graph *graph)
8181
v->glbl = NULL;
8282
}
8383
remove_vertex(graph, v);
84-
}
84+
}*/
8585
destroy_graph(graph);
8686
graph = NULL;
8787
}

testing/vgcore.20924

-7.58 MB
Binary file not shown.

0 commit comments

Comments
 (0)