AM263Px MCU+ SDK: cb_thread.h Source File

| AM263Px MCU+ SDK 26.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;
75 int stack_size;
77 char name[CB_TSN_THREAD_NAME_SIZE];
79 void *stack_addr;
81 } cb_tsn_thread_attr_t;
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 <unistd.h>
90 #include <pthread.h>
91 #include <semaphore.h>
92 #include <string.h>
93
94 #define CB_THREAD_T pthread_t
95 #define CB_THREAD_CREATE(th, attr, func, arg) pthread_create(th, NULL, func, arg)
96 #define CB_THREAD_JOIN pthread_join
97 #define CB_THREAD_EXIT pthread_exit
98 #define CB_THREAD_MUTEX_T pthread_mutex_t
99 #define CB_THREAD_MUTEX_LOCK pthread_mutex_lock
100 #define CB_THREAD_MUTEX_TRYLOCK pthread_mutex_trylock
101 #define CB_THREAD_MUTEX_TIMEDLOCK pthread_mutex_timedlock
102 #define CB_THREAD_MUTEX_UNLOCK pthread_mutex_unlock
103 #define CB_THREAD_MUTEX_INIT pthread_mutex_init
104 #define CB_THREAD_MUTEX_DESTROY pthread_mutex_destroy
105 #define CB_THREAD_MUTEXATTR_T pthread_mutexattr_t
106 #define CB_THREAD_MUTEXATTR_INIT pthread_mutexattr_init
107 #define CB_THREAD_MUTEXATTR_SETPSHARED pthread_mutexattr_setpshared
108 #define CB_THREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
109 #define CB_THREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
110 #define CB_THREAD_COND_T pthread_cond_t
111 #define CB_THREAD_COND_INIT pthread_cond_init
112 #define CB_THREAD_COND_DESTROY pthread_cond_destroy
113 #define CB_THREAD_COND_WAIT pthread_cond_wait
114 #define CB_THREAD_COND_SIGNAL pthread_cond_signal
115 #define CB_THREAD_COND_BROADCAST pthread_cond_broadcast
116 #if !defined(CB_STATIC_MUTEX_INITIALIZER) && defined(PTHREAD_MUTEX_INITIALIZER)
117 #define CB_STATIC_MUTEX_INITIALIZER(x) x=PTHREAD_MUTEX_INITIALIZER
118 #define CB_STATIC_MUTEX_CONSTRUCTOR(x)
119 #define CB_STATIC_MUTEX_DESTRUCTOR(x)
120 #endif
121 #define CB_SEM_T sem_t
122 #define CB_SEM_INIT sem_init
123 #define CB_SEM_GETVALUE sem_getvalue
124 #define CB_SEM_WAIT sem_wait
125 #define CB_SEM_TRYWAIT sem_trywait
126 #define CB_SEM_TIMEDWAIT sem_timedwait
127 #define CB_SEM_POST sem_post
128 #define CB_SEM_DESTROY sem_destroy
129 #define CB_SEM_OPEN sem_open
130 #define CB_SEM_CLOSE sem_close
131 #define CB_SEM_UNLINK sem_unlink
132 #define CB_SEM_FAILED SEM_FAILED
133
134 #define CB_GETPID getpid
135 #endif
136
137 static inline int cb_tsn_thread_attr_init(cb_tsn_thread_attr_t *attr, int pri,
138 int stack_size, const char* name)
139 {
140 if(!attr) {return -1;}
141 (void)memset(attr, 0, sizeof(cb_tsn_thread_attr_t));
142 if(pri>0){attr->pri=pri;}
143 if(stack_size>0){attr->stack_size=stack_size;}
144 if(name!=NULL){
145 (void)ub_strncpy(attr->name, name, CB_TSN_THREAD_NAME_SIZE);
146 }
147 return 0;
148 }
149
150 static inline int cb_tsn_thread_attr_set_stackaddr(cb_tsn_thread_attr_t *attr,
151 void *stack_addr)
152 {
153 attr->stack_addr = stack_addr;
154 return 0;
155 }
156
164 typedef struct cb_waitpoint {
165 CB_THREAD_MUTEX_T lock;
167 CB_THREAD_COND_T condition;
169 uint64_t time;
171 bool wakeup;
173 } cb_waitpoint_t;
174
179 static inline void cb_waitpoint_init(cb_waitpoint_t *wp){
180 CB_THREAD_MUTEX_INIT(&wp->lock, NULL);
181 CB_THREAD_COND_INIT(&wp->condition, NULL);
182 wp->time=0;
183 wp->wakeup=false;
184 }
185
190 static inline void cb_waitpoint_deinit(cb_waitpoint_t *wp){
191 CB_THREAD_COND_DESTROY(&wp->condition);
192 CB_THREAD_MUTEX_DESTROY(&wp->lock);
193 }
194
201 static inline bool cb_waitpoint_check(cb_waitpoint_t *wp, uint64_t time){
202 if(wp->wakeup==true){
203 // check time diff, consider wrap around to uint64_t max value
204 // unsigned substract typecasted to unsigned stores the 2s complement
205 if(((uint64_t)(time-wp->time)<((uint64_t)1<<63))){
206 wp->wakeup=false;
207 return true;
208 }
209 }
210 return false;
211 }
212
222 static inline void cb_waitpoint_wakeup_at(cb_waitpoint_t *wp, uint64_t time, bool dosleep){
223 wp->time=time;
224 wp->wakeup = true;
225 if(dosleep){
226 CB_THREAD_COND_WAIT(&wp->condition, &wp->lock);
227 }
228 }
229
236 #define cb_waitpoint_wakeup(wp) CB_THREAD_COND_SIGNAL(&(wp)->condition)
237
244 #define cb_waitpoint_broadcast(wp) CB_THREAD_COND_BROADCAST(&(wp)->condition)
245
250 #define cb_waitpoint_lock(wp) CB_THREAD_MUTEX_LOCK(&(wp)->lock)
251
256 #define cb_waitpoint_unlock(wp) CB_THREAD_MUTEX_UNLOCK(&(wp)->lock)
257
266 int cb_killproc(int64_t objnum, bool thread_mode);
267
268 void cb_waitproc(int64_t objnum, bool thread_mode);
269
270 #endif
271
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:222
CB_THREAD_MUTEX_DESTROY
#define CB_THREAD_MUTEX_DESTROY
Definition: cb_thread.h:104
CB_THREAD_COND_DESTROY
#define CB_THREAD_COND_DESTROY
Definition: cb_thread.h:112
cb_waitpoint_t::time
uint64_t time
Definition: cb_thread.h:169
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:164
cb_waitpoint_t::condition
CB_THREAD_COND_T condition
Definition: cb_thread.h:167
CB_THREAD_COND_T
#define CB_THREAD_COND_T
Definition: cb_thread.h:110
cb_waitpoint_t::lock
CB_THREAD_MUTEX_T lock
Definition: cb_thread.h:165
CB_THREAD_COND_WAIT
#define CB_THREAD_COND_WAIT
Definition: cb_thread.h:113
unibase.h
unibase general global header
CB_THREAD_MUTEX_INIT
#define CB_THREAD_MUTEX_INIT
Definition: cb_thread.h:103
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:171
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:201
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:179
CB_THREAD_COND_INIT
#define CB_THREAD_COND_INIT
Definition: cb_thread.h:111
cb_waitproc
void cb_waitproc(int64_t objnum, bool thread_mode)
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:190
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:137
CB_THREAD_MUTEX_T
#define CB_THREAD_MUTEX_T
Definition: cb_thread.h:98
cb_tsn_thread_attr_t
parameters to initialize thread
Definition: cb_thread.h:72
cb_killproc
int cb_killproc(int64_t objnum, bool thread_mode)
kill forcefully running process or thread
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:150