1 /*
2  * Copyright (c) 2017 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * Header to be able to compile the Zephyr kernel on top of a POSIX OS via the
9  * POSIX ARCH
10  *
11  * This file is only used in the POSIX ARCH, and not in any other architecture
12  *
13  * Most users will be normally unaware of this file existence, unless they have
14  * a link issue in which their POSIX functions calls are reported in errors (as
15  * zap_<origian_func_name>).
16  * If you do see a link error telling you that zap_something is undefined, it is
17  * likely that you forgot to select the corresponding Zephyr POSIX API.
18  *
19  * This header is included automatically when targeting POSIX ARCH boards
20  * (for ex. native_posix).
21  * It will be included in _all_ Zephyr and application source files
22  * (it is passed with the option "-include" to the compiler call)
23  *
24  * A few files (those which need to access the host OS APIs) will set
25  * NO_POSIX_CHEATS to avoid including this file. These are typically only
26  * the POSIX arch private files and some of the drivers meant only for the POSIX
27  * architecture.
28  * No file which is meant to run in an embedded target should set
29  * NO_POSIX_CHEATS
30  */
31 
32 #if !defined(ZEPHYR_ARCH_POSIX_INCLUDE_POSIX_CHEATS_H_) && !defined(NO_POSIX_CHEATS)
33 #define ZEPHYR_ARCH_POSIX_INCLUDE_POSIX_CHEATS_H_
34 
35 /*
36  * Normally main() is the main entry point of a C executable.
37  * When compiling for native_posix, the Zephyr "application" is not the actual
38  * entry point of the executable but something the Zephyr OS calls during
39  * boot.
40  * Therefore we need to rename this application main something else, so
41  * we free the function name "main" for its normal purpose
42  */
43 #ifndef main
44 #define main(...) zephyr_app_main(__VA_ARGS__)
45 #endif
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 /* To be able to define main() in C++ code we need to have its prototype
51  * defined somewhere visibly. Otherwise name mangling will prevent the linker
52  * from finding it. Zephyr assumes a void main(void) prototype and therefore
53  * this will be the prototype after renaming:
54  */
55 void zephyr_app_main(void);
56 
57 #ifdef __cplusplus
58 }
59 #endif
60 
61 #ifdef CONFIG_POSIX_API
62 
63 /*
64  * The defines below in this header exist only to enable the Zephyr POSIX API
65  * (include/posix/), and applications using it, to be compiled on top of
66  * native_posix.
67  *
68  * Without this header, both the Zephyr POSIX API functions and the equivalent
69  * host OS functions would have the same name. This would result in the linker
70  * not picking the correct ones.
71  *
72  * Renaming these functions allows the linker to distinguish
73  * which calls are meant for the Zephyr POSIX API (zap_something), and
74  * which are meant for the host OS.
75  *
76  * The zap_ prefix should be understood as an attempt to namespace them
77  * into something which is unlikely to collide with other real functions
78  * (Any unlikely string would have done)
79  *
80  * If you want to link an external library together with Zephyr code for the
81  * native_posix target, where that external library calls into the Zephyr
82  * POSIX API, you may want to include this header when compiling that library,
83  * or rename the calls to match the ones in the defines below.
84  */
85 
86 /* Condition variables */
87 #define pthread_cond_init(...)        zap_pthread_cond_init(__VA_ARGS__)
88 #define pthread_cond_destroy(...)     zap_pthread_cond_destroy(__VA_ARGS__)
89 #define pthread_cond_signal(...)      zap_pthread_cond_signal(__VA_ARGS__)
90 #define pthread_cond_broadcast(...)   zap_pthread_cond_broadcast(__VA_ARGS__)
91 #define pthread_cond_wait(...)        zap_pthread_cond_wait(__VA_ARGS__)
92 #define pthread_cond_timedwait(...)   zap_pthread_cond_timedwait(__VA_ARGS__)
93 #define pthread_condattr_init(...)    zap_pthread_condattr_init(__VA_ARGS__)
94 #define pthread_condattr_destroy(...) zap_pthread_condattr_destroy(__VA_ARGS__)
95 
96 /* Semaphore */
97 #define sem_destroy(...)	      zap_sem_destroy(__VA_ARGS__)
98 #define sem_getvalue(...)	      zap_sem_getvalue(__VA_ARGS__)
99 #define sem_init(...)		      zap_sem_init(__VA_ARGS__)
100 #define sem_post(...)		      zap_sem_post(__VA_ARGS__)
101 #define sem_timedwait(...)	      zap_sem_timedwait(__VA_ARGS__)
102 #define sem_trywait(...)	      zap_sem_trywait(__VA_ARGS__)
103 #define sem_wait(...)		      zap_sem_wait(__VA_ARGS__)
104 
105 /* Mutex */
106 #define pthread_mutex_init(...)       zap_pthread_mutex_init(__VA_ARGS__)
107 #define pthread_mutex_destroy(...)    zap_pthread_mutex_destroy(__VA_ARGS__)
108 #define pthread_mutex_lock(...)       zap_pthread_mutex_lock(__VA_ARGS__)
109 #define pthread_mutex_timedlock(...)  zap_pthread_mutex_timedlock(__VA_ARGS__)
110 #define pthread_mutex_trylock(...)    zap_pthread_mutex_trylock(__VA_ARGS__)
111 #define pthread_mutex_unlock(...)     zap_pthread_mutex_unlock(__VA_ARGS__)
112 #define pthread_mutexattr_init(...)   zap_pthread_mutexattr_init(__VA_ARGS__)
113 #define pthread_mutexattr_destroy(...) \
114 		zap_pthread_mutexattr_destroy(__VA_ARGS__)
115 /* Barrier */
116 #define pthread_barrier_wait(...)     zap_pthread_barrier_wait(__VA_ARGS__)
117 #define pthread_barrier_init(...)     zap_pthread_barrier_init(__VA_ARGS__)
118 #define pthread_barrier_destroy(...)  zap_pthread_barrier_destroy(__VA_ARGS__)
119 #define pthread_barrierattr_init(...) zap_pthread_barrierattr_init(__VA_ARGS__)
120 #define pthread_barrierattr_destroy(...) \
121 	zap_pthread_barrierattr_destroy(__VA_ARGS__)
122 
123 /* Pthread */
124 #define pthread_attr_init(...)		zap_pthread_attr_init(__VA_ARGS__)
125 #define pthread_attr_destroy(...)	zap_pthread_attr_destroy(__VA_ARGS__)
126 #define pthread_attr_getschedparam(...) \
127 			zap_pthread_attr_getschedparam(__VA_ARGS__)
128 #define pthread_attr_getstack(...)	zap_pthread_attr_getstack(__VA_ARGS__)
129 #define pthread_attr_getstacksize(...)	\
130 			zap_pthread_attr_getstacksize(__VA_ARGS__)
131 #define pthread_equal(...)		zap_pthread_equal(__VA_ARGS__)
132 #define pthread_self(...)		zap_pthread_self(__VA_ARGS__)
133 #define pthread_getschedparam(...)	zap_pthread_getschedparam(__VA_ARGS__)
134 #define pthread_once(...)		zap_pthread_once(__VA_ARGS__)
135 #define pthread_exit(...)		zap_pthread_exit(__VA_ARGS__)
136 #define pthread_join(...)		zap_pthread_join(__VA_ARGS__)
137 #define pthread_detach(...)		zap_pthread_detach(__VA_ARGS__)
138 #define pthread_cancel(...)		zap_pthread_cancel(__VA_ARGS__)
139 #define pthread_attr_getdetachstate(...)	\
140 		zap_pthread_attr_getdetachstate(__VA_ARGS__)
141 #define pthread_attr_setdetachstate(...)	\
142 		zap_pthread_attr_setdetachstate(__VA_ARGS__)
143 #define pthread_attr_setschedparam(...)	\
144 		zap_pthread_attr_setschedparam(__VA_ARGS__)
145 #define pthread_attr_setschedpolicy(...)\
146 		zap_pthread_attr_setschedpolicy(__VA_ARGS__)
147 #define pthread_attr_getschedpolicy(...)\
148 		zap_pthread_attr_getschedpolicy(__VA_ARGS__)
149 
150 #define pthread_attr_setstack(...)	zap_pthread_attr_setstack(__VA_ARGS__)
151 #define pthread_create(...)		zap_pthread_create(__VA_ARGS__)
152 #define pthread_setcancelstate(...)	zap_pthread_setcancelstate(__VA_ARGS__)
153 #define pthread_setschedparam(...)	zap_pthread_setschedparam(__VA_ARGS__)
154 
155 /* Scheduler */
156 #define sched_yield(...)		zap_sched_yield(__VA_ARGS__)
157 #define sched_get_priority_min(...)	zap_sched_get_priority_min(__VA_ARGS__)
158 #define sched_get_priority_max(...)	zap_sched_get_priority_max(__VA_ARGS__)
159 
160 /* Sleep */
161 #define sleep(...)			zap_sleep(__VA_ARGS__)
162 #define usleep(...)			zap_usleep(__VA_ARGS__)
163 
164 /* Clock */
165 #define clock_gettime(...)		zap_clock_gettime(__VA_ARGS__)
166 #define clock_settime(...)		zap_clock_settime(__VA_ARGS__)
167 #define gettimeofday(...)		zap_clock_gettimeofday(__VA_ARGS__)
168 
169 /* Timer */
170 #define timer_create(...)	zap_timer_create(__VA_ARGS__)
171 #define timer_delete(...)	zap_timer_delete(__VA_ARGS__)
172 #define timer_gettime(...)	zap_timer_gettime(__VA_ARGS__)
173 #define timer_settime(...)	zap_timer_settime(__VA_ARGS__)
174 
175 /* Read/Write lock */
176 #define pthread_rwlock_destroy(...)	zap_pthread_rwlock_destroy(__VA_ARGS__)
177 #define pthread_rwlock_init(...)	zap_pthread_rwlock_init(__VA_ARGS__)
178 #define pthread_rwlock_rdlock(...)	zap_pthread_rwlock_rdlock(__VA_ARGS__)
179 #define pthread_rwlock_unlock(...)	zap_pthread_rwlock_unlock(__VA_ARGS__)
180 #define pthread_rwlock_wrlock(...)	zap_pthread_rwlock_wrlock(__VA_ARGS__)
181 #define pthread_rwlockattr_init(...)	zap_pthread_rwlockattr_init(__VA_ARGS__)
182 #define pthread_rwlock_timedrdlock(...)\
183 		zap_pthread_rwlock_timedrdlock(__VA_ARGS__)
184 #define pthread_rwlock_timedwrlock(...)\
185 		zap_pthread_rwlock_timedwrlock(__VA_ARGS__)
186 #define pthread_rwlock_tryrdlock(...)\
187 		zap_pthread_rwlock_tryrdlock(__VA_ARGS__)
188 #define pthread_rwlock_trywrlock(...)\
189 		zap_pthread_rwlock_trywrlock(__VA_ARGS__)
190 #define pthread_rwlockattr_destroy(...)\
191 		zap_pthread_rwlockattr_destroy(__VA_ARGS__)
192 
193 /* Pthread key */
194 #define pthread_key_create(...)		zap_pthread_key_create(__VA_ARGS__)
195 #define pthread_key_delete(...)		zap_pthread_key_delete(__VA_ARGS__)
196 #define pthread_setspecific(...)	zap_pthread_setspecific(__VA_ARGS__)
197 #define pthread_getspecific(...)	zap_pthread_getspecific(__VA_ARGS__)
198 
199 /* message queue */
200 #define mq_open(...)	zap_mq_open(__VA_ARGS__)
201 #define mq_close(...)	zap_mq_close(__VA_ARGS__)
202 #define mq_unlink(...)	zap_mq_unlink(__VA_ARGS__)
203 #define mq_getattr(...)	zap_mq_getattr(__VA_ARGS__)
204 #define mq_receive(...)	zap_mq_receive(__VA_ARGS__)
205 #define mq_send(...)	zap_mq_send(__VA_ARGS__)
206 #define mq_setattr(...)	zap_mq_setattr(__VA_ARGS__)
207 #define mq_timedreceive(...)	zap_mq_timedreceive(__VA_ARGS__)
208 #define mq_timedsend(...)	zap_mq_timedsend(__VA_ARGS__)
209 
210 /* File system */
211 #define open		zap_open
212 #define close		zap_close
213 #define write		zap_write
214 #define read		zap_read
215 #define lseek		zap_lseek
216 #define opendir		zap_opendir
217 #define closedir	zap_closedir
218 #define readdir		zap_readdir
219 #define rename		zap_rename
220 #define unlink		zap_unlink
221 #define stat		zap_stat
222 #define mkdir		zap_mkdir
223 
224 /* eventfd */
225 #define eventfd		zap_eventfd
226 #define eventfd_read	zap_eventfd_read
227 #define eventfd_write	zap_eventfd_write
228 
229 #endif /* CONFIG_POSIX_API */
230 
231 #endif /* ZEPHYR_ARCH_POSIX_INCLUDE_POSIX_CHEATS_H_ */
232