summaryrefslogtreecommitdiff
path: root/utility/rtos_compatibility_layers/posix/px_sem_trywait.c
blob: d15a208238b1f0a8634ec352a791ecd312201b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**************************************************************************/
/*                                                                        */
/*       Copyright (c) Microsoft Corporation. All rights reserved.        */
/*                                                                        */
/*       This software is licensed under the Microsoft Software License   */
/*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
/*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
/*       and in the root directory of this software.                      */
/*                                                                        */
/**************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */ 
/** POSIX wrapper for THREADX                                             */ 
/**                                                                       */
/**                                                                       */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/* Include necessary system files.  */

#include "tx_api.h"     /* Threadx API */
#include "pthread.h"    /* Posix API */
#include "px_int.h"     /* Posix helper functions */


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    sem_trywait                                         PORTABLE C      */
/*                                                           6.x          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This routine locks(takes) a semaphore if the semaphore is not       */
/*    currently locked.                                                   */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    *sem                      Pointer to Semaphore                      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    OK                        If successful                             */
/*    ERROR                     If error occurs                           */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    posix_internal_error      Returns generic error                     */
/*    tx_semaphore_get          ThreadX Semaphore get                     */
/*    tx_thread_identify        Returns currently running thread          */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  xx-xx-xxxx     William E. Lamie         Initial Version 6.x           */
/*                                                                        */
/**************************************************************************/
INT sem_trywait(sem_t * sem)
{

TX_SEMAPHORE          * TheSem;
INT                    retval;
INT                    pxerror;

     /* Make sure we're calling this routine from a thread context.  */
    if (!tx_thread_identify() )
    {
        /* No error in POSIX.  */
        posix_internal_error(242);

        /* Return error.  */
        return (ERROR);
    }

     /* Get ThreadX semaphore.  */
    TheSem = (TX_SEMAPHORE *)sem;

    /* Check for an invalid semaphore pointer.  */
    if ((!TheSem) || (TheSem -> tx_semaphore_id != TX_SEMAPHORE_ID))
    {
        /* error in POSIX.  */
        posix_errno = EINVAL;
	    posix_set_pthread_errno(EINVAL);
        return (EINVAL);
    }
    else
        retval = tx_semaphore_get(TheSem,TX_NO_WAIT);

    switch(retval)
    {

        case TX_SUCCESS :

            sem->refCnt -= 1;
            pxerror = OK;
            break;

        case TX_NO_INSTANCE:

            posix_errno = EAGAIN;
	        posix_set_pthread_errno(EAGAIN);
            pxerror = ERROR;
            break;

        case  TX_DELETED :
        case  TX_WAIT_ABORTED :
        case  TX_SEMAPHORE_ERROR :
        case  TX_WAIT_ERROR :

            /* No error in POSIX, give default.  */
            posix_errno = EINVAL;
	        posix_set_pthread_errno(EINVAL);
            pxerror = ERROR;
            break;
    }
    return(pxerror);
}