AM64x MCU+ SDK  09.02.01
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 */
57 #ifndef CB_LLD_THREAD_H
58 #define CB_LLD_THREAD_H
59 
60 #include <stdint.h>
61 #include <string.h>
62 #include <time.h>
63 #include "lld_thread.h"
64 
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
69 typedef struct cb_lld_sem cb_lld_sem_t;
70 typedef struct cb_lld_task cb_lld_task_t;
71 
72 #define TILLD_SUCCESS (0)
73 #define TILLD_FAILURE (-1)
74 #define TILLD_TIMEDOUT (-2)
75 
76 #define CB_THREAD_T cb_lld_task_t*
77 #define CB_THREAD_CREATE cb_lld_task_create
78 #define CB_THREAD_JOIN cb_lld_task_join
79 #define CB_THREAD_EXIT cb_lld_task_exit
80 #define CB_THREAD_MUTEX_T cb_lld_mutex_t
81 #define CB_THREAD_MUTEX_LOCK cb_lld_mutex_lock
82 #define CB_THREAD_MUTEX_TRYLOCK cb_lld_mutex_trylock
83 #define CB_THREAD_MUTEX_TIMEDLOCK cb_lld_mutex_timedlock
84 #define CB_THREAD_MUTEX_UNLOCK cb_lld_mutex_unlock
85 #define CB_THREAD_MUTEX_INIT cb_lld_mutex_init
86 #define CB_THREAD_MUTEX_DESTROY cb_lld_mutex_destroy
87 #define CB_THREAD_IS_MUTEX_INITIALIZED(x) ((x).lldmutex!=NULL)
88 #define CB_STATIC_MUTEX_INITIALIZER(x) x=TILLD_MUTEX_INITIALIZER
89 #define CB_STATIC_MUTEX_CONSTRUCTOR(x) \
90  UB_PROTECTED_FUNC_VOID(cb_lld_mutex_init_protect,&(x))
91 #define CB_STATIC_MUTEX_DESTRUCTOR(x) \
92  UB_PROTECTED_FUNC_VOID(cb_lld_mutex_destroy_protect, &(x))
93 
94 /* does not need to support these macros */
95 #define CB_THREAD_MUTEXATTR_T void*
96 #define CB_THREAD_MUTEXATTR_INIT(a)
97 #define CB_THREAD_MUTEXATTR_SETPSHARED(a,b)
98 #define CB_THREAD_PROCESS_SHARED 0 //PTHREAD_PROCESS_SHARED
99 
100 #define CB_SEM_T cb_lld_sem_t*
101 #define CB_SEM_INIT cb_lld_sem_init
102 #define CB_SEM_GETVALUE cb_lld_sem_getvalue
103 #define CB_SEM_WAIT cb_lld_sem_wait
104 #define CB_SEM_TRYWAIT cb_lld_sem_trywait
105 #define CB_SEM_TIMEDWAIT cb_lld_sem_timedwait
106 #define CB_SEM_POST cb_lld_sem_post
107 #define CB_SEM_DESTROY cb_lld_sem_destroy
108 
109 /* does not support condition variable */
110 #define CB_THREAD_COND_T void*
111 #define CB_THREAD_COND_INIT(a, b)
112 #define CB_THREAD_COND_DESTROY(a)
113 #define CB_THREAD_COND_WAIT(a, b)
114 #define CB_THREAD_COND_SIGNAL(a)
115 #define CB_THREAD_COND_BROADCAST(a)
116 
117 /* Implement the same prototype as POSIX APIs */
126 int cb_lld_sem_init(CB_SEM_T *sem, int pshared, unsigned int value);
127 
135 
147 
156 int cb_lld_sem_timedwait(CB_SEM_T *sem, struct timespec *abstime);
157 
165 
173 int cb_lld_sem_getvalue(CB_SEM_T* sem, int* sval);
174 
182 
194 
203 
214 static inline int cb_lld_mutex_init_protect(void *mutex)
215 {
216  if(((CB_THREAD_MUTEX_T *)mutex)->lldmutex) {return 0;}
217  return cb_lld_mutex_init((CB_THREAD_MUTEX_T *)mutex, NULL);
218 }
219 
227 
237 static inline void cb_lld_mutex_destroy_protect(void *mutex)
238 {
239  CB_THREAD_MUTEX_T *cbmutex = (CB_THREAD_MUTEX_T *)mutex;
240  if(cbmutex->lldmutex == NULL) {return;}
241  cb_lld_mutex_destroy(cbmutex);
242  cbmutex->lldmutex = NULL;
243 }
244 
252 
260 
275 int cb_lld_mutex_timedlock(CB_THREAD_MUTEX_T *mutex, struct timespec *abstime);
276 
287 int cb_lld_task_create(CB_THREAD_T *th, void *attr, void *(*func)(void*), void *arg);
288 
297 int cb_lld_task_join(CB_THREAD_T th, void **retval);
298 
303 void cb_lld_task_exit(void *retval);
304 
305 #ifdef __cplusplus
306 }
307 #endif
308 
309 #endif //CB_LLD_THREAD_H
310 
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:214
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:237
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:76
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:69
CB_THREAD_MUTEXATTR_T
#define CB_THREAD_MUTEXATTR_T
Definition: cb_lld_thread.h:95
cb_lld_task_exit
void cb_lld_task_exit(void *retval)
Terminates the calling thread.
CB_SEM_T
#define CB_SEM_T
Definition: cb_lld_thread.h:100
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.
value
uint32_t value
Definition: tisci_otp_revision.h:2
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_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:80
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:70
cb_lld_sem_wait
int cb_lld_sem_wait(CB_SEM_T *sem)
Waits on a semaphore until it becomes available.