|
3 | 3 |
|
4 | 4 | #include "../include/topologic.h" |
5 | 5 |
|
| 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 | + |
6 | 18 | int start_set(struct graph *graph, int id[], int num_vertices) |
7 | 19 | { |
8 | 20 | 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, |
409 | 421 | } |
410 | 422 | return -1; |
411 | 423 | } |
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 | + |
451 | 425 | if (graph->state == TERMINATE) |
452 | 426 | { |
453 | 427 | topologic_debug("%s;%s;%d", "fire", "terminate", -1); |
@@ -488,6 +462,44 @@ int fire(struct graph *graph, struct vertex *vertex, struct vertex_result *args, |
488 | 462 | return -1; |
489 | 463 | } |
490 | 464 |
|
| 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 | + |
491 | 503 | (vertex->f)(graph, args, vertex->glbl, vertex->shared->vertex_data); |
492 | 504 |
|
493 | 505 | struct vertex *next_vertex = NULL; |
@@ -604,6 +616,7 @@ int fire(struct graph *graph, struct vertex *vertex, struct vertex_result *args, |
604 | 616 | ++(graph->black_vertex_count); |
605 | 617 | pthread_mutex_unlock(&graph->lock); |
606 | 618 | topologic_debug("%s;%s;%p", "fire", "firing next vertex", next_vertex); |
| 619 | + sleep_ms(PTHREAD_SLEEP_TIME); |
607 | 620 | return fire(graph, next_vertex, args, flip_color, iloop_b); |
608 | 621 | } |
609 | 622 | else |
@@ -645,6 +658,7 @@ void *fire_pthread(void *vargp) |
645 | 658 | int iloop = fireable->iloop; |
646 | 659 |
|
647 | 660 | free(vargp); |
| 661 | + sleep_ms(PTHREAD_SLEEP_TIME); |
648 | 662 | int ret_val = fire(graph, v, args, color, iloop); |
649 | 663 | topologic_debug("%s;%s;%d", "fire_pthread", "finished", ret_val); |
650 | 664 | pthread_exit((void *)(intptr_t)ret_val); |
|
0 commit comments