AM64x MCU+ SDK  10.00.00
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 
62 #include <tsn_unibase/unibase.h>
63 
67 #define CB_TSN_THREAD_NAME_SIZE 128
68 
72 typedef struct cb_tsn_thread_attr{
73  int pri;
79  void *stack_addr;
82 
83 #ifdef CB_THREAD_NON_POSIX_H
84 /* non-posix platforms need to support necessary POSIX compatible
85  * functions and types which are defined as CB_* macros below.
86  * And provide them in a header file defined as CB_THREAD_NON_POSIX_H */
87 #include CB_THREAD_NON_POSIX_H
88 #else
89 #include <pthread.h>
90 #include <semaphore.h>
91 #include <string.h>
92 
93 #define CB_THREAD_T pthread_t
94 #define CB_THREAD_CREATE(th, attr, func, arg) pthread_create(th, NULL, func, arg)
95 #define CB_THREAD_JOIN pthread_join
96 #define CB_THREAD_EXIT pthread_exit
97 #define CB_THREAD_MUTEX_T pthread_mutex_t
98 #define CB_THREAD_MUTEX_LOCK pthread_mutex_lock
99 #define CB_THREAD_MUTEX_TRYLOCK pthread_mutex_trylock
100 #define CB_THREAD_MUTEX_TIMEDLOCK pthread_mutex_timedlock
101 #define CB_THREAD_MUTEX_UNLOCK pthread_mutex_unlock
102 #define CB_THREAD_MUTEX_INIT pthread_mutex_init
103 #define CB_THREAD_MUTEX_DESTROY pthread_mutex_destroy
104 #define CB_THREAD_MUTEXATTR_T pthread_mutexattr_t
105 #define CB_THREAD_MUTEXATTR_INIT pthread_mutexattr_init
106 #define CB_THREAD_MUTEXATTR_SETPSHARED pthread_mutexattr_setpshared
107 #define CB_THREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
108 #define CB_THREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
109 #define CB_THREAD_COND_T pthread_cond_t
110 #define CB_THREAD_COND_INIT pthread_cond_init
111 #define CB_THREAD_COND_DESTROY pthread_cond_destroy
112 #define CB_THREAD_COND_WAIT pthread_cond_wait
113 #define CB_THREAD_COND_SIGNAL pthread_cond_signal
114 #define CB_THREAD_COND_BROADCAST pthread_cond_broadcast
115 #if !defined(CB_STATIC_MUTEX_INITIALIZER) && defined(PTHREAD_MUTEX_INITIALIZER)
116 #define CB_STATIC_MUTEX_INITIALIZER(x) x=PTHREAD_MUTEX_INITIALIZER
117 #define CB_STATIC_MUTEX_CONSTRUCTOR(x)
118 #define CB_STATIC_MUTEX_DESTRUCTOR(x)
119 #endif
120 #define CB_SEM_T sem_t
121 #define CB_SEM_INIT sem_init
122 #define CB_SEM_GETVALUE sem_getvalue
123 #define CB_SEM_WAIT sem_wait
124 #define CB_SEM_TRYWAIT sem_trywait
125 #define CB_SEM_TIMEDWAIT sem_timedwait
126 #define CB_SEM_POST sem_post
127 #define CB_SEM_DESTROY sem_destroy
128 #define CB_SEM_OPEN sem_open
129 #define CB_SEM_CLOSE sem_close
130 #define CB_SEM_UNLINK sem_unlink
131 #define CB_SEM_FAILED SEM_FAILED
132 #endif
133 
134 static inline int cb_tsn_thread_attr_init(cb_tsn_thread_attr_t *attr, int pri,
135  int stack_size, const char* name)
136 {
137  if(!attr) {return -1;}
138  (void)memset(attr, 0, sizeof(cb_tsn_thread_attr_t));
139  if(pri>0){attr->pri=pri;}
140  if(stack_size>0){attr->stack_size=stack_size;}
141  if(name!=NULL){
142  (void)ub_strncpy(attr->name, name, CB_TSN_THREAD_NAME_SIZE);
143  }
144  return 0;
145 }
146 
148  void *stack_addr)
149 {
150  attr->stack_addr = stack_addr;
151  return 0;
152 }
153 
161 typedef struct cb_waitpoint {
166  uint64_t time;
168  bool wakeup;
171 
176 static inline void cb_waitpoint_init(cb_waitpoint_t *wp){
177  CB_THREAD_MUTEX_INIT(&wp->lock, NULL);
178  CB_THREAD_COND_INIT(&wp->condition, NULL);
179  wp->time=0;
180  wp->wakeup=false;
181 }
182 
187 static inline void cb_waitpoint_deinit(cb_waitpoint_t *wp){
190 }
191 
198 static inline bool cb_waitpoint_check(cb_waitpoint_t *wp, uint64_t time){
199  if(wp->wakeup==true){
200  // check time diff, consider wrap around to uint64_t max value
201  // unsigned substract typecasted to unsigned stores the 2s complement
202  if(((uint64_t)(time-wp->time)<((uint64_t)1<<63))){
203  wp->wakeup=false;
204  return true;
205  }
206  }
207  return false;
208 }
209 
219 static inline void cb_waitpoint_wakeup_at(cb_waitpoint_t *wp, uint64_t time, bool dosleep){
220  wp->time=time;
221  wp->wakeup = true;
222  if(dosleep){
223  CB_THREAD_COND_WAIT(&wp->condition, &wp->lock);
224  }
225 }
226 
233 #define cb_waitpoint_wakeup(wp) CB_THREAD_COND_SIGNAL(&(wp)->condition)
234 
241 #define cb_waitpoint_broadcast(wp) CB_THREAD_COND_BROADCAST(&(wp)->condition)
242 
247 #define cb_waitpoint_lock(wp) CB_THREAD_MUTEX_LOCK(&(wp)->lock)
248 
253 #define cb_waitpoint_unlock(wp) CB_THREAD_MUTEX_UNLOCK(&(wp)->lock)
254 
255 #endif
256 
cb_waitpoint_wakeup_at
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:219
CB_THREAD_MUTEX_DESTROY
#define CB_THREAD_MUTEX_DESTROY
Definition: cb_thread.h:103
CB_THREAD_COND_DESTROY
#define CB_THREAD_COND_DESTROY
Definition: cb_thread.h:111
cb_waitpoint_t::time
uint64_t time
Definition: cb_thread.h:166
CB_TSN_THREAD_NAME_SIZE
#define CB_TSN_THREAD_NAME_SIZE
data structure for thread attributes
Definition: cb_thread.h:67
cb_waitpoint_t
Wait points that blocks the execution of a thread at a specific code point until an event occurs.
Definition: cb_thread.h:161
cb_waitpoint_t::condition
CB_THREAD_COND_T condition
Definition: cb_thread.h:164
CB_THREAD_COND_T
#define CB_THREAD_COND_T
Definition: cb_thread.h:109
cb_waitpoint_t::lock
CB_THREAD_MUTEX_T lock
Definition: cb_thread.h:162
CB_THREAD_COND_WAIT
#define CB_THREAD_COND_WAIT
Definition: cb_thread.h:112
unibase.h
unibase general global header
CB_THREAD_MUTEX_INIT
#define CB_THREAD_MUTEX_INIT
Definition: cb_thread.h:102
cb_tsn_thread_attr_t::stack_size
int stack_size
Definition: cb_thread.h:75
cb_waitpoint_t::wakeup
bool wakeup
Definition: cb_thread.h:168
ub_strncpy
int ub_strncpy(char *dest, const char *src, int maxlen)
copy a string
cb_waitpoint_check
static bool cb_waitpoint_check(cb_waitpoint_t *wp, uint64_t time)
Wait point check.
Definition: cb_thread.h:198
cb_tsn_thread_attr_t::name
char name[CB_TSN_THREAD_NAME_SIZE]
Definition: cb_thread.h:77
cb_waitpoint_init
static void cb_waitpoint_init(cb_waitpoint_t *wp)
Wait point initialization.
Definition: cb_thread.h:176
CB_THREAD_COND_INIT
#define CB_THREAD_COND_INIT
Definition: cb_thread.h:110
cb_tsn_thread_attr_t::stack_addr
void * stack_addr
Definition: cb_thread.h:79
cb_waitpoint_deinit
static void cb_waitpoint_deinit(cb_waitpoint_t *wp)
Wait point de-initialization.
Definition: cb_thread.h:187
cb_tsn_thread_attr_init
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:134
CB_THREAD_MUTEX_T
#define CB_THREAD_MUTEX_T
Definition: cb_thread.h:97
cb_tsn_thread_attr_t
parameters to initialize thread
Definition: cb_thread.h:72
cb_tsn_thread_attr_t::pri
int pri
Definition: cb_thread.h:73
cb_tsn_thread_attr_set_stackaddr
static int cb_tsn_thread_attr_set_stackaddr(cb_tsn_thread_attr_t *attr, void *stack_addr)
Definition: cb_thread.h:147