?? taskvarlib.c
字號:
* }** if ((opGlobal = (OP_GLOBAL *) malloc (sizeof (OP_GLOBAL))) == NULL)* {* printErr ("operator%d: can't malloc opGlobal\en", opNum);* taskSuspend (0);* }* ...* }* .CE** RETURNS:* OK, or ERROR if memory is insufficient for the task variable descriptor.** INTERNAL* The first time this routine is called the task switch and delete* routines, taskVarSwitchHook() and taskVarDeleteHook(), are installed.** SEE ALSO: taskVarDelete(), taskVarGet(), taskVarSet()*/STATUS taskVarAdd ( int tid, /* ID of task to have new variable */ int *pVar /* pointer to variable to be switched for task */ ) { FAST WIND_TCB *pTcb = taskTcb (tid); FAST TASK_VAR *pTaskVar; int level; /* make sure task variable facility is installed */ if (taskVarInit () != OK) return (ERROR); /* allocate descriptor for new task variable */ pTaskVar = (TASK_VAR *) malloc (sizeof (TASK_VAR)); if (pTaskVar == NULL) return (ERROR); pTaskVar->address = pVar; pTaskVar->value = *pVar; /* * Link new task variable into list in stack header. Locking interrupts * was chosen over task locking since locking and unlocking interrupts is * is much faster than task locking and very little code is in the * critical section. */ level = intLock (); pTaskVar->next = pTcb->pTaskVar; pTcb->pTaskVar = pTaskVar; intUnlock (level); return (OK); }/********************************************************************************* taskVarDelete - remove a task variable from a task** This routine removes a specified task variable, <pVar>, from the specified* task's context. The private value of that variable is lost.** RETURNS* OK, or* ERROR if the task variable does not exist for the specified task.** SEE ALSO: taskVarAdd(), taskVarGet(), taskVarSet()*/STATUS taskVarDelete ( int tid, /* ID of task whose variable is to be removed */ int *pVar /* pointer to task variable to be removed */ ) { FAST TASK_VAR **ppTaskVar; /* ptr to ptr to next node */ FAST TASK_VAR *pTaskVar; WIND_TCB *pTcb = taskTcb (tid); if (pTcb == NULL) /* check that task is valid */ return (ERROR); /* find descriptor for specified task variable */ for (ppTaskVar = &pTcb->pTaskVar; *ppTaskVar != NULL; ppTaskVar = &((*ppTaskVar)->next)) { pTaskVar = *ppTaskVar; if (pTaskVar->address == pVar) { /* if active task, replace background value */ if (taskIdCurrent == pTcb) *pVar = pTaskVar->value; *ppTaskVar = pTaskVar->next;/* delete variable from list */ free ((char *)pTaskVar); /* free storage of deleted cell */ return (OK); } } /* specified address is not a task variable for specified task */ errnoSet (S_taskLib_TASK_VAR_NOT_FOUND); return (ERROR); }/********************************************************************************* taskVarGet - get the value of a task variable** This routine returns the private value of a task variable for a* specified task. The specified task is usually not the calling task,* which can get its private value by directly accessing the variable.* This routine is provided primarily for debugging purposes.** RETURNS:* The private value of the task variable, or* ERROR if the task is not found or it* does not own the task variable.** SEE ALSO: taskVarAdd(), taskVarDelete(), taskVarSet()*/int taskVarGet ( int tid, /* ID of task whose task variable is to be retrieved */ int *pVar /* pointer to task variable */ ) { FAST TASK_VAR *pTaskVar; /* ptr to next node */ WIND_TCB *pTcb = taskTcb (tid); if (pTcb == NULL) return (ERROR); /* find descriptor for specified task variable */ for (pTaskVar = pTcb->pTaskVar; pTaskVar != NULL; pTaskVar = pTaskVar->next) { if (pTaskVar->address == pVar) return (taskIdCurrent == pTcb ? *pVar : pTaskVar->value); } /* specified address is not a task variable for specified task */ errnoSet (S_taskLib_TASK_VAR_NOT_FOUND); return (ERROR); }/********************************************************************************* taskVarSet - set the value of a task variable** This routine sets the private value of the task variable for a specified* task. The specified task is usually not the calling task, which can set* its private value by directly modifying the variable. This routine is* provided primarily for debugging purposes.** RETURNS:* OK, or* ERROR if the task is not found or it* does not own the task variable.** SEE ALSO: taskVarAdd(), taskVarDelete(), taskVarGet()*/STATUS taskVarSet ( int tid, /* ID of task whose task variable is to be set */ int *pVar, /* pointer to task variable to be set for this task */ int value /* new value of task variable */ ) { FAST TASK_VAR *pTaskVar; /* ptr to next node */ WIND_TCB *pTcb = taskTcb (tid); if (pTcb == NULL) return (ERROR); /* find descriptor for specified task variable */ for (pTaskVar = pTcb->pTaskVar; pTaskVar != NULL; pTaskVar = pTaskVar->next) { if (pTaskVar->address == pVar) { if (taskIdCurrent == pTcb) { *pVar = value; } else pTaskVar->value = value; return (OK); } } /* specified address is not a task variable for specified task */ errnoSet (S_taskLib_TASK_VAR_NOT_FOUND); return (ERROR); }/********************************************************************************* taskVarInfo - get a list of task variables of a task** This routine provides the calling task with a list of all of the task* variables of a specified task. The unsorted array of task variables is* copied to <varList>.** CAVEATS* Kernel rescheduling is disabled with taskLock() while task variables are* looked up. There is no guarantee that all the task variables are still* valid or that new task variables have not been created by the time this* routine returns.** RETURNS: The number of task variables in the list.*/int taskVarInfo ( int tid, /* ID of task whose task variable is to be set */ TASK_VAR varList[], /* array to hold task variable addresses */ int maxVars /* maximum variables varList can accommodate */ ) { FAST TASK_VAR *pTaskVar; /* ptr to next node */ WIND_TCB * pTcb = taskTcb (tid); int active = 0; if (pTcb == NULL) return (ERROR); taskLock (); /* LOCK PREEMPTION */ for (pTaskVar = pTcb->pTaskVar; (pTaskVar != NULL) && (active < maxVars); pTaskVar = pTaskVar->next, active ++) { varList[active] = *pTaskVar; } taskUnlock (); /* UNLOCK PREEMPTION */ return (active); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -