libusb 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +8 -2
  3. data/Gemfile +2 -0
  4. data/History.md +12 -0
  5. data/README.md +25 -14
  6. data/Rakefile +64 -109
  7. data/appveyor.yml +23 -0
  8. data/ext/extconf.rb +10 -12
  9. data/ext/libusb_recipe.rb +29 -0
  10. data/lib/libusb/call.rb +14 -11
  11. data/lib/libusb/compat.rb +9 -9
  12. data/lib/libusb/context.rb +16 -1
  13. data/lib/libusb/dependencies.rb +7 -0
  14. data/lib/libusb/dev_handle.rb +13 -3
  15. data/lib/libusb/eventmachine.rb +4 -4
  16. data/lib/libusb/transfer.rb +71 -10
  17. data/lib/libusb/version_gem.rb +1 -1
  18. data/libusb.gemspec +7 -5
  19. data/ports/archives/libusb-1.0.21.tar.bz2 +0 -0
  20. data/test/test_libusb_bulk_stream_transfer.rb +1 -1
  21. data/test/test_libusb_descriptors.rb +4 -4
  22. data/test/test_libusb_event_machine.rb +7 -7
  23. data/test/test_libusb_hotplug.rb +15 -3
  24. data/test/test_libusb_iso_transfer.rb +1 -1
  25. data/test/test_libusb_mass_storage.rb +19 -19
  26. data/test/test_libusb_mass_storage2.rb +1 -1
  27. data/test/test_libusb_structs.rb +13 -0
  28. data/test/test_libusb_threads.rb +2 -2
  29. data/wireshark-usb-sniffer.png +0 -0
  30. metadata +32 -156
  31. checksums.yaml.gz.sig +0 -0
  32. data.tar.gz.sig +0 -0
  33. data/ext/libusb-1.0.20/AUTHORS +0 -89
  34. data/ext/libusb-1.0.20/COPYING +0 -504
  35. data/ext/libusb-1.0.20/ChangeLog +0 -227
  36. data/ext/libusb-1.0.20/INSTALL +0 -234
  37. data/ext/libusb-1.0.20/Makefile.am +0 -28
  38. data/ext/libusb-1.0.20/Makefile.in +0 -897
  39. data/ext/libusb-1.0.20/NEWS +0 -2
  40. data/ext/libusb-1.0.20/PORTING +0 -94
  41. data/ext/libusb-1.0.20/README +0 -29
  42. data/ext/libusb-1.0.20/TODO +0 -2
  43. data/ext/libusb-1.0.20/Xcode/common.xcconfig +0 -49
  44. data/ext/libusb-1.0.20/Xcode/config.h +0 -28
  45. data/ext/libusb-1.0.20/Xcode/debug.xcconfig +0 -29
  46. data/ext/libusb-1.0.20/Xcode/libusb.xcconfig +0 -21
  47. data/ext/libusb-1.0.20/Xcode/libusb.xcodeproj/project.pbxproj +0 -865
  48. data/ext/libusb-1.0.20/Xcode/libusb_debug.xcconfig +0 -21
  49. data/ext/libusb-1.0.20/Xcode/libusb_release.xcconfig +0 -21
  50. data/ext/libusb-1.0.20/Xcode/release.xcconfig +0 -30
  51. data/ext/libusb-1.0.20/aclocal.m4 +0 -1193
  52. data/ext/libusb-1.0.20/android/README +0 -114
  53. data/ext/libusb-1.0.20/android/config.h +0 -81
  54. data/ext/libusb-1.0.20/android/jni/Android.mk +0 -23
  55. data/ext/libusb-1.0.20/android/jni/Application.mk +0 -24
  56. data/ext/libusb-1.0.20/android/jni/examples.mk +0 -134
  57. data/ext/libusb-1.0.20/android/jni/libusb.mk +0 -54
  58. data/ext/libusb-1.0.20/android/jni/tests.mk +0 -56
  59. data/ext/libusb-1.0.20/compile +0 -347
  60. data/ext/libusb-1.0.20/config.guess +0 -1421
  61. data/ext/libusb-1.0.20/config.h.in +0 -155
  62. data/ext/libusb-1.0.20/config.sub +0 -1807
  63. data/ext/libusb-1.0.20/configure +0 -15466
  64. data/ext/libusb-1.0.20/configure.ac +0 -326
  65. data/ext/libusb-1.0.20/depcomp +0 -791
  66. data/ext/libusb-1.0.20/doc/Makefile.am +0 -9
  67. data/ext/libusb-1.0.20/doc/Makefile.in +0 -456
  68. data/ext/libusb-1.0.20/doc/doxygen.cfg.in +0 -2334
  69. data/ext/libusb-1.0.20/examples/Makefile.am +0 -19
  70. data/ext/libusb-1.0.20/examples/Makefile.in +0 -713
  71. data/ext/libusb-1.0.20/examples/dpfp.c +0 -506
  72. data/ext/libusb-1.0.20/examples/dpfp_threaded.c +0 -549
  73. data/ext/libusb-1.0.20/examples/ezusb.c +0 -831
  74. data/ext/libusb-1.0.20/examples/ezusb.h +0 -120
  75. data/ext/libusb-1.0.20/examples/fxload.c +0 -308
  76. data/ext/libusb-1.0.20/examples/getopt/getopt.c +0 -1060
  77. data/ext/libusb-1.0.20/examples/getopt/getopt.h +0 -180
  78. data/ext/libusb-1.0.20/examples/getopt/getopt1.c +0 -188
  79. data/ext/libusb-1.0.20/examples/hotplugtest.c +0 -122
  80. data/ext/libusb-1.0.20/examples/listdevs.c +0 -71
  81. data/ext/libusb-1.0.20/examples/sam3u_benchmark.c +0 -193
  82. data/ext/libusb-1.0.20/examples/xusb.c +0 -1130
  83. data/ext/libusb-1.0.20/install-sh +0 -501
  84. data/ext/libusb-1.0.20/libusb-1.0.pc.in +0 -11
  85. data/ext/libusb-1.0.20/libusb/Makefile.am +0 -90
  86. data/ext/libusb-1.0.20/libusb/Makefile.in +0 -1053
  87. data/ext/libusb-1.0.20/libusb/core.c +0 -2452
  88. data/ext/libusb-1.0.20/libusb/descriptor.c +0 -1201
  89. data/ext/libusb-1.0.20/libusb/hotplug.c +0 -344
  90. data/ext/libusb-1.0.20/libusb/hotplug.h +0 -90
  91. data/ext/libusb-1.0.20/libusb/io.c +0 -2760
  92. data/ext/libusb-1.0.20/libusb/libusb-1.0.def +0 -168
  93. data/ext/libusb-1.0.20/libusb/libusb-1.0.rc +0 -61
  94. data/ext/libusb-1.0.20/libusb/libusb.h +0 -1999
  95. data/ext/libusb-1.0.20/libusb/libusbi.h +0 -1102
  96. data/ext/libusb-1.0.20/libusb/os/darwin_usb.c +0 -1969
  97. data/ext/libusb-1.0.20/libusb/os/darwin_usb.h +0 -158
  98. data/ext/libusb-1.0.20/libusb/os/haiku/Makefile.am +0 -5
  99. data/ext/libusb-1.0.20/libusb/os/haiku/Makefile.in +0 -810
  100. data/ext/libusb-1.0.20/libusb/os/haiku/aclocal.m4 +0 -1193
  101. data/ext/libusb-1.0.20/libusb/os/haiku/compile +0 -347
  102. data/ext/libusb-1.0.20/libusb/os/haiku/config.guess +0 -1421
  103. data/ext/libusb-1.0.20/libusb/os/haiku/config.sub +0 -1807
  104. data/ext/libusb-1.0.20/libusb/os/haiku/configure +0 -17579
  105. data/ext/libusb-1.0.20/libusb/os/haiku/configure.ac +0 -8
  106. data/ext/libusb-1.0.20/libusb/os/haiku/depcomp +0 -791
  107. data/ext/libusb-1.0.20/libusb/os/haiku/haiku_pollfs.cpp +0 -378
  108. data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb.h +0 -112
  109. data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb_backend.cpp +0 -550
  110. data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb_raw.cpp +0 -255
  111. data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb_raw.h +0 -180
  112. data/ext/libusb-1.0.20/libusb/os/haiku/install-sh +0 -501
  113. data/ext/libusb-1.0.20/libusb/os/haiku/ltmain.sh +0 -9655
  114. data/ext/libusb-1.0.20/libusb/os/haiku/m4/libtool.m4 +0 -7992
  115. data/ext/libusb-1.0.20/libusb/os/haiku/m4/ltoptions.m4 +0 -384
  116. data/ext/libusb-1.0.20/libusb/os/haiku/m4/ltsugar.m4 +0 -123
  117. data/ext/libusb-1.0.20/libusb/os/haiku/m4/ltversion.m4 +0 -23
  118. data/ext/libusb-1.0.20/libusb/os/haiku/m4/lt~obsolete.m4 +0 -98
  119. data/ext/libusb-1.0.20/libusb/os/haiku/missing +0 -215
  120. data/ext/libusb-1.0.20/libusb/os/linux_netlink.c +0 -369
  121. data/ext/libusb-1.0.20/libusb/os/linux_udev.c +0 -306
  122. data/ext/libusb-1.0.20/libusb/os/linux_usbfs.c +0 -2692
  123. data/ext/libusb-1.0.20/libusb/os/linux_usbfs.h +0 -192
  124. data/ext/libusb-1.0.20/libusb/os/netbsd_usb.c +0 -674
  125. data/ext/libusb-1.0.20/libusb/os/openbsd_usb.c +0 -768
  126. data/ext/libusb-1.0.20/libusb/os/poll_posix.c +0 -53
  127. data/ext/libusb-1.0.20/libusb/os/poll_posix.h +0 -11
  128. data/ext/libusb-1.0.20/libusb/os/poll_windows.c +0 -728
  129. data/ext/libusb-1.0.20/libusb/os/poll_windows.h +0 -131
  130. data/ext/libusb-1.0.20/libusb/os/threads_posix.c +0 -82
  131. data/ext/libusb-1.0.20/libusb/os/threads_posix.h +0 -50
  132. data/ext/libusb-1.0.20/libusb/os/threads_windows.c +0 -214
  133. data/ext/libusb-1.0.20/libusb/os/threads_windows.h +0 -87
  134. data/ext/libusb-1.0.20/libusb/os/wince_usb.c +0 -869
  135. data/ext/libusb-1.0.20/libusb/os/wince_usb.h +0 -131
  136. data/ext/libusb-1.0.20/libusb/os/windows_common.h +0 -108
  137. data/ext/libusb-1.0.20/libusb/os/windows_usb.c +0 -4643
  138. data/ext/libusb-1.0.20/libusb/os/windows_usb.h +0 -973
  139. data/ext/libusb-1.0.20/libusb/strerror.c +0 -202
  140. data/ext/libusb-1.0.20/libusb/sync.c +0 -308
  141. data/ext/libusb-1.0.20/libusb/version.h +0 -18
  142. data/ext/libusb-1.0.20/libusb/version_nano.h +0 -1
  143. data/ext/libusb-1.0.20/ltmain.sh +0 -9655
  144. data/ext/libusb-1.0.20/m4/libtool.m4 +0 -7992
  145. data/ext/libusb-1.0.20/m4/ltoptions.m4 +0 -384
  146. data/ext/libusb-1.0.20/m4/ltsugar.m4 +0 -123
  147. data/ext/libusb-1.0.20/m4/ltversion.m4 +0 -23
  148. data/ext/libusb-1.0.20/m4/lt~obsolete.m4 +0 -98
  149. data/ext/libusb-1.0.20/missing +0 -215
  150. data/ext/libusb-1.0.20/tests/Makefile.am +0 -6
  151. data/ext/libusb-1.0.20/tests/Makefile.in +0 -596
  152. data/ext/libusb-1.0.20/tests/libusb_testlib.h +0 -107
  153. data/ext/libusb-1.0.20/tests/stress.c +0 -160
  154. data/ext/libusb-1.0.20/tests/testlib.c +0 -277
  155. metadata.gz.sig +0 -0
@@ -1,131 +0,0 @@
1
- /*
2
- * Windows compat: POSIX compatibility wrapper
3
- * Copyright © 2012-2013 RealVNC Ltd.
4
- * Copyright © 2009-2010 Pete Batard <pete@akeo.ie>
5
- * With contributions from Michael Plante, Orin Eman et al.
6
- * Parts of poll implementation from libusb-win32, by Stephan Meyer et al.
7
- *
8
- * This library is free software; you can redistribute it and/or
9
- * modify it under the terms of the GNU Lesser General Public
10
- * License as published by the Free Software Foundation; either
11
- * version 2.1 of the License, or (at your option) any later version.
12
- *
13
- * This library is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
- * Lesser General Public License for more details.
17
- *
18
- * You should have received a copy of the GNU Lesser General Public
19
- * License along with this library; if not, write to the Free Software
20
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
- *
22
- */
23
- #pragma once
24
-
25
- #if defined(_MSC_VER)
26
- // disable /W4 MSVC warnings that are benign
27
- #pragma warning(disable:4127) // conditional expression is constant
28
- #endif
29
-
30
- // Handle synchronous completion through the overlapped structure
31
- #if !defined(STATUS_REPARSE) // reuse the REPARSE status code
32
- #define STATUS_REPARSE ((LONG)0x00000104L)
33
- #endif
34
- #define STATUS_COMPLETED_SYNCHRONOUSLY STATUS_REPARSE
35
- #if defined(_WIN32_WCE)
36
- // WinCE doesn't have a HasOverlappedIoCompleted() macro, so attempt to emulate it
37
- #define HasOverlappedIoCompleted(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) != STATUS_PENDING)
38
- #endif
39
- #define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
40
-
41
- #define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
42
-
43
- /* Windows versions */
44
- enum windows_version {
45
- WINDOWS_CE = -2,
46
- WINDOWS_UNDEFINED = -1,
47
- WINDOWS_UNSUPPORTED = 0,
48
- WINDOWS_XP = 0x51,
49
- WINDOWS_2003 = 0x52, // Also XP x64
50
- WINDOWS_VISTA = 0x60,
51
- WINDOWS_7 = 0x61,
52
- WINDOWS_8 = 0x62,
53
- WINDOWS_8_1_OR_LATER = 0x63,
54
- WINDOWS_MAX
55
- };
56
- extern int windows_version;
57
-
58
- #define MAX_FDS 256
59
-
60
- #define POLLIN 0x0001 /* There is data to read */
61
- #define POLLPRI 0x0002 /* There is urgent data to read */
62
- #define POLLOUT 0x0004 /* Writing now will not block */
63
- #define POLLERR 0x0008 /* Error condition */
64
- #define POLLHUP 0x0010 /* Hung up */
65
- #define POLLNVAL 0x0020 /* Invalid request: fd not open */
66
-
67
- struct pollfd {
68
- int fd; /* file descriptor */
69
- short events; /* requested events */
70
- short revents; /* returned events */
71
- };
72
-
73
- // access modes
74
- enum rw_type {
75
- RW_NONE,
76
- RW_READ,
77
- RW_WRITE,
78
- };
79
-
80
- // fd struct that can be used for polling on Windows
81
- typedef int cancel_transfer(struct usbi_transfer *itransfer);
82
-
83
- struct winfd {
84
- int fd; // what's exposed to libusb core
85
- HANDLE handle; // what we need to attach overlapped to the I/O op, so we can poll it
86
- OVERLAPPED* overlapped; // what will report our I/O status
87
- struct usbi_transfer *itransfer; // Associated transfer, or NULL if completed
88
- cancel_transfer *cancel_fn; // Function pointer to cancel transfer API
89
- enum rw_type rw; // I/O transfer direction: read *XOR* write (NOT BOTH)
90
- };
91
- extern const struct winfd INVALID_WINFD;
92
-
93
- int usbi_pipe(int pipefd[2]);
94
- int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout);
95
- ssize_t usbi_write(int fd, const void *buf, size_t count);
96
- ssize_t usbi_read(int fd, void *buf, size_t count);
97
- int usbi_close(int fd);
98
-
99
- void init_polling(void);
100
- void exit_polling(void);
101
- struct winfd usbi_create_fd(HANDLE handle, int access_mode,
102
- struct usbi_transfer *transfer, cancel_transfer *cancel_fn);
103
- void usbi_free_fd(struct winfd* winfd);
104
- struct winfd fd_to_winfd(int fd);
105
- struct winfd handle_to_winfd(HANDLE handle);
106
- struct winfd overlapped_to_winfd(OVERLAPPED* overlapped);
107
-
108
- /*
109
- * Timeval operations
110
- */
111
- #if defined(DDKBUILD)
112
- #include <winsock.h> // defines timeval functions on DDK
113
- #endif
114
-
115
- #if !defined(TIMESPEC_TO_TIMEVAL)
116
- #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
117
- (tv)->tv_sec = (long)(ts)->tv_sec; \
118
- (tv)->tv_usec = (long)(ts)->tv_nsec / 1000; \
119
- }
120
- #endif
121
- #if !defined(timersub)
122
- #define timersub(a, b, result) \
123
- do { \
124
- (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
125
- (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
126
- if ((result)->tv_usec < 0) { \
127
- --(result)->tv_sec; \
128
- (result)->tv_usec += 1000000; \
129
- } \
130
- } while (0)
131
- #endif
@@ -1,82 +0,0 @@
1
- /*
2
- * libusb synchronization using POSIX Threads
3
- *
4
- * Copyright © 2011 Vitali Lovich <vlovich@aliph.com>
5
- * Copyright © 2011 Peter Stuge <peter@stuge.se>
6
- *
7
- * This library is free software; you can redistribute it and/or
8
- * modify it under the terms of the GNU Lesser General Public
9
- * License as published by the Free Software Foundation; either
10
- * version 2.1 of the License, or (at your option) any later version.
11
- *
12
- * This library is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
- * Lesser General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU Lesser General Public
18
- * License along with this library; if not, write to the Free Software
19
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
- */
21
-
22
- #include <config.h>
23
-
24
- #if defined(__linux__) || defined(__OpenBSD__)
25
- # if defined(__OpenBSD__)
26
- # define _BSD_SOURCE
27
- # endif
28
- # include <unistd.h>
29
- # include <sys/syscall.h>
30
- #elif defined(__APPLE__)
31
- # include <mach/mach.h>
32
- #elif defined(__CYGWIN__)
33
- # include <windows.h>
34
- #endif
35
-
36
- #include "threads_posix.h"
37
-
38
- int usbi_mutex_init_recursive(pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
39
- {
40
- int err;
41
- pthread_mutexattr_t stack_attr;
42
- if (!attr) {
43
- attr = &stack_attr;
44
- err = pthread_mutexattr_init(&stack_attr);
45
- if (err != 0)
46
- return err;
47
- }
48
-
49
- /* mutexattr_settype requires _GNU_SOURCE or _XOPEN_SOURCE >= 500 on Linux */
50
- err = pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE);
51
- if (err != 0)
52
- goto finish;
53
-
54
- err = pthread_mutex_init(mutex, attr);
55
-
56
- finish:
57
- if (attr == &stack_attr)
58
- pthread_mutexattr_destroy(&stack_attr);
59
-
60
- return err;
61
- }
62
-
63
- int usbi_get_tid(void)
64
- {
65
- int ret = -1;
66
- #if defined(__ANDROID__)
67
- ret = gettid();
68
- #elif defined(__linux__)
69
- ret = syscall(SYS_gettid);
70
- #elif defined(__OpenBSD__)
71
- /* The following only works with OpenBSD > 5.1 as it requires
72
- real thread support. For 5.1 and earlier, -1 is returned. */
73
- ret = syscall(SYS_getthrid);
74
- #elif defined(__APPLE__)
75
- ret = mach_thread_self();
76
- mach_port_deallocate(mach_task_self(), ret);
77
- #elif defined(__CYGWIN__)
78
- ret = GetCurrentThreadId();
79
- #endif
80
- /* TODO: NetBSD thread ID support */
81
- return ret;
82
- }
@@ -1,50 +0,0 @@
1
- /*
2
- * libusb synchronization using POSIX Threads
3
- *
4
- * Copyright © 2010 Peter Stuge <peter@stuge.se>
5
- *
6
- * This library is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU Lesser General Public
8
- * License as published by the Free Software Foundation; either
9
- * version 2.1 of the License, or (at your option) any later version.
10
- *
11
- * This library is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
- * Lesser General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU Lesser General Public
17
- * License along with this library; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
- */
20
-
21
- #ifndef LIBUSB_THREADS_POSIX_H
22
- #define LIBUSB_THREADS_POSIX_H
23
-
24
- #include <pthread.h>
25
-
26
- #define usbi_mutex_static_t pthread_mutex_t
27
- #define USBI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
28
- #define usbi_mutex_static_lock pthread_mutex_lock
29
- #define usbi_mutex_static_unlock pthread_mutex_unlock
30
-
31
- #define usbi_mutex_t pthread_mutex_t
32
- #define usbi_mutex_init pthread_mutex_init
33
- #define usbi_mutex_lock pthread_mutex_lock
34
- #define usbi_mutex_unlock pthread_mutex_unlock
35
- #define usbi_mutex_trylock pthread_mutex_trylock
36
- #define usbi_mutex_destroy pthread_mutex_destroy
37
-
38
- #define usbi_cond_t pthread_cond_t
39
- #define usbi_cond_init pthread_cond_init
40
- #define usbi_cond_wait pthread_cond_wait
41
- #define usbi_cond_timedwait pthread_cond_timedwait
42
- #define usbi_cond_broadcast pthread_cond_broadcast
43
- #define usbi_cond_destroy pthread_cond_destroy
44
- #define usbi_cond_signal pthread_cond_signal
45
-
46
- extern int usbi_mutex_init_recursive(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
47
-
48
- int usbi_get_tid(void);
49
-
50
- #endif /* LIBUSB_THREADS_POSIX_H */
@@ -1,214 +0,0 @@
1
- /*
2
- * libusb synchronization on Microsoft Windows
3
- *
4
- * Copyright © 2010 Michael Plante <michael.plante@gmail.com>
5
- *
6
- * This library is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU Lesser General Public
8
- * License as published by the Free Software Foundation; either
9
- * version 2.1 of the License, or (at your option) any later version.
10
- *
11
- * This library is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
- * Lesser General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU Lesser General Public
17
- * License along with this library; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
- */
20
-
21
- #include <config.h>
22
-
23
- #include <objbase.h>
24
- #include <errno.h>
25
- #include <stdarg.h>
26
-
27
- #include "libusbi.h"
28
-
29
- extern const uint64_t epoch_time;
30
-
31
- int usbi_mutex_init(usbi_mutex_t *mutex,
32
- const usbi_mutexattr_t *attr) {
33
- UNUSED(attr);
34
- if(! mutex) return ((errno=EINVAL));
35
- *mutex = CreateMutex(NULL, FALSE, NULL);
36
- if(!*mutex) return ((errno=ENOMEM));
37
- return 0;
38
- }
39
- int usbi_mutex_destroy(usbi_mutex_t *mutex) {
40
- // It is not clear if CloseHandle failure is due to failure to unlock.
41
- // If so, this should be errno=EBUSY.
42
- if(!mutex || !CloseHandle(*mutex)) return ((errno=EINVAL));
43
- *mutex = NULL;
44
- return 0;
45
- }
46
- int usbi_mutex_trylock(usbi_mutex_t *mutex) {
47
- DWORD result;
48
- if(!mutex) return ((errno=EINVAL));
49
- result = WaitForSingleObject(*mutex, 0);
50
- if(result == WAIT_OBJECT_0 || result == WAIT_ABANDONED)
51
- return 0; // acquired (ToDo: check that abandoned is ok)
52
- if(result == WAIT_TIMEOUT)
53
- return ((errno=EBUSY));
54
- return ((errno=EINVAL)); // don't know how this would happen
55
- // so don't know proper errno
56
- }
57
- int usbi_mutex_lock(usbi_mutex_t *mutex) {
58
- DWORD result;
59
- if(!mutex) return ((errno=EINVAL));
60
- result = WaitForSingleObject(*mutex, INFINITE);
61
- if(result == WAIT_OBJECT_0 || result == WAIT_ABANDONED)
62
- return 0; // acquired (ToDo: check that abandoned is ok)
63
- return ((errno=EINVAL)); // don't know how this would happen
64
- // so don't know proper errno
65
- }
66
- int usbi_mutex_unlock(usbi_mutex_t *mutex) {
67
- if(!mutex) return ((errno=EINVAL));
68
- if(!ReleaseMutex(*mutex)) return ((errno=EPERM ));
69
- return 0;
70
- }
71
-
72
- int usbi_mutex_static_lock(usbi_mutex_static_t *mutex) {
73
- if(!mutex) return ((errno=EINVAL));
74
- while (InterlockedExchange((LONG *)mutex, 1) == 1) {
75
- SleepEx(0, TRUE);
76
- }
77
- return 0;
78
- }
79
- int usbi_mutex_static_unlock(usbi_mutex_static_t *mutex) {
80
- if(!mutex) return ((errno=EINVAL));
81
- *mutex = 0;
82
- return 0;
83
- }
84
-
85
- int usbi_cond_init(usbi_cond_t *cond,
86
- const usbi_condattr_t *attr) {
87
- UNUSED(attr);
88
- if(!cond) return ((errno=EINVAL));
89
- list_init(&cond->waiters );
90
- list_init(&cond->not_waiting);
91
- return 0;
92
- }
93
- int usbi_cond_destroy(usbi_cond_t *cond) {
94
- // This assumes no one is using this anymore. The check MAY NOT BE safe.
95
- struct usbi_cond_perthread *pos, *next_pos = NULL;
96
- if(!cond) return ((errno=EINVAL));
97
- if(!list_empty(&cond->waiters)) return ((errno=EBUSY )); // (!see above!)
98
- list_for_each_entry_safe(pos, next_pos, &cond->not_waiting, list, struct usbi_cond_perthread) {
99
- CloseHandle(pos->event);
100
- list_del(&pos->list);
101
- free(pos);
102
- }
103
-
104
- return 0;
105
- }
106
-
107
- int usbi_cond_broadcast(usbi_cond_t *cond) {
108
- // Assumes mutex is locked; this is not in keeping with POSIX spec, but
109
- // libusb does this anyway, so we simplify by not adding more sync
110
- // primitives to the CV definition!
111
- int fail = 0;
112
- struct usbi_cond_perthread *pos;
113
- if(!cond) return ((errno=EINVAL));
114
- list_for_each_entry(pos, &cond->waiters, list, struct usbi_cond_perthread) {
115
- if(!SetEvent(pos->event))
116
- fail = 1;
117
- }
118
- // The wait function will remove its respective item from the list.
119
- return fail ? ((errno=EINVAL)) : 0;
120
- }
121
- int usbi_cond_signal(usbi_cond_t *cond) {
122
- // Assumes mutex is locked; this is not in keeping with POSIX spec, but
123
- // libusb does this anyway, so we simplify by not adding more sync
124
- // primitives to the CV definition!
125
- struct usbi_cond_perthread *pos;
126
- if(!cond) return ((errno=EINVAL));
127
- if(list_empty(&cond->waiters)) return 0; // no one to wakeup.
128
- pos = list_entry(&cond->waiters.next, struct usbi_cond_perthread, list);
129
- // The wait function will remove its respective item from the list.
130
- return SetEvent(pos->event) ? 0 : ((errno=EINVAL));
131
- }
132
- __inline static int usbi_cond_intwait(usbi_cond_t *cond,
133
- usbi_mutex_t *mutex,
134
- DWORD timeout_ms) {
135
- struct usbi_cond_perthread *pos;
136
- int found = 0, r;
137
- DWORD r2,tid = GetCurrentThreadId();
138
- if(!cond || !mutex) return ((errno=EINVAL));
139
- list_for_each_entry(pos, &cond->not_waiting, list, struct usbi_cond_perthread) {
140
- if(tid == pos->tid) {
141
- found = 1;
142
- break;
143
- }
144
- }
145
- if(!found) {
146
- pos = (struct usbi_cond_perthread*) calloc(1, sizeof(struct usbi_cond_perthread));
147
- if(!pos) return ((errno=ENOMEM)); // This errno is not POSIX-allowed.
148
- pos->tid = tid;
149
- pos->event = CreateEvent(NULL, FALSE, FALSE, NULL); // auto-reset.
150
- if(!pos->event) {
151
- free(pos);
152
- return ((errno=ENOMEM));
153
- }
154
- list_add(&pos->list, &cond->not_waiting);
155
- }
156
-
157
- list_del(&pos->list); // remove from not_waiting list.
158
- list_add(&pos->list, &cond->waiters);
159
-
160
- r = usbi_mutex_unlock(mutex);
161
- if(r) return r;
162
- r2 = WaitForSingleObject(pos->event, timeout_ms);
163
- r = usbi_mutex_lock(mutex);
164
- if(r) return r;
165
-
166
- list_del(&pos->list);
167
- list_add(&pos->list, &cond->not_waiting);
168
-
169
- if(r2 == WAIT_TIMEOUT) return ((errno=ETIMEDOUT));
170
-
171
- return 0;
172
- }
173
- // N.B.: usbi_cond_*wait() can also return ENOMEM, even though pthread_cond_*wait cannot!
174
- int usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex) {
175
- return usbi_cond_intwait(cond, mutex, INFINITE);
176
- }
177
- int usbi_cond_timedwait(usbi_cond_t *cond,
178
- usbi_mutex_t *mutex,
179
- const struct timespec *abstime) {
180
- FILETIME filetime;
181
- ULARGE_INTEGER rtime;
182
- struct timeval targ_time, cur_time, delta_time;
183
- struct timespec cur_time_ns;
184
- DWORD millis;
185
-
186
- // GetSystemTimeAsFileTime() is not available on CE
187
- SYSTEMTIME st;
188
- GetSystemTime(&st);
189
- if (!SystemTimeToFileTime(&st, &filetime))
190
- return 0;
191
- rtime.LowPart = filetime.dwLowDateTime;
192
- rtime.HighPart = filetime.dwHighDateTime;
193
- rtime.QuadPart -= epoch_time;
194
- cur_time_ns.tv_sec = (long)(rtime.QuadPart / 10000000);
195
- cur_time_ns.tv_nsec = (long)((rtime.QuadPart % 10000000)*100);
196
- TIMESPEC_TO_TIMEVAL(&cur_time, &cur_time_ns);
197
-
198
- TIMESPEC_TO_TIMEVAL(&targ_time, abstime);
199
- timersub(&targ_time, &cur_time, &delta_time);
200
- if(delta_time.tv_sec < 0) // abstime already passed?
201
- millis = 0;
202
- else {
203
- millis = delta_time.tv_usec/1000;
204
- millis += delta_time.tv_sec *1000;
205
- if (delta_time.tv_usec % 1000) // round up to next millisecond
206
- millis++;
207
- }
208
-
209
- return usbi_cond_intwait(cond, mutex, millis);
210
- }
211
-
212
- int usbi_get_tid(void) {
213
- return GetCurrentThreadId();
214
- }