PDK API Guide for J721E
cb_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 */
59 #ifndef CB_THREAD_H_
60 #define CB_THREAD_H_
61 
65 #define CB_TSN_THREAD_NAME_SIZE 128
66 
70 typedef struct cb_tsn_thread_attr{
71  int pri;
77  void *stack_addr;
80 
81 #ifdef CB_THREAD_NON_POSIX_H
82 /* non-posix platforms need to support necessary POSIX compatible
83  * functions and types which are defined as CB_* macros below.
84  * And provide them in a header file defined as CB_THREAD_NON_POSIX_H */
85 #include CB_THREAD_NON_POSIX_H
86 #else
87 #include <pthread.h>
88 #include <semaphore.h>
89 #include <string.h>
90 
91 #define CB_THREAD_T pthread_t
92 #define CB_THREAD_CREATE(th, attr, func, arg) pthread_create(th, NULL, func, arg)
93 #define CB_THREAD_JOIN pthread_join
94 #define CB_THREAD_EXIT pthread_exit
95 #define CB_THREAD_MUTEX_T pthread_mutex_t
96 #define CB_THREAD_MUTEX_LOCK pthread_mutex_lock
97 #define CB_THREAD_MUTEX_TRYLOCK pthread_mutex_trylock
98 #define CB_THREAD_MUTEX_TIMEDLOCK pthread_mutex_timedlock
99 #define CB_THREAD_MUTEX_UNLOCK pthread_mutex_unlock
100 #define CB_THREAD_MUTEX_INIT pthread_mutex_init
101 #define CB_THREAD_MUTEX_DESTROY pthread_mutex_destroy
102 #define CB_THREAD_MUTEXATTR_T pthread_mutexattr_t
103 #define CB_THREAD_MUTEXATTR_INIT pthread_mutexattr_init
104 #define CB_THREAD_MUTEXATTR_SETPSHARED pthread_mutexattr_setpshared
105 #define CB_THREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
106 #define CB_THREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
107 #define CB_THREAD_COND_T pthread_cond_t
108 #define CB_THREAD_COND_INIT pthread_cond_init
109 #define CB_THREAD_COND_DESTROY pthread_cond_destroy
110 #define CB_THREAD_COND_WAIT pthread_cond_wait
111 #define CB_THREAD_COND_SIGNAL pthread_cond_signal
112 #define CB_THREAD_COND_BROADCAST pthread_cond_broadcast
113 #if !defined(CB_STATIC_MUTEX_INITIALIZER) && defined(PTHREAD_MUTEX_INITIALIZER)
114 #define CB_STATIC_MUTEX_INITIALIZER(x) x=PTHREAD_MUTEX_INITIALIZER
115 #define CB_STATIC_MUTEX_CONSTRUCTOR(x)
116 #endif
117 #define CB_SEM_T sem_t
118 #define CB_SEM_INIT sem_init
119 #define CB_SEM_GETVALUE sem_getvalue
120 #define CB_SEM_WAIT sem_wait
121 #define CB_SEM_TRYWAIT sem_trywait
122 #define CB_SEM_TIMEDWAIT sem_timedwait
123 #define CB_SEM_POST sem_post
124 #define CB_SEM_DESTROY sem_destroy
125 #define CB_SEM_OPEN sem_open
126 #define CB_SEM_CLOSE sem_close
127 #define CB_SEM_UNLINK sem_unlink
128 #define CB_SEM_FAILED SEM_FAILED
129 #endif
130 
131 static inline int cb_tsn_thread_attr_init(cb_tsn_thread_attr_t *attr, int pri,
132  int stack_size, const char* name)
133 {
134  if(!attr) {return -1;}
135  (void)memset(attr, 0, sizeof(cb_tsn_thread_attr_t));
136  if(pri>0){attr->pri=pri;}
137  if(stack_size>0){attr->stack_size=stack_size;}
138  if(name!=NULL){
139  (void)strncpy(attr->name, name, CB_TSN_THREAD_NAME_SIZE-1);
140  }
141  return 0;
142 }
143 
145  void *stack_addr)
146 {
147  attr->stack_addr = stack_addr;
148  return 0;
149 }
150 
158 typedef struct cb_waitpoint {
163  uint64_t time;
165  bool wakeup;
168 
173 static inline void cb_waitpoint_init(cb_waitpoint_t *wp){
176  wp->time=0;
177  wp->wakeup=false;
178 }
179 
184 static inline void cb_waitpoint_deinit(cb_waitpoint_t *wp){
187 }
188 
195 static inline bool cb_waitpoint_check(cb_waitpoint_t *wp, uint64_t time){
196  if(wp->wakeup==true){
197  // check time diff, consider wrap around to uint64_t max value
198  // unsigned substract typecasted to unsigned stores the 2s complement
199  if(((uint64_t)(time-wp->time)<((uint64_t)1<<63))){
200  wp->wakeup=false;
201  return true;
202  }
203  }
204  return false;
205 }
206 
217 static inline void cb_waitpoint_wakeup_at(cb_waitpoint_t *wp, uint64_t time, bool dosleep){
218  wp->time=time;
219  wp->wakeup = true;
220  if(dosleep){
221  CB_THREAD_COND_WAIT(&wp->condition, &wp->lock);
222  }
223 }
224 
231 #define cb_waitpoint_wakeup(wp) CB_THREAD_COND_SIGNAL(&(wp)->condition)
232 
239 #define cb_waitpoint_broadcast(wp) CB_THREAD_COND_BROADCAST(&(wp)->condition)
240 
245 #define cb_waitpoint_lock(wp) CB_THREAD_MUTEX_LOCK(&(wp)->lock)
246 
251 #define cb_waitpoint_unlock(wp) CB_THREAD_MUTEX_UNLOCK(&(wp)->lock)
252 
253 #endif
254 
#define CB_TSN_THREAD_NAME_SIZE
data structure for thread attributes
Definition: cb_thread.h:65
CB_THREAD_COND_T condition
Definition: cb_thread.h:161
int pri
Definition: cb_thread.h:71
static int cb_tsn_thread_attr_set_stackaddr(cb_tsn_thread_attr_t *attr, void *stack_addr)
Definition: cb_thread.h:144
uint64_t time
Definition: cb_thread.h:163
static bool cb_waitpoint_check(cb_waitpoint_t *wp, uint64_t time)
Wait point check.
Definition: cb_thread.h:195
static void cb_waitpoint_init(cb_waitpoint_t *wp)
Wait point initialization.
Definition: cb_thread.h:173
int stack_size
Definition: cb_thread.h:73
#define CB_THREAD_MUTEX_T
Definition: cb_thread.h:95
CB_THREAD_MUTEX_T lock
Definition: cb_thread.h:159
#define CB_THREAD_MUTEX_INIT
Definition: cb_thread.h:100
char name[CB_TSN_THREAD_NAME_SIZE]
Definition: cb_thread.h:75
bool wakeup
Definition: cb_thread.h:165
#define CB_THREAD_COND_T
Definition: cb_thread.h:107
#define CB_THREAD_COND_INIT
Definition: cb_thread.h:108
#define NULL
Define NULL if not defined.
Definition: csl_types.h:107
parameters to initialize thread
Definition: cb_thread.h:70
#define CB_THREAD_COND_WAIT
Definition: cb_thread.h:110
static void cb_waitpoint_deinit(cb_waitpoint_t *wp)
Wait point de-initialization.
Definition: cb_thread.h:184
static void cb_waitpoint_wakeup_at(cb_waitpoint_t *wp, uint64_t time, bool dosleep)
Wait point blocks until wake point has been awoken and after specified time.
Definition: cb_thread.h:217
void * stack_addr
Definition: cb_thread.h:77
static int cb_tsn_thread_attr_init(cb_tsn_thread_attr_t *attr, int pri, int stack_size, const char *name)
Definition: cb_thread.h:131
Wait points that blocks the execution of a thread at a specific code point until an event occurs.
Definition: cb_thread.h:158
#define CB_THREAD_MUTEX_DESTROY
Definition: cb_thread.h:101
#define CB_THREAD_COND_DESTROY
Definition: cb_thread.h:109