PageRenderTime 504ms CodeModel.GetById 119ms app.highlight 161ms RepoModel.GetById 123ms app.codeStats 0ms

/src/compiler/ucos-vs2008/UCOS_SIM/src/ucosii/os_mutex.c

http://ftk.googlecode.com/
C | 603 lines | 390 code | 34 blank | 179 comment | 94 complexity | 09fbeec509c78b1996a94780188c5569 MD5 | raw file
  1/*
  2*********************************************************************************************************
  3*                                                uC/OS-II
  4*                                          The Real-Time Kernel
  5*                                  MUTUAL EXCLUSION SEMAPHORE MANAGEMENT
  6*
  7*                          (c) Copyright 1992-2005, Jean J. Labrosse, Weston, FL
  8*                                           All Rights Reserved
  9*
 10* File    : OS_MUTEX.C
 11* By      : Jean J. Labrosse
 12* Version : V2.80
 13*********************************************************************************************************
 14*/
 15
 16#ifndef  OS_MASTER_FILE

 17#include <ucos_ii.h>

 18#endif

 19
 20/*
 21*********************************************************************************************************
 22*                                            LOCAL CONSTANTS
 23*********************************************************************************************************
 24*/
 25
 26#define  OS_MUTEX_KEEP_LOWER_8   0x00FFu

 27#define  OS_MUTEX_KEEP_UPPER_8   0xFF00u

 28
 29#define  OS_MUTEX_AVAILABLE      0x00FFu

 30
 31
 32#if OS_MUTEX_EN > 0

 33/*
 34*********************************************************************************************************
 35*                                   ACCEPT MUTUAL EXCLUSION SEMAPHORE
 36*
 37* Description: This  function checks the mutual exclusion semaphore to see if a resource is available.
 38*              Unlike OSMutexPend(), OSMutexAccept() does not suspend the calling task if the resource is
 39*              not available or the event did not occur.
 40*
 41* Arguments  : pevent     is a pointer to the event control block
 42*
 43*              err        is a pointer to an error code which will be returned to your application:
 44*                            OS_NO_ERR          if the call was successful.
 45*                            OS_ERR_EVENT_TYPE  if 'pevent' is not a pointer to a mutex
 46*                            OS_ERR_PEVENT_NULL 'pevent' is a NULL pointer
 47*                            OS_ERR_PEND_ISR     if you called this function from an ISR
 48*
 49* Returns    : == 1       if the resource is available, the mutual exclusion semaphore is acquired
 50*              == 0       a) if the resource is not available
 51*                         b) you didn't pass a pointer to a mutual exclusion semaphore
 52*                         c) you called this function from an ISR
 53*
 54* Warning(s) : This function CANNOT be called from an ISR because mutual exclusion semaphores are
 55*              intended to be used by tasks only.
 56*********************************************************************************************************
 57*/
 58
 59#if OS_MUTEX_ACCEPT_EN > 0

 60INT8U  OSMutexAccept (OS_EVENT *pevent, INT8U *err)
 61{
 62#if OS_CRITICAL_METHOD == 3                            /* Allocate storage for CPU status register     */

 63    OS_CPU_SR  cpu_sr = 0;
 64#endif

 65
 66
 67
 68#if OS_ARG_CHK_EN > 0

 69    if (err == (INT8U *)0) {                           /* Validate 'err'                               */
 70        return (0);
 71    }
 72    if (pevent == (OS_EVENT *)0) {                     /* Validate 'pevent'                            */
 73        *err = OS_ERR_PEVENT_NULL;
 74        return (0);
 75    }
 76#endif

 77    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) {  /* Validate event block type                    */
 78        *err = OS_ERR_EVENT_TYPE;
 79        return (0);
 80    }
 81    if (OSIntNesting > 0) {                            /* Make sure it's not called from an ISR        */
 82        *err = OS_ERR_PEND_ISR;
 83        return (0);
 84    }
 85    OS_ENTER_CRITICAL();							   /* Get value (0 or 1) of Mutex                  */
 86    if ((pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8) == OS_MUTEX_AVAILABLE) {
 87        pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8;   /*      Mask off LSByte (Acquire Mutex)         */
 88        pevent->OSEventCnt |= OSTCBCur->OSTCBPrio;     /*      Save current task priority in LSByte    */
 89        pevent->OSEventPtr  = (void *)OSTCBCur;        /*      Link TCB of task owning Mutex           */
 90        OS_EXIT_CRITICAL();
 91        *err = OS_NO_ERR;
 92        return (1);
 93    }
 94    OS_EXIT_CRITICAL();
 95    *err = OS_NO_ERR;
 96    return (0);
 97}
 98#endif

 99
