[Pntool-developers] SF.net SVN: pntool:[63] codegen/src
Brought to you by:
compaqdrew,
miordache
From: <ee...@us...> - 2009-06-17 20:56:37
|
Revision: 63 http://pntool.svn.sourceforge.net/pntool/?rev=63&view=rev Author: eegs002 Date: 2009-06-17 20:56:13 +0000 (Wed, 17 Jun 2009) Log Message: ----------- Edited to adhere to new messaging contract Modified Paths: -------------- codegen/src/plantCompiler.c codegen/src/plantCompiler.h Modified: codegen/src/plantCompiler.c =================================================================== --- codegen/src/plantCompiler.c 2009-06-17 03:30:37 UTC (rev 62) +++ codegen/src/plantCompiler.c 2009-06-17 20:56:13 UTC (rev 63) @@ -66,7 +66,7 @@ genFunctionPrototype(plantFile); - createGlobals(plantFile, pnsPlant->pnum); + createGlobals(plantFile); createMain(plantFile); @@ -76,6 +76,8 @@ createSignalVars(plantFile); + unbufferIO(plantFile); + genInitialStart(pnsPlant, plantFile); genPlaceCode(pnsPlant, plantFile); @@ -83,6 +85,7 @@ genTransitionCode(pnsPlant, plantFile); finalizeFile(plantFile); + //This must be called once the written main is complete since it //changes the position indicator to the end of the file genEnabledFunction(pnsPlant, plantFile); @@ -103,8 +106,6 @@ genGotoCurrentMark(plantFile); - genCallMainFunc(plantFile); - fclose(plantFile); }//End compileSinglePlant @@ -201,35 +202,18 @@ //Creates Global Variables -void createGlobals(FILE* plantFile, int pnum) +void createGlobals(FILE* plantFile) { - int i = 0; fprintf(plantFile,"\n#ifndef PIPES\n"); //This FLAG is set to 1 when a transition is allowed to fire fprintf(plantFile,"sig_atomic_t ___FIRE_FLAG;\n"); - fprintf(plantFile,"#else\n"); - - fprintf(plantFile,"const int ___placesArray[] = {"); - for(i = 0; i<pnum; i++) - { - fprintf(plantFile,"%d",i); - if(i != pnum - 1) - fprintf(plantFile,","); - } - fprintf(plantFile,"};\n"); - - fprintf(plantFile,"\n"); - fprintf(plantFile,"pthread_mutex_t ___alertMutex;\n"); - fprintf(plantFile,"pthread_attr_t ___detachState;\n"); - fprintf(plantFile,"#endif\n"); fprintf(plantFile,"\n//The maximum size a pipe message can be\n"); fprintf(plantFile,"//This restricts the maximum number of digits in"\ " a transition label\n"); fprintf(plantFile,"#define MAX_MESSAGE_SIZE 200\n"); - fprintf(plantFile,"\n//Restricts plant to 50 digit long place numbers max\n"); fprintf(plantFile,"#define MAX_PLACE_DIGITS 50\n"); }//End createGlobals @@ -348,8 +332,16 @@ }//End createPNSCode +//Creates the code to unbuffer stdin, stdout +void unbufferIO(FILE* plantFile) +{ + fprintf(plantFile, "\n//Unbuffer IO\n"); + fprintf(plantFile, "setvbuf ( stdout , NULL , _IONBF , 1024 );\n"); + fprintf(plantFile, "setvbuf ( stdin , NULL , _IONBF , 1024 );\n"); +} + //Creates the initial goto statement //based on the initial marking void genInitialStart(pns *pnsPlant, FILE* plantFile) @@ -558,11 +550,13 @@ fprintf(plantFile,"\t#else\n"); fprintf(plantFile,"\t//Alert the supervisor and wait for"\ " permission to fire\n"); - fprintf(plantFile,"\twaitForPermission((___pnsPlant.t[%d]).l);\n",i); + fprintf(plantFile,"\twaitForPermission((___pnsPlant.t[%d]).l,%d);\n",i,i); fprintf(plantFile,"\t#endif\n"); fprintf(plantFile,"\n\t//Update the current marking\n"); fprintf(plantFile,"\tupdatePns(&___pnsPlant,%d,"\ "___currentMarking);\n",i); + + fprintf(plantFile,"\tprintf(\"___F%%d\\n\",pthread_self());\n"); fprintf(plantFile,"\n\tgoto CM;\n"); fprintf(plantFile,"\n\t} //End if(isEnabled)\n"); @@ -771,9 +765,8 @@ //Generates and outputs the code for the following function: - -// void waitForPermission(int ___lblNbr); - +// +// void waitForPermission(int ___lblNbr, int ___transNbr); // // This function is the algorithm that causes the transition // to signal the supervisor and wait for permission from @@ -784,7 +777,7 @@ fprintf(plantFile,"\n//Transition calls this function to wait"\ " for permission to fire from the supervisor\n"); fprintf(plantFile,"//PIPES VERSION\n"); - fprintf(plantFile,"void waitForPermission(int ___lblNbr)\n{\n"); + fprintf(plantFile,"void waitForPermission(int ___lblNbr, int ___transNbr)\n{\n"); fprintf(plantFile,"\n\tchar ___buffer[MAX_MESSAGE_SIZE];\n"); @@ -799,7 +792,8 @@ //Alert the supervisor fprintf(plantFile,"\n\t//Alert the supervisor\n"); - fprintf(plantFile,"\tprintf(\"?%%d\\n\",___lblNbr);\n"); + fprintf(plantFile,"\tprintf(\"___?%%d:%%d-%%d\\n\",pthread_self(),"\ + "___lblNbr,___transNbr);\n"); //Suspend Until Response Received fprintf(plantFile,"\n\t//Wait until a response received\n"); @@ -807,7 +801,7 @@ //If the answer is no, wait and try again fprintf(plantFile,"\n\t//Keep trying until permission is received\n"); - fprintf(plantFile,"\twhile(tolower(___buffer[0]) != 'y')\n\t{\n"); + fprintf(plantFile,"\twhile(tolower(___buffer[3]) != 'y')\n\t{\n"); //DEBUG statements fprintf(plantFile,"\t\t#ifdef DEBUG\n"); @@ -823,7 +817,8 @@ "\\n\",getpid());\n"); fprintf(plantFile,"\t\t#endif\n"); - fprintf(plantFile,"\t\tprintf(\"\\n___?%%d\\n\",___lblNbr);\n"); + fprintf(plantFile,"\t\tprintf(\"___?%%d:%%d-%%d\\n\",pthread_self(),"\ + "___lblNbr,___transNbr);\n"); fprintf(plantFile,"\t\tfgets(___buffer, MAX_MESSAGE_SIZE, stdin);\n"); fprintf(plantFile,"\t}//End while\n"); @@ -892,122 +887,49 @@ // // This function returns an integer that indicates the place number -// to begin at based on the current marking (this is the first token found) -// A new thread created for every other token +// to begin at based on the current marking. void genGotoCurrentMark(FILE *plantFile) { - fprintf(plantFile,"\n//Returns an int that indicates place number to goto"\ - " based on first token in the current marking\n"); - fprintf(plantFile,"//Also spawns threads for all other tokens\n"); - fprintf(plantFile,"int gotoCurrentMark(pns *___pnsPtr,"\ - " int *___currentMarking)\n"); - fprintf(plantFile,"{\n"); - fprintf(plantFile,"\tint ___placeCounter = 0;\n"); - fprintf(plantFile,"\tint ___tokenCounter = 0;\n"); - fprintf(plantFile,"\tint ___threadIndex = 0;\n"); - fprintf(plantFile,"\tint ___nbrInitPlaces = 0;\n"); - fprintf(plantFile,"\tint ___firstMark = -1;\n"); - - - fprintf(plantFile,"\tfor(___placeCounter; ___placeCounter<(___pnsPtr->pnum); ___placeCounter++)\n"); - fprintf(plantFile,"\t{\n"); - fprintf(plantFile,"\t\t\t___nbrInitPlaces+=___currentMarking[___placeCounter];\n"); - fprintf(plantFile,"\t}//End for\n\n"); - - - fprintf(plantFile,"\t#ifdef DEBUG\n"); - fprintf(plantFile,"\t\tprintf(\"\\nPLANT %d: FOUND %d TOKENS\\n\","\ - "getpid(),___tokenIndex);\n"); - fprintf(plantFile,"\t#endif\n"); - - - fprintf(plantFile,"\tif(___nbrInitPlaces == 0)\n"); - fprintf(plantFile,"\t{\n"); - fprintf(plantFile,"\t\tperror(\"No initial marking!\");\n"); - fprintf(plantFile,"\t\texit(0);\n"); - fprintf(plantFile,"\t}//End if\n"); - fprintf(plantFile,"\telse\n"); - fprintf(plantFile,"\t{\n"); - - - fprintf(plantFile,"\t\t#ifdef DEBUG\n"); - fprintf(plantFile,"\t\t\tprintf(\"\\nPLANT %d: CREATING"\ - " THREADS\\n\",getpid());\n"); - fprintf(plantFile,"\t\t#endif\n"); - - - fprintf(plantFile,"\t\t pthread_t ___threads[___nbrInitPlaces];\n"); - fprintf(plantFile,"\t\t \n"); - fprintf(plantFile,"\t\t for(___placeCounter = 0;"\ - "___placeCounter<(___pnsPtr->pnum);___placeCounter++)\n"); - fprintf(plantFile,"\t\t {\n"); - - - fprintf(plantFile,"\t\t\t for(___tokenCounter = 0; ___tokenCounter < "\ - "___currentMarking[___placeCounter]; ___tokenCounter++)\n"); - fprintf(plantFile,"\t\t\t {\n"); - fprintf(plantFile,"\t\t\t\t\tif(___firstMark == -1)\n"); - fprintf(plantFile,"\t\t\t\t\t\t___firstMark = ___placeCounter;\n"); - fprintf(plantFile,"\t\t\t\t\telse\n"); - fprintf(plantFile,"\t\t\t\t\t{\n"); - fprintf(plantFile,"\t\t\t\t\t\tpthread_create(&___threads[___threadIndex],"\ - "&___detachState, callMain,"\ - "(void *)&___placesArray[___placeCounter]);\n"); - fprintf(plantFile,"\t\t\t\t\t\t___threadIndex++;\n"); - fprintf(plantFile,"\t\t\t\t\t}//End else\n"); - - fprintf(plantFile,"\t\t\t }//End for\n"); - fprintf(plantFile,"\t\t }//End for\n"); - - fprintf(plantFile,"\t\t \n"); - - fprintf(plantFile,"\t\t#ifdef DEBUG\n"); - fprintf(plantFile,"\t\t\tprintf(\"\\nPLANT %d: TRHEADS"\ - " CREATED\\n\",getpid());\n"); - fprintf(plantFile,"\t\t#endif\n"); - - fprintf(plantFile,"\n"); - - fprintf(plantFile,"\t\treturn ___firstMark;\n"); - fprintf(plantFile,"\t}//End else\n"); - fprintf(plantFile,"}//End gotoInitMark\n"); + fprintf(plantFile,"\n\n//Returns an int that indicates place"\ + " number to goto based on the current marking\n"); -}//End genGotoInitMark + fprintf(plantFile,"int gotoCurrentMark(pns *___pnsPtr,"\ + " int *___currentMarking)\n"); + fprintf(plantFile,"{\n\tint ___placeCounter = 0;\n");; + fprintf(plantFile,"\tint ___nbrInitPlaces = 0;\n"); + fprintf(plantFile,"\tint ___firstMark = -1;\n"); + + fprintf(plantFile,"\tfor(___placeCounter;"\ + " ___placeCounter<(___pnsPtr->pnum); ___placeCounter++)\n\t{\n"); + + fprintf(plantFile,"\t\tif(___currentMarking[___placeCounter] > 0)"\ + "\n\t\t{\n"); + fprintf(plantFile,"\t\t\t___nbrInitPlaces++;\n"); + fprintf(plantFile,"\t\t\tif(___firstMark == -1)\n"); + fprintf(plantFile,"\t\t\t\t___firstMark = ___placeCounter;\n"); + fprintf(plantFile,"\t\t}//End if\n"); + fprintf(plantFile,"\t}//End for\n"); + + fprintf(plantFile,"\n\tif(___nbrInitPlaces == 0)\n\t{\n"); + fprintf(plantFile,"\t\tperror(\"No initial marking!\");\n"); + fprintf(plantFile,"\t\texit(0);\n"); + fprintf(plantFile,"\t}//End if\n"); + fprintf(plantFile,"\telse\n\t{\n"); + + fprintf(plantFile,"\t\tif(___nbrInitPlaces > 1)\n\t\t{\n"); + fprintf(plantFile,"\t\t\t printf(\"CURRENT MARKING MORE"\ + " THAN 1 PLACE\\n\");\n"); + fprintf(plantFile,"\t\t\t printf(\"STARTING AT FIRST PLACE"\ + " DETECTED\\n\");\n"); + fprintf(plantFile,"\t\t}//End if\n"); + fprintf(plantFile,"\n\t\treturn ___firstMark;\n"); + fprintf(plantFile,"\t}//End else\n"); + fprintf(plantFile,"}//End gotoInitMark\n"); -//Generates and outputs the code for the following function: - -// void *callMain(void *___currentMarking) + +}//End genGotoInitMark -// -// This is the entry point for all new threads and calls main -// with the appropriate arguments -void genCallMainFunc(FILE *plantFile) -{ - fprintf(plantFile,"\n//WORKS WITH PIPES ONLY\n"); - fprintf(plantFile,"//The entry point for all new threads and calls main"\ - " with the appropriate arguments\n"); - fprintf(plantFile,"void *callMain(void *___currentMarking)\n"); - fprintf(plantFile,"{\n"); - fprintf(plantFile," char ___newMarking[MAX_PLACE_DIGITS];\n"); - fprintf(plantFile," sprintf(___newMarking,\"%d\",*(int *)"\ - " ___currentMarking);\n"); - - fprintf(plantFile," char *argv[] = {\"THREAD MAIN\",\"-m\","\ - "___newMarking};\n"); - fprintf(plantFile," \n"); - - fprintf(plantFile," #ifdef DEBUG\n"); - fprintf(plantFile,"\t\tprintf(\"\\nPLANT %d: TRHEAD %d CALLING "\ - "MAIN\\n\",getpid(),pthread_self());\n"); - fprintf(plantFile,"\t#endif\n"); - - fprintf(plantFile," \n"); - fprintf(plantFile," main(3, argv);\n"); - fprintf(plantFile,"\n}//End call Main\n"); -} - - //Outputs the function prototypes to plantFile void genFunctionPrototype(FILE* plantFile) { @@ -1020,11 +942,10 @@ "pid_t ___pidSup,union sigval *___sValPtr);\n"); fprintf(plantFile,"#else\n"); //fprintf(plantFile,"\t//PIPES DEFINITION\n"); - fprintf(plantFile,"\tvoid waitForPermission(int ___lblNbr);\n"); + fprintf(plantFile,"\tvoid waitForPermission(int ___lblNbr,int ___transNbr);\n"); fprintf(plantFile,"#endif\n"); fprintf(plantFile,"\nvoid updatePns(pns *___pnsPtr,int ___cTnum,"\ " int *___currentMarking);\n"); fprintf(plantFile,"int gotoCurrentMark(pns *___pnsPtr,"\ " int *___currentMarking);\n"); - fprintf(plantFile,"void *callMain(void *___currentMarking);\n"); }//End genFunction Prototype Modified: codegen/src/plantCompiler.h =================================================================== --- codegen/src/plantCompiler.h 2009-06-17 03:30:37 UTC (rev 62) +++ codegen/src/plantCompiler.h 2009-06-17 20:56:13 UTC (rev 63) @@ -76,9 +76,13 @@ //Creates Global Variables -void createGlobals(FILE* plantFile, int pnum); +void createGlobals(FILE* plantFile); +//Creates the code to unbuffer stdin, stdout +void unbufferIO(FILE* plantFile); + + //Creates the initial goto statement //based on the initial marking //Currently only one token is supported! @@ -161,8 +165,7 @@ //Generates and outputs the code for the following function: -// void waitForPermission(sigset_t *___sMskPtr,pid_t ___pidSup, -// union sigval *____sValPtr); +// void waitForPermission(int ___lblNbr, int ___transNbr); // // This function is the algorithm that causes the transition @@ -191,22 +194,11 @@ // // This function returns an integer that indicates the place number -// to begin at based on the current marking (this is the first token found) -// A new thread created for every other token +// to begin at based on the current marking. void genGotoCurrentMark(FILE *plantFile); -//Generates and outputs the code for the following function: - -// void *callMain(void *___currentMarking) -// -// This is the entry point for all new threads and calls main -// with the appropriate arguments -void genCallMainFunc(FILE *plantFile); - - - //Outputs the function prototypes to plantFile void genFunctionPrototype(FILE* plantFile); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |