From 9196f58bdc8b9e967261df39865215faa5d39cfa Mon Sep 17 00:00:00 2001 From: Nathan Hjelm Date: Mon, 16 Feb 2009 21:25:18 -0300 Subject: [PATCH] Abstract clock reading into OS layer This will differ on Linux and Darwin, at least. [dsd: minor style tweaks] --- libusb/io.c | 8 ++++---- libusb/libusbi.h | 15 +++++++++++++++ libusb/os/linux_usbfs.c | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/libusb/io.c b/libusb/io.c index b053f85..6c3aadb 100644 --- a/libusb/io.c +++ b/libusb/io.c @@ -972,7 +972,7 @@ static int calculate_timeout(struct usbi_transfer *transfer) if (!timeout) return 0; - r = clock_gettime(CLOCK_MONOTONIC, ¤t_time); + r = usbi_backend->clock_gettime(USBI_CLOCK_MONOTONIC, ¤t_time); if (r < 0) { usbi_err(ITRANSFER_CTX(transfer), "failed to read monotonic clock, errno=%d", errno); @@ -1445,7 +1445,7 @@ API_EXPORTED int libusb_wait_for_event(libusb_context *ctx, struct timeval *tv) return 0; } - r = clock_gettime(CLOCK_REALTIME, &timeout); + r = usbi_backend->clock_gettime(USBI_CLOCK_REALTIME, &timeout); if (r < 0) { usbi_err(ctx, "failed to read realtime clock, error %d", errno); return LIBUSB_ERROR_OTHER; @@ -1489,7 +1489,7 @@ static int handle_timeouts(struct libusb_context *ctx) goto out; /* get current time */ - r = clock_gettime(CLOCK_MONOTONIC, &systime_ts); + r = usbi_backend->clock_gettime(USBI_CLOCK_MONOTONIC, &systime_ts); if (r < 0) goto out; @@ -1807,7 +1807,7 @@ API_EXPORTED int libusb_get_next_timeout(libusb_context *ctx, return 0; } - r = clock_gettime(CLOCK_MONOTONIC, &cur_ts); + r = usbi_backend->clock_gettime(USBI_CLOCK_MONOTONIC, &cur_ts); if (r < 0) { usbi_err(ctx, "failed to read monotonic clock, errno=%d", errno); return LIBUSB_ERROR_OTHER; diff --git a/libusb/libusbi.h b/libusb/libusbi.h index b51ba0b..da636b2 100644 --- a/libusb/libusbi.h +++ b/libusb/libusbi.h @@ -218,6 +218,11 @@ struct libusb_device_handle { #define USBI_TRANSFER_TIMED_OUT (1<<0) +enum { + USBI_CLOCK_MONOTONIC, + USBI_CLOCK_REALTIME +}; + /* in-memory transfer layout: * * 1. struct usbi_transfer @@ -733,6 +738,16 @@ struct usbi_os_backend { int (*handle_events)(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds, int num_ready); + /* Get time from specified clock. At least two clocks must be implemented + by the backend: USBI_CLOCK_REALTIME, and USBI_CLOCK_MONOTONIC. + + Description of clocks: + USBI_CLOCK_REALTIME : clock returns time since system epoch. + USBI_CLOCK_MONOTONIC: clock returns time since unspecified start + time (usually boot). + */ + int (*clock_gettime)(int clkid, struct timespec *tp); + /* Number of bytes to reserve for per-device private backend data. * This private data area is accessible through the "os_priv" field of * struct libusb_device. */ diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c index 46e770c..7727ba6 100644 --- a/libusb/os/linux_usbfs.c +++ b/libusb/os/linux_usbfs.c @@ -1999,6 +1999,18 @@ out: return r; } +static int op_clock_gettime(int clk_id, struct timespec *tp) +{ + switch (clk_id) { + case USBI_CLOCK_MONOTONIC: + return clock_gettime(CLOCK_MONOTONIC, tp); + case USBI_CLOCK_REALTIME: + return clock_gettime(CLOCK_REALTIME, tp); + default: + return LIBUSB_ERROR_INVALID_PARAM; + } +} + const struct usbi_os_backend linux_usbfs_backend = { .name = "Linux usbfs", .init = op_init, @@ -2031,6 +2043,8 @@ const struct usbi_os_backend linux_usbfs_backend = { .handle_events = op_handle_events, + .clock_gettime = op_clock_gettime, + .device_priv_size = sizeof(struct linux_device_priv), .device_handle_priv_size = sizeof(struct linux_device_handle_priv), .transfer_priv_size = sizeof(struct linux_transfer_priv), -- 2.7.4