-
Notifications
You must be signed in to change notification settings - Fork 2
/
pte_generic_osal.h
468 lines (407 loc) · 12.8 KB
/
pte_generic_osal.h
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* pte_cancellable_wait.c
*
* Description:
*
* --------------------------------------------------------------------------
*
* Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
* Copyright(C) 2008 Jason Schmidlapp
*
* Contact Email: [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library in the file COPYING.LIB;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifndef _GENERIC_OS_SUPPORT_H_
#define _GENERIC_OS_SUPPORT_H_
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/** @name Misc */
//@{
typedef enum pte_osResult
{
/** Operation completed successfully */
PTE_OS_OK = 0,
/** Operation failed because there insufficient resources */
PTE_OS_NO_RESOURCES,
/** Operation failed due to a general failure */
PTE_OS_GENERAL_FAILURE,
/** Operation did not complete because a user specified timeout expired. */
PTE_OS_TIMEOUT,
/** The operation was interrupted before it could complete. */
PTE_OS_INTERRUPTED,
/** An invalid parameter was specified */
PTE_OS_INVALID_PARAM
} pte_osResult;
/**
* Provides a hook for the OSAL to implement any OS specific initialization. This is guaranteed to be
* called before any other OSAL function.
*/
pte_osResult pte_osInit(void);
//@}
/** @name Mutexes */
//@{
/**
* Creates a mutex
*
* @param pHandle Set to the handle of the newly created mutex.
*
* @return PTE_OS_OK - Mutex successfully created
* @return PTE_OS_NO_RESOURCESs - Insufficient resources to create mutex
*/
pte_osResult pte_osMutexCreate(pte_osMutexHandle *pHandle);
/**
* Deletes a mutex and frees any associated resources.
*
* @param handle Handle of mutex to delete.
*
* @return PTE_OS_OK - Mutex successfully deleted.
*/
pte_osResult pte_osMutexDelete(pte_osMutexHandle handle);
/**
* Locks the mutex
*
* @param handle Handle of mutex to lock.
*
* @return PTE_OS_OK - Mutex successfully locked.
*/
pte_osResult pte_osMutexLock(pte_osMutexHandle handle);
/**
* Locks the mutex, returning after @p timeoutMsecs if the resources is not
* available. Can be used for polling mutex by using @p timeoutMsecs of zero.
*
* @param handle Handle of mutex to lock.
* @param timeoutMsecs Number of milliseconds to wait for resource before returning.
*
* @return PTE_OS_OK - Mutex successfully locked.
* @return PTE_OS_TIMEOUT - Timeout expired before lock was obtained.
*/
pte_osResult pte_osMutexTimedLock(pte_osMutexHandle handle, unsigned int timeoutMsecs);
/**
* Unlocks the mutex
*
* @param handle Handle of mutex to unlock
*
* @return PTE_OS_OK - Mutex successfully unlocked.
*/
pte_osResult pte_osMutexUnlock(pte_osMutexHandle handle);
//@}
/** @name Threads */
//@{
typedef int (*pte_osThreadEntryPoint)(void *params);
/**
* Creates a new thread. The thread must be started in a suspended state - it will be
* explicitly started when pte_osThreadStart() is called.
*
* @param entryPoint Entry point to the new thread.
* @param stackSize The initial stack size, in bytes. Note that this can be considered a minimum -
* for instance if the OS requires a larger stack space than what the caller specified.
* @param initialPriority The priority that the new thread should be initially set to.
* @param argv Parameter to pass to the new thread.
* @param ppte_osThreadHandle set to the handle of the new thread.
*
* @return PTE_OS_OK - New thread successfully created.
* @return PTE_OS_NO_RESOURCESs - Insufficient resources to create thread
*/
pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint,
int stackSize,
int initialPriority,
void *argv,
pte_osThreadHandle* ppte_osThreadHandle);
/**
* Starts executing the specified thread.
*
* @param osThreadHandle handle of the thread to start.
*
* @return PTE_OS_OK - thread successfully started.
*/
pte_osResult pte_osThreadStart(pte_osThreadHandle osThreadHandle);
/**
* Causes the current thread to stop executing.
*
* @return Never returns (thread terminated)
*/
void pte_osThreadExit();
/**
* Waits for the specified thread to end. If the thread has already terminated, this returns
* immediately.
*
* @param threadHandle Handle fo thread to wait for.
*
* @return PTE_OS_OK - specified thread terminated.
*/
pte_osResult pte_osThreadWaitForEnd(pte_osThreadHandle threadHandle);
/**
* Returns the handle of the currently executing thread.
*/
pte_osThreadHandle pte_osThreadGetHandle(void);
/**
* Returns the priority of the specified thread.
*/
int pte_osThreadGetPriority(pte_osThreadHandle threadHandle);
/**
* Sets the priority of the specified thread.
*
* @return PTE_OS_OK - thread priority successfully set
*/
pte_osResult pte_osThreadSetPriority(pte_osThreadHandle threadHandle, int newPriority);
/**
* Frees resources associated with the specified thread. This is called after the thread has terminated
* and is no longer needed (e.g. after pthread_join returns). This call will always be made
* from a different context than that of the target thread.
*/
pte_osResult pte_osThreadDelete(pte_osThreadHandle handle);
/**
* Frees resources associated with the specified thread and then causes the thread to exit.
* This is called after the thread has terminated and is no longer needed (e.g. after
* pthread_join returns). This call will always be made from the context of the target thread.
*/
pte_osResult pte_osThreadExitAndDelete(pte_osThreadHandle handle);
/**
* Cancels the specified thread. This should cause pte_osSemaphoreCancellablePend() and for pte_osThreadCheckCancel()
* to return @p PTE_OS_INTERRUPTED.
*
* @param threadHandle handle to the thread to cancel.
*
* @return Thread successfully canceled.
*/
pte_osResult pte_osThreadCancel(pte_osThreadHandle threadHandle);
/**
* Check if pte_osThreadCancel() has been called on the specified thread.
*
* @param threadHandle handle of thread to check the state of.
*
* @return PTE_OS_OK - Thread has not been cancelled
* @return PTE_OS_INTERRUPTED - Thread has been cancelled.
*/
pte_osResult pte_osThreadCheckCancel(pte_osThreadHandle threadHandle);
/**
* Causes the current thread to sleep for the specified number of milliseconds.
*/
void pte_osThreadSleep(unsigned int msecs);
#ifdef __hermit__
void sys_yield(void);
static inline void pte_osYield(void)
{
sys_yield();
}
#else
/**
* pte_osYield causes the calling thread to relinquish the CPU.
* The thread is moved to the end of the queue for its static priority
* and a new thread gets to run.
*/
static inline void pte_osYield(void)
{
pte_osThreadSleep(1);
}
#endif
/**
* Returns the maximum allowable priority
*/
int pte_osThreadGetMaxPriority();
/**
* Returns the minimum allowable priority
*/
int pte_osThreadGetMinPriority();
/**
* Returns the priority that should be used if the caller to pthread_create doesn't
* explicitly set one.
*/
int pte_osThreadGetDefaultPriority();
//@}
/** @name Semaphores */
//@{
/**
* Creates a semaphore
*
* @param initialValue Initial value of the semaphore
* @param pHandle Set to the handle of the newly created semaphore.
*
* @return PTE_OS_OK - Semaphore successfully created
* @return PTE_OS_NO_RESOURCESs - Insufficient resources to create semaphore
*/
pte_osResult pte_osSemaphoreCreate(int initialValue, pte_osSemaphoreHandle *pHandle);
/**
* Deletes a semaphore and frees any associated resources.
*
* @param handle Handle of semaphore to delete.
*
* @return PTE_OS_OK - Semaphore successfully deleted.
*/
pte_osResult pte_osSemaphoreDelete(pte_osSemaphoreHandle handle);
/**
* Posts to the semaphore
*
* @param handle Semaphore to release
* @param count Amount to increment the semaphore by.
*
* @return PTE_OS_OK - semaphore successfully released.
*/
pte_osResult pte_osSemaphorePost(pte_osSemaphoreHandle handle, int count);
/**
* Acquire a semaphore, returning after @p timeoutMsecs if the semaphore is not
* available. Can be used for polling a semaphore by using @p timeoutMsecs of zero.
*
* @param handle Handle of semaphore to acquire.
* @param pTimeout Pointer to the number of milliseconds to wait to acquire the semaphore
* before returning. If set to NULL, wait forever.
*
* @return PTE_OS_OK - Semaphore successfully acquired.
* @return PTE_OS_TIMEOUT - Timeout expired before semaphore was obtained.
*/
pte_osResult pte_osSemaphorePend(pte_osSemaphoreHandle handle, unsigned int *pTimeout);
/**
* Acquire a semaphore, returning after @p timeoutMsecs if the semaphore is not
* available. Can be used for polling a semaphore by using @p timeoutMsecs of zero.
* Call must return immediately if pte_osThreadCancel() is called on the thread waiting for
* the semaphore.
*
* @param handle Handle of semaphore to acquire.
* @param pTimeout Pointer to the number of milliseconds to wait to acquire the semaphore
* before returning. If set to NULL, wait forever.
*
* @return PTE_OS_OK - Semaphore successfully acquired.
* @return PTE_OS_TIMEOUT - Timeout expired before semaphore was obtained.
*/
pte_osResult pte_osSemaphoreCancellablePend(pte_osSemaphoreHandle handle, unsigned int *pTimeout);
//@}
/** @name Thread Local Storage */
//@{
/**
* Sets the thread specific value for the specified key for the
* currently executing thread.
*
* @param index The TLS key for the value.
* @param value The value to save
*/
pte_osResult pte_osTlsSetValue(unsigned int key, void * value);
/**
* Retrieves the thread specific value for the specified key for
* the currently executing thread. If a value has not been set
* for this key, NULL should be returned (i.e. TLS values default
* to NULL).
*
* @param index The TLS key for the value.
*
* @return The value associated with @p key for the current thread.
*/
void * pte_osTlsGetValue(unsigned int key);
/**
* Initializes the OS TLS support. This is called by the PTE library
* prior to performing ANY TLS operation.
*/
void pte_osTlsInit(void);
/**
* Allocates a new TLS key.
*
* @param pKey On success will be set to the newly allocated key.
*
* @return PTE_OS_OK - TLS key successfully allocated.
* @return PTE_OS_NO_RESOURCESs - Insufficient resources to allocate key (e.g.
* maximum number of keys reached).
*/
pte_osResult pte_osTlsAlloc(unsigned int *pKey);
/**
* Frees the specified TLS key.
*
* @param index TLS key to free
*
* @return PTE_OS_OK - TLS key was successfully freed.
*/
pte_osResult pte_osTlsFree(unsigned int key);
//@}
/** @name Atomic operations */
//@{
/**
* Sets the target to the specified value as an atomic operation.
*
* \code
* origVal = *ptarg
* *ptarg = val
* return origVal
* \endcode
*
* @param pTarg Pointer to the value to be exchanged.
* @param val Value to be exchanged
*
* @return original value of destination
*/
int pte_osAtomicExchange(int *pTarg, int val);
/**
* Performs an atomic compare-and-exchange oepration on the specified
* value. That is:
*
* \code
* origVal = *pdest
* if (*pdest == comp)
* then *pdest = exchange
* return origVal
* \endcode
*
* @param pdest Pointer to the destination value.
* @param exchange Exchange value (value to set destination to if destination == comparand)
* @param comp The value to compare to destination.
*
* @return Original value of destination
*/
int pte_osAtomicCompareExchange(int *pdest, int exchange, int comp);
/**
* Adds the value to target as an atomic operation
*
* \code
* origVal = *pdest
* *pAddend += value
* return origVal
* \endcode
*
* @param pdest Pointer to the variable to be updated.
* @param value Value to be added to the variable.
*
* @return Original value of destination
*/
int pte_osAtomicExchangeAdd(int volatile* pdest, int value);
/**
* Decrements the destination.
*
* \code
* origVal = *pdest
* *pdest++
* return origVal
* \endcode
*
* @param pdest Destination value to decrement
*
* @return Original destination value
*/
int pte_osAtomicDecrement(int *pdest);
/**
* Increments the destination value
*
* \code
* origVal = *pdest;
* *pdest++;
* return origVal;
*/
int pte_osAtomicIncrement(int *pdest);
//@}
struct timeb;
int ftime(struct timeb *tb);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // _OS_SUPPORT_H_