AM261x MCU+ SDK: cb_lld_thread.h Source File

| AM261x MCU+ SDK 26.00.00 |

cb_lld_thread.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2023 Texas Instruments Incorporated
3 * Copyright (c) 2023 Excelfore Corporation (https://excelfore.com)
4 *
5 * All rights reserved not granted herein.
6 * Limited License.
7 *
8 * Texas Instruments Incorporated grants a world-wide, royalty-free,
9 * non-exclusive license under copyrights and patents it now or hereafter
10 * owns or controls to make, have made, use, import, offer to sell and sell ("Utilize")
11 * this software subject to the terms herein. With respect to the foregoing patent
12 * license, such license is granted solely to the extent that any such patent is necessary
13 * to Utilize the software alone. The patent license shall not apply to any combinations which
14 * include this software, other than combinations with devices manufactured by or for TI ("TI Devices").
15 * No hardware patent is licensed hereunder.
16 *
17 * Redistributions must preserve existing copyright notices and reproduce this license (including the
18 * above copyright notice and the disclaimer and (if applicable) source code license limitations below)
19 * in the documentation and/or other materials provided with the distribution
20 *
21 * Redistribution and use in binary form, without modification, are permitted provided that the following
22 * conditions are met:
23 *
24 * * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any
25 * software provided in binary form.
26 * * any redistribution and use are licensed by TI for use only with TI Devices.
27 * * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code.
28 *
29 * If software source code is provided to you, modification and redistribution of the source code are permitted
30 * provided that the following conditions are met:
31 *
32 * * any redistribution and use of the source code, including any resulting derivative works, are licensed by
33 * TI for use only with TI Devices.
34 * * any redistribution and use of any object code compiled from the source code and any resulting derivative
35 * works, are licensed by TI for use only with TI Devices.
36 *
37 * Neither the name of Texas Instruments Incorporated nor the names of its suppliers may be used to endorse or
38 * promote products derived from this software without specific prior written permission.
39 *
40 * DISCLAIMER.
41 *
42 * THIS SOFTWARE IS PROVIDED BY TI AND TI"S LICENSORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
43 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44 * IN NO EVENT SHALL TI AND TI"S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
46 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48 * POSSIBILITY OF SUCH DAMAGE.
49 */
58 #ifndef CB_LLD_THREAD_H
59 #define CB_LLD_THREAD_H
60
61 #include <stdint.h>
62 #include <string.h>
63 #include <time.h>
64 #include "lld_thread.h"
65
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69
70 typedef struct cb_lld_sem cb_lld_sem_t;
71 typedef struct cb_lld_task cb_lld_task_t;
72
73 #define TILLD_SUCCESS (0)
74 #define TILLD_FAILURE (-1)
75 #define TILLD_TIMEDOUT (-2)
76
77 #define CB_THREAD_T cb_lld_task_t*
78 #define CB_THREAD_CREATE cb_lld_task_create
79 #define CB_THREAD_JOIN cb_lld_task_join
80 #define CB_THREAD_EXIT cb_lld_task_exit
81 #define CB_THREAD_MUTEX_T cb_lld_mutex_t
82 #define CB_THREAD_MUTEX_LOCK cb_lld_mutex_lock
83 #define CB_THREAD_MUTEX_TRYLOCK cb_lld_mutex_trylock
84 #define CB_THREAD_MUTEX_TIMEDLOCK cb_lld_mutex_timedlock
85 #define CB_THREAD_MUTEX_UNLOCK cb_lld_mutex_unlock
86 #define CB_THREAD_MUTEX_INIT cb_lld_mutex_init
87 #define CB_THREAD_MUTEX_DESTROY cb_lld_mutex_destroy
88 #define CB_THREAD_IS_MUTEX_INITIALIZED(x) ((x).lldmutex!=NULL)
89 #define CB_STATIC_MUTEX_INITIALIZER(x) x=TILLD_MUTEX_INITIALIZER
90 #define CB_STATIC_MUTEX_CONSTRUCTOR(x) \
91 UB_PROTECTED_FUNC_VOID(cb_lld_mutex_init_protect,&(x))
92 #define CB_STATIC_MUTEX_DESTRUCTOR(x) \
93 UB_PROTECTED_FUNC_VOID(cb_lld_mutex_destroy_protect, &(x))
94 #define CB_GETPID() ( 0 )
95 #define CB_GETTID() ( 0 )
96
97 /* does not need to support these macros */
98 #define CB_THREAD_MUTEXATTR_T void*
99 #define CB_THREAD_MUTEXATTR_INIT(a)
100 #define CB_THREAD_MUTEXATTR_SETPSHARED(a,b)
101 #define CB_THREAD_PROCESS_SHARED 0 //PTHREAD_PROCESS_SHARED
102
103 #define CB_SEM_T cb_lld_sem_t*
104 #define CB_SEM_INIT cb_lld_sem_init
105 #define CB_SEM_GETVALUE cb_lld_sem_getvalue
106 #define CB_SEM_WAIT cb_lld_sem_wait
107 #define CB_SEM_TRYWAIT cb_lld_sem_trywait
108 #define CB_SEM_TIMEDWAIT cb_lld_sem_timedwait
109 #define CB_SEM_POST cb_lld_sem_post
110 #define CB_SEM_DESTROY cb_lld_sem_destroy
111
112 /* does not support condition variable */
113 #define CB_THREAD_COND_T void*
114 #define CB_THREAD_COND_INIT(a, b)
115 #define CB_THREAD_COND_DESTROY(a)
116 #define CB_THREAD_COND_WAIT(a, b)
117 #define CB_THREAD_COND_SIGNAL(a)
118 #define CB_THREAD_COND_BROADCAST(a)
119
120 /* Implement the same prototype as POSIX APIs */
129 int cb_lld_sem_init(CB_SEM_T *sem, int pshared, unsigned int value);
130
137 int cb_lld_sem_wait(CB_SEM_T *sem);
138
149 int cb_lld_sem_trywait(CB_SEM_T *sem);
150
159 int cb_lld_sem_timedwait(CB_SEM_T *sem, struct timespec *abstime);
160
167 int cb_lld_sem_post(CB_SEM_T *sem);
168
176 int cb_lld_sem_getvalue(CB_SEM_T* sem, int* sval);
177
184 int cb_lld_sem_destroy(CB_SEM_T *sem);
185
196 int cb_lld_sem_wait_status(CB_SEM_T *sem);
197
205 int cb_lld_mutex_init(CB_THREAD_MUTEX_T *mutex, CB_THREAD_MUTEXATTR_T attr);
206
217 static inline int cb_lld_mutex_init_protect(void *mutex)
218 {
219 if(((CB_THREAD_MUTEX_T *)mutex)->lldmutex) {return 0;}
220 return cb_lld_mutex_init((CB_THREAD_MUTEX_T *)mutex, NULL);
221 }
222
229 int cb_lld_mutex_destroy(CB_THREAD_MUTEX_T *mutex);
230
240 static inline void cb_lld_mutex_destroy_protect(void *mutex)
241 {
242 CB_THREAD_MUTEX_T *cbmutex = (CB_THREAD_MUTEX_T *)mutex;
243 if(cbmutex->lldmutex == NULL) {return;}
244 cb_lld_mutex_destroy(cbmutex);
245 cbmutex->lldmutex = NULL;
246 }
247
254 int cb_lld_mutex_lock(CB_THREAD_MUTEX_T *mutex);
255
262 int cb_lld_mutex_unlock(CB_THREAD_MUTEX_T *mutex);
263
270 int cb_lld_mutex_trylock(CB_THREAD_MUTEX_T *mutex);
278 int cb_lld_mutex_timedlock(CB_THREAD_MUTEX_T *mutex, struct timespec *abstime);
279
290 int cb_lld_task_create(CB_THREAD_T *th, void *attr, void *(*func)(void*), void *arg);
291
300 int cb_lld_task_join(CB_THREAD_T th, void **retval);
301
306 void cb_lld_task_exit(void *retval);
307
312 int cb_killproc(int64_t objnum, bool thread_mode);
313
317 void cb_waitproc(int64_t objnum, bool thread_mode);
318
319 #ifdef __cplusplus
320 }
321 #endif
322
323 #endif //CB_LLD_THREAD_H
324
cb_lld_sem_init
int cb_lld_sem_init(CB_SEM_T *sem, int pshared, unsigned int value)
Initializes a semaphore.
cb_lld_mutex_init_protect
static int cb_lld_mutex_init_protect(void *mutex)
Initializes a mutex that will be protected by the UB_PROTECTED_FUNC_VOID().
Definition: cb_lld_thread.h:217
cb_lld_mutex_trylock
int cb_lld_mutex_trylock(CB_THREAD_MUTEX_T *mutex)
Attempts to lock a mutex without blocking.
cb_lld_mutex_destroy_protect
static void cb_lld_mutex_destroy_protect(void *mutex)
Destroy a mutex that will be protected by the UB_PROTECTED_FUNC_VOID().
Definition: cb_lld_thread.h:240
cb_lld_sem_post
int cb_lld_sem_post(CB_SEM_T *sem)
Posts (signals) a semaphore, releasing a waiting thread.
CB_THREAD_T
#define CB_THREAD_T
Definition: cb_lld_thread.h:77
cb_lld_mutex_destroy
int cb_lld_mutex_destroy(CB_THREAD_MUTEX_T *mutex)
Destroys a mutex.
cb_lld_sem_t
struct cb_lld_sem cb_lld_sem_t
Definition: cb_lld_thread.h:70
CB_THREAD_MUTEXATTR_T
#define CB_THREAD_MUTEXATTR_T
Definition: cb_lld_thread.h:98
cb_lld_task_exit
void cb_lld_task_exit(void *retval)
Terminates the calling thread.
cb_waitproc
void cb_waitproc(int64_t objnum, bool thread_mode)
Wait process to be finished.
CB_SEM_T
#define CB_SEM_T
Definition: cb_lld_thread.h:103
cb_lld_mutex_unlock
int cb_lld_mutex_unlock(CB_THREAD_MUTEX_T *mutex)
Unlocks a mutex.
cb_lld_mutex_timedlock
int cb_lld_mutex_timedlock(CB_THREAD_MUTEX_T *mutex, struct timespec *abstime)
Locks a mutex and waits until a timeout occurs.
cb_lld_sem_wait_status
int cb_lld_sem_wait_status(CB_SEM_T *sem)
Get the semaphore wait status. The return value of this function determines the wait status of cb_lld...
cb_lld_task_create
int cb_lld_task_create(CB_THREAD_T *th, void *attr, void *(*func)(void *), void *arg)
Creates a new thread. This function creates a new thread with the specified attributes and starts exe...
cb_lld_task_join
int cb_lld_task_join(CB_THREAD_T th, void **retval)
Waits for a thread to terminate. This function waits for the specified thread to terminate.
cb_killproc
int cb_killproc(int64_t objnum, bool thread_mode)
Kill thread by ID.
cb_lld_sem_trywait
int cb_lld_sem_trywait(CB_SEM_T *sem)
Attempts to wait on a semaphore without blocking. To know if the semaphore was acquired,...
CB_THREAD_MUTEX_T
#define CB_THREAD_MUTEX_T
Definition: cb_lld_thread.h:81
cb_lld_sem_timedwait
int cb_lld_sem_timedwait(CB_SEM_T *sem, struct timespec *abstime)
Waits on a semaphore until it becomes available or until a timeout occurs. To know if the semaphore w...
cb_lld_mutex_lock
int cb_lld_mutex_lock(CB_THREAD_MUTEX_T *mutex)
Locks a mutex.
cb_lld_sem_getvalue
int cb_lld_sem_getvalue(CB_SEM_T *sem, int *sval)
return the count of a semaphore
cb_lld_mutex_init
int cb_lld_mutex_init(CB_THREAD_MUTEX_T *mutex, CB_THREAD_MUTEXATTR_T attr)
Initializes a mutex.
cb_lld_sem_destroy
int cb_lld_sem_destroy(CB_SEM_T *sem)
Destroys a semaphore.
cb_lld_task_t
struct cb_lld_task cb_lld_task_t
Definition: cb_lld_thread.h:71
cb_lld_sem_wait
int cb_lld_sem_wait(CB_SEM_T *sem)
Waits on a semaphore until it becomes available.