Abstract clock reading into OS layer
authorNathan Hjelm <hjelmn@mac.com>
Tue, 17 Feb 2009 00:25:18 +0000 (21:25 -0300)
committerDaniel Drake <dsd@gentoo.org>
Tue, 17 Feb 2009 00:25:51 +0000 (21:25 -0300)
This will differ on Linux and Darwin, at least.

[dsd: minor style tweaks]

libusb/io.c
libusb/libusbi.h
libusb/os/linux_usbfs.c

index b053f85..6c3aadb 100644 (file)
@@ -972,7 +972,7 @@ static int calculate_timeout(struct usbi_transfer *transfer)
        if (!timeout)
                return 0;
 
-       r = clock_gettime(CLOCK_MONOTONIC, &current_time);
+       r = usbi_backend->clock_gettime(USBI_CLOCK_MONOTONIC, &current_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;
index b51ba0b..da636b2 100644 (file)
@@ -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. */
index 46e770c..7727ba6 100644 (file)
@@ -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),