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_<original_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 some 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(...) _posix_zephyr_main(__VA_ARGS__)
45 #endif
46 
47 #if defined(__cplusplus)
48 /* To be able to define main() in C++ code we need to have its prototype
49  * defined somewhere visibly. Otherwise name mangling will prevent the linker
50  * from finding it. Zephyr assumes an int main(void) prototype and therefore
51  * this will be the prototype after renaming:
52  */
53 extern "C" int _posix_zephyr_main(void);
54 #endif
55 
56 #ifdef CONFIG_POSIX_API
57 
58 /*
59  * The defines below in this header exist only to enable the Zephyr POSIX API
60  * (include/posix/), and applications using it, to be compiled on top of
61  * native_posix.
62  *
63  * Without this header, both the Zephyr POSIX API functions and the equivalent
64  * host OS functions would have the same name. This would result in the linker
65  * not picking the correct ones.
66  *
67  * Renaming these functions allows the linker to distinguish
68  * which calls are meant for the Zephyr POSIX API (zap_something), and
69  * which are meant for the host OS.
70  *
71  * The zap_ prefix should be understood as an attempt to namespace them
72  * into something which is unlikely to collide with other real functions
73  * (Any unlikely string would have done)
74  *
75  * If you want to link an external library together with Zephyr code for the
76  * native_posix target, where that external library calls into the Zephyr
77  * POSIX API, you may want to include this header when compiling that library,
78  * or rename the calls to match the ones in the defines below.
79  */
80 
81 /* Condition variables */
82 #define pthread_cond_init(...)        zap_pthread_cond_init(__VA_ARGS__)
83 #define pthread_cond_destroy(...)     zap_pthread_cond_destroy(__VA_ARGS__)
84 #define pthread_cond_signal(...)      zap_pthread_cond_signal(__VA_ARGS__)
85 #define pthread_cond_broadcast(...)   zap_pthread_cond_broadcast(__VA_ARGS__)
86 #define pthread_cond_wait(...)        zap_pthread_cond_wait(__VA_ARGS__)
87 #define pthread_cond_timedwait(...)   zap_pthread_cond_timedwait(__VA_ARGS__)
88 #define pthread_condattr_init(...)    zap_pthread_condattr_init(__VA_ARGS__)
89 #define pthread_condattr_destroy(...) zap_pthread_condattr_destroy(__VA_ARGS__)
90 
91 /* Semaphore */
92 #define sem_destroy(...)	      zap_sem_destroy(__VA_ARGS__)
93 #define sem_getvalue(...)	      zap_sem_getvalue(__VA_ARGS__)
94 #define sem_init(...)		      zap_sem_init(__VA_ARGS__)
95 #define sem_post(...)		      zap_sem_post(__VA_ARGS__)
96 #define sem_timedwait(...)	      zap_sem_timedwait(__VA_ARGS__)
97 #define sem_trywait(...)	      zap_sem_trywait(__VA_ARGS__)
98 #define sem_wait(...)		      zap_sem_wait(__VA_ARGS__)
99 
100 /* Mutex */
101 #define pthread_mutex_init(...)       zap_pthread_mutex_init(__VA_ARGS__)
102 #define pthread_mutex_destroy(...)    zap_pthread_mutex_destroy(__VA_ARGS__)
103 #define pthread_mutex_lock(...)       zap_pthread_mutex_lock(__VA_ARGS__)
104 #define pthread_mutex_timedlock(...)  zap_pthread_mutex_timedlock(__VA_ARGS__)
105 #define pthread_mutex_trylock(...)    zap_pthread_mutex_trylock(__VA_ARGS__)
106 #define pthread_mutex_unlock(...)     zap_pthread_mutex_unlock(__VA_ARGS__)
107 #define pthread_mutexattr_init(...)   zap_pthread_mutexattr_init(__VA_ARGS__)
108 #define pthread_mutexattr_destroy(...) \
109 		zap_pthread_mutexattr_destroy(__VA_ARGS__)
110 /* Barrier */
111 #define pthread_barrier_wait(...)     zap_pthread_barrier_wait(__VA_ARGS__)
112 #define pthread_barrier_init(...)     zap_pthread_barrier_init(__VA_ARGS__)
113 #define pthread_barrier_destroy(...)  zap_pthread_barrier_destroy(__VA_ARGS__)
114 #define pthread_barrierattr_init(...) zap_pthread_barrierattr_init(__VA_ARGS__)
115 #define pthread_barrierattr_destroy(...) \
116 	zap_pthread_barrierattr_destroy(__VA_ARGS__)
117 
118 /* Pthread */
119 #define pthread_attr_init(...)		zap_pthread_attr_init(__VA_ARGS__)
120 #define pthread_attr_destroy(...)	zap_pthread_attr_destroy(__VA_ARGS__)
121 #define pthread_attr_getschedparam(...) \
122 			zap_pthread_attr_getschedparam(__VA_ARGS__)
123 #define pthread_attr_getstack(...)	zap_pthread_attr_getstack(__VA_ARGS__)
124 #define pthread_attr_getstacksize(...)	\
125 			zap_pthread_attr_getstacksize(__VA_ARGS__)
126 #define pthread_equal(...)		zap_pthread_equal(__VA_ARGS__)
127 #define pthread_self(...)		zap_pthread_self(__VA_ARGS__)
128 #define pthread_getschedparam(...)	zap_pthread_getschedparam(__VA_ARGS__)
129 #define pthread_once(...)		zap_pthread_once(__VA_ARGS__)
130 #define pthread_exit(...)		zap_pthread_exit(__VA_ARGS__)
131 #define pthread_join(...)		zap_pthread_join(__VA_ARGS__)
132 #define pthread_detach(...)		zap_pthread_detach(__VA_ARGS__)
133 #define pthread_cancel(...)		zap_pthread_cancel(__VA_ARGS__)
134 #define pthread_attr_getdetachstate(...)	\
135 		zap_pthread_attr_getdetachstate(__VA_ARGS__)
136 #define pthread_attr_setdetachstate(...)	\
137 		zap_pthread_attr_setdetachstate(__VA_ARGS__)
138 #define pthread_attr_setschedparam(...)	\
139 		zap_pthread_attr_setschedparam(__VA_ARGS__)
140 #define pthread_attr_setschedpolicy(...)\
141 		zap_pthread_attr_setschedpolicy(__VA_ARGS__)
142 #define pthread_attr_getschedpolicy(...)\
143 		zap_pthread_attr_getschedpolicy(__VA_ARGS__)
144 
145 #define pthread_attr_setstack(...)	zap_pthread_attr_setstack(__VA_ARGS__)
146 #define pthread_create(...)		zap_pthread_create(__VA_ARGS__)
147 #define pthread_setcancelstate(...)	zap_pthread_setcancelstate(__VA_ARGS__)
148 #define pthread_setschedparam(...)	zap_pthread_setschedparam(__VA_ARGS__)
149 
150 /* Scheduler */
151 #define sched_yield(...)		zap_sched_yield(__VA_ARGS__)
152 #define sched_get_priority_min(...)	zap_sched_get_priority_min(__VA_ARGS__)
153 #define sched_get_priority_max(...)	zap_sched_get_priority_max(__VA_ARGS__)
154 #define sched_getparam(...)		zap_sched_getparam(__VA_ARGS__)
155 #define sched_getscheduler(...)		zap_sched_getscheduler(__VA_ARGS__)
156 
157 /* Sleep */
158 #define sleep(...)			zap_sleep(__VA_ARGS__)
159 #define usleep(...)			zap_usleep(__VA_ARGS__)
160 
161 /* Clock */
162 #define clock_gettime(...)		zap_clock_gettime(__VA_ARGS__)
163 #define clock_settime(...)		zap_clock_settime(__VA_ARGS__)
164 #define gettimeofday(...)		zap_clock_gettimeofday(__VA_ARGS__)
165 
166 /* Timer */
167 #define timer_create(...)	zap_timer_create(__VA_ARGS__)
168 #define timer_delete(...)	zap_timer_delete(__VA_ARGS__)
169 #define timer_gettime(...)	zap_timer_gettime(__VA_ARGS__)
170 #define timer_settime(...)	zap_timer_settime(__VA_ARGS__)
171 
172 /* Read/Write lock */
173 #define pthread_rwlock_destroy(...)	zap_pthread_rwlock_destroy(__VA_ARGS__)
174 #define pthread_rwlock_init(...)	zap_pthread_rwlock_init(__VA_ARGS__)
175 #define pthread_rwlock_rdlock(...)	zap_pthread_rwlock_rdlock(__VA_ARGS__)
176 #define pthread_rwlock_unlock(...)	zap_pthread_rwlock_unlock(__VA_ARGS__)
177 #define pthread_rwlock_wrlock(...)	zap_pthread_rwlock_wrlock(__VA_ARGS__)
178 #define pthread_rwlockattr_init(...)	zap_pthread_rwlockattr_init(__VA_ARGS__)
179 #define pthread_rwlock_timedrdlock(...)\
180 		zap_pthread_rwlock_timedrdlock(__VA_ARGS__)
181 #define pthread_rwlock_timedwrlock(...)\
182 		zap_pthread_rwlock_timedwrlock(__VA_ARGS__)
183 #define pthread_rwlock_tryrdlock(...)\
184 		zap_pthread_rwlock_tryrdlock(__VA_ARGS__)
185 #define pthread_rwlock_trywrlock(...)\
186 		zap_pthread_rwlock_trywrlock(__VA_ARGS__)
187 #define pthread_rwlockattr_destroy(...)\
188 		zap_pthread_rwlockattr_destroy(__VA_ARGS__)
189 
190 /* Pthread key */
191 #define pthread_key_create(...)		zap_pthread_key_create(__VA_ARGS__)
192 #define pthread_key_delete(...)		zap_pthread_key_delete(__VA_ARGS__)
193 #define pthread_setspecific(...)	zap_pthread_setspecific(__VA_ARGS__)
194 #define pthread_getspecific(...)	zap_pthread_getspecific(__VA_ARGS__)
195 
196 /* message queue */
197 #define mq_open(...)	zap_mq_open(__VA_ARGS__)
198 #define mq_close(...)	zap_mq_close(__VA_ARGS__)
199 #define mq_unlink(...)	zap_mq_unlink(__VA_ARGS__)
200 #define mq_getattr(...)	zap_mq_getattr(__VA_ARGS__)
201 #define mq_receive(...)	zap_mq_receive(__VA_ARGS__)
202 #define mq_send(...)	zap_mq_send(__VA_ARGS__)
203 #define mq_setattr(...)	zap_mq_setattr(__VA_ARGS__)
204 #define mq_timedreceive(...)	zap_mq_timedreceive(__VA_ARGS__)
205 #define mq_timedsend(...)	zap_mq_timedsend(__VA_ARGS__)
206 
207 /* File system */
208 #define open		zap_open
209 #define close		zap_close
210 #define write		zap_write
211 #define read		zap_read
212 #define lseek		zap_lseek
213 #define opendir		zap_opendir
214 #define closedir	zap_closedir
215 #define readdir		zap_readdir
216 #define rename		zap_rename
217 #define unlink		zap_unlink
218 #define stat		zap_stat
219 #define mkdir		zap_mkdir
220 
221 /* eventfd */
222 #define eventfd		zap_eventfd
223 #define eventfd_read	zap_eventfd_read
224 #define eventfd_write	zap_eventfd_write
225 
226 #endif /* CONFIG_POSIX_API */
227 
228 #endif /* ZEPHYR_ARCH_POSIX_INCLUDE_POSIX_CHEATS_H_ */
229