-
Notifications
You must be signed in to change notification settings - Fork 217
/
osapi-mutex.c
276 lines (227 loc) · 8.38 KB
/
osapi-mutex.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 United States Government as represented by
* the Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* \file osapi-mutex.c
* \ingroup shared
* \author [email protected]
*
* This file contains some of the OS APIs abstraction layer code
* that is shared/common across all OS-specific implementations.
*/
/****************************************************************************************
INCLUDE FILES
***************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*
* User defined include files
*/
#include "os-shared-idmap.h"
#include "os-shared-mutex.h"
/*
* Other OSAL public APIs used by this module
*/
#include "osapi-task.h"
/*
* Sanity checks on the user-supplied configuration
* The relevent OS_MAX limit should be defined and greater than zero
*/
#if !defined(OS_MAX_MUTEXES) || (OS_MAX_MUTEXES <= 0)
#error "osconfig.h must define OS_MAX_MUTEXES to a valid value"
#endif
/*
* Global data for the API
*/
enum
{
LOCAL_NUM_OBJECTS = OS_MAX_MUTEXES,
LOCAL_OBJID_TYPE = OS_OBJECT_TYPE_OS_MUTEX
};
OS_mutex_internal_record_t OS_mutex_table[LOCAL_NUM_OBJECTS];
/****************************************************************************************
MUTEX API
***************************************************************************************/
/*----------------------------------------------------------------
*
* Function: OS_MutexAPI_Init
*
* Purpose: Local helper routine, not part of OSAL API.
* Init function for OS-independent layer
*
*-----------------------------------------------------------------*/
int32 OS_MutexAPI_Init(void)
{
memset(OS_mutex_table, 0, sizeof(OS_mutex_table));
return OS_SUCCESS;
} /* end OS_MutexAPI_Init */
/*----------------------------------------------------------------
*
* Function: OS_MutSemCreate
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_MutSemCreate(osal_id_t *sem_id, const char *sem_name, uint32 options)
{
int32 return_code;
OS_object_token_t token;
OS_mutex_internal_record_t *mutex;
/* Check parameters */
OS_CHECK_POINTER(sem_id);
OS_CHECK_APINAME(sem_name);
/* Note - the common ObjectIdAllocate routine will lock the object type and leave it locked. */
return_code = OS_ObjectIdAllocateNew(LOCAL_OBJID_TYPE, sem_name, &token);
if (return_code == OS_SUCCESS)
{
mutex = OS_OBJECT_TABLE_GET(OS_mutex_table, token);
/* Reset the table entry and save the name */
OS_OBJECT_INIT(token, mutex, obj_name, sem_name);
/* Now call the OS-specific implementation. This reads info from the table. */
return_code = OS_MutSemCreate_Impl(&token, options);
/* Check result, finalize record, and unlock global table. */
return_code = OS_ObjectIdFinalizeNew(return_code, &token, sem_id);
}
return return_code;
} /* end OS_MutSemCreate */
/*----------------------------------------------------------------
*
* Function: OS_MutSemDelete
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_MutSemDelete(osal_id_t sem_id)
{
OS_object_token_t token;
int32 return_code;
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_EXCLUSIVE, LOCAL_OBJID_TYPE, sem_id, &token);
if (return_code == OS_SUCCESS)
{
return_code = OS_MutSemDelete_Impl(&token);
/* Complete the operation via the common routine */
return_code = OS_ObjectIdFinalizeDelete(return_code, &token);
}
return return_code;
} /* end OS_MutSemDelete */
/*----------------------------------------------------------------
*
* Function: OS_MutSemGive
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_MutSemGive(osal_id_t sem_id)
{
OS_mutex_internal_record_t *mutex;
OS_object_token_t token;
int32 return_code;
osal_id_t self_task;
/* Check Parameters */
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_NONE, LOCAL_OBJID_TYPE, sem_id, &token);
if (return_code == OS_SUCCESS)
{
mutex = OS_OBJECT_TABLE_GET(OS_mutex_table, token);
self_task = OS_TaskGetId();
if (!OS_ObjectIdEqual(mutex->last_owner, self_task))
{
OS_DEBUG("WARNING: Task %lu giving mutex %lu while owned by task %lu\n", OS_ObjectIdToInteger(self_task),
OS_ObjectIdToInteger(sem_id), OS_ObjectIdToInteger(mutex->last_owner));
}
mutex->last_owner = OS_OBJECT_ID_UNDEFINED;
return_code = OS_MutSemGive_Impl(&token);
}
return return_code;
} /* end OS_MutSemGive */
/*----------------------------------------------------------------
*
* Function: OS_MutSemTake
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_MutSemTake(osal_id_t sem_id)
{
OS_mutex_internal_record_t *mutex;
OS_object_token_t token;
int32 return_code;
/* Check Parameters */
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_NONE, LOCAL_OBJID_TYPE, sem_id, &token);
if (return_code == OS_SUCCESS)
{
mutex = OS_OBJECT_TABLE_GET(OS_mutex_table, token);
return_code = OS_MutSemTake_Impl(&token);
if (return_code == OS_SUCCESS)
{
/* Always set the owner if OS_MutSemTake_Impl() returned success */
mutex->last_owner = OS_TaskGetId();
}
}
return return_code;
} /* end OS_MutSemTake */
/*----------------------------------------------------------------
*
* Function: OS_MutSemGetIdByName
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_MutSemGetIdByName(osal_id_t *sem_id, const char *sem_name)
{
int32 return_code;
/* Check parameters */
OS_CHECK_POINTER(sem_id);
OS_CHECK_POINTER(sem_name);
return_code = OS_ObjectIdFindByName(LOCAL_OBJID_TYPE, sem_name, sem_id);
return return_code;
} /* end OS_MutSemGetIdByName */
/*----------------------------------------------------------------
*
* Function: OS_MutSemGetInfo
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_MutSemGetInfo(osal_id_t sem_id, OS_mut_sem_prop_t *mut_prop)
{
OS_common_record_t *record;
int32 return_code;
OS_object_token_t token;
/* Check parameters */
OS_CHECK_POINTER(mut_prop);
memset(mut_prop, 0, sizeof(OS_mut_sem_prop_t));
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, sem_id, &token);
if (return_code == OS_SUCCESS)
{
record = OS_OBJECT_TABLE_GET(OS_global_mutex_table, token);
strncpy(mut_prop->name, record->name_entry, sizeof(mut_prop->name) - 1);
mut_prop->creator = record->creator;
return_code = OS_MutSemGetInfo_Impl(&token, mut_prop);
OS_ObjectIdRelease(&token);
}
return return_code;
} /* end OS_MutSemGetInfo */