100/*$PAGE*/
101/*
102*********************************************************************************************************
103*                                  CREATE A MUTUAL EXCLUSION SEMAPHORE
104*
105* Description: This function creates a mutual exclusion semaphore.
106*
107* Arguments  : prio          is the priority to use when accessing the mutual exclusion semaphore.  In
108*                            other words, when the semaphore is acquired and a higher priority task
109*                            attempts to obtain the semaphore then the priority of the task owning the
110*                            semaphore is raised to this priority.  It is assumed that you will specify
111*                            a priority that is LOWER in value than ANY of the tasks competing for the
112*                            mutex.
113*
114*              err           is a pointer to an error code which will be returned to your application:
115*                               OS_NO_ERR           if the call was successful.
116*                               OS_ERR_CREATE_ISR   if you attempted to create a MUTEX from an ISR
117*                               OS_PRIO_EXIST       if a task at the priority inheritance priority
118*                                                   already exist.
119*                               OS_ERR_PEVENT_NULL  No more event control blocks available.
120*                               OS_PRIO_INVALID     if the priority you specify is higher that the
121*                                                   maximum allowed (i.e. > OS_LOWEST_PRIO)
122*
123* Returns    : != (void *)0  is a pointer to the event control clock (OS_EVENT) associated with the
124*                            created mutex.
125*              == (void *)0  if an error is detected.
126*
127* Note(s)    : 1) The LEAST significant 8 bits of '.OSEventCnt' are used to hold the priority number
128*                 of the task owning the mutex or 0xFF if no task owns the mutex.
129*              2) The MOST  significant 8 bits of '.OSEventCnt' are used to hold the priority number
130*                 to use to reduce priority inversion.
131*********************************************************************************************************
132*/
133
134OS_EVENT  *OSMutexCreate (INT8U prio, INT8U *err)
135{
136    OS_EVENT  *pevent;
137#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */

138    OS_CPU_SR  cpu_sr = 0;
139#endif

140
141
142
143#if OS_ARG_CHK_EN > 0

144    if (err == (INT8U *)0) {                               /* Validate 'err'                           */
145        return ((OS_EVENT *)0);
146    }
147    if (prio >= OS_LOWEST_PRIO) {                          /* Validate PIP                             */
148        *err = OS_PRIO_INVALID;
149        return ((OS_EVENT *)0);
150    }
151#endif

152    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */
153        *err = OS_ERR_CREATE_ISR;                          /* ... can't CREATE mutex from an ISR       */
154        return ((OS_EVENT *)0);
155    }
156    OS_ENTER_CRITICAL();
157    if (OSTCBPrioTbl[prio] != (OS_TCB *)0) {               /* Mutex priority must not already exist    */
158        OS_EXIT_CRITICAL();                                /* Task already exist at priority ...       */
159        *err = OS_PRIO_EXIST;                              /* ... inheritance priority                 */
160        return ((OS_EVENT *)0);
161    }
162    OSTCBPrioTbl[prio] = (OS_TCB *)1;                      /* Reserve the table entry                  */
163    pevent             = OSEventFreeList;                  /* Get next free event control block        */
164    if (pevent == (OS_EVENT *)0) {                         /* See if an ECB was available              */
165        OSTCBPrioTbl[prio] = (OS_TCB *)0;                  /* No, Release the table entry              */
166        OS_EXIT_CRITICAL();
167        *err               = OS_ERR_PEVENT_NULL;           /* No more event control blocks             */
168        return (pevent);
169    }
170    OSEventFreeList        = (OS_EVENT *)OSEventFreeList->OSEventPtr;   /* Adjust the free list        */
171    OS_EXIT_CRITICAL();
172    pevent->OSEventType    = OS_EVENT_TYPE_MUTEX;
173    pevent->OSEventCnt     = (INT16U)((INT16U)prio << 8) | OS_MUTEX_AVAILABLE; /* Resource is avail.   */
174    pevent->OSEventPtr     = (void *)0;                                 /* No task owning the mutex    */
175#if OS_EVENT_NAME_SIZE > 1

176    pevent->OSEventName[0] = '?';
177    pevent->OSEventName[1] = OS_ASCII_NUL;
178#endif

179    OS_EventWaitListInit(pevent);
180    *err                   = OS_NO_ERR;
181    return (pevent);
182}
183
184/*$PAGE*/
185/*
186*********************************************************************************************************
187*                                          DELETE A MUTEX
188*
189* Description: This function deletes a mutual exclusion semaphore and readies all tasks pending on the it.
190*
191* Arguments  : pevent        is a pointer to the event control block associated with the desired mutex.
192*
193*              opt           determines delete options as follows:
194*                            opt == OS_DEL_NO_PEND   Delete mutex ONLY if no task pending
195*                            opt == OS_DEL_ALWAYS    Deletes the mutex even if tasks are waiting.
196*                                                    In this case, all the tasks pending will be readied.
197*
198*              err           is a pointer to an error code that can contain one of the following values:
199*                            OS_NO_ERR               The call was successful and the mutex was deleted
200*                            OS_ERR_DEL_ISR          If you attempted to delete the MUTEX from an ISR
201*                            OS_ERR_INVALID_OPT      An invalid option was specified
202*                            OS_ERR_TASK_WAITING     One or more tasks were waiting on the mutex
203*                            OS_ERR_EVENT_TYPE       If you didn't pass a pointer to a mutex
204*                            OS_ERR_PEVENT_NULL      If 'pevent' is a NULL pointer.
205*
206* Returns    : pevent        upon error
207*              (OS_EVENT *)0 if the mutex was successfully deleted.
208*
209* Note(s)    : 1) This function must be used with care.  Tasks that would normally expect the presence of
210*                 the mutex MUST check the return code of OSMutexPend().
211*              2) This call can potentially disable interrupts for a long time.  The interrupt disable
212*                 time is directly proportional to the number of tasks waiting on the mutex.
213*              3) Because ALL tasks pending on the mutex will be readied, you MUST be careful because the
214*                 resource(s) will no longer be guarded by the mutex.
215*********************************************************************************************************
216*/
217
218#if OS_MUTEX_DEL_EN

219OS_EVENT  *OSMutexDel (OS_EVENT *pevent, INT8U opt, INT8U *err)
220{
221    BOOLEAN    tasks_waiting;
222    OS_EVENT  *pevent_return;
223    INT8U      pip;
224#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */

225    OS_CPU_SR  cpu_sr = 0;
226#endif

227
228
229
230#if OS_ARG_CHK_EN > 0

231    if (err == (INT8U *)0) {                               /* Validate 'err'                           */
232        return ((OS_EVENT *)0);
233    }
234    if (pevent == (OS_EVENT *)0) {                         /* Validate 'pevent'                        */
235        *err = OS_ERR_PEVENT_NULL;
236        return ((OS_EVENT *)0);
237    }
238#endif

239    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) {      /* Validate event block type                */
240        *err = OS_ERR_EVENT_TYPE;
241        return (pevent);
242    }
243    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */
244        *err = OS_ERR_DEL_ISR;                             /* ... can't DELETE from an ISR             */
245        return (pevent);
246    }
247    OS_ENTER_CRITICAL();
248    if (pevent->OSEventGrp != 0) {                         /* See if any tasks waiting on mutex        */
249        tasks_waiting = TRUE;                              /* Yes                                      */
250    } else {
251        tasks_waiting = FALSE;                             /* No                                       */
252    }
253    switch (opt) {
254        case OS_DEL_NO_PEND:                               /* Delete mutex only if no task waiting     */
255             if (tasks_waiting == FALSE) {
256#if OS_EVENT_NAME_SIZE > 1

257                 pevent->OSEventName[0] = '?';             /* Unknown name                             */
258                 pevent->OSEventName[1] = OS_ASCII_NUL;
259#endif

260                 pip                 = (INT8U)(pevent->OSEventCnt >> 8);
261                 OSTCBPrioTbl[pip]   = (OS_TCB *)0;        /* Free up the PIP                          */
262                 pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
263                 pevent->OSEventPtr  = OSEventFreeList;    /* Return Event Control Block to free list  */
264                 pevent->OSEventCnt  = 0;
265                 OSEventFreeList     = pevent;
266                 OS_EXIT_CRITICAL();
267                 *err                = OS_NO_ERR;
268                 pevent_return       = (OS_EVENT *)0;      /* Mutex has been deleted                   */
269             } else {
270                 OS_EXIT_CRITICAL();
271                 *err                = OS_ERR_TASK_WAITING;
272                 pevent_return       = pevent;
273             }
274             break;
275
276        case OS_DEL_ALWAYS:                                /* Always delete the mutex                  */
277             while (pevent->OSEventGrp != 0) {             /* Ready ALL tasks waiting for mutex        */
278                 (void)OS_EventTaskRdy(pevent, (void *)0, OS_STAT_MUTEX);
279             }
280#if OS_EVENT_NAME_SIZE > 1

281             pevent->OSEventName[0] = '?';                 /* Unknown name                             */
282             pevent->OSEventName[1] = OS_ASCII_NUL;
283#endif

284             pip                 = (INT8U)(pevent->OSEventCnt >> 8);
285             OSTCBPrioTbl[pip]   = (OS_TCB *)0;            /* Free up the PIP                          */
286             pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
287             pevent->OSEventPtr  = OSEventFreeList;        /* Return Event Control Block to free list  */
288             pevent->OSEventCnt  = 0;
289             OSEventFreeList     = pevent;                 /* Get next free event control block        */
290             OS_EXIT_CRITICAL();
291             if (tasks_waiting == TRUE) {                  /* Reschedule only if task(s) were waiting  */
292                 OS_Sched();                               /* Find highest priority task ready to run  */
293             }
294             *err          = OS_NO_ERR;
295             pevent_return = (OS_EVENT *)0;                /* Mutex has been deleted                   */
296             break;
297
298        default:
299             OS_EXIT_CRITICAL();
300             *err          = OS_ERR_INVALID_OPT;
301             pevent_return = pevent;
302             break;
303    }
304    return (pevent_return);
305}
306#endif

307
308/*$PAGE*/
309/*
310*********************************************************************************************************
311*                                  PEND ON MUTUAL EXCLUSION SEMAPHORE
312*
313* Description: This function waits for a mutual exclusion semaphore.
314*
315* Arguments  : pevent        is a pointer to the event control block associated with the desired
316*                            mutex.
317*
318*              timeout       is an optional timeout period (in clock ticks).  If non-zero, your task will
319*                            wait for the resource up to the amount of time specified by this argument.
320*                            If you specify 0, however, your task will wait forever at the specified
321*                            mutex or, until the resource becomes available.
322*
323*              err           is a pointer to where an error message will be deposited.  Possible error
324*                            messages are:
325*                               OS_NO_ERR          The call was successful and your task owns the mutex
326*                               OS_TIMEOUT         The mutex was not available within the specified time.
327*                               OS_ERR_EVENT_TYPE  If you didn't pass a pointer to a mutex
328*                               OS_ERR_PEVENT_NULL 'pevent' is a NULL pointer
329*                               OS_ERR_PEND_ISR    If you called this function from an ISR and the result
330*                                                  would lead to a suspension.
331*                               OS_ERR_PIP_LOWER   If the priority of the task that owns the Mutex is
332*                                                  HIGHER (i.e. a lower number) than the PIP.  This error
333*                                                  indicates that you did not set the PIP higher (lower
334*                                                  number) than ALL the tasks that compete for the Mutex.
335*                                                  Unfortunately, this is something that could not be
336*                                                  detected when the Mutex is created because we don't know
337*                                                  what tasks will be using the Mutex.
338*
339* Returns    : none
340*
341* Note(s)    : 1) The task that owns the Mutex MUST NOT pend on any other event while it owns the mutex.
342*              2) You MUST NOT change the priority of the task that owns the mutex
343*********************************************************************************************************
344*/
345void  OSMutexPend (OS_EVENT *pevent, INT16U timeout, INT8U *err)
346{
347    INT8U      pip;                                        /* Priority Inheritance Priority (PIP)      */
348    INT8U      mprio;                                      /* Mutex owner priority                     */
349    BOOLEAN    rdy;                                        /* Flag indicating task was ready           */
350    OS_TCB    *ptcb;
351    OS_EVENT  *pevent2;
352    INT8U      y;
353#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */

354    OS_CPU_SR  cpu_sr = 0;
355#endif

356
357
358
359#if OS_ARG_CHK_EN > 0

360    if (err == (INT8U *)0) {                               /* Validate 'err'                           */
361        return;
362    }
363    if (pevent == (OS_EVENT *)0) {                         /* Validate 'pevent'                        */
364        *err = OS_ERR_PEVENT_NULL;
365        return;
366    }
367#endif

368    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) {      /* Validate event block type                */
369        *err = OS_ERR_EVENT_TYPE;
370        return;
371    }
372    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */
373        *err = OS_ERR_PEND_ISR;                            /* ... can't PEND from an ISR               */
374        return;
375    }
376    OS_ENTER_CRITICAL();								
377    pip = (INT8U)(pevent->OSEventCnt >> 8);                /* Get PIP from mutex                       */
378                                                           /* Is Mutex available?                      */
379    if ((INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8) == OS_MUTEX_AVAILABLE) {
380        pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8;       /* Yes, Acquire the resource                */
381        pevent->OSEventCnt |= OSTCBCur->OSTCBPrio;         /*      Save priority of owning task        */
382        pevent->OSEventPtr  = (void *)OSTCBCur;            /*      Point to owning task's OS_TCB       */
383        if (OSTCBCur->OSTCBPrio <= pip) {                  /*      PIP 'must' have a SMALLER prio ...  */
384            OS_EXIT_CRITICAL();                            /*      ... than current task!              */
385            *err  = OS_ERR_PIP_LOWER;
386        } else {
387            OS_EXIT_CRITICAL();
388            *err  = OS_NO_ERR;
389        }
390        return;
391    }
392    mprio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8);  /* No, Get priority of mutex owner   */
393    ptcb  = (OS_TCB *)(pevent->OSEventPtr);                       /*     Point to TCB of mutex owner   */
394    if (ptcb->OSTCBPrio > pip) {                                  /*     Need to promote prio of owner?*/
395        if (mprio > OSTCBCur->OSTCBPrio) {
396            y = ptcb->OSTCBY;
397            if ((OSRdyTbl[y] & ptcb->OSTCBBitX) != 0) {           /*     See if mutex owner is ready   */
398                OSRdyTbl[y] &= ~ptcb->OSTCBBitX;                  /*     Yes, Remove owner from Rdy ...*/
399                if (OSRdyTbl[y] == 0) {                           /*          ... list at current prio */
400                    OSRdyGrp &= ~ptcb->OSTCBBitY;
401                }
402                rdy = TRUE;
403            } else {
404                pevent2 = ptcb->OSTCBEventPtr;
405                if (pevent2 != (OS_EVENT *)0) {                   /* Remove from event wait list       */
406                    if ((pevent2->OSEventTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) {
407                        pevent2->OSEventGrp &= ~ptcb->OSTCBBitY;
408                    }
409                }
410                rdy = FALSE;                               /* No                                       */
411            }
412            ptcb->OSTCBPrio = pip;                         /* Change owner task prio to PIP            */
413#if OS_LOWEST_PRIO <= 63

414            ptcb->OSTCBY    =  ptcb->OSTCBPrio >> 3;
415            ptcb->OSTCBX    =  ptcb->OSTCBPrio & 0x07;
416#else

417            ptcb->OSTCBY    = (ptcb->OSTCBPrio >> 4) & 0xFF;
418            ptcb->OSTCBX    =  ptcb->OSTCBPrio & 0x0F;
419#endif

420            ptcb->OSTCBBitY = 1 << ptcb->OSTCBY;
421            ptcb->OSTCBBitX = 1 << ptcb->OSTCBX;
422            if (rdy == TRUE) {                             /* If task was ready at owner's priority ...*/
423                OSRdyGrp               |= ptcb->OSTCBBitY; /* ... make it ready at new priority.       */
424                OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
425            } else {
426                pevent2 = ptcb->OSTCBEventPtr;
427                if (pevent2 != (OS_EVENT *)0) {            /* Remove from event wait list              */
428                    pevent2->OSEventGrp               |= ptcb->OSTCBBitY;
429                    pevent2->OSEventTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
430                }
431            }
432            OSTCBPrioTbl[pip] = ptcb;
433        }
434    }
435    OSTCBCur->OSTCBStat   |= OS_STAT_MUTEX;           /* Mutex not available, pend current task        */
436    OSTCBCur->OSTCBPendTO  = FALSE;
437    OSTCBCur->OSTCBDly     = timeout;                 /* Store timeout in current task's TCB           */
438    OS_EventTaskWait(pevent);                         /* Suspend task until event or timeout occurs    */
439    OS_EXIT_CRITICAL();
440    OS_Sched();                                       /* Find next highest priority task ready         */
441    OS_ENTER_CRITICAL();
442    if (OSTCBCur->OSTCBPendTO == TRUE) {              /* See if we timed out during the pend           */
443        OS_EventTO(pevent);
444        OS_EXIT_CRITICAL();
445        *err = OS_TIMEOUT;                            /* Indicate that we didn't get mutex within TO   */
446        return;
447    }
448    OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0;
449    OS_EXIT_CRITICAL();
450    *err = OS_NO_ERR;
451}
452/*$PAGE*/
453/*
454*********************************************************************************************************
455*                                  POST TO A MUTUAL EXCLUSION SEMAPHORE
456*
457* Description: This function signals a mutual exclusion semaphore
458*
459* Arguments  : pevent              is a pointer to the event control block associated with the desired
460*                                  mutex.
461*
462* Returns    : OS_NO_ERR               The call was successful and the mutex was signaled.
463*              OS_ERR_EVENT_TYPE       If you didn't pass a pointer to a mutex
464*              OS_ERR_PEVENT_NULL      'pevent' is a NULL pointer
465*              OS_ERR_POST_ISR         Attempted to post from an ISR (not valid for MUTEXes)
466*              OS_ERR_NOT_MUTEX_OWNER  The task that did the post is NOT the owner of the MUTEX.
467*********************************************************************************************************
468*/
469
470INT8U  OSMutexPost (OS_EVENT *pevent)
471{
472    INT8U      pip;                                   /* Priority inheritance priority                 */
473    INT8U      prio;
474    INT8U      y;
475#if OS_CRITICAL_METHOD == 3                           /* Allocate storage for CPU status register      */

476    OS_CPU_SR  cpu_sr = 0;
477#endif

478
479
480
481    if (OSIntNesting > 0) {                           /* See if called from ISR ...                    */
482        return (OS_ERR_POST_ISR);                     /* ... can't POST mutex from an ISR              */
483    }
484#if OS_ARG_CHK_EN > 0

485    if (pevent == (OS_EVENT *)0) {                    /* Validate 'pevent'                             */
486        return (OS_ERR_PEVENT_NULL);
487    }
488#endif

489    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) { /* Validate event block type                     */
490        return (OS_ERR_EVENT_TYPE);
491    }
492    OS_ENTER_CRITICAL();
493    pip  = (INT8U)(pevent->OSEventCnt >> 8);          /* Get priority inheritance priority of mutex    */
494    prio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8);  /* Get owner's original priority      */
495    if (OSTCBCur != (OS_TCB *)pevent->OSEventPtr) {   /* See if posting task owns the MUTEX            */
496        OS_EXIT_CRITICAL();
497        return (OS_ERR_NOT_MUTEX_OWNER);
498    }
499    if (OSTCBCur->OSTCBPrio == pip) {                 /* Did we have to raise current task's priority? */
500        y            = OSTCBCur->OSTCBY;              /* Yes, Return to original priority              */
501        OSRdyTbl[y] &= ~OSTCBCur->OSTCBBitX;          /*      Remove owner from ready list at 'pip'    */
502        if (OSRdyTbl[y] == 0) {
503            OSRdyGrp &= ~OSTCBCur->OSTCBBitY;
504        }
505        OSTCBCur->OSTCBPrio         = prio;
506#if OS_LOWEST_PRIO <= 63

507        OSTCBCur->OSTCBY            = prio >> 3;
508        OSTCBCur->OSTCBX            = prio & 0x07;
509#else

510        OSTCBCur->OSTCBY            = (prio >> 4) & 0xFF;
511        OSTCBCur->OSTCBX            = prio & 0x0F;
512#endif

513        OSTCBCur->OSTCBBitY         = 1 << OSTCBCur->OSTCBY;
514        OSTCBCur->OSTCBBitX         = 1 << OSTCBCur->OSTCBX;
515        OSRdyGrp                   |= OSTCBCur->OSTCBBitY;
516        OSRdyTbl[OSTCBCur->OSTCBY] |= OSTCBCur->OSTCBBitX;
517        OSTCBPrioTbl[prio]          = OSTCBCur;
518    }
519    OSTCBPrioTbl[pip] = (OS_TCB *)1;                  /* Reserve table entry                           */
520    if (pevent->OSEventGrp != 0) {                    /* Any task waiting for the mutex?               */
521                                                      /* Yes, Make HPT waiting for mutex ready         */
522        prio                = OS_EventTaskRdy(pevent, (void *)0, OS_STAT_MUTEX);
523        pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8;  /*      Save priority of mutex's new owner       */
524        pevent->OSEventCnt |= prio;
525        pevent->OSEventPtr  = OSTCBPrioTbl[prio];     /*      Link to mutex owner's OS_TCB             */
526        OS_EXIT_CRITICAL();
527        OS_Sched();                                   /*      Find highest priority task ready to run  */
528        return (OS_NO_ERR);
529    }
530    pevent->OSEventCnt |= OS_MUTEX_AVAILABLE;         /* No,  Mutex is now available                   */
531    pevent->OSEventPtr  = (void *)0;
532    OS_EXIT_CRITICAL();
533    return (OS_NO_ERR);
534}
535/*$PAGE*/
536/*
537*********************************************************************************************************
538*                                     QUERY A MUTUAL EXCLUSION SEMAPHORE
539*
540* Description: This function obtains information about a mutex
541*
542* Arguments  : pevent          is a pointer to the event control block associated with the desired mutex
543*
544*              p_mutex_data    is a pointer to a structure that will contain information about the mutex
545*
546* Returns    : OS_NO_ERR            The call was successful and the message was sent
547*              OS_ERR_QUERY_ISR     If you called this function from an ISR
548*              OS_ERR_PEVENT_NULL   If 'pevent'       is a NULL pointer
549*              OS_ERR_PDATA_NULL    If 'p_mutex_data' is a NULL pointer
550*              OS_ERR_EVENT_TYPE    If you are attempting to obtain data from a non mutex.
551*********************************************************************************************************
552*/
553
554#if OS_MUTEX_QUERY_EN > 0

555INT8U  OSMutexQuery (OS_EVENT *pevent, OS_MUTEX_DATA *p_mutex_data)
556{
557    INT8U      i;
558#if OS_LOWEST_PRIO <= 63

559    INT8U     *psrc;
560    INT8U     *pdest;
561#else

562    INT16U    *psrc;
563    INT16U    *pdest;
564#endif

565#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */

566    OS_CPU_SR  cpu_sr = 0;
567#endif

568
569
570
571    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */
572        return (OS_ERR_QUERY_ISR);                         /* ... can't QUERY mutex from an ISR        */
573    }
574#if OS_ARG_CHK_EN > 0

575    if (pevent == (OS_EVENT *)0) {                         /* Validate 'pevent'                        */
576        return (OS_ERR_PEVENT_NULL);
577    }
578    if (p_mutex_data == (OS_MUTEX_DATA *)0) {              /* Validate 'p_mutex_data'                  */
579        return (OS_ERR_PDATA_NULL);
580    }
581#endif

582    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) {      /* Validate event block type                */
583        return (OS_ERR_EVENT_TYPE);
584    }
585    OS_ENTER_CRITICAL();
586    p_mutex_data->OSMutexPIP  = (INT8U)(pevent->OSEventCnt >> 8);
587    p_mutex_data->OSOwnerPrio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8);
588    if (p_mutex_data->OSOwnerPrio == 0xFF) {
589        p_mutex_data->OSValue = 1;
590    } else {
591        p_mutex_data->OSValue = 0;
592    }
593    p_mutex_data->OSEventGrp  = pevent->OSEventGrp;        /* Copy wait list                           */
594    psrc                      = &pevent->OSEventTbl[0];
595    pdest                     = &p_mutex_data->OSEventTbl[0];
596    for (i = 0; i < OS_EVENT_TBL_SIZE; i++) {
597        *pdest++ = *psrc++;
598    }
599    OS_EXIT_CRITICAL();
600    return (OS_NO_ERR);
601}
602#endif                                                     /* OS_MUTEX_QUERY_EN                        */

603#endif                                                     /* OS_MUTEX_EN                              */