Chris@16: // (C) Copyright Dustin Spicuzza 2009. Chris@16: // Adapted to vxWorks 6.9 by Peter Brockamp 2012. Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org for most recent version. Chris@16: Chris@16: // Since WRS does not yet properly support boost under vxWorks Chris@16: // and this file was badly outdated, but I was keen on using it, Chris@16: // I patched boost myself to make things work. This has been tested Chris@16: // and adapted by me for vxWorks 6.9 *only*, as I'm lacking access Chris@16: // to earlier 6.X versions! The only thing I know for sure is that Chris@16: // very old versions of vxWorks (namely everything below 6.x) are Chris@16: // absolutely unable to use boost. This is mainly due to the completely Chris@16: // outdated libraries and ancient compiler (GCC 2.96 or worse). Do Chris@16: // not even think of getting this to work, a miserable failure will Chris@16: // be guaranteed! Chris@16: // Equally, this file has been tested for RTPs (Real Time Processes) Chris@16: // only, not for DKMs (Downloadable Kernel Modules). These two types Chris@16: // of executables differ largely in the available functionality of Chris@16: // the C-library, STL, and so on. A DKM uses a library similar to those Chris@16: // of vxWorks 5.X - with all its limitations and incompatibilities Chris@16: // with respect to ANSI C++ and STL. So probably there might be problems Chris@16: // with the usage of boost from DKMs. WRS or any voluteers are free to Chris@16: // prove the opposite! Chris@16: Chris@16: // ==================================================================== Chris@16: // Chris@16: // Some important information regarding the usage of POSIX semaphores: Chris@16: // ------------------------------------------------------------------- Chris@16: // Chris@16: // VxWorks as a real time operating system handles threads somewhat Chris@16: // different from what "normal" OSes do, regarding their scheduling! Chris@16: // This could lead to a scenario called "priority inversion" when using Chris@16: // semaphores, see http://en.wikipedia.org/wiki/Priority_inversion. Chris@16: // Chris@16: // Now, VxWorks POSIX-semaphores for DKM's default to the usage of Chris@16: // priority inverting semaphores, which is fine. On the other hand, Chris@16: // for RTP's it defaults to using non priority inverting semaphores, Chris@16: // which could easily pose a serious problem for a real time process, Chris@16: // i.e. deadlocks! To overcome this two possibilities do exist: Chris@16: // Chris@16: // a) Patch every piece of boost that uses semaphores to instanciate Chris@16: // the proper type of semaphores. This is non-intrusive with respect Chris@16: // to the OS and could relatively easy been done by giving all Chris@16: // semaphores attributes deviating from the default (for in-depth Chris@16: // information see the POSIX functions pthread_mutexattr_init() Chris@16: // and pthread_mutexattr_setprotocol()). However this breaks all Chris@16: // too easily, as with every new version some boost library could Chris@16: // all in a sudden start using semaphores, resurrecting the very Chris@16: // same, hard to locate problem over and over again! Chris@16: // Chris@16: // b) We could change the default properties for POSIX-semaphores Chris@16: // that VxWorks uses for RTP's and this is being suggested here, Chris@16: // as it will more or less seamlessly integrate with boost. I got Chris@16: // the following information from WRS how to do this, compare Chris@16: // Wind River TSR# 1209768: Chris@16: // Chris@16: // Instructions for changing the default properties of POSIX- Chris@16: // semaphores for RTP's in VxWorks 6.9: Chris@16: // - Edit the file /vxworks-6.9/target/usr/src/posix/pthreadLib.c Chris@16: // in the root of your Workbench-installation. Chris@16: // - Around line 917 there should be the definition of the default Chris@16: // mutex attributes: Chris@16: // Chris@16: // LOCAL pthread_mutexattr_t defaultMutexAttr = Chris@16: // { Chris@16: // PTHREAD_INITIALIZED_OBJ, PTHREAD_PRIO_NONE, 0, Chris@16: // PTHREAD_MUTEX_DEFAULT Chris@16: // }; Chris@16: // Chris@16: // Here, replace PTHREAD_PRIO_NONE by PTHREAD_PRIO_INHERIT. Chris@16: // - Around line 1236 there should be a definition for the function Chris@16: // pthread_mutexattr_init(). A couple of lines below you should Chris@16: // find a block of code like this: Chris@16: // Chris@16: // pAttr->mutexAttrStatus = PTHREAD_INITIALIZED_OBJ; Chris@16: // pAttr->mutexAttrProtocol = PTHREAD_PRIO_NONE; Chris@16: // pAttr->mutexAttrPrioceiling = 0; Chris@16: // pAttr->mutexAttrType = PTHREAD_MUTEX_DEFAULT; Chris@16: // Chris@16: // Here again, replace PTHREAD_PRIO_NONE by PTHREAD_PRIO_INHERIT. Chris@16: // - Finally, rebuild your VSB. This will create a new VxWorks kernel Chris@16: // with the changed properties. That's it! Now, using boost should Chris@16: // no longer cause any problems with task deadlocks! Chris@16: // Chris@16: // And here's another useful piece of information concerning VxWorks' Chris@16: // POSIX-functionality in general: Chris@16: // VxWorks is not a genuine POSIX-OS in itself, rather it is using a Chris@16: // kind of compatibility layer (sort of a wrapper) to emulate the Chris@16: // POSIX-functionality by using its own resources and functions. Chris@16: // At the time a task (thread) calls it's first POSIX-function during Chris@16: // runtime it is being transformed by the OS into a POSIX-thread. Chris@16: // This transformation does include a call to malloc() to allocate the Chris@16: // memory required for the housekeeping of POSIX-threads. In a high Chris@16: // priority RTP this malloc() call may be highly undesirable, as its Chris@16: // timing is more or less unpredictable (depending on what your actual Chris@16: // heap looks like). You can circumvent this problem by calling the Chris@16: // function thread_self() at a well defined point in the code of the Chris@16: // task, e.g. shortly after the task spawns up. Thereby you are able Chris@16: // to define the time when the task-transformation will take place and Chris@16: // you could shift it to an uncritical point where a malloc() call is Chris@16: // tolerable. So, if this could pose a problem for your code, remember Chris@16: // to call thread_self() from the affected task at an early stage. Chris@16: // Chris@16: // ==================================================================== Chris@16: Chris@16: // Block out all versions before vxWorks 6.x, as these don't work: Chris@16: // Include header with the vxWorks version information and query them Chris@16: #include Chris@16: #if !defined(_WRS_VXWORKS_MAJOR) || (_WRS_VXWORKS_MAJOR < 6) Chris@16: # error "The vxWorks version you're using is so badly outdated,\ Chris@16: it doesn't work at all with boost, sorry, no chance!" Chris@16: #endif Chris@16: Chris@16: // Handle versions above 5.X but below 6.9 Chris@16: #if (_WRS_VXWORKS_MAJOR == 6) && (_WRS_VXWORKS_MINOR < 9) Chris@16: // TODO: Starting from what version does vxWorks work with boost? Chris@16: // We can't reasonably insert a #warning "" as a user hint here, Chris@16: // as this will show up with every file including some boost header, Chris@16: // badly bugging the user... So for the time being we just leave it. Chris@16: #endif Chris@16: Chris@16: // vxWorks specific config options: Chris@16: // -------------------------------- Chris@16: #define BOOST_PLATFORM "vxWorks" Chris@16: Chris@16: // Special behaviour for DKMs: Chris@16: #ifdef _WRS_KERNEL Chris@16: // DKMs do not have the -header, Chris@16: // but apparently they do have an intrinsic wchar_t meanwhile! Chris@16: # define BOOST_NO_CWCHAR Chris@16: Chris@16: // Lots of wide-functions and -headers are unavailable for DKMs as well: Chris@16: # define BOOST_NO_CWCTYPE Chris@16: # define BOOST_NO_SWPRINTF Chris@16: # define BOOST_NO_STD_WSTRING Chris@16: # define BOOST_NO_STD_WSTREAMBUF Chris@16: #endif Chris@16: Chris@16: // Generally available headers: Chris@16: #define BOOST_HAS_UNISTD_H Chris@16: #define BOOST_HAS_STDINT_H Chris@16: #define BOOST_HAS_DIRENT_H Chris@16: #define BOOST_HAS_SLIST Chris@16: Chris@16: // vxWorks does not have installed an iconv-library by default, Chris@16: // so unfortunately no Unicode support from scratch is available! Chris@16: // Thus, instead it is suggested to switch to ICU, as this seems Chris@16: // to be the most complete and portable option... Chris@16: #define BOOST_LOCALE_WITH_ICU Chris@16: Chris@16: // Generally available functionality: Chris@16: #define BOOST_HAS_THREADS Chris@16: #define BOOST_HAS_NANOSLEEP Chris@16: #define BOOST_HAS_GETTIMEOFDAY Chris@16: #define BOOST_HAS_CLOCK_GETTIME Chris@16: #define BOOST_HAS_MACRO_USE_FACET Chris@16: Chris@16: // Generally unavailable functionality, delivered by boost's test function: Chris@16: //#define BOOST_NO_DEDUCED_TYPENAME // Commented this out, boost's test gives an errorneous result! Chris@16: #define BOOST_NO_CXX11_EXTERN_TEMPLATE Chris@16: #define BOOST_NO_CXX11_VARIADIC_MACROS Chris@16: Chris@16: // Generally available threading API's: Chris@16: #define BOOST_HAS_PTHREADS Chris@16: #define BOOST_HAS_SCHED_YIELD Chris@16: #define BOOST_HAS_SIGACTION Chris@16: Chris@16: // Functionality available for RTPs only: Chris@16: #ifdef __RTP__ Chris@16: # define BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE Chris@16: # define BOOST_HAS_LOG1P Chris@16: # define BOOST_HAS_EXPM1 Chris@16: #endif Chris@16: Chris@16: // Functionality available for DKMs only: Chris@16: #ifdef _WRS_KERNEL Chris@16: // Luckily, at the moment there seems to be none! Chris@16: #endif Chris@16: Chris@16: // These #defines allow posix_features to work, since vxWorks doesn't Chris@16: // #define them itself for DKMs (for RTPs on the contrary it does): Chris@16: #ifdef _WRS_KERNEL Chris@16: # ifndef _POSIX_TIMERS Chris@16: # define _POSIX_TIMERS 1 Chris@16: # endif Chris@16: # ifndef _POSIX_THREADS Chris@16: # define _POSIX_THREADS 1 Chris@16: # endif Chris@16: #endif Chris@16: Chris@16: // vxWorks doesn't work with asio serial ports: Chris@16: #define BOOST_ASIO_DISABLE_SERIAL_PORT Chris@16: // TODO: The problem here seems to bee that vxWorks uses its own, very specific Chris@16: // ways to handle serial ports, incompatible with POSIX or anything... Chris@16: // Maybe a specific implementation would be possible, but until the Chris@16: // straight need arises... This implementation would presumably consist Chris@16: // of some vxWorks specific ioctl-calls, etc. Any voluteers? Chris@16: Chris@16: // vxWorks-around: #defines CLOCKS_PER_SEC as sysClkRateGet() but Chris@16: // miserably fails to #include the required to make Chris@16: // sysClkRateGet() available! So we manually include it here. Chris@16: #ifdef __RTP__ Chris@16: # include Chris@16: # include Chris@16: #endif Chris@16: Chris@16: // vxWorks-around: In the macros INT32_C(), UINT32_C(), INT64_C() and Chris@16: // UINT64_C() are defined errorneously, yielding not a signed/ Chris@16: // unsigned long/long long type, but a signed/unsigned int/long Chris@16: // type. Eventually this leads to compile errors in ratio_fwd.hpp, Chris@16: // when trying to define several constants which do not fit into a Chris@16: // long type! We correct them here by redefining. Chris@16: #include Chris@16: Chris@16: // Some macro-magic to do the job Chris@16: #define VX_JOIN(X, Y) VX_DO_JOIN(X, Y) Chris@16: #define VX_DO_JOIN(X, Y) VX_DO_JOIN2(X, Y) Chris@16: #define VX_DO_JOIN2(X, Y) X##Y Chris@16: Chris@16: // Correctly setup the macros Chris@16: #undef INT32_C Chris@16: #undef UINT32_C Chris@16: #undef INT64_C Chris@16: #undef UINT64_C Chris@16: #define INT32_C(x) VX_JOIN(x, L) Chris@16: #define UINT32_C(x) VX_JOIN(x, UL) Chris@16: #define INT64_C(x) VX_JOIN(x, LL) Chris@16: #define UINT64_C(x) VX_JOIN(x, ULL) Chris@16: Chris@16: // #include Libraries required for the following function adaption Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // Use C-linkage for the following helper functions Chris@16: extern "C" { Chris@16: Chris@16: // vxWorks-around: The required functions getrlimit() and getrlimit() are missing. Chris@16: // But we have the similar functions getprlimit() and setprlimit(), Chris@16: // which may serve the purpose. Chris@16: // Problem: The vxWorks-documentation regarding these functions Chris@16: // doesn't deserve its name! It isn't documented what the first two Chris@16: // parameters idtype and id mean, so we must fall back to an educated Chris@16: // guess - null, argh... :-/ Chris@16: Chris@16: // TODO: getprlimit() and setprlimit() do exist for RTPs only, for whatever reason. Chris@16: // Thus for DKMs there would have to be another implementation. Chris@16: #ifdef __RTP__ Chris@16: inline int getrlimit(int resource, struct rlimit *rlp){ Chris@16: return getprlimit(0, 0, resource, rlp); Chris@16: } Chris@16: Chris@16: inline int setrlimit(int resource, const struct rlimit *rlp){ Chris@16: return setprlimit(0, 0, resource, const_cast(rlp)); Chris@16: } Chris@16: #endif Chris@16: Chris@16: // vxWorks has ftruncate() only, so we do simulate truncate(): Chris@16: inline int truncate(const char *p, off_t l){ Chris@16: int fd = open(p, O_WRONLY); Chris@16: if (fd == -1){ Chris@16: errno = EACCES; Chris@16: return -1; Chris@16: } Chris@16: if (ftruncate(fd, l) == -1){ Chris@16: close(fd); Chris@16: errno = EACCES; Chris@16: return -1; Chris@16: } Chris@16: return close(fd); Chris@16: } Chris@16: Chris@16: // Fake symlink handling by dummy functions: Chris@16: inline int symlink(const char*, const char*){ Chris@16: // vxWorks has no symlinks -> always return an error! Chris@16: errno = EACCES; Chris@16: return -1; Chris@16: } Chris@16: Chris@16: inline ssize_t readlink(const char*, char*, size_t){ Chris@16: // vxWorks has no symlinks -> always return an error! Chris@16: errno = EACCES; Chris@16: return -1; Chris@16: } Chris@16: Chris@16: // vxWorks claims to implement gettimeofday in sys/time.h Chris@16: // but nevertheless does not provide it! See Chris@16: // https://support.windriver.com/olsPortal/faces/maintenance/techtipDetail_noHeader.jspx?docId=16442&contentId=WR_TECHTIP_006256 Chris@16: // We implement a surrogate version here via clock_gettime: Chris@16: inline int gettimeofday(struct timeval *tv, void * /*tzv*/) { Chris@16: struct timespec ts; Chris@16: clock_gettime(CLOCK_MONOTONIC, &ts); Chris@16: tv->tv_sec = ts.tv_sec; Chris@16: tv->tv_usec = ts.tv_nsec / 1000; Chris@16: return 0; Chris@16: } Chris@16: Chris@16: // vxWorks does provide neither struct tms nor function times()! Chris@16: // We implement an empty dummy-function, simply setting the user Chris@16: // and system time to the half of thew actual system ticks-value Chris@16: // and the child user and system time to 0. Chris@16: // Rather ugly but at least it suppresses compiler errors... Chris@16: // Unfortunately, this of course *does* have an severe impact on Chris@16: // dependant libraries, actually this is chrono only! Here it will Chris@16: // not be possible to correctly use user and system times! But Chris@16: // as vxWorks is lacking the ability to calculate user and system Chris@16: // process times there seems to be no other possible solution. Chris@16: struct tms{ Chris@16: clock_t tms_utime; // User CPU time Chris@16: clock_t tms_stime; // System CPU time Chris@16: clock_t tms_cutime; // User CPU time of terminated child processes Chris@16: clock_t tms_cstime; // System CPU time of terminated child processes Chris@16: }; Chris@16: Chris@16: inline clock_t times(struct tms *t){ Chris@16: struct timespec ts; Chris@16: clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); Chris@16: clock_t ticks(static_cast(static_cast(ts.tv_sec) * CLOCKS_PER_SEC + Chris@16: static_cast(ts.tv_nsec) * CLOCKS_PER_SEC / 1000000.0)); Chris@16: t->tms_utime = ticks/2U; Chris@16: t->tms_stime = ticks/2U; Chris@16: t->tms_cutime = 0; // vxWorks is lacking the concept of a child process! Chris@16: t->tms_cstime = 0; // -> Set the wait times for childs to 0 Chris@16: return ticks; Chris@16: } Chris@16: Chris@16: } // extern "C" Chris@16: Chris@16: // Put the selfmade functions into the std-namespace, just in case Chris@16: namespace std { Chris@16: # ifdef __RTP__ Chris@16: using ::getrlimit; Chris@16: using ::setrlimit; Chris@16: # endif Chris@16: using ::truncate; Chris@16: using ::symlink; Chris@16: using ::readlink; Chris@16: using ::times; Chris@16: using ::gettimeofday; Chris@16: } Chris@16: Chris@16: // Some more macro-magic: Chris@16: // vxWorks-around: Some functions are not present or broken in vxWorks Chris@16: // but may be patched to life via helper macros... Chris@16: Chris@16: // Include signal.h which might contain a typo to be corrected here Chris@16: #include Chris@16: Chris@16: #define getpagesize() sysconf(_SC_PAGESIZE) // getpagesize is deprecated anyway! Chris@16: #ifndef S_ISSOCK Chris@16: # define S_ISSOCK(mode) ((mode & S_IFMT) == S_IFSOCK) // Is file a socket? Chris@16: #endif Chris@16: #define lstat(p, b) stat(p, b) // lstat() == stat(), as vxWorks has no symlinks! Chris@16: #ifndef FPE_FLTINV Chris@16: # define FPE_FLTINV (FPE_FLTSUB+1) // vxWorks has no FPE_FLTINV, so define one as a dummy Chris@16: #endif Chris@16: #if !defined(BUS_ADRALN) && defined(BUS_ADRALNR) Chris@16: # define BUS_ADRALN BUS_ADRALNR // Correct a supposed typo in vxWorks' Chris@16: #endif Chris@16: //typedef int locale_t; // locale_t is a POSIX-extension, currently unpresent in vxWorks! Chris@16: Chris@16: // #include boilerplate code: Chris@16: #include Chris@16: Chris@16: // vxWorks lies about XSI conformance, there is no nl_types.h: Chris@16: #undef BOOST_HAS_NL_TYPES_H