openbcm

Git mirror of https://github.com/Broadcom-Network-Switching-Software/OpenBCM
git clone git://git.finwo.net/mirror/broadcom/openbcm
Log | Files | Refs | README

protect.c (2283B)


      1 /*
      2  * 
      3  * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file.
      4  * 
      5  * Copyright 2007-2019 Broadcom Inc. All rights reserved.
      6  *
      7  * File:    protect.c
      8  *
      9  * Purpose: 
     10  *
     11  * Functions:
     12  *      _bcm_ptp_intr_context
     13  *      _bcm_ptp_mutex_create
     14  *      _bcm_ptp_mutex_destroy
     15  *      _bcm_ptp_mutex_take
     16  *      _bcm_ptp_mutex_give
     17  *      _bcm_ptp_sem_create
     18  *      _bcm_ptp_sem_destroy
     19  *      _bcm_ptp_sem_take
     20  *      _bcm_ptp_sem_give
     21  */
     22 
     23 #if defined(INCLUDE_PTP)
     24 
     25 /* ---------------------------------------------------------------------------------------------- */
     26 /* ABSTRACT: Allow work around for broken SAL semaphore and mutex issues                          */
     27 /* ---------------------------------------------------------------------------------------------- */
     28 
     29 #include <sal/core/time.h>
     30 
     31 #include <bcm_int/common/ptp.h>
     32 
     33 /* ============================= */
     34 /* Mutex functions               */
     35 /* ============================= */
     36 
     37 _bcm_ptp_mutex_t
     38 _bcm_ptp_mutex_create(char *desc)
     39 {
     40     return sal_mutex_create(desc);
     41 }
     42 
     43 void
     44 _bcm_ptp_mutex_destroy(_bcm_ptp_mutex_t m)
     45 {
     46     sal_mutex_destroy(m);
     47 }
     48 
     49 int
     50 _bcm_ptp_mutex_take(_bcm_ptp_mutex_t m, int usec)
     51 {
     52     int rv = -1;
     53     sal_usecs_t wait_time = sal_time_usecs() + usec;
     54 
     55     while ((rv != 0) && (int32)(wait_time - sal_time_usecs()) > 0) {
     56         rv = sal_mutex_take(m, wait_time - sal_time_usecs());
     57     }
     58 
     59     return rv;
     60 }
     61 
     62 int
     63 _bcm_ptp_mutex_give(_bcm_ptp_mutex_t m)
     64 {
     65     return sal_mutex_give(m);
     66 }
     67 
     68 
     69 /* ============================= */
     70 /* Semaphore functions           */
     71 /* ============================= */
     72 
     73 _bcm_ptp_sem_t
     74 _bcm_ptp_sem_create(char *desc, int binary, int initial_count)
     75 {
     76     return sal_sem_create(desc, binary, initial_count);
     77 }
     78 
     79 void
     80 _bcm_ptp_sem_destroy(_bcm_ptp_sem_t b)
     81 {
     82     sal_sem_destroy(b);
     83 }
     84 
     85 int
     86 _bcm_ptp_sem_take(_bcm_ptp_sem_t b, int usec)
     87 {
     88     int rv = -1;
     89     sal_usecs_t end_time = sal_time_usecs() + usec;
     90     int32 wait_time = usec;
     91 
     92     while ((rv != 0) && (wait_time > 0)) {
     93         rv = sal_sem_take(b, wait_time);
     94         wait_time = end_time - sal_time_usecs();
     95     }
     96 
     97     return rv;
     98 }
     99 
    100 int
    101 _bcm_ptp_sem_give(_bcm_ptp_sem_t b)
    102 {
    103     return sal_sem_give(b);
    104 }
    105 
    106 #endif /* INCLUDE_PTP */