blob: ad9468a8fa301a0b58bd41604069eecae6c1ca29 (
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2026-present Eclipse ThreadX contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** 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_open PORTABLE C */
/* 6.2.0 */
/* AUTHOR */
/* */
/* William E. Lamie, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function creates/initialize a semaphore. */
/* */
/* INPUT */
/* */
/* name Semaphore name */
/* oflags Flags */
/* mode Optional parameter */
/* value Optional parameter */
/* */
/* OUTPUT */
/* */
/* sem Semaphore descriptor */
/* */
/* CALLS */
/* */
/* posix_in_thread_context Make sure caller is thread */
/* posix_find_sem Finds the required semaphore */
/* posix_get_new_sem Get new semaphore block */
/* posix_internal_error Internal posix error */
/* tx_semaphore_create ThreadX semaphore create */
/* posix_set_sem_name Sets name for semaphore */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
sem_t * sem_open(const CHAR * name, ULONG oflag, ...)
{
TX_INTERRUPT_SAVE_AREA
TX_SEMAPHORE *TheSem;
sem_t *semid;
ULONG retval;
ULONG len;
sem_t *posix_sem;
va_list vl;
UINT value;
mode_t mode;
/* Make sure we're calling this routine from a thread context. */
if (!posix_in_thread_context())
{
/* return POSIX error. */
posix_internal_error(444);
/* return error. */
return (( sem_t * )SEM_FAILED);
}
/* Find length of the name. The actual length is not known yet. */
len = strlen(name);
if(len > SEM_NAME_MAX)
{
/* Set appropriate errno. */
posix_errno = ENAMETOOLONG;
posix_set_pthread_errno(ENAMETOOLONG);
/* Return ERROR. */
return (( sem_t * ) SEM_FAILED);
}
/* Check if semaphore exists. */
if((semid = posix_find_sem(name)) != NULL)
{
if(semid->unlink_flag ==TX_TRUE )
{
/* Return error. */
posix_errno = EINVAL;
posix_set_pthread_errno(EINVAL);
return(( sem_t * )SEM_FAILED);
}
}
/* Semaphore not found. */
if(!(semid))
{
/* Check if flag set is O_EXCL. */
if( oflag == O_EXCL )
{
/* Set error for sem does not exist and O_CREAT not set. */
posix_errno = ENOENT;
posix_set_pthread_errno(ENOENT);
/* Return the SEM_FAILED error. */
return(( sem_t * )SEM_FAILED);
}
if( (oflag == O_CREAT) || ( (oflag & (O_CREAT|O_EXCL )) == (O_CREAT|O_EXCL) ) )
{
/* Get the variable arguments pointers. */
va_start( vl, oflag );
mode = va_arg( vl, mode_t);
mode = mode; /* just to keep compiler happy */
value = va_arg( vl, ULONG);
if(value > SEM_VALUE_MAX)
{
/* Semaphore value large. */
posix_errno = EINVAL;
posix_set_pthread_errno(EINVAL);
/* Return ERROR */
return (( sem_t * )SEM_FAILED);
}
/* Disable interrupts. */
TX_DISABLE
/* Get a new semaphore from the POSIX semaphore pool. */
TheSem = posix_get_new_sem();
/* Make sure we actually got a semaphore. */
if (!TheSem)
{
/* Semaphore cannot be initialized due to resource constraint. */
posix_errno = ENOSPC;
posix_set_pthread_errno(ENOSPC);
/* Restore interrupts. */
TX_RESTORE
/* return appropriate error code. */
return(( sem_t * )SEM_FAILED);
}
/* Set the semaphore name. */
posix_set_sem_name((sem_t *)TheSem, ( CHAR * ) name);
/* Now actually create the ThreadX semaphore. */
posix_sem = (struct POSIX_SEMAPHORE_STRUCT *)TheSem;
retval = tx_semaphore_create(TheSem, ( CHAR * ) posix_sem->sem_name , value);
/* Make sure it worked. */
if (retval)
{
/* Return generic error. */
posix_internal_error(100);
/* Restore interrupts. */
TX_RESTORE
/* Return appropriate error code. */
return(( sem_t * )SEM_FAILED);
}
/* Add the calling thread to the thread list. */
posix_sem -> count += 1;
/* Set initial count */
posix_sem->refCnt = value;
/* Give the caller the semaphore ID. */
semid = (sem_t * )TheSem;
/* Restore interrupts. */
TX_RESTORE
/* All done. */
return(semid);
}
}
/* Semaphore found. */
if(semid)
{
/* Check if flags are O_CREAT|O_EXCL. */
if( (oflag == O_EXCL) || (oflag & (O_CREAT|O_EXCL )) == (O_CREAT|O_EXCL) )
{
/* Set appropriate errno. */
posix_errno = EEXIST;
posix_set_pthread_errno(EEXIST);
/* Return ERROR. */
return (( sem_t * ) SEM_FAILED);
}
/* Check if flag is only O_CREAT. */
if( (oflag == O_CREAT) || (oflag==0))
{
/* Disable interrupts. */
TX_DISABLE
/* Add the calling thread to the thread list. */
semid -> count++;
/* Restore interrupts. */
TX_RESTORE
/* Return semaphore. */
return (( sem_t * ) semid);
}
}
/* Semaphore value large. */
posix_errno = EINVAL;
posix_set_pthread_errno(EINVAL);
/* Return ERROR. */
return (( sem_t * ) SEM_FAILED);
}
|