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

📄 os_sema.c

📁 Usb Host/Periphel Control TD1120 codes
💻 C
字号:
/*------------------------------------------------------------------------------
$File: //hodad/usblink/3.4/source/os/linux_2_4_18/os_sema.c $
$DateTime: 2003/11/14 17:19:59 $
$Revision: #2 $
Purpose: This file contains the Semaphore library abstraction for USBLink.
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
CONFIDENTIAL AND PROPRIETARY INFORMATION OF SOFTCONNEX TECHNOLOGIES, INC.

THIS NOTICE IS NOT TO BE DELETED, MODIFIED, MOVED OR CHANGED IN ANY WAY.

Copyright (c) 1999 - 2003 by SoftConnex Technologies, Inc.

This software is protected by copyright laws and international copyright
treaties, as well as other intellectual property laws and treaties.  This
software is a CONFIDENTIAL, unpublished work of authorship, and with portions
constituting TRADE SECRETS of SoftConnex Technologies, Inc., a Delaware USA
corporation.  Any unauthorized use, disclosure, and/or reproduction of this
software, or any part of this software; or distribution of this software in any
form or by any means; or storage of this software in any database or retrieval
system, without the express written consent of, and license from, SoftConnex
Technologies, Inc. is strictly prohibited.  This software is protected under the
copyright and/or trade secret laws in other countries in addition to USA.  All
Rights Reserved.  Failure to abide by the use, disclosure and/or reproduction
restrictions may result in civil and /or criminal penalties, and will be
prosecuted to the maximum extent of the law.
------------------------------------------------------------------------------*/
#include "usblink.h"
#include <linux/sched.h>


/*------------------------------------------------------------------------------
Name      : OS_SemaphoreCreate
Purpose   : Create a binary semaphore.
Arguments : None.
Returns   : The handle to the semaphore or 0 if the semaphore could not be
            created.
Notes     : The semaphore is created in the Empty State.
------------------------------------------------------------------------------*/
SctSemaphore OS_SemaphoreCreate(void)
   {
   S32 * semaphore = OS_Malloc(sizeof(S32));
   if (semaphore == NULL)
      {
      PRINT1("(OS_SemaphoreCreate) ERROR: "
         "Could not allocate semaphore.\n");
      }
   *semaphore = 0;
   return(semaphore);
   }


/*------------------------------------------------------------------------------
Name      : OS_SemaphoreDelete
Purpose   : None.
Arguments : None.
Returns   : None.
Notes     : None.
------------------------------------------------------------------------------*/
SctStatus OS_SemaphoreDelete(SctSemaphore semaphore)
   {
   OS_Free(semaphore);
   return(SCS_SUCCESS);
   }


/*------------------------------------------------------------------------------
Name      : OS_SemaphoreSignal
Purpose   : None.
Arguments : None.
Returns   : None.
Notes     : None.
------------------------------------------------------------------------------*/
SctStatus OS_SemaphoreSignal(SctSemaphore semaphore)
   {
   S32 semval;

   /*Operate on semaphore 0, return 1 resource, don't wait*/
   OS_DisableInterrupts(SCC_MASK_ALL_INTERRUPTS);
   semval = *semaphore;

   if (semval == 1)
      {
      /*PRINTI3("(OS_SemaphoreSignal) Semaphore is already signaled.\n");*/
      OS_EnableInterrupts(SCC_MASK_ALL_INTERRUPTS);
      return(SCS_SUCCESS);
      }
   else
      {
      (*semaphore)++;
      OS_EnableInterrupts(SCC_MASK_ALL_INTERRUPTS);
      return(SCS_SUCCESS);
      }
   }


/*------------------------------------------------------------------------------
Name      : OS_SemaphoreWait
Purpose   : Wait for a semaphore for a specified amount of time.
Arguments : Semaphore - The handle to the semaphore.
            TimeoutMS - The time to wait for the semaphore before returning an
                        error. SC_SEMA_WAIT_FOREVER will block the thread until
                        the semaphore is signaled with no timeout.
Returns   : SCS_SUCCESS is returned if the semaphore was taken before the
            timeout expired. SCS_ERROR_SEMAPHORE_TIMEOUT if the semaphore timed
            out before the semaphore could be taken.
Notes     : When semTake returns error it could also mean invalid semaphore.
            In that case, the error would be mistaken for a timeout.
------------------------------------------------------------------------------*/
SctStatus OS_SemaphoreWait(SctSemaphore semaphore, U32 timeoutMs)
   {
   U32 waitJiffies;
   SctStatus status;

   /*Operate on semaphore 0, take 1 resource, wait*/

   
      {
      /*Most OSes have timeout functionality built in and while there are
        many ways to build this in Linux/Sys V; using the schedule() function
        appears to be the most thread library and kernel
        version portable so while this looks like we're polling, we're
        really not (described in Rubini's book.)*/

      waitJiffies = ((timeoutMs + MILLISECONDS_PER_TICK - 1) /
                      MILLISECONDS_PER_TICK) + jiffies;

      while ((jiffies < waitJiffies)||(timeoutMs == SCC_SEMA_WAIT_FOREVER))
         {
         if (*semaphore == 1)
            {
            status = SCS_SUCCESS;
            (*semaphore)--;
            }
         else
            {
            status = 1;
            }

         if (status == SCS_SUCCESS)
            {
            return(status);
            }
         else
            {
            schedule();
            }
         }
      return(SCS_ERROR_SEMAPHORE_TIMEOUT);
      }
   }

⌨️ 快捷键说明

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