/* ========================================================================== Licensed under BSD 2clause license See LICENSE file for more information Author: Michał Łyszczek ========================================================================== ------------------------------------------------------- / This file contains some common utils code that can be \ \ shared between lib: programs and tests / ------------------------------------------------------- \ . . \ / `. .' " \ .---. < > < > .---. \ | \ \ - ~ ~ - / / | _____ ..-~ ~-..-~ | | \~~~\.' `./~~~/ --------- \__/ \__/ .' O \ / / \ " (_____, `._.' | } \/~~~/ `----. / } | / \__/ `-. | / | / `. ,~~| ~-.__| /_ - ~ ^| /- _ `..-' | / | / ~-. `-. _ _ _ |_____| |_____| ~ - . _ _ _ _ _> ========================================================================== _ __ __ ____ _ __ (_)____ _____ / /__ __ ____/ /___ / __/(_)/ /___ _____ / // __ \ / ___// // / / // __ // _ \ / /_ / // // _ \ / ___/ / // / / // /__ / // /_/ // /_/ // __/ / __// // // __/(__ ) /_//_/ /_/ \___//_/ \__,_/ \__,_/ \___/ /_/ /_//_/ \___//____/ ========================================================================== */ #include #include /* ========================================================================== ____ __ _ / __/__ __ ____ _____ / /_ (_)____ ____ _____ / /_ / / / // __ \ / ___// __// // __ \ / __ \ / ___/ / __// /_/ // / / // /__ / /_ / // /_/ // / / /(__ ) /_/ \__,_//_/ /_/ \___/ \__//_/ \____//_/ /_//____/ ========================================================================== */ /* ========================================================================== Converts millisecond into absolute timespec time. If ms is 0, return tp with values set to 0, which is 1970.01.01, when this is passed to mq_timedreceive(), function will timeout immediately ========================================================================== */ void psmq_ms_to_tp ( size_t ms, /* time in milliseconds */ struct timespec *tp /* ms will be converted to tp here */ ) { size_t nsec; /* number of nanoseconds to add to timer */ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ tp->tv_sec = 0; tp->tv_nsec = 0; if (!ms) return; clock_gettime(CLOCK_REALTIME, tp); nsec = ms % 1000; nsec *= 1000000l; tp->tv_sec += ms / 1000; tp->tv_nsec += nsec; if (tp->tv_nsec >= 1000000000l) { /* overflow on nsec part */ tp->tv_nsec -= 1000000000l; tp->tv_sec += 1; } }