📄 vslib.c
字号:
) { int vsIndex; semTake (vsTblLock, WAIT_FOREVER); if ( (vsid < 0) || (vsid > VSID_MAX) ) return (ERROR); strcpy (pName, vsTbl[vsid]->pName); semGive (vsTblLock); return (OK); }/********************************************************************************* virtualStackInfo - print out some information about a Virtual Stack** This routine takes the VSID in vsid and prints some information on* the console about it.** RETURNS: OK or ERROR if the VSID was somehow invalid**/void virtualStackInfo ( VSID vsid ) { int vsIndex; if (vsid == NULL) return; semTake (vsTblLock, WAIT_FOREVER); for (vsIndex = 0; vsIndex < VSID_MAX; vsIndex++) if (vsTbl[vsIndex] == vsid) break; /* Hey, we found it! */ /* Check that we did find it. */ if (vsIndex == VSID_MAX) { semGive(vsTblLock); return; } printf ("Index: %d \tName: %s\n", vsIndex, vsTbl[vsIndex]->pName); semGive(vsTblLock); }/********************************************************************************* virtualStackNumGet - fetches the virtual stack number ** This functions returns a valid stack number in <num> if it returns an OK.** RETURNS: OK --- means that <num> points to a valid stack number;* ERROR --- <num> is not valid and/or VSID is invalid;*/STATUS virtualStackNumGet ( VSID vsid, int *num ) { if ((vsid == NULL) && (num == NULL)) return (ERROR); semTake (vsTblLock, WAIT_FOREVER); for (*num = 0; (*num < VSID_MAX) && (vsTbl[*num] != vsid); (*num)++); semGive(vsTblLock); if ( vsTbl[*num] != vsid ) return (ERROR); return OK; }/******************************************************************************** virtualStackShow - print out the list of Virtual Stacks** This routine goes through the list of Virtual Stacks and calls* virtualStackInfo on each.** RETURNS N/A**/void virtualStackShow () { int vsIndex; for (vsIndex = 0; vsIndex < VSID_MAX; vsIndex++) virtualStackInfo(vsTbl[vsIndex]); }/********************************************************************************* virtualStackNameTaskIdSet - set the task variable myTaskNum by name** This routine takes a stack name in pName and will set the task variable* myTaskNum to the appropriate index.** RETURNS OK or ERROR if the stack named in pName is not found** WARNING* This routine is NOT to be called in the context of tNetTask.**/STATUS virtualStackNameTaskIdSet ( char* pName /* Name that was used in virtualStackCreate */ ) { int vsIndex; semTake(vsTblLock, WAIT_FOREVER); for (vsIndex = 0; vsIndex < VSID_MAX; vsIndex++) if (!strncmp (vsTbl[vsIndex]->pName, pName, min (strlen (pName), VS_NAME_MAX))) break; /* Hey, we found it! */ semGive (vsTblLock); if (vsIndex < VSID_MAX) return (virtualStackNumTaskIdSet (vsIndex)); return (ERROR); }/********************************************************************************* virtualStackNumTaskIdSet - set the task variable myTaskNum by index** This routine takes a stack index in vsNum and will set the task variable* myTaskNum to the appropriate index.** RETURNS OK or ERROR if the stack number in vsNum is invalid** WARNING* This routine is NOT to be called in the context of tNetTask.**/STATUS virtualStackNumTaskIdSet ( int vsNum ) { /* * If we this task does not have the task variable then * create it as part of this task. */ if (taskVarGet(0, (int *) &myStackNum) == ERROR) { if (errnoGet () == S_taskLib_TASK_VAR_NOT_FOUND) errnoSet (0); /* Resets the error caused by taskVarGet */ taskVarAdd (0, (int *) &myStackNum); } if (vsNum > VSID_MAX) return (ERROR); if (vsTbl[vsNum] == NULL) return (ERROR); myStackNum = vsNum; return (OK); }/******************************************************************************* * * virtualStackNameSet - Set the stack name, given the stack id * * This routine sets the name of the stack identified by vsid to the * contents of pName. Doing so will overwrite the name provided to this * stack when it was created. * * RETURNS: OK or ERROR is the stack id was not found. * * This routine is not to be called in the context of tNetTask. */STATUS virtualStackNameSet ( char *pName, /* Name to be set */ int vsid /* Stack id */ ) { semTake (vsTblLock, WAIT_FOREVER); if ( (vsid < 0) || (vsid > VSID_MAX) ) return (ERROR); strcpy (vsTbl[vsid]->pName, pName); semGive (vsTblLock); return (OK); }/********************************************************************************* virtualStackIdCheck - check to see if myStackNum is set in this task** This routine checks to see if myStackNum is set in this task. If it is not* set then it is added to the task's context and set to the VS_MGMT_STACK.** RETURNS OK or ERROR** WARNING* This routine is not to be called in the context of tNetTask**/STATUS virtualStackIdCheck ( ) { if (taskVarGet(0, (int *)&myStackNum) == ERROR) { taskVarAdd(0, (int *)&myStackNum); myStackNum = VS_MGMT_STACK; } return (OK); }int numTestTasks;SEM_ID virtualStackTestSem;/********************************************************************************* virtualStackTestDummy - dummy routine called to check the myTaskNum variable** This routine is called by the test harness in this module to test the* functionality of setting task variables for stack numbers.** NOMANUAL**/void virtualStackTestDummy ( vsNum ) { numTestTasks++; taskDelay (vsNum * sysClkRateGet()); printf ("Attempting to set myself to Stack %d\n", vsNum); if (virtualStackNumTaskIdSet(vsNum) == ERROR) { printf ("Task for stack %d failed.\n", vsNum); return; } printf ("Task %d has myStackNum of %d\n", vsNum, myStackNum); printf ("Task %d has stack info of...\n", vsNum); virtualStackInfo (vsTbl[myStackNum]); numTestTasks--; if (numTestTasks == 0) semGive(virtualStackTestSem); }/********************************************************************************* virtualStackTest - test the virtual stack library** This routine tests the virtual stack library by creating VSID_MAX stacks* and then calling the virtualStackInfo command on each. Once this passes* we spawn VSID_MAX seperate tasks which each attempt to set their myTaskNum* Task Variable to the stack number they will inhabit (passed as the only* argument to the test). When the last task exits it releases a semaphore* which the test is pending on. Cleanup is then done.** NOMANUAL**/void virtualStackTest ( ) { int count; VSID myStacks[VSID_MAX]; printf ("Starting Virtual Stack Library Test...\n"); for (count = 0; count < VSID_MAX; count++) { printf ("Creating stack %d\n", count); myStacks[count] = NULL; virtualStackCreate(NULL, &myStacks[count]); } printf ("Creating complete, checking for failures...\n"); for (count = 0; count < VSID_MAX; count++) { printf ("Checking stack %d\n", count); if (myStacks[count] == NULL) { printf ("Stack creation on %d failed\n", count); goto cleanup; } } printf ("Stacks 0 through %d created.\n", count); printf ("Getting stack information...\n"); for (count = 0; count < VSID_MAX; count++) { virtualStackInfo (myStacks[count]); } printf ("Checking task variable madness...\n"); virtualStackTestSem = semBCreate (SEM_Q_FIFO, SEM_EMPTY); /* * Set the number to 0, then let them count them up * and down and release the semaphore. */ numTestTasks = 0; for (count = 0; count < VSID_MAX; count++) { taskSpawn (NULL, 255, 0, 20000, (FUNCPTR)virtualStackTestDummy, count, 2, 3, 4, 5, 6, 7 , 8 , 9, 10); } /* Wait here for all the sub tasks to end. */ semTake (virtualStackTestSem, WAIT_FOREVER); printf ("Done testing...\n"); cleanup: printf ("Cleaning up...\n"); for (count = 0; count < VSID_MAX; count++) { if (myStacks[count] != NULL) virtualStackDelete (myStacks[count]); } semDelete(virtualStackTestSem); }/***************************************************************************** vsMakeStack - create and initialize a virtual stack* * This function takes a stack name and creates a new virtual stack* and initializes it.** This function has been replaced by usrVirtualStackCreate () in* target/config/comps/src/net/usrVirtualStack.c** RETURNS: OK or ERROR*/STATUS vsMakeStack ( char * vsName, /* name of the stack */ int maxUnits /* max number of devices attached to IP */ ) { int oldStackNum = myStackNum; VSID vsNum; if (vsName == NULL) return ERROR; if (virtualStackCreate(vsName, &vsNum) == ERROR) return ERROR; if (virtualStackInit(vsNum, maxUnits) == ERROR) return ERROR; myStackNum = oldStackNum; return OK; }/***************************************************************************** vsexec - call a function given by a pointer argument, using a specified virtual stack* * This function is most useful when calling a function from the (target or wind) * shell. It is the only way to specify a stack other than 0 from the windshell.* When spawning a task, it is a convenient way to let the spawned function for* a different stack than 0.* Example: sp vse, 1, rdisc - spawns a new task for rdisc in the context of* router stack 1.** RETURNS: return status from calling command*/int vsexec ( int stackNum, /* the stack number to use */ int (*f)(), /* function to call */ int par1, /* parameters to function */ int par2, int par3, int par4, int par5, int par6, int par7, int par8, int par9, int par10 ) { if (0 == f){ printf("no function given\n"); return -1; } if (virtualStackNumTaskIdSet(stackNum) == ERROR){ printf("error setting myStackNum\n"); return -1; } return f(par1, par2, par3, par4, par5, par6, par7, par8, par9, par10); }#endif /* VIRTUAL_STACK */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -