From afc59a400c4be1deea1b7fecbee782be3a994c87 Mon Sep 17 00:00:00 2001 From: Alan Antonuk Date: Tue, 21 Apr 2015 21:11:31 -0700 Subject: Rename amqp_timer.[ch] to amqp_time.[ch]. Rename the files to better reflect the functions that they contain. --- Makefile.am | 4 +- librabbitmq/CMakeLists.txt | 2 +- librabbitmq/amqp_api.c | 2 +- librabbitmq/amqp_connection.c | 2 +- librabbitmq/amqp_openssl.c | 2 +- librabbitmq/amqp_private.h | 2 +- librabbitmq/amqp_socket.c | 2 +- librabbitmq/amqp_socket.h | 2 +- librabbitmq/amqp_time.c | 182 ++++++++++++++++++++++++++++++++++++++++++ librabbitmq/amqp_time.h | 67 ++++++++++++++++ librabbitmq/amqp_timer.c | 182 ------------------------------------------ librabbitmq/amqp_timer.h | 67 ---------------- 12 files changed, 258 insertions(+), 258 deletions(-) create mode 100644 librabbitmq/amqp_time.c create mode 100644 librabbitmq/amqp_time.h delete mode 100644 librabbitmq/amqp_timer.c delete mode 100644 librabbitmq/amqp_timer.h diff --git a/Makefile.am b/Makefile.am index 3f94714..9b0e529 100644 --- a/Makefile.am +++ b/Makefile.am @@ -35,8 +35,8 @@ librabbitmq_librabbitmq_la_SOURCES = \ librabbitmq/amqp_table.c \ librabbitmq/amqp_tcp_socket.c \ librabbitmq/amqp_tcp_socket.h \ - librabbitmq/amqp_timer.c \ - librabbitmq/amqp_timer.h \ + librabbitmq/amqp_time.c \ + librabbitmq/amqp_time.h \ librabbitmq/amqp_url.c diff --git a/librabbitmq/CMakeLists.txt b/librabbitmq/CMakeLists.txt index 2c3032e..3c86094 100644 --- a/librabbitmq/CMakeLists.txt +++ b/librabbitmq/CMakeLists.txt @@ -129,7 +129,7 @@ set(RABBITMQ_SOURCES ${AMQP_FRAMING_C_PATH} amqp_api.c amqp.h amqp_connection.c amqp_mem.c amqp_private.h amqp_socket.c amqp_table.c amqp_url.c amqp_socket.h amqp_tcp_socket.c amqp_tcp_socket.h - amqp_timer.c amqp_timer.h + amqp_time.c amqp_time.h amqp_consumer.c ${AMQP_SSL_SRCS} ) diff --git a/librabbitmq/amqp_api.c b/librabbitmq/amqp_api.c index c5a999d..ec2f9a1 100644 --- a/librabbitmq/amqp_api.c +++ b/librabbitmq/amqp_api.c @@ -39,7 +39,7 @@ #endif #include "amqp_private.h" -#include "amqp_timer.h" +#include "amqp_time.h" #include #include #include diff --git a/librabbitmq/amqp_connection.c b/librabbitmq/amqp_connection.c index 649c3c4..6fb2a62 100644 --- a/librabbitmq/amqp_connection.c +++ b/librabbitmq/amqp_connection.c @@ -40,7 +40,7 @@ #include "amqp_tcp_socket.h" #include "amqp_private.h" -#include "amqp_timer.h" +#include "amqp_time.h" #include #include #include diff --git a/librabbitmq/amqp_openssl.c b/librabbitmq/amqp_openssl.c index ceb0641..833f5a2 100644 --- a/librabbitmq/amqp_openssl.c +++ b/librabbitmq/amqp_openssl.c @@ -33,7 +33,7 @@ #include "amqp_socket.h" #include "amqp_hostcheck.h" #include "amqp_private.h" -#include "amqp_timer.h" +#include "amqp_time.h" #include "threads.h" #include diff --git a/librabbitmq/amqp_private.h b/librabbitmq/amqp_private.h index 99c45ab..62c8ff0 100644 --- a/librabbitmq/amqp_private.h +++ b/librabbitmq/amqp_private.h @@ -90,7 +90,7 @@ amqp_ssl_error_string(int err); #endif #include "amqp_socket.h" -#include "amqp_timer.h" +#include "amqp_time.h" /* * Connection states: XXX FIX THIS diff --git a/librabbitmq/amqp_socket.c b/librabbitmq/amqp_socket.c index 4ca86e6..e715897 100644 --- a/librabbitmq/amqp_socket.c +++ b/librabbitmq/amqp_socket.c @@ -39,7 +39,7 @@ #endif #include "amqp_private.h" -#include "amqp_timer.h" +#include "amqp_time.h" #include #include diff --git a/librabbitmq/amqp_socket.h b/librabbitmq/amqp_socket.h index 82ad39b..2d999d6 100644 --- a/librabbitmq/amqp_socket.h +++ b/librabbitmq/amqp_socket.h @@ -33,7 +33,7 @@ #define AMQP_SOCKET_H #include "amqp_private.h" -#include "amqp_timer.h" +#include "amqp_time.h" AMQP_BEGIN_DECLS diff --git a/librabbitmq/amqp_time.c b/librabbitmq/amqp_time.c new file mode 100644 index 0000000..a141eab --- /dev/null +++ b/librabbitmq/amqp_time.c @@ -0,0 +1,182 @@ +/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ +/* + * Portions created by Alan Antonuk are Copyright (c) 2013-2014 Alan Antonuk. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ +#include "amqp.h" +#include "amqp_time.h" +#include +#include +#include + +#if (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) +# define AMQP_WIN_TIMER_API +#elif (defined(machintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) +# define AMQP_MAC_TIMER_API +#else +# define AMQP_POSIX_TIMER_API +#endif + + +#ifdef AMQP_WIN_TIMER_API +#define WIN32_LEAN_AND_MEAN +#include + +uint64_t +amqp_get_monotonic_timestamp(void) +{ + static double NS_PER_COUNT = 0; + LARGE_INTEGER perf_count; + + if (0 == NS_PER_COUNT) { + LARGE_INTEGER perf_frequency; + if (!QueryPerformanceFrequency(&perf_frequency)) { + return 0; + } + NS_PER_COUNT = (double)AMQP_NS_PER_S / perf_frequency.QuadPart; + } + + if (!QueryPerformanceCounter(&perf_count)) { + return 0; + } + + return (uint64_t)(perf_count.QuadPart * NS_PER_COUNT); +} +#endif /* AMQP_WIN_TIMER_API */ + +#ifdef AMQP_MAC_TIMER_API +# include + +uint64_t +amqp_get_monotonic_timestamp(void) +{ + static mach_timebase_info_data_t s_timebase = {0, 0}; + uint64_t timestamp; + + timestamp = mach_absolute_time(); + + if (s_timebase.denom == 0) { + mach_timebase_info(&s_timebase); + if (0 == s_timebase.denom) { + return 0; + } + } + + timestamp *= (uint64_t)s_timebase.numer; + timestamp /= (uint64_t)s_timebase.denom; + + return timestamp; +} +#endif /* AMQP_MAC_TIMER_API */ + +#ifdef AMQP_POSIX_TIMER_API +#include + +uint64_t +amqp_get_monotonic_timestamp(void) +{ +#ifdef __hpux + return (uint64_t)gethrtime(); +#else + struct timespec tp; + if (-1 == clock_gettime(CLOCK_MONOTONIC, &tp)) { + return 0; + } + + return ((uint64_t)tp.tv_sec * AMQP_NS_PER_S + (uint64_t)tp.tv_nsec); +#endif +} +#endif /* AMQP_POSIX_TIMER_API */ + +int amqp_timer_start(amqp_timer_t *timer, struct timeval *timeout) { + uint64_t now_ns; + uint64_t delta_ns; + + assert(NULL != timer); + + if (NULL == timeout) { + *timer = amqp_timer_start_infinite(); + return AMQP_STATUS_OK; + } + if (0 == timeout->tv_sec && 0 == timeout->tv_usec) { + *timer = amqp_timer_start_immediate(); + return AMQP_STATUS_OK; + } + + if (timeout->tv_sec < 0 || timeout->tv_usec < 0) { + return AMQP_STATUS_INVALID_PARAMETER; + } + + delta_ns = (uint64_t)timeout->tv_sec * AMQP_NS_PER_S + + (uint64_t)timeout->tv_usec * AMQP_NS_PER_US; + + now_ns = amqp_get_monotonic_timestamp(); + if (0 == now_ns) { + return AMQP_STATUS_TIMER_FAILURE; + } + + timer->expiration_ns = now_ns + delta_ns; + if (now_ns > timer->expiration_ns || + delta_ns > timer->expiration_ns) { + return AMQP_STATUS_INVALID_PARAMETER; + } + + return AMQP_STATUS_OK; +} + +amqp_timer_t amqp_timer_start_immediate(void) { + amqp_timer_t timer; + timer.expiration_ns = 0; + return timer; +} + +amqp_timer_t amqp_timer_start_infinite(void) { + amqp_timer_t timer; + timer.expiration_ns = UINT64_MAX; + return timer; +} + +int amqp_timer_ms_left(amqp_timer_t timer) { + uint64_t now_ns; + uint64_t delta_ns; + int left_ms; + + if (UINT64_MAX == timer.expiration_ns) { + return -1; + } + if (0 == timer.expiration_ns) { + return 0; + } + + now_ns = amqp_get_monotonic_timestamp(); + if (0 == now_ns) { + return AMQP_STATUS_TIMER_FAILURE; + } + + if (now_ns >= timer.expiration_ns) { + return 0; + } + + delta_ns = timer.expiration_ns - now_ns; + left_ms = delta_ns / AMQP_NS_PER_MS; + + return left_ms; +} diff --git a/librabbitmq/amqp_time.h b/librabbitmq/amqp_time.h new file mode 100644 index 0000000..195f70b --- /dev/null +++ b/librabbitmq/amqp_time.h @@ -0,0 +1,67 @@ +/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ +/* + * Portions created by Alan Antonuk are Copyright (c) 2013-2014 Alan Antonuk. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ +#ifndef AMQP_TIMER_H +#define AMQP_TIMER_H + +#include + +#ifdef _WIN32 +# ifndef WINVER +# define WINVER 0x0502 +# endif +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include +#else +# include +#endif + +#define AMQP_MS_PER_S 1000 +#define AMQP_US_PER_MS 1000 +#define AMQP_NS_PER_S 1000000000 +#define AMQP_NS_PER_MS 1000000 +#define AMQP_NS_PER_US 1000 + +typedef struct amqp_timer_t_ { + uint64_t expiration_ns; +} amqp_timer_t; + +/* Gets a monotonic timestamp in ns */ +uint64_t +amqp_get_monotonic_timestamp(void); + +/* Start an amqp_timer_t set to expire timeout from now. */ +int amqp_timer_start(amqp_timer_t *timer, struct timeval *timeout); + +amqp_timer_t amqp_timer_start_immediate(void); + +amqp_timer_t amqp_timer_start_infinite(void); + +/* Get the (positive) number of ms left on the timer, or -1 for an infinite + * timer, or AMQP_STATUS_TIMEOUT on time out, or AMQP_STATUS_TIMER_FAILURE on + * failure. */ +int amqp_timer_ms_left(amqp_timer_t timer); + +#endif /* AMQP_TIMER_H */ diff --git a/librabbitmq/amqp_timer.c b/librabbitmq/amqp_timer.c deleted file mode 100644 index c4722bb..0000000 --- a/librabbitmq/amqp_timer.c +++ /dev/null @@ -1,182 +0,0 @@ -/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ -/* - * Portions created by Alan Antonuk are Copyright (c) 2013-2014 Alan Antonuk. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ -#include "amqp.h" -#include "amqp_timer.h" -#include -#include -#include - -#if (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) -# define AMQP_WIN_TIMER_API -#elif (defined(machintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) -# define AMQP_MAC_TIMER_API -#else -# define AMQP_POSIX_TIMER_API -#endif - - -#ifdef AMQP_WIN_TIMER_API -#define WIN32_LEAN_AND_MEAN -#include - -uint64_t -amqp_get_monotonic_timestamp(void) -{ - static double NS_PER_COUNT = 0; - LARGE_INTEGER perf_count; - - if (0 == NS_PER_COUNT) { - LARGE_INTEGER perf_frequency; - if (!QueryPerformanceFrequency(&perf_frequency)) { - return 0; - } - NS_PER_COUNT = (double)AMQP_NS_PER_S / perf_frequency.QuadPart; - } - - if (!QueryPerformanceCounter(&perf_count)) { - return 0; - } - - return (uint64_t)(perf_count.QuadPart * NS_PER_COUNT); -} -#endif /* AMQP_WIN_TIMER_API */ - -#ifdef AMQP_MAC_TIMER_API -# include - -uint64_t -amqp_get_monotonic_timestamp(void) -{ - static mach_timebase_info_data_t s_timebase = {0, 0}; - uint64_t timestamp; - - timestamp = mach_absolute_time(); - - if (s_timebase.denom == 0) { - mach_timebase_info(&s_timebase); - if (0 == s_timebase.denom) { - return 0; - } - } - - timestamp *= (uint64_t)s_timebase.numer; - timestamp /= (uint64_t)s_timebase.denom; - - return timestamp; -} -#endif /* AMQP_MAC_TIMER_API */ - -#ifdef AMQP_POSIX_TIMER_API -#include - -uint64_t -amqp_get_monotonic_timestamp(void) -{ -#ifdef __hpux - return (uint64_t)gethrtime(); -#else - struct timespec tp; - if (-1 == clock_gettime(CLOCK_MONOTONIC, &tp)) { - return 0; - } - - return ((uint64_t)tp.tv_sec * AMQP_NS_PER_S + (uint64_t)tp.tv_nsec); -#endif -} -#endif /* AMQP_POSIX_TIMER_API */ - -int amqp_timer_start(amqp_timer_t *timer, struct timeval *timeout) { - uint64_t now_ns; - uint64_t delta_ns; - - assert(NULL != timer); - - if (NULL == timeout) { - *timer = amqp_timer_start_infinite(); - return AMQP_STATUS_OK; - } - if (0 == timeout->tv_sec && 0 == timeout->tv_usec) { - *timer = amqp_timer_start_immediate(); - return AMQP_STATUS_OK; - } - - if (timeout->tv_sec < 0 || timeout->tv_usec < 0) { - return AMQP_STATUS_INVALID_PARAMETER; - } - - delta_ns = (uint64_t)timeout->tv_sec * AMQP_NS_PER_S + - (uint64_t)timeout->tv_usec * AMQP_NS_PER_US; - - now_ns = amqp_get_monotonic_timestamp(); - if (0 == now_ns) { - return AMQP_STATUS_TIMER_FAILURE; - } - - timer->expiration_ns = now_ns + delta_ns; - if (now_ns > timer->expiration_ns || - delta_ns > timer->expiration_ns) { - return AMQP_STATUS_INVALID_PARAMETER; - } - - return AMQP_STATUS_OK; -} - -amqp_timer_t amqp_timer_start_immediate(void) { - amqp_timer_t timer; - timer.expiration_ns = 0; - return timer; -} - -amqp_timer_t amqp_timer_start_infinite(void) { - amqp_timer_t timer; - timer.expiration_ns = UINT64_MAX; - return timer; -} - -int amqp_timer_ms_left(amqp_timer_t timer) { - uint64_t now_ns; - uint64_t delta_ns; - int left_ms; - - if (UINT64_MAX == timer.expiration_ns) { - return -1; - } - if (0 == timer.expiration_ns) { - return 0; - } - - now_ns = amqp_get_monotonic_timestamp(); - if (0 == now_ns) { - return AMQP_STATUS_TIMER_FAILURE; - } - - if (now_ns >= timer.expiration_ns) { - return 0; - } - - delta_ns = timer.expiration_ns - now_ns; - left_ms = delta_ns / AMQP_NS_PER_MS; - - return left_ms; -} diff --git a/librabbitmq/amqp_timer.h b/librabbitmq/amqp_timer.h deleted file mode 100644 index 195f70b..0000000 --- a/librabbitmq/amqp_timer.h +++ /dev/null @@ -1,67 +0,0 @@ -/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ -/* - * Portions created by Alan Antonuk are Copyright (c) 2013-2014 Alan Antonuk. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ -#ifndef AMQP_TIMER_H -#define AMQP_TIMER_H - -#include - -#ifdef _WIN32 -# ifndef WINVER -# define WINVER 0x0502 -# endif -# ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN -# endif -# include -#else -# include -#endif - -#define AMQP_MS_PER_S 1000 -#define AMQP_US_PER_MS 1000 -#define AMQP_NS_PER_S 1000000000 -#define AMQP_NS_PER_MS 1000000 -#define AMQP_NS_PER_US 1000 - -typedef struct amqp_timer_t_ { - uint64_t expiration_ns; -} amqp_timer_t; - -/* Gets a monotonic timestamp in ns */ -uint64_t -amqp_get_monotonic_timestamp(void); - -/* Start an amqp_timer_t set to expire timeout from now. */ -int amqp_timer_start(amqp_timer_t *timer, struct timeval *timeout); - -amqp_timer_t amqp_timer_start_immediate(void); - -amqp_timer_t amqp_timer_start_infinite(void); - -/* Get the (positive) number of ms left on the timer, or -1 for an infinite - * timer, or AMQP_STATUS_TIMEOUT on time out, or AMQP_STATUS_TIMER_FAILURE on - * failure. */ -int amqp_timer_ms_left(amqp_timer_t timer); - -#endif /* AMQP_TIMER_H */ -- cgit v1.2.1