⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pic.c

📁 nucleus 2006 source code
💻 C
📖 第 1 页 / 共 5 页
字号:
                    /* Variable-size messages are supported.  Processing must
                       check for pipe wrap-around conditions.  */

                    /* Place message size in first location.  */
                    *((UNSIGNED *) destination) =  size;
                    destination =  destination + sizeof(UNSIGNED);

                    /* Check for a wrap-around condition on the pipe.  */
                    if (destination >= pipe -> pi_end)

                        /* Wrap the write pointer back to the top of the pipe
                           area.  */
                        destination =  pipe -> pi_start;

                    /* Decrement the number of bytes remaining for this
                       extra word of overhead.  */
                    pipe -> pi_available =  pipe -> pi_available -
                                                        sizeof(UNSIGNED);

                    /* Calculate the number of bytes remaining from the write
                       pointer to the bottom of the pipe.  */
                    copy_size =  pipe -> pi_end - destination;

                    /* Determine if the message needs to be wrapped around the
                       edge of the pipe area.  */
                    if (copy_size >= size)
                    {

                        /* Copy the whole message at once.  */
                        i =  (INT) size;
                        do
                        {
                            *(destination++) =  *(source);
                            if ((--i) == 0)
                                break;
                            source++;
                        } while (1);
                    }
                    else
                    {

                        /* Copy the first half of the message.  */
                        i =  (INT) copy_size;
                        do
                        {
                            *(destination) =  *(source++);
                            if ((--i) == 0)
                                break;
                            destination++;
                        } while (1);

                        /* Copy the second half of the message.  */
                        destination =  pipe -> pi_start;
                        i =  (INT) (size - copy_size);
                        do
                        {
                            *(destination++) =  *(source);
                            if ((--i) == 0)
                                break;
                            source++;
                        } while (1);
                    }
                }

                /* Check again for wrap-around condition on the write
                   pointer. */
                if (destination >= pipe -> pi_end)

                    /* Move the write pointer to the top of the pipe area.  */
                    destination =  pipe -> pi_start;

                /* Determine if the pipe supports variable-length messages.  If
                   so, pad bytes are needed to keep UNSIGNED alignment.  */
                if (pad)
                {

                    /* Variable-size message.  Add pad bytes to the write
                       pointer.  */

                    /* Calculate the number of bytes remaining from the write
                       pointer to the bottom of the pipe.  */
                    copy_size =  pipe -> pi_end - destination;

                    /* If there is not enough room at the bottom of the pipe,
                       the pad bytes must be wrapped around to the top.  */
                    if (copy_size <= pad)

                        /* Move write pointer to the top of the pipe and make
                           the necessary adjustment.  */
                        destination =  pipe -> pi_start + (pad - copy_size);
                    else

                        /* There is enough room in the pipe to simply add
                           the pad bytes to the write pointer.  */
                        destination =  destination + pad;

                    /* Decrement the number of available bytes.  */
                    pipe -> pi_available =  pipe -> pi_available - pad;
                }

                /* Update the actual write pointer.  */
                pipe -> pi_write =  destination;

                /* Decrement the number of available bytes.  */
                pipe -> pi_available =  pipe -> pi_available - size;

                /* Increment the number of messages in the pipe.  */
                pipe -> pi_messages++;

                /* Decrement the number of tasks waiting counter.  */
                pipe -> pi_tasks_waiting--;

                /* Remove the first suspended block from the list.  */
                CSC_Remove_From_List((CS_NODE **)
                    &(pipe -> pi_suspension_list),
                                &(suspend_ptr -> pi_suspend_link));

                /* Return a successful status.  */
                suspend_ptr -> pi_return_status =  NU_SUCCESS;

                /* Resume the suspended task.  */
                preempt =  preempt |
                 TCC_Resume_Task((NU_TASK *) suspend_ptr -> pi_suspended_task,
                                                       NU_PIPE_SUSPEND);

                /* Setup suspend pointer to the head of the list.  */
                suspend_ptr =  pipe -> pi_suspension_list;

                /* Determine if there really is another suspended block.  If
                   there is and the pipe supports variable length messages,
                   calculate new size and padding parameters.  */
                if ((suspend_ptr) && (!pipe -> pi_fixed_size))
                {

                    /* Get the next message size.  */
                    size =   suspend_ptr -> pi_message_size;

                    /* Variable messages have one additional word of
                       overhead.  */
                    i =  sizeof(UNSIGNED);

                    /* Calculate the number of pad bytes necessary to
                       keep the pipe write pointer on an UNSIGNED data element
                       alignment.  */
                    pad =  (((size + sizeof(UNSIGNED) - 1)/sizeof(UNSIGNED)) *
                                          sizeof(UNSIGNED)) - size;

                    /* Insure that padding is included in the overhead.  */
                    i =  i + ((INT) pad);
                }
            }

            /* Determine if a preempt condition is present.  */
            if (preempt)

                /* Transfer control to the system if the resumed task function
                   detects a preemption condition.  */
                TCT_Control_To_System();
        }
    }
    else
    {

        /* pipe is empty.  Determine if the task wants to suspend.  */
        if (suspend)
        {

            /* Increment the number of tasks waiting on the pipe counter. */
            pipe -> pi_tasks_waiting++;

#ifdef NU_PROFILE_PLUS

            PRF_PLUS_PIC_RECEIVE_FROM_PIPE_1

#endif /* NU_PROFILE_PLUS */

            /* Setup the suspend block and suspend the calling task.  */
            suspend_ptr =  &suspend_block;
            suspend_ptr -> pi_pipe =                     pipe;
            suspend_ptr -> pi_suspend_link.cs_next =     NU_NULL;
            suspend_ptr -> pi_suspend_link.cs_previous = NU_NULL;
            suspend_ptr -> pi_message_area =             (BYTE_PTR) message;
            suspend_ptr -> pi_message_size =             size;
            task =                            (TC_TCB *) TCT_Current_Thread();
            suspend_ptr -> pi_suspended_task =           task;

            /* Determine if priority or FIFO suspension is associated with the
               pipe.  */
            if (pipe -> pi_fifo_suspend)
            {

                /* FIFO suspension is required.  Link the suspend block into
                   the list of suspended tasks on this pipe.  */
                CSC_Place_On_List((CS_NODE **) &(pipe -> pi_suspension_list),
                                        &(suspend_ptr -> pi_suspend_link));
            }
            else
            {

                /* Get the priority of the current thread so the suspend block
                   can be placed in the appropriate place.  */
                suspend_ptr -> pi_suspend_link.cs_priority =
                                                    TCC_Task_Priority(task);

                CSC_Priority_Place_On_List((CS_NODE **)
                            &(pipe -> pi_suspension_list),
                                            &(suspend_ptr -> pi_suspend_link));
            }

            /* Finally, suspend the calling task. Note that the suspension call
               automatically clears the protection on the pipe.  */
            TCC_Suspend_Task((NU_TASK *) task, NU_PIPE_SUSPEND,
                                        PIC_Cleanup, suspend_ptr, suspend);

            /* Pickup the status of the request.  */
            status =  suspend_ptr -> pi_return_status;
            *actual_size =  suspend_ptr -> pi_actual_size;
        }
        else
        {

            /* Return a status of NU_PIPE_EMPTY because there are no
               messages in the pipe.  */
            status =  NU_PIPE_EMPTY;

#ifdef NU_PROFILE_PLUS

            PRF_PLUS_PIC_RECEIVE_FROM_PIPE_2

#endif /* NU_PROFILE_PLUS */

        }
    }

    /* Release protection against access to the pipe.  */
    TCT_Unprotect();

    /* Return to user mode */
    NU_USER_MODE();

    /* Return the completion status.  */
    return(status);
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      PIC_Cleanup                                                      */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is responsible for removing a suspension block     */
/*      from a pipe.  It is not called unless a timeout or a task        */
/*      terminate is in progress.  Note that protection is already in    */
/*      effect - the same protection at suspension time.  This routine   */
/*      must be called from Supervisor mode in Supervisor/User mode      */
/*      switching kernels.                                               */
/*                                                                       */
/* CALLED BY                                                             */
/*                                                                       */
/*      TCC_Timeout                         Task timeout                 */
/*      TCC_Terminate                       Task terminate               */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*      CSC_Remove_From_List                Remove suspend block from    */
/*                                            the suspension list        */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      information                         Pointer to suspend block     */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/*************************************************************************/
VOID  PIC_Cleanup(VOID *information)
{

PI_SUSPEND      *suspend_ptr;               /* Suspension block pointer  */
NU_SUPERV_USER_VARIABLES

    /* Switch to supervisor mode */
    NU_SUPERVISOR_MODE();

    /* Use the information pointer as a suspend pointer.  */
    suspend_ptr =  (PI_SUSPEND *) information;

    /* By default, indicate that the service timed-out.  It really does not
       matter if this function is called from a terminate request since
       the task does not resume.  */
    suspend_ptr -> pi_return_status =  NU_TIMEOUT;

    /* Decrement the number of tasks waiting counter.  */
    (suspend_ptr -> pi_pipe) -> pi_tasks_waiting--;

    /* Determine if the suspend block is one the urgent list. */
    if ((suspend_ptr -> pi_pipe) -> pi_urgent_list)
    {
        /* Unlink the suspend block from the urgent list.  */
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -