1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @brief Private Support Information for POSIX Shared Memory |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * Copyright (c) 2016 Gedare Bloom. |
---|
12 | * |
---|
13 | * Redistribution and use in source and binary forms, with or without |
---|
14 | * modification, are permitted provided that the following conditions |
---|
15 | * are met: |
---|
16 | * 1. Redistributions of source code must retain the above copyright |
---|
17 | * notice, this list of conditions and the following disclaimer. |
---|
18 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer in the |
---|
20 | * documentation and/or other materials provided with the distribution. |
---|
21 | * |
---|
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
32 | * POSSIBILITY OF SUCH DAMAGE. |
---|
33 | */ |
---|
34 | |
---|
35 | #ifndef _RTEMS_POSIX_SHMIMPL_H |
---|
36 | #define _RTEMS_POSIX_SHMIMPL_H |
---|
37 | |
---|
38 | #include <rtems/fs.h> |
---|
39 | #include <rtems/libio.h> |
---|
40 | #include <rtems/posix/posixapi.h> |
---|
41 | #include <rtems/posix/shm.h> |
---|
42 | #include <rtems/score/objectimpl.h> |
---|
43 | |
---|
44 | #ifdef __cplusplus |
---|
45 | extern "C" { |
---|
46 | #endif |
---|
47 | |
---|
48 | /** |
---|
49 | * @ingroup POSIXShmPrivate |
---|
50 | * @{ |
---|
51 | */ |
---|
52 | |
---|
53 | static inline POSIX_Shm_Control *_POSIX_Shm_Allocate_unprotected( void ) |
---|
54 | { |
---|
55 | return (POSIX_Shm_Control *) |
---|
56 | _Objects_Allocate_unprotected( &_POSIX_Shm_Information ); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * @brief POSIX Shared Memory Free |
---|
61 | * |
---|
62 | * This routine frees a shm control block. |
---|
63 | */ |
---|
64 | static inline void _POSIX_Shm_Free ( |
---|
65 | POSIX_Shm_Control *the_shm |
---|
66 | ) |
---|
67 | { |
---|
68 | _Objects_Free( &_POSIX_Shm_Information, &the_shm->Object ); |
---|
69 | } |
---|
70 | |
---|
71 | static inline POSIX_Shm_Control *_POSIX_Shm_Get_by_name( |
---|
72 | const char *name, |
---|
73 | size_t *name_length_p, |
---|
74 | Objects_Get_by_name_error *error |
---|
75 | ) |
---|
76 | { |
---|
77 | return (POSIX_Shm_Control *) _Objects_Get_by_name( |
---|
78 | &_POSIX_Shm_Information, |
---|
79 | name, |
---|
80 | name_length_p, |
---|
81 | error |
---|
82 | ); |
---|
83 | } |
---|
84 | |
---|
85 | static inline void _POSIX_Shm_Update_atime( |
---|
86 | POSIX_Shm_Control *shm |
---|
87 | ) |
---|
88 | { |
---|
89 | struct timeval now; |
---|
90 | gettimeofday( &now, 0 ); |
---|
91 | shm->atime = now.tv_sec; |
---|
92 | } |
---|
93 | |
---|
94 | static inline void _POSIX_Shm_Update_mtime_ctime( |
---|
95 | POSIX_Shm_Control *shm |
---|
96 | ) |
---|
97 | { |
---|
98 | struct timeval now; |
---|
99 | gettimeofday( &now, 0 ); |
---|
100 | shm->mtime = now.tv_sec; |
---|
101 | shm->ctime = now.tv_sec; |
---|
102 | } |
---|
103 | |
---|
104 | static inline POSIX_Shm_Control* iop_to_shm( rtems_libio_t *iop ) |
---|
105 | { |
---|
106 | return (POSIX_Shm_Control*) iop->data1; |
---|
107 | } |
---|
108 | |
---|
109 | static inline POSIX_Shm_Control* loc_to_shm( |
---|
110 | const rtems_filesystem_location_info_t *loc |
---|
111 | ) |
---|
112 | { |
---|
113 | return (POSIX_Shm_Control*) loc->node_access; |
---|
114 | } |
---|
115 | |
---|
116 | static inline int POSIX_Shm_Attempt_delete( |
---|
117 | POSIX_Shm_Control* shm |
---|
118 | ) |
---|
119 | { |
---|
120 | Objects_Control *obj; |
---|
121 | ISR_lock_Context lock_ctx; |
---|
122 | int err; |
---|
123 | |
---|
124 | err = 0; |
---|
125 | |
---|
126 | _Objects_Allocator_lock(); |
---|
127 | --shm->reference_count; |
---|
128 | if ( shm->reference_count == 0 ) { |
---|
129 | if ( (*shm->shm_object.ops->object_delete)( &shm->shm_object ) != 0 ) { |
---|
130 | err = EIO; |
---|
131 | } |
---|
132 | } |
---|
133 | /* check if the object has been unlinked yet. */ |
---|
134 | obj = _Objects_Get( shm->Object.id, &lock_ctx, &_POSIX_Shm_Information ); |
---|
135 | if ( obj == NULL ) { |
---|
136 | /* if it was unlinked, then it can be freed. */ |
---|
137 | _POSIX_Shm_Free( shm ); |
---|
138 | } else { |
---|
139 | /* it will be freed when it is unlinked. */ |
---|
140 | _ISR_lock_ISR_enable( &lock_ctx ); |
---|
141 | } |
---|
142 | _Objects_Allocator_unlock(); |
---|
143 | return err; |
---|
144 | } |
---|
145 | |
---|
146 | /** @} */ |
---|
147 | |
---|
148 | #ifdef __cplusplus |
---|
149 | } |
---|
150 | #endif |
---|
151 | |
---|
152 | #endif |
---|