Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:12

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  * 
0006  * @brief Private Inlined Routines for POSIX Semaphores
0007  *
0008  * This include file contains the static inline implementation of the private 
0009  * inlined routines for POSIX Semaphores.
0010  */
0011 
0012 /*
0013  *  COPYRIGHT (c) 1989-2013.
0014  *  On-Line Applications Research Corporation (OAR).
0015  *
0016  * Redistribution and use in source and binary forms, with or without
0017  * modification, are permitted provided that the following conditions
0018  * are met:
0019  * 1. Redistributions of source code must retain the above copyright
0020  *    notice, this list of conditions and the following disclaimer.
0021  * 2. Redistributions in binary form must reproduce the above copyright
0022  *    notice, this list of conditions and the following disclaimer in the
0023  *    documentation and/or other materials provided with the distribution.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #ifndef _RTEMS_POSIX_SEMAPHOREIMPL_H
0039 #define _RTEMS_POSIX_SEMAPHOREIMPL_H
0040 
0041 #include <rtems/posix/semaphore.h>
0042 #include <rtems/posix/posixapi.h>
0043 #include <rtems/score/semaphoreimpl.h>
0044 #include <rtems/seterr.h>
0045 
0046 #ifdef __cplusplus
0047 extern "C" {
0048 #endif
0049 
0050 /**
0051  * @brief This is a random number used to check if a semaphore object is
0052  * properly initialized.
0053  */
0054 #define POSIX_SEMAPHORE_MAGIC 0x5d367fe7UL
0055 
0056 static inline POSIX_Semaphore_Control *
0057   _POSIX_Semaphore_Allocate_unprotected( void )
0058 {
0059   return (POSIX_Semaphore_Control *)
0060     _Objects_Allocate_unprotected( &_POSIX_Semaphore_Information );
0061 }
0062 
0063 /**
0064  *  @brief POSIX Semaphore Free
0065  *
0066  *  This routine frees a semaphore control block to the
0067  *  inactive chain of free semaphore control blocks.
0068  */
0069 static inline void _POSIX_Semaphore_Free (
0070   POSIX_Semaphore_Control *the_semaphore
0071 )
0072 {
0073   _Objects_Free( &_POSIX_Semaphore_Information, &the_semaphore->Object );
0074 }
0075 
0076 static inline POSIX_Semaphore_Control *_POSIX_Semaphore_Get(
0077   sem_t *sem
0078 )
0079 {
0080   return RTEMS_CONTAINER_OF( sem, POSIX_Semaphore_Control, Semaphore );
0081 }
0082 
0083 static inline bool _POSIX_Semaphore_Is_named( const sem_t *sem )
0084 {
0085   return sem->_Semaphore._Queue._name != NULL;
0086 }
0087 
0088 static inline bool _POSIX_Semaphore_Is_busy( const sem_t *sem )
0089 {
0090   return sem->_Semaphore._Queue._heads != NULL;
0091 }
0092 
0093 static inline void _POSIX_Semaphore_Initialize(
0094   sem_t        *sem,
0095   const char   *name,
0096   unsigned int  value
0097 )
0098 {
0099   sem->_flags = (uintptr_t) sem ^ POSIX_SEMAPHORE_MAGIC;
0100   _Semaphore_Initialize_named( &sem->_Semaphore, name, value );
0101 }
0102 
0103 static inline void _POSIX_Semaphore_Destroy( sem_t *sem )
0104 {
0105   sem->_flags = 0;
0106   _Semaphore_Destroy( &sem->_Semaphore );
0107 }
0108 
0109 /**
0110  *  @brief POSIX Semaphore Delete
0111  *
0112  * This routine supports the sem_close and sem_unlink routines.
0113  */
0114 void _POSIX_Semaphore_Delete( POSIX_Semaphore_Control *the_semaphore );
0115 
0116 /**
0117  *  @brief POSIX Semaphore Namespace Remove
0118  */
0119 static inline void _POSIX_Semaphore_Namespace_remove (
0120   POSIX_Semaphore_Control *the_semaphore
0121 )
0122 {
0123   _Objects_Namespace_remove_string(
0124     &_POSIX_Semaphore_Information,
0125     &the_semaphore->Object
0126   );
0127 }
0128 
0129 static inline POSIX_Semaphore_Control *_POSIX_Semaphore_Get_by_name(
0130   const char                *name,
0131   size_t                    *name_length_p,
0132   Objects_Get_by_name_error *error
0133 )
0134 {
0135   return (POSIX_Semaphore_Control *) _Objects_Get_by_name(
0136     &_POSIX_Semaphore_Information,
0137     name,
0138     name_length_p,
0139     error
0140   );
0141 }
0142 
0143 #define POSIX_SEMAPHORE_VALIDATE_OBJECT( sem ) \
0144   do { \
0145     if ( \
0146       ( sem ) == NULL \
0147         || ( (uintptr_t) ( sem ) ^ POSIX_SEMAPHORE_MAGIC ) != ( sem )->_flags \
0148     ) { \
0149       rtems_set_errno_and_return_minus_one( EINVAL ); \
0150     } \
0151   } while ( 0 )
0152 
0153 #ifdef __cplusplus
0154 }
0155 #endif
0156 
0157 #endif
0158 /*  end of include file */