vxworks.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // (C) Copyright Dustin Spicuzza 2009.
  2. // Adapted to vxWorks 6.9 by Peter Brockamp 2012.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for most recent version.
  7. // Since WRS does not yet properly support boost under vxWorks
  8. // and this file was badly outdated, but I was keen on using it,
  9. // I patched boost myself to make things work. This has been tested
  10. // and adapted by me for vxWorks 6.9 *only*, as I'm lacking access
  11. // to earlier 6.X versions! The only thing I know for sure is that
  12. // very old versions of vxWorks (namely everything below 6.x) are
  13. // absolutely unable to use boost. This is mainly due to the completely
  14. // outdated libraries and ancient compiler (GCC 2.96 or worse). Do
  15. // not even think of getting this to work, a miserable failure will
  16. // be guaranteed!
  17. // Equally, this file has been tested for RTPs (Real Time Processes)
  18. // only, not for DKMs (Downloadable Kernel Modules). These two types
  19. // of executables differ largely in the available functionality of
  20. // the C-library, STL, and so on. A DKM uses a library similar to those
  21. // of vxWorks 5.X - with all its limitations and incompatibilities
  22. // with respect to ANSI C++ and STL. So probably there might be problems
  23. // with the usage of boost from DKMs. WRS or any voluteers are free to
  24. // prove the opposite!
  25. // ====================================================================
  26. //
  27. // Some important information regarding the usage of POSIX semaphores:
  28. // -------------------------------------------------------------------
  29. //
  30. // VxWorks as a real time operating system handles threads somewhat
  31. // different from what "normal" OSes do, regarding their scheduling!
  32. // This could lead to a scenario called "priority inversion" when using
  33. // semaphores, see http://en.wikipedia.org/wiki/Priority_inversion.
  34. //
  35. // Now, VxWorks POSIX-semaphores for DKM's default to the usage of
  36. // priority inverting semaphores, which is fine. On the other hand,
  37. // for RTP's it defaults to using non priority inverting semaphores,
  38. // which could easily pose a serious problem for a real time process,
  39. // i.e. deadlocks! To overcome this two possibilities do exist:
  40. //
  41. // a) Patch every piece of boost that uses semaphores to instanciate
  42. // the proper type of semaphores. This is non-intrusive with respect
  43. // to the OS and could relatively easy been done by giving all
  44. // semaphores attributes deviating from the default (for in-depth
  45. // information see the POSIX functions pthread_mutexattr_init()
  46. // and pthread_mutexattr_setprotocol()). However this breaks all
  47. // too easily, as with every new version some boost library could
  48. // all in a sudden start using semaphores, resurrecting the very
  49. // same, hard to locate problem over and over again!
  50. //
  51. // b) We could change the default properties for POSIX-semaphores
  52. // that VxWorks uses for RTP's and this is being suggested here,
  53. // as it will more or less seamlessly integrate with boost. I got
  54. // the following information from WRS how to do this, compare
  55. // Wind River TSR# 1209768:
  56. //
  57. // Instructions for changing the default properties of POSIX-
  58. // semaphores for RTP's in VxWorks 6.9:
  59. // - Edit the file /vxworks-6.9/target/usr/src/posix/pthreadLib.c
  60. // in the root of your Workbench-installation.
  61. // - Around line 917 there should be the definition of the default
  62. // mutex attributes:
  63. //
  64. // LOCAL pthread_mutexattr_t defaultMutexAttr =
  65. // {
  66. // PTHREAD_INITIALIZED_OBJ, PTHREAD_PRIO_NONE, 0,
  67. // PTHREAD_MUTEX_DEFAULT
  68. // };
  69. //
  70. // Here, replace PTHREAD_PRIO_NONE by PTHREAD_PRIO_INHERIT.
  71. // - Around line 1236 there should be a definition for the function
  72. // pthread_mutexattr_init(). A couple of lines below you should
  73. // find a block of code like this:
  74. //
  75. // pAttr->mutexAttrStatus = PTHREAD_INITIALIZED_OBJ;
  76. // pAttr->mutexAttrProtocol = PTHREAD_PRIO_NONE;
  77. // pAttr->mutexAttrPrioceiling = 0;
  78. // pAttr->mutexAttrType = PTHREAD_MUTEX_DEFAULT;
  79. //
  80. // Here again, replace PTHREAD_PRIO_NONE by PTHREAD_PRIO_INHERIT.
  81. // - Finally, rebuild your VSB. This will create a new VxWorks kernel
  82. // with the changed properties. That's it! Now, using boost should
  83. // no longer cause any problems with task deadlocks!
  84. //
  85. // And here's another useful piece of information concerning VxWorks'
  86. // POSIX-functionality in general:
  87. // VxWorks is not a genuine POSIX-OS in itself, rather it is using a
  88. // kind of compatibility layer (sort of a wrapper) to emulate the
  89. // POSIX-functionality by using its own resources and functions.
  90. // At the time a task (thread) calls it's first POSIX-function during
  91. // runtime it is being transformed by the OS into a POSIX-thread.
  92. // This transformation does include a call to malloc() to allocate the
  93. // memory required for the housekeeping of POSIX-threads. In a high
  94. // priority RTP this malloc() call may be highly undesirable, as its
  95. // timing is more or less unpredictable (depending on what your actual
  96. // heap looks like). You can circumvent this problem by calling the
  97. // function thread_self() at a well defined point in the code of the
  98. // task, e.g. shortly after the task spawns up. Thereby you are able
  99. // to define the time when the task-transformation will take place and
  100. // you could shift it to an uncritical point where a malloc() call is
  101. // tolerable. So, if this could pose a problem for your code, remember
  102. // to call thread_self() from the affected task at an early stage.
  103. //
  104. // ====================================================================
  105. // Block out all versions before vxWorks 6.x, as these don't work:
  106. // Include header with the vxWorks version information and query them
  107. #include <version.h>
  108. #if !defined(_WRS_VXWORKS_MAJOR) || (_WRS_VXWORKS_MAJOR < 6)
  109. # error "The vxWorks version you're using is so badly outdated,\
  110. it doesn't work at all with boost, sorry, no chance!"
  111. #endif
  112. // Handle versions above 5.X but below 6.9
  113. #if (_WRS_VXWORKS_MAJOR == 6) && (_WRS_VXWORKS_MINOR < 9)
  114. // TODO: Starting from what version does vxWorks work with boost?
  115. // We can't reasonably insert a #warning "" as a user hint here,
  116. // as this will show up with every file including some boost header,
  117. // badly bugging the user... So for the time being we just leave it.
  118. #endif
  119. // vxWorks specific config options:
  120. // --------------------------------
  121. #define BOOST_PLATFORM "vxWorks"
  122. // Special behaviour for DKMs:
  123. #ifdef _WRS_KERNEL
  124. // DKMs do not have the <cwchar>-header,
  125. // but apparently they do have an intrinsic wchar_t meanwhile!
  126. # define BOOST_NO_CWCHAR
  127. // Lots of wide-functions and -headers are unavailable for DKMs as well:
  128. # define BOOST_NO_CWCTYPE
  129. # define BOOST_NO_SWPRINTF
  130. # define BOOST_NO_STD_WSTRING
  131. # define BOOST_NO_STD_WSTREAMBUF
  132. #endif
  133. // Generally available headers:
  134. #define BOOST_HAS_UNISTD_H
  135. #define BOOST_HAS_STDINT_H
  136. #define BOOST_HAS_DIRENT_H
  137. #define BOOST_HAS_SLIST
  138. // vxWorks does not have installed an iconv-library by default,
  139. // so unfortunately no Unicode support from scratch is available!
  140. // Thus, instead it is suggested to switch to ICU, as this seems
  141. // to be the most complete and portable option...
  142. #define BOOST_LOCALE_WITH_ICU
  143. // Generally available functionality:
  144. #define BOOST_HAS_THREADS
  145. #define BOOST_HAS_NANOSLEEP
  146. #define BOOST_HAS_GETTIMEOFDAY
  147. #define BOOST_HAS_CLOCK_GETTIME
  148. #define BOOST_HAS_MACRO_USE_FACET
  149. // Generally unavailable functionality, delivered by boost's test function:
  150. //#define BOOST_NO_DEDUCED_TYPENAME // Commented this out, boost's test gives an errorneous result!
  151. #define BOOST_NO_CXX11_EXTERN_TEMPLATE
  152. #define BOOST_NO_CXX11_VARIADIC_MACROS
  153. // Generally available threading API's:
  154. #define BOOST_HAS_PTHREADS
  155. #define BOOST_HAS_SCHED_YIELD
  156. #define BOOST_HAS_SIGACTION
  157. // Functionality available for RTPs only:
  158. #ifdef __RTP__
  159. # define BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE
  160. # define BOOST_HAS_LOG1P
  161. # define BOOST_HAS_EXPM1
  162. #endif
  163. // Functionality available for DKMs only:
  164. #ifdef _WRS_KERNEL
  165. // Luckily, at the moment there seems to be none!
  166. #endif
  167. // These #defines allow posix_features to work, since vxWorks doesn't
  168. // #define them itself for DKMs (for RTPs on the contrary it does):
  169. #ifdef _WRS_KERNEL
  170. # ifndef _POSIX_TIMERS
  171. # define _POSIX_TIMERS 1
  172. # endif
  173. # ifndef _POSIX_THREADS
  174. # define _POSIX_THREADS 1
  175. # endif
  176. #endif
  177. // vxWorks doesn't work with asio serial ports:
  178. #define BOOST_ASIO_DISABLE_SERIAL_PORT
  179. // TODO: The problem here seems to bee that vxWorks uses its own, very specific
  180. // ways to handle serial ports, incompatible with POSIX or anything...
  181. // Maybe a specific implementation would be possible, but until the
  182. // straight need arises... This implementation would presumably consist
  183. // of some vxWorks specific ioctl-calls, etc. Any voluteers?
  184. // vxWorks-around: <time.h> #defines CLOCKS_PER_SEC as sysClkRateGet() but
  185. // miserably fails to #include the required <sysLib.h> to make
  186. // sysClkRateGet() available! So we manually include it here.
  187. #ifdef __RTP__
  188. # include <time.h>
  189. # include <sysLib.h>
  190. #endif
  191. // vxWorks-around: In <stdint.h> the macros INT32_C(), UINT32_C(), INT64_C() and
  192. // UINT64_C() are defined errorneously, yielding not a signed/
  193. // unsigned long/long long type, but a signed/unsigned int/long
  194. // type. Eventually this leads to compile errors in ratio_fwd.hpp,
  195. // when trying to define several constants which do not fit into a
  196. // long type! We correct them here by redefining.
  197. #include <cstdint>
  198. // Some macro-magic to do the job
  199. #define VX_JOIN(X, Y) VX_DO_JOIN(X, Y)
  200. #define VX_DO_JOIN(X, Y) VX_DO_JOIN2(X, Y)
  201. #define VX_DO_JOIN2(X, Y) X##Y
  202. // Correctly setup the macros
  203. #undef INT32_C
  204. #undef UINT32_C
  205. #undef INT64_C
  206. #undef UINT64_C
  207. #define INT32_C(x) VX_JOIN(x, L)
  208. #define UINT32_C(x) VX_JOIN(x, UL)
  209. #define INT64_C(x) VX_JOIN(x, LL)
  210. #define UINT64_C(x) VX_JOIN(x, ULL)
  211. // #include Libraries required for the following function adaption
  212. #include <ioLib.h>
  213. #include <tickLib.h>
  214. #include <sys/time.h>
  215. // Use C-linkage for the following helper functions
  216. extern "C" {
  217. // vxWorks-around: The required functions getrlimit() and getrlimit() are missing.
  218. // But we have the similar functions getprlimit() and setprlimit(),
  219. // which may serve the purpose.
  220. // Problem: The vxWorks-documentation regarding these functions
  221. // doesn't deserve its name! It isn't documented what the first two
  222. // parameters idtype and id mean, so we must fall back to an educated
  223. // guess - null, argh... :-/
  224. // TODO: getprlimit() and setprlimit() do exist for RTPs only, for whatever reason.
  225. // Thus for DKMs there would have to be another implementation.
  226. #ifdef __RTP__
  227. inline int getrlimit(int resource, struct rlimit *rlp){
  228. return getprlimit(0, 0, resource, rlp);
  229. }
  230. inline int setrlimit(int resource, const struct rlimit *rlp){
  231. return setprlimit(0, 0, resource, const_cast<struct rlimit*>(rlp));
  232. }
  233. #endif
  234. // vxWorks has ftruncate() only, so we do simulate truncate():
  235. inline int truncate(const char *p, off_t l){
  236. int fd = open(p, O_WRONLY);
  237. if (fd == -1){
  238. errno = EACCES;
  239. return -1;
  240. }
  241. if (ftruncate(fd, l) == -1){
  242. close(fd);
  243. errno = EACCES;
  244. return -1;
  245. }
  246. return close(fd);
  247. }
  248. // Fake symlink handling by dummy functions:
  249. inline int symlink(const char*, const char*){
  250. // vxWorks has no symlinks -> always return an error!
  251. errno = EACCES;
  252. return -1;
  253. }
  254. inline ssize_t readlink(const char*, char*, size_t){
  255. // vxWorks has no symlinks -> always return an error!
  256. errno = EACCES;
  257. return -1;
  258. }
  259. // vxWorks claims to implement gettimeofday in sys/time.h
  260. // but nevertheless does not provide it! See
  261. // https://support.windriver.com/olsPortal/faces/maintenance/techtipDetail_noHeader.jspx?docId=16442&contentId=WR_TECHTIP_006256
  262. // We implement a surrogate version here via clock_gettime:
  263. inline int gettimeofday(struct timeval *tv, void * /*tzv*/) {
  264. struct timespec ts;
  265. clock_gettime(CLOCK_MONOTONIC, &ts);
  266. tv->tv_sec = ts.tv_sec;
  267. tv->tv_usec = ts.tv_nsec / 1000;
  268. return 0;
  269. }
  270. // vxWorks does provide neither struct tms nor function times()!
  271. // We implement an empty dummy-function, simply setting the user
  272. // and system time to the half of thew actual system ticks-value
  273. // and the child user and system time to 0.
  274. // Rather ugly but at least it suppresses compiler errors...
  275. // Unfortunately, this of course *does* have an severe impact on
  276. // dependant libraries, actually this is chrono only! Here it will
  277. // not be possible to correctly use user and system times! But
  278. // as vxWorks is lacking the ability to calculate user and system
  279. // process times there seems to be no other possible solution.
  280. struct tms{
  281. clock_t tms_utime; // User CPU time
  282. clock_t tms_stime; // System CPU time
  283. clock_t tms_cutime; // User CPU time of terminated child processes
  284. clock_t tms_cstime; // System CPU time of terminated child processes
  285. };
  286. inline clock_t times(struct tms *t){
  287. struct timespec ts;
  288. clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
  289. clock_t ticks(static_cast<clock_t>(static_cast<double>(ts.tv_sec) * CLOCKS_PER_SEC +
  290. static_cast<double>(ts.tv_nsec) * CLOCKS_PER_SEC / 1000000.0));
  291. t->tms_utime = ticks/2U;
  292. t->tms_stime = ticks/2U;
  293. t->tms_cutime = 0; // vxWorks is lacking the concept of a child process!
  294. t->tms_cstime = 0; // -> Set the wait times for childs to 0
  295. return ticks;
  296. }
  297. } // extern "C"
  298. // Put the selfmade functions into the std-namespace, just in case
  299. namespace std {
  300. # ifdef __RTP__
  301. using ::getrlimit;
  302. using ::setrlimit;
  303. # endif
  304. using ::truncate;
  305. using ::symlink;
  306. using ::readlink;
  307. using ::times;
  308. using ::gettimeofday;
  309. }
  310. // Some more macro-magic:
  311. // vxWorks-around: Some functions are not present or broken in vxWorks
  312. // but may be patched to life via helper macros...
  313. // Include signal.h which might contain a typo to be corrected here
  314. #include <signal.h>
  315. #define getpagesize() sysconf(_SC_PAGESIZE) // getpagesize is deprecated anyway!
  316. #ifndef S_ISSOCK
  317. # define S_ISSOCK(mode) ((mode & S_IFMT) == S_IFSOCK) // Is file a socket?
  318. #endif
  319. #define lstat(p, b) stat(p, b) // lstat() == stat(), as vxWorks has no symlinks!
  320. #ifndef FPE_FLTINV
  321. # define FPE_FLTINV (FPE_FLTSUB+1) // vxWorks has no FPE_FLTINV, so define one as a dummy
  322. #endif
  323. #if !defined(BUS_ADRALN) && defined(BUS_ADRALNR)
  324. # define BUS_ADRALN BUS_ADRALNR // Correct a supposed typo in vxWorks' <signal.h>
  325. #endif
  326. //typedef int locale_t; // locale_t is a POSIX-extension, currently unpresent in vxWorks!
  327. // #include boilerplate code:
  328. #include <boost/config/posix_features.hpp>
  329. // vxWorks lies about XSI conformance, there is no nl_types.h:
  330. #undef BOOST_HAS_NL_TYPES_H