From c4e3a3e34e94ed337b089eb6339bf9b6fd5b9657 Mon Sep 17 00:00:00 2001 From: alegrand Date: Wed, 22 Dec 2004 20:41:34 +0000 Subject: [PATCH] Modifying the API so as to prevent a use of the context that would make valgrind unhappy. See, I really care about the happiness of valgrind... git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@695 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- include/xbt/context.h | 4 +- src/xbt/context.c | 87 ++++++++++++++++++------------- testsuite/Makefile.am | 12 +++-- testsuite/xbt/context_usage.c | 97 +++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 42 deletions(-) create mode 100644 testsuite/xbt/context_usage.c diff --git a/include/xbt/context.h b/include/xbt/context.h index c09a08a44d..656caaecc3 100644 --- a/include/xbt/context.h +++ b/include/xbt/context.h @@ -16,7 +16,7 @@ void xbt_context_exit(void); void xbt_context_empty_trash(void); xbt_context_t xbt_context_new(xbt_context_function_t code, int argc, char *argv[]); void xbt_context_start(xbt_context_t context); -void xbt_context_yield(xbt_context_t context); -int xbt_context_get_id(xbt_context_t context); +void xbt_context_yield(void); +void xbt_context_schedule(xbt_context_t context); #endif /* _XBT_CONTEXT_H */ diff --git a/src/xbt/context.c b/src/xbt/context.c index cc79209a32..e84ffa25fc 100644 --- a/src/xbt/context.c +++ b/src/xbt/context.c @@ -32,6 +32,46 @@ static xbt_context_t init_context = NULL; static xbt_swag_t context_to_destroy = NULL; static xbt_swag_t context_living = NULL; +static void __xbt_context_yield(xbt_context_t context) +{ + int return_value = 0; + + xbt_assert0(current_context,"You have to call context_init() first."); + +/* WARNING("--------- current_context (%p) is yielding to context(%p) ---------",current_context,context); */ +/* VOIRP(current_context); */ +/* if(current_context) VOIRP(current_context->save); */ +/* VOIRP(context); */ +/* if(context) VOIRP(context->save); */ + + if (context) { + if(context->save==NULL) { +/* WARNING("**** Yielding to somebody else ****"); */ +/* WARNING("Saving current_context value (%p) to context(%p)->save",current_context,context); */ + context->save = current_context ; +/* WARNING("current_context becomes context(%p) ",context); */ + current_context = context ; +/* WARNING("Current position memorized (context->save). Jumping to context (%p)",context); */ + return_value = swapcontext (&(context->save->uc), &(context->uc)); + xbt_assert0((return_value==0),"Context swapping failure"); +/* WARNING("I am (%p). Coming back\n",context); */ + } else { + xbt_context_t old_context = context->save ; +/* WARNING("**** Back ! ****"); */ +/* WARNING("Setting current_context (%p) to context(%p)->save",current_context,context); */ + current_context = context->save ; +/* WARNING("Setting context(%p)->save to NULL",context); */ + context->save = NULL ; +/* WARNING("Current position memorized (%p). Jumping to context (%p)",context,old_context); */ + return_value = swapcontext (&(context->uc), &(old_context->uc)); + xbt_assert0((return_value==0),"Context swapping failure"); +/* WARNING("I am (%p). Coming back\n",context); */ + } + } + + return; +} + static void xbt_context_destroy(xbt_context_t context) { xbt_free(context); @@ -75,7 +115,7 @@ static void *__context_wrapper(void *c) xbt_swag_remove(context, context_living); xbt_swag_insert(context, context_to_destroy); - xbt_context_yield(context); + __xbt_context_yield(context); return NULL; } @@ -113,44 +153,17 @@ xbt_context_t xbt_context_new(xbt_context_function_t code, return res; } -void xbt_context_yield(xbt_context_t context) -{ - int return_value = 0; - - xbt_assert0(current_context,"You have to call context_init() first."); - -/* WARNING("--------- current_context (%p) is yielding to context(%p) ---------",current_context,context); */ -/* VOIRP(current_context); */ -/* if(current_context) VOIRP(current_context->save); */ -/* VOIRP(context); */ -/* if(context) VOIRP(context->save); */ - if (context) { - if(context->save==NULL) { -/* WARNING("**** Yielding to somebody else ****"); */ -/* WARNING("Saving current_context value (%p) to context(%p)->save",current_context,context); */ - context->save = current_context ; -/* WARNING("current_context becomes context(%p) ",context); */ - current_context = context ; -/* WARNING("Current position memorized (context->save). Jumping to context (%p)",context); */ - return_value = swapcontext (&(context->save->uc), &(context->uc)); - xbt_assert0((return_value==0),"Context swapping failure"); -/* WARNING("I am (%p). Coming back\n",context); */ - } else { - xbt_context_t old_context = context->save ; -/* WARNING("**** Back ! ****"); */ -/* WARNING("Setting current_context (%p) to context(%p)->save",current_context,context); */ - current_context = context->save ; -/* WARNING("Setting context(%p)->save to NULL",context); */ - context->save = NULL ; -/* WARNING("Current position memorized (%p). Jumping to context (%p)",context,old_context); */ - return_value = swapcontext (&(context->uc), &(old_context->uc)); - xbt_assert0((return_value==0),"Context swapping failure"); -/* WARNING("I am (%p). Coming back\n",context); */ - } - } +void xbt_context_yield(void) +{ + __xbt_context_yield(current_context); +} - return; +void xbt_context_schedule(xbt_context_t context) +{ + xbt_assert0((current_context==init_context), + "You are not supposed to run this function here!"); + __xbt_context_yield(context); } void xbt_context_exit(void) { diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am index dc9c2d8765..cf622fb6d0 100644 --- a/testsuite/Makefile.am +++ b/testsuite/Makefile.am @@ -2,13 +2,14 @@ INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/src AM_CFLAGS=-g CLEANFILES = *~ test a.out *.o gras/datadesc_usage.out datadesc_usage.out -DISTCLEANFILES = gras/.libs/* xbt/.libs/* surf/.libs/* +DISTCLEANFILES = gras/.libs/* xbt/.libs/* surf/.libs/* msg/.libs/* MAINTAINERCLEANFILES=Makefile.in EXTRA_DIST=run_tests.in \ gras/datadesc.little32 gras/datadesc.little64 gras/datadesc.big32 gras/datadesc.big64 \ gras/datadesc.aix gras/datadesc.win32 gras/datadesc.g5 \ gras/mk_datadesc_structs.pl \ - surf/trace_A.txt surf/trace_B.txt surf/trace_A_failure.txt + surf/trace_A.txt surf/trace_B.txt surf/trace_A_failure.txt surf/platform.txt \ + msg/platform.txt # Test stuff @@ -20,6 +21,7 @@ xbt_tests = \ xbt/log_usage \ xbt/heap_bench \ xbt/swag_usage \ + xbt/context_usage \ xbt/config_usage # xbt/multidict_crash @@ -30,7 +32,8 @@ RL_tests = \ SG_tests = \ surf/maxmin_usage surf/maxmin_bench \ - surf/trace_usage surf/surf_usage surf/surf_usage2 + surf/trace_usage surf/surf_usage surf/surf_usage2 \ + msg/msg_test check_PROGRAMS = $(xbt_tests) $(RL_tests) $(SG_tests) check_SCRIPTS = run_tests gras/trp_tcp_usage @@ -61,6 +64,7 @@ xbt_swag_usage_LDADD= $(LDADD_UTILS) xbt_config_usage_LDADD= $(LDADD_UTILS) xbt_heap_bench_LDADD= $(LDADD_UTILS) +xbt_context_usage_LDADD= $(LDADD_UTILS) surf_maxmin_usage_LDADD= $(LDADD_UTILS) surf_maxmin_bench_LDADD= $(LDADD_UTILS) @@ -68,6 +72,8 @@ surf_trace_usage_LDADD= $(LDADD_UTILS) surf_surf_usage_LDADD= $(LDADD_UTILS) surf_surf_usage2_LDADD= $(LDADD_UTILS) +msg_msg_test_LDADD= $(LDADD_UTILS) + gras_trp_tcp_client_LDADD= $(LDADD_RL) gras_trp_tcp_server_LDADD= $(LDADD_RL) gras_trp_file_client_LDADD= $(LDADD_RL) diff --git a/testsuite/xbt/context_usage.c b/testsuite/xbt/context_usage.c new file mode 100644 index 0000000000..9431617754 --- /dev/null +++ b/testsuite/xbt/context_usage.c @@ -0,0 +1,97 @@ +#include "xbt/context.h" +#include "xbt/fifo.h" +#include "stdlib.h" +#include "stdio.h" + +xbt_context_t cA = NULL; +xbt_context_t cB = NULL; +xbt_context_t cC = NULL; +xbt_fifo_t fifo = NULL; + +void print_args(int argc, char** argv); +void print_args(int argc, char** argv) +{ + int i ; + + printf("<"); + for(i=0; i\n"); +} + +int fA(int argc, char** argv); +int fA(int argc, char** argv) +{ + printf("fA: "); + print_args(argc,argv); + + printf("\tA: Yield\n"); + xbt_context_yield(); + printf("\tA: Yield\n"); + xbt_context_yield(); + printf("\tA : bye\n"); + + return 0; +} + +int fB(int argc, char** argv); +int fB(int argc, char** argv) +{ + printf("fB: "); + print_args(argc,argv); + + printf("\tB: Yield\n"); + xbt_context_yield(); + xbt_fifo_push(fifo,cA); + printf("\tB->A\n"); + printf("\tB: Yield\n"); + xbt_context_yield(); + printf("\tB : bye\n"); + + return 0; +} + +int fC(int argc, char** argv); +int fC(int argc, char** argv) +{ + printf("fC: "); + print_args(argc,argv); + + + printf("\tC: Yield\n"); + xbt_context_yield(); + + + return 0; +} + +int main(int argc, char** argv) +{ + xbt_context_t context = NULL; + + xbt_context_init(); + + cA = xbt_context_new(fA, 0, NULL); + cB = xbt_context_new(fB, 0, NULL); + cC = xbt_context_new(fC, 0, NULL); + + fifo = xbt_fifo_new(); + + xbt_context_start(cA); + printf("\tO->A\n");xbt_fifo_push(fifo,cA); + xbt_context_start(cB); + printf("\tO->B\n");xbt_fifo_push(fifo,cB); + xbt_context_start(cC);xbt_fifo_push(fifo,cC); + printf("\tO->C\n");xbt_fifo_push(fifo,cC); + + while((context=xbt_fifo_shift(fifo))) { + printf("\tO:Yield\n"); + xbt_context_schedule(context); + } + + xbt_fifo_free(fifo); + xbt_context_exit(); + + cA=cB=cC=NULL; + return 0; +} -- 2.20.1