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 Support Information for POSIX Shared Memory
0007  *
0008  */
0009 
0010 /*
0011  * Copyright (c) 2016 Gedare Bloom.
0012  *
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #ifndef _RTEMS_POSIX_SHMIMPL_H
0036 #define _RTEMS_POSIX_SHMIMPL_H
0037 
0038 #include <rtems/fs.h>
0039 #include <rtems/libio.h>
0040 #include <rtems/posix/posixapi.h>
0041 #include <rtems/posix/shm.h>
0042 #include <rtems/score/objectimpl.h>
0043 
0044 #ifdef __cplusplus
0045 extern "C" {
0046 #endif
0047 
0048 /**
0049  * @ingroup POSIXShmPrivate
0050  * @{
0051  */
0052 
0053 static inline POSIX_Shm_Control *_POSIX_Shm_Allocate_unprotected( void )
0054 {
0055   return (POSIX_Shm_Control *)
0056     _Objects_Allocate_unprotected( &_POSIX_Shm_Information );
0057 }
0058 
0059 /**
0060  * @brief POSIX Shared Memory Free
0061  *
0062  * This routine frees a shm control block.
0063  */
0064 static inline void _POSIX_Shm_Free (
0065   POSIX_Shm_Control *the_shm
0066 )
0067 {
0068   _Objects_Free( &_POSIX_Shm_Information, &the_shm->Object );
0069 }
0070 
0071 static inline POSIX_Shm_Control *_POSIX_Shm_Get_by_name(
0072   const char                *name,
0073   size_t                    *name_length_p,
0074   Objects_Get_by_name_error *error
0075 )
0076 {
0077   return (POSIX_Shm_Control *) _Objects_Get_by_name(
0078     &_POSIX_Shm_Information,
0079     name,
0080     name_length_p,
0081     error
0082   );
0083 }
0084 
0085 static inline void _POSIX_Shm_Update_atime(
0086   POSIX_Shm_Control *shm
0087 )
0088 {
0089   struct timeval now;
0090   gettimeofday( &now, 0 );
0091   shm->atime = now.tv_sec;
0092 }
0093 
0094 static inline void _POSIX_Shm_Update_mtime_ctime(
0095   POSIX_Shm_Control *shm
0096 )
0097 {
0098   struct timeval now;
0099   gettimeofday( &now, 0 );
0100   shm->mtime = now.tv_sec;
0101   shm->ctime = now.tv_sec;
0102 }
0103 
0104 static inline POSIX_Shm_Control* iop_to_shm( rtems_libio_t *iop )
0105 {
0106   return (POSIX_Shm_Control*) iop->data1;
0107 }
0108 
0109 static inline POSIX_Shm_Control* loc_to_shm(
0110     const rtems_filesystem_location_info_t *loc
0111 )
0112 {
0113   return (POSIX_Shm_Control*) loc->node_access;
0114 }
0115 
0116 static inline int POSIX_Shm_Attempt_delete(
0117     POSIX_Shm_Control* shm
0118 )
0119 {
0120   Objects_Control       *obj;
0121   ISR_lock_Context       lock_ctx;
0122   int err;
0123 
0124   err = 0;
0125 
0126   _Objects_Allocator_lock();
0127   --shm->reference_count;
0128   if ( shm->reference_count == 0 ) {
0129     if ( (*shm->shm_object.ops->object_delete)( &shm->shm_object ) != 0 ) {
0130       err = EIO;
0131     }
0132   }
0133   /* check if the object has been unlinked yet. */
0134   obj = _Objects_Get( shm->Object.id, &lock_ctx, &_POSIX_Shm_Information );
0135   if ( obj == NULL ) {
0136     /* if it was unlinked, then it can be freed. */
0137     _POSIX_Shm_Free( shm );
0138   } else {
0139     /* it will be freed when it is unlinked. */
0140     _ISR_lock_ISR_enable( &lock_ctx );
0141   }
0142   _Objects_Allocator_unlock();
0143   return err;
0144 }
0145 
0146 /** @} */
0147 
0148 #ifdef __cplusplus
0149 }
0150 #endif
0151 
0152 #endif