File indexing completed on 2025-05-11 08:24:22
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 #ifdef HAVE_CONFIG_H
0038 #include "config.h"
0039 #endif
0040
0041 #include <rtems/posix/semaphoreimpl.h>
0042 #include <rtems/score/wkspace.h>
0043 #include <rtems/sysinit.h>
0044
0045 #include <stdarg.h>
0046 #include <fcntl.h>
0047 #include <limits.h>
0048
0049 static sem_t *_POSIX_Semaphore_Create_support(
0050 const char *name_arg,
0051 size_t name_len,
0052 unsigned int value
0053 )
0054 {
0055 POSIX_Semaphore_Control *the_semaphore;
0056 char *name;
0057
0058 if ( value > SEM_VALUE_MAX ) {
0059 rtems_set_errno_and_return_value( EINVAL, SEM_FAILED );
0060 }
0061
0062
0063
0064
0065
0066 name = _Workspace_String_duplicate( name_arg, name_len );
0067 if ( name == NULL ) {
0068 rtems_set_errno_and_return_value( ENOMEM, SEM_FAILED );
0069 }
0070
0071 the_semaphore = _POSIX_Semaphore_Allocate_unprotected();
0072 if ( the_semaphore == NULL ) {
0073 _Workspace_Free( name );
0074 rtems_set_errno_and_return_value( ENOSPC, SEM_FAILED );
0075 }
0076
0077 the_semaphore->open_count = 1;
0078 the_semaphore->linked = true;
0079
0080
0081
0082
0083
0084
0085
0086
0087 _POSIX_Semaphore_Initialize( &the_semaphore->Semaphore, name, value );
0088
0089
0090
0091
0092 _Objects_Open_string(
0093 &_POSIX_Semaphore_Information,
0094 &the_semaphore->Object,
0095 name
0096 );
0097
0098 return &the_semaphore->Semaphore;
0099 }
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 sem_t *sem_open(
0113 const char *name,
0114 int oflag,
0115 ...
0116
0117
0118 )
0119 {
0120
0121
0122
0123
0124
0125
0126 mode_t mode RTEMS_UNUSED;
0127
0128 va_list arg;
0129 unsigned int value = 0;
0130 POSIX_Semaphore_Control *the_semaphore;
0131 size_t name_len;
0132 Objects_Get_by_name_error error;
0133 sem_t *sem;
0134
0135 if ( oflag & O_CREAT ) {
0136 va_start(arg, oflag);
0137 mode = va_arg( arg, mode_t );
0138 value = va_arg( arg, unsigned int );
0139 va_end(arg);
0140 }
0141
0142 _Objects_Allocator_lock();
0143 the_semaphore = _POSIX_Semaphore_Get_by_name( name, &name_len, &error );
0144
0145
0146
0147
0148
0149
0150
0151
0152 if ( the_semaphore == NULL ) {
0153
0154
0155
0156
0157
0158
0159 if ( !( error == OBJECTS_GET_BY_NAME_NO_OBJECT && (oflag & O_CREAT) ) ) {
0160 _Objects_Allocator_unlock();
0161 rtems_set_errno_and_return_value(
0162 _POSIX_Get_by_name_error( error ),
0163 SEM_FAILED
0164 );
0165 }
0166 } else {
0167
0168
0169
0170
0171
0172 if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) {
0173 _Objects_Allocator_unlock();
0174 rtems_set_errno_and_return_value( EEXIST, SEM_FAILED );
0175 }
0176
0177 the_semaphore->open_count += 1;
0178 _Objects_Allocator_unlock();
0179 return &the_semaphore->Semaphore;
0180 }
0181
0182
0183
0184
0185
0186
0187 sem = _POSIX_Semaphore_Create_support(
0188 name,
0189 name_len,
0190 value
0191 );
0192
0193 _Objects_Allocator_unlock();
0194 return sem;
0195 }
0196
0197 static void _POSIX_Semaphore_Manager_initialization( void )
0198 {
0199 _Objects_Initialize_information( &_POSIX_Semaphore_Information );
0200 }
0201
0202 RTEMS_SYSINIT_ITEM(
0203 _POSIX_Semaphore_Manager_initialization,
0204 RTEMS_SYSINIT_POSIX_SEMAPHORE,
0205 RTEMS_SYSINIT_ORDER_MIDDLE
0206 );