Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /*- |
michael@0 | 2 | * Copyright (c) 2011-2012 Irene Ruengeler |
michael@0 | 3 | * Copyright (c) 2011-2012 Michael Tuexen |
michael@0 | 4 | * All rights reserved. |
michael@0 | 5 | * |
michael@0 | 6 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | * modification, are permitted provided that the following conditions |
michael@0 | 8 | * are met: |
michael@0 | 9 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 12 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 13 | * documentation and/or other materials provided with the distribution. |
michael@0 | 14 | * |
michael@0 | 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
michael@0 | 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
michael@0 | 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
michael@0 | 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
michael@0 | 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
michael@0 | 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
michael@0 | 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
michael@0 | 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
michael@0 | 25 | * SUCH DAMAGE. |
michael@0 | 26 | * |
michael@0 | 27 | */ |
michael@0 | 28 | |
michael@0 | 29 | |
michael@0 | 30 | #ifdef _WIN32 |
michael@0 | 31 | #include <netinet/sctp_pcb.h> |
michael@0 | 32 | #include <sys/timeb.h> |
michael@0 | 33 | #include <iphlpapi.h> |
michael@0 | 34 | #pragma comment(lib, "IPHLPAPI.lib") |
michael@0 | 35 | #endif |
michael@0 | 36 | #include <netinet/sctp_os_userspace.h> |
michael@0 | 37 | |
michael@0 | 38 | #ifndef _WIN32 |
michael@0 | 39 | int |
michael@0 | 40 | sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af) |
michael@0 | 41 | { |
michael@0 | 42 | struct ifreq ifr; |
michael@0 | 43 | int fd; |
michael@0 | 44 | |
michael@0 | 45 | if_indextoname(if_index, ifr.ifr_name); |
michael@0 | 46 | /* TODO can I use the raw socket here and not have to open a new one with each query? */ |
michael@0 | 47 | if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) |
michael@0 | 48 | return (0); |
michael@0 | 49 | if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) { |
michael@0 | 50 | close(fd); |
michael@0 | 51 | return (0); |
michael@0 | 52 | } |
michael@0 | 53 | close(fd); |
michael@0 | 54 | return ifr.ifr_mtu; |
michael@0 | 55 | } |
michael@0 | 56 | #endif |
michael@0 | 57 | |
michael@0 | 58 | #ifdef _WIN32 |
michael@0 | 59 | int |
michael@0 | 60 | sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af) |
michael@0 | 61 | { |
michael@0 | 62 | PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt; |
michael@0 | 63 | DWORD AdapterAddrsSize, Err; |
michael@0 | 64 | |
michael@0 | 65 | AdapterAddrsSize = 0; |
michael@0 | 66 | if ((Err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &AdapterAddrsSize)) != 0) { |
michael@0 | 67 | if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) { |
michael@0 | 68 | SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersAddresses() sizing failed with error code %d, AdapterAddrsSize = %d\n", Err, AdapterAddrsSize); |
michael@0 | 69 | return (-1); |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) { |
michael@0 | 73 | SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n"); |
michael@0 | 74 | return (-1); |
michael@0 | 75 | } |
michael@0 | 76 | if ((Err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) { |
michael@0 | 77 | SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersAddresses() failed with error code %d\n", Err); |
michael@0 | 78 | return (-1); |
michael@0 | 79 | } |
michael@0 | 80 | for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) { |
michael@0 | 81 | if (pAdapt->IfIndex == if_index) |
michael@0 | 82 | return (pAdapt->Mtu); |
michael@0 | 83 | } |
michael@0 | 84 | return (0); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | void |
michael@0 | 88 | getwintimeofday(struct timeval *tv) |
michael@0 | 89 | { |
michael@0 | 90 | struct timeb tb; |
michael@0 | 91 | |
michael@0 | 92 | ftime(&tb); |
michael@0 | 93 | tv->tv_sec = tb.time; |
michael@0 | 94 | tv->tv_usec = tb.millitm * 1000; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | int |
michael@0 | 98 | Win_getifaddrs(struct ifaddrs** interfaces) |
michael@0 | 99 | { |
michael@0 | 100 | #if defined(INET) || defined(INET6) |
michael@0 | 101 | DWORD Err, AdapterAddrsSize; |
michael@0 | 102 | int count; |
michael@0 | 103 | PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt; |
michael@0 | 104 | struct ifaddrs *ifa; |
michael@0 | 105 | #endif |
michael@0 | 106 | #if defined(INET) |
michael@0 | 107 | struct sockaddr_in *addr; |
michael@0 | 108 | #endif |
michael@0 | 109 | #if defined(INET6) |
michael@0 | 110 | struct sockaddr_in6 *addr6; |
michael@0 | 111 | #endif |
michael@0 | 112 | #if defined(INET) || defined(INET6) |
michael@0 | 113 | count = 0; |
michael@0 | 114 | #endif |
michael@0 | 115 | #if defined(INET) |
michael@0 | 116 | AdapterAddrsSize = 0; |
michael@0 | 117 | if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, NULL, &AdapterAddrsSize)) != 0) { |
michael@0 | 118 | if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) { |
michael@0 | 119 | SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV4Addresses() sizing failed with error code %d and AdapterAddrsSize = %d\n", Err, AdapterAddrsSize); |
michael@0 | 120 | return (-1); |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | /* Allocate memory from sizing information */ |
michael@0 | 124 | if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) { |
michael@0 | 125 | SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n"); |
michael@0 | 126 | return (-1); |
michael@0 | 127 | } |
michael@0 | 128 | /* Get actual adapter information */ |
michael@0 | 129 | if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) { |
michael@0 | 130 | SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV4Addresses() failed with error code %d\n", Err); |
michael@0 | 131 | return (-1); |
michael@0 | 132 | } |
michael@0 | 133 | /* Enumerate through each returned adapter and save its information */ |
michael@0 | 134 | for (pAdapt = pAdapterAddrs, count; pAdapt; pAdapt = pAdapt->Next, count++) { |
michael@0 | 135 | addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in)); |
michael@0 | 136 | ifa = (struct ifaddrs *)malloc(sizeof(struct ifaddrs)); |
michael@0 | 137 | if ((addr == NULL) || (ifa == NULL)) { |
michael@0 | 138 | SCTPDBG(SCTP_DEBUG_USR, "Can't allocate memory\n"); |
michael@0 | 139 | return (-1); |
michael@0 | 140 | } |
michael@0 | 141 | ifa->ifa_name = strdup(pAdapt->AdapterName); |
michael@0 | 142 | ifa->ifa_flags = pAdapt->Flags; |
michael@0 | 143 | ifa->ifa_addr = (struct sockaddr *)addr; |
michael@0 | 144 | memcpy(&addr, &pAdapt->FirstUnicastAddress->Address.lpSockaddr, sizeof(struct sockaddr_in)); |
michael@0 | 145 | interfaces[count] = ifa; |
michael@0 | 146 | } |
michael@0 | 147 | #endif |
michael@0 | 148 | #if defined(INET6) |
michael@0 | 149 | if (SCTP_BASE_VAR(userspace_rawsctp6) != -1) { |
michael@0 | 150 | AdapterAddrsSize = 0; |
michael@0 | 151 | if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, NULL, &AdapterAddrsSize)) != 0) { |
michael@0 | 152 | if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) { |
michael@0 | 153 | SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV6Addresses() sizing failed with error code %d AdapterAddrsSize = %d\n", Err, AdapterAddrsSize); |
michael@0 | 154 | return (-1); |
michael@0 | 155 | } |
michael@0 | 156 | } |
michael@0 | 157 | /* Allocate memory from sizing information */ |
michael@0 | 158 | if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) { |
michael@0 | 159 | SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n"); |
michael@0 | 160 | return (-1); |
michael@0 | 161 | } |
michael@0 | 162 | /* Get actual adapter information */ |
michael@0 | 163 | if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) { |
michael@0 | 164 | SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV6Addresses() failed with error code %d\n", Err); |
michael@0 | 165 | return (-1); |
michael@0 | 166 | } |
michael@0 | 167 | /* Enumerate through each returned adapter and save its information */ |
michael@0 | 168 | for (pAdapt = pAdapterAddrs, count; pAdapt; pAdapt = pAdapt->Next, count++) { |
michael@0 | 169 | addr6 = (struct sockaddr_in6 *)malloc(sizeof(struct sockaddr_in6)); |
michael@0 | 170 | ifa = (struct ifaddrs *)malloc(sizeof(struct ifaddrs)); |
michael@0 | 171 | if ((addr6 == NULL) || (ifa == NULL)) { |
michael@0 | 172 | SCTPDBG(SCTP_DEBUG_USR, "Can't allocate memory\n"); |
michael@0 | 173 | return (-1); |
michael@0 | 174 | } |
michael@0 | 175 | ifa->ifa_name = strdup(pAdapt->AdapterName); |
michael@0 | 176 | ifa->ifa_flags = pAdapt->Flags; |
michael@0 | 177 | ifa->ifa_addr = (struct sockaddr *)addr6; |
michael@0 | 178 | memcpy(&addr6, &pAdapt->FirstUnicastAddress->Address.lpSockaddr, sizeof(struct sockaddr_in6)); |
michael@0 | 179 | interfaces[count] = ifa; |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | #endif |
michael@0 | 183 | return (0); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | int |
michael@0 | 187 | win_if_nametoindex(const char *ifname) |
michael@0 | 188 | { |
michael@0 | 189 | IP_ADAPTER_ADDRESSES *addresses, *addr; |
michael@0 | 190 | ULONG status, size; |
michael@0 | 191 | int index = 0; |
michael@0 | 192 | |
michael@0 | 193 | if (!ifname) { |
michael@0 | 194 | return 0; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | size = 0; |
michael@0 | 198 | status = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &size); |
michael@0 | 199 | if (status != ERROR_BUFFER_OVERFLOW) { |
michael@0 | 200 | return 0; |
michael@0 | 201 | } |
michael@0 | 202 | addresses = malloc(size); |
michael@0 | 203 | status = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, addresses, &size); |
michael@0 | 204 | if (status == ERROR_SUCCESS) { |
michael@0 | 205 | for (addr = addresses; addr; addr = addr->Next) { |
michael@0 | 206 | if (addr->AdapterName && !strcmp(ifname, addr->AdapterName)) { |
michael@0 | 207 | index = addr->IfIndex; |
michael@0 | 208 | break; |
michael@0 | 209 | } |
michael@0 | 210 | } |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | free(addresses); |
michael@0 | 214 | return index; |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | #if WINVER < 0x0600 |
michael@0 | 218 | /* These functions are written based on the code at |
michael@0 | 219 | * http://www.cs.wustl.edu/~schmidt/win32-cv-1.html |
michael@0 | 220 | * Therefore, for the rest of the file the following applies: |
michael@0 | 221 | * |
michael@0 | 222 | * |
michael@0 | 223 | * Copyright and Licensing Information for ACE(TM), TAO(TM), CIAO(TM), |
michael@0 | 224 | * DAnCE(TM), and CoSMIC(TM) |
michael@0 | 225 | * |
michael@0 | 226 | * [1]ACE(TM), [2]TAO(TM), [3]CIAO(TM), DAnCE(TM), and [4]CoSMIC(TM) |
michael@0 | 227 | * (henceforth referred to as "DOC software") are copyrighted by |
michael@0 | 228 | * [5]Douglas C. Schmidt and his [6]research group at [7]Washington |
michael@0 | 229 | * University, [8]University of California, Irvine, and [9]Vanderbilt |
michael@0 | 230 | * University, Copyright (c) 1993-2012, all rights reserved. Since DOC |
michael@0 | 231 | * software is open-source, freely available software, you are free to |
michael@0 | 232 | * use, modify, copy, and distribute--perpetually and irrevocably--the |
michael@0 | 233 | * DOC software source code and object code produced from the source, as |
michael@0 | 234 | * well as copy and distribute modified versions of this software. You |
michael@0 | 235 | * must, however, include this copyright statement along with any code |
michael@0 | 236 | * built using DOC software that you release. No copyright statement |
michael@0 | 237 | * needs to be provided if you just ship binary executables of your |
michael@0 | 238 | * software products. |
michael@0 | 239 | * |
michael@0 | 240 | * You can use DOC software in commercial and/or binary software releases |
michael@0 | 241 | * and are under no obligation to redistribute any of your source code |
michael@0 | 242 | * that is built using DOC software. Note, however, that you may not |
michael@0 | 243 | * misappropriate the DOC software code, such as copyrighting it yourself |
michael@0 | 244 | * or claiming authorship of the DOC software code, in a way that will |
michael@0 | 245 | * prevent DOC software from being distributed freely using an |
michael@0 | 246 | * open-source development model. You needn't inform anyone that you're |
michael@0 | 247 | * using DOC software in your software, though we encourage you to let |
michael@0 | 248 | * [10]us know so we can promote your project in the [11]DOC software |
michael@0 | 249 | * success stories. |
michael@0 | 250 | * |
michael@0 | 251 | * The [12]ACE, [13]TAO, [14]CIAO, [15]DAnCE, and [16]CoSMIC web sites |
michael@0 | 252 | * are maintained by the [17]DOC Group at the [18]Institute for Software |
michael@0 | 253 | * Integrated Systems (ISIS) and the [19]Center for Distributed Object |
michael@0 | 254 | * Computing of Washington University, St. Louis for the development of |
michael@0 | 255 | * open-source software as part of the open-source software community. |
michael@0 | 256 | * Submissions are provided by the submitter ``as is'' with no warranties |
michael@0 | 257 | * whatsoever, including any warranty of merchantability, noninfringement |
michael@0 | 258 | * of third party intellectual property, or fitness for any particular |
michael@0 | 259 | * purpose. In no event shall the submitter be liable for any direct, |
michael@0 | 260 | * indirect, special, exemplary, punitive, or consequential damages, |
michael@0 | 261 | * including without limitation, lost profits, even if advised of the |
michael@0 | 262 | * possibility of such damages. Likewise, DOC software is provided as is |
michael@0 | 263 | * with no warranties of any kind, including the warranties of design, |
michael@0 | 264 | * merchantability, and fitness for a particular purpose, |
michael@0 | 265 | * noninfringement, or arising from a course of dealing, usage or trade |
michael@0 | 266 | * practice. Washington University, UC Irvine, Vanderbilt University, |
michael@0 | 267 | * their employees, and students shall have no liability with respect to |
michael@0 | 268 | * the infringement of copyrights, trade secrets or any patents by DOC |
michael@0 | 269 | * software or any part thereof. Moreover, in no event will Washington |
michael@0 | 270 | * University, UC Irvine, or Vanderbilt University, their employees, or |
michael@0 | 271 | * students be liable for any lost revenue or profits or other special, |
michael@0 | 272 | * indirect and consequential damages. |
michael@0 | 273 | * |
michael@0 | 274 | * DOC software is provided with no support and without any obligation on |
michael@0 | 275 | * the part of Washington University, UC Irvine, Vanderbilt University, |
michael@0 | 276 | * their employees, or students to assist in its use, correction, |
michael@0 | 277 | * modification, or enhancement. A [20]number of companies around the |
michael@0 | 278 | * world provide commercial support for DOC software, however. DOC |
michael@0 | 279 | * software is Y2K-compliant, as long as the underlying OS platform is |
michael@0 | 280 | * Y2K-compliant. Likewise, DOC software is compliant with the new US |
michael@0 | 281 | * daylight savings rule passed by Congress as "The Energy Policy Act of |
michael@0 | 282 | * 2005," which established new daylight savings times (DST) rules for |
michael@0 | 283 | * the United States that expand DST as of March 2007. Since DOC software |
michael@0 | 284 | * obtains time/date and calendaring information from operating systems |
michael@0 | 285 | * users will not be affected by the new DST rules as long as they |
michael@0 | 286 | * upgrade their operating systems accordingly. |
michael@0 | 287 | * |
michael@0 | 288 | * The names ACE(TM), TAO(TM), CIAO(TM), DAnCE(TM), CoSMIC(TM), |
michael@0 | 289 | * Washington University, UC Irvine, and Vanderbilt University, may not |
michael@0 | 290 | * be used to endorse or promote products or services derived from this |
michael@0 | 291 | * source without express written permission from Washington University, |
michael@0 | 292 | * UC Irvine, or Vanderbilt University. This license grants no permission |
michael@0 | 293 | * to call products or services derived from this source ACE(TM), |
michael@0 | 294 | * TAO(TM), CIAO(TM), DAnCE(TM), or CoSMIC(TM), nor does it grant |
michael@0 | 295 | * permission for the name Washington University, UC Irvine, or |
michael@0 | 296 | * Vanderbilt University to appear in their names. |
michael@0 | 297 | * |
michael@0 | 298 | * If you have any suggestions, additions, comments, or questions, please |
michael@0 | 299 | * let [21]me know. |
michael@0 | 300 | * |
michael@0 | 301 | * [22]Douglas C. Schmidt |
michael@0 | 302 | * |
michael@0 | 303 | * References |
michael@0 | 304 | * |
michael@0 | 305 | * 1. http://www.cs.wustl.edu/~schmidt/ACE.html |
michael@0 | 306 | * 2. http://www.cs.wustl.edu/~schmidt/TAO.html |
michael@0 | 307 | * 3. http://www.dre.vanderbilt.edu/CIAO/ |
michael@0 | 308 | * 4. http://www.dre.vanderbilt.edu/cosmic/ |
michael@0 | 309 | * 5. http://www.dre.vanderbilt.edu/~schmidt/ |
michael@0 | 310 | * 6. http://www.cs.wustl.edu/~schmidt/ACE-members.html |
michael@0 | 311 | * 7. http://www.wustl.edu/ |
michael@0 | 312 | * 8. http://www.uci.edu/ |
michael@0 | 313 | * 9. http://www.vanderbilt.edu/ |
michael@0 | 314 | * 10. mailto:doc_group@cs.wustl.edu |
michael@0 | 315 | * 11. http://www.cs.wustl.edu/~schmidt/ACE-users.html |
michael@0 | 316 | * 12. http://www.cs.wustl.edu/~schmidt/ACE.html |
michael@0 | 317 | * 13. http://www.cs.wustl.edu/~schmidt/TAO.html |
michael@0 | 318 | * 14. http://www.dre.vanderbilt.edu/CIAO/ |
michael@0 | 319 | * 15. http://www.dre.vanderbilt.edu/~schmidt/DOC_ROOT/DAnCE/ |
michael@0 | 320 | * 16. http://www.dre.vanderbilt.edu/cosmic/ |
michael@0 | 321 | * 17. http://www.dre.vanderbilt.edu/ |
michael@0 | 322 | * 18. http://www.isis.vanderbilt.edu/ |
michael@0 | 323 | * 19. http://www.cs.wustl.edu/~schmidt/doc-center.html |
michael@0 | 324 | * 20. http://www.cs.wustl.edu/~schmidt/commercial-support.html |
michael@0 | 325 | * 21. mailto:d.schmidt@vanderbilt.edu |
michael@0 | 326 | * 22. http://www.dre.vanderbilt.edu/~schmidt/ |
michael@0 | 327 | * 23. http://www.cs.wustl.edu/ACE.html |
michael@0 | 328 | */ |
michael@0 | 329 | |
michael@0 | 330 | void |
michael@0 | 331 | InitializeXPConditionVariable(userland_cond_t *cv) |
michael@0 | 332 | { |
michael@0 | 333 | cv->waiters_count = 0; |
michael@0 | 334 | InitializeCriticalSection(&(cv->waiters_count_lock)); |
michael@0 | 335 | cv->events_[C_SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL); |
michael@0 | 336 | cv->events_[C_BROADCAST] = CreateEvent (NULL, TRUE, FALSE, NULL); |
michael@0 | 337 | } |
michael@0 | 338 | |
michael@0 | 339 | void |
michael@0 | 340 | DeleteXPConditionVariable(userland_cond_t *cv) |
michael@0 | 341 | { |
michael@0 | 342 | CloseHandle(cv->events_[C_BROADCAST]); |
michael@0 | 343 | CloseHandle(cv->events_[C_SIGNAL]); |
michael@0 | 344 | DeleteCriticalSection(&(cv->waiters_count_lock)); |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | int |
michael@0 | 348 | SleepXPConditionVariable(userland_cond_t *cv, userland_mutex_t *mtx) |
michael@0 | 349 | { |
michael@0 | 350 | int result, last_waiter; |
michael@0 | 351 | |
michael@0 | 352 | EnterCriticalSection(&cv->waiters_count_lock); |
michael@0 | 353 | cv->waiters_count++; |
michael@0 | 354 | LeaveCriticalSection(&cv->waiters_count_lock); |
michael@0 | 355 | LeaveCriticalSection (mtx); |
michael@0 | 356 | result = WaitForMultipleObjects(2, cv->events_, FALSE, INFINITE); |
michael@0 | 357 | if (result==-1) { |
michael@0 | 358 | result = GetLastError(); |
michael@0 | 359 | } |
michael@0 | 360 | EnterCriticalSection(&cv->waiters_count_lock); |
michael@0 | 361 | cv->waiters_count--; |
michael@0 | 362 | last_waiter = |
michael@0 | 363 | result == (C_SIGNAL + C_BROADCAST && (cv->waiters_count == 0)); |
michael@0 | 364 | LeaveCriticalSection(&cv->waiters_count_lock); |
michael@0 | 365 | if (last_waiter) |
michael@0 | 366 | ResetEvent(cv->events_[C_BROADCAST]); |
michael@0 | 367 | EnterCriticalSection (mtx); |
michael@0 | 368 | return result; |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | void |
michael@0 | 372 | WakeAllXPConditionVariable(userland_cond_t *cv) |
michael@0 | 373 | { |
michael@0 | 374 | int have_waiters; |
michael@0 | 375 | EnterCriticalSection(&cv->waiters_count_lock); |
michael@0 | 376 | have_waiters = cv->waiters_count > 0; |
michael@0 | 377 | LeaveCriticalSection(&cv->waiters_count_lock); |
michael@0 | 378 | if (have_waiters) |
michael@0 | 379 | SetEvent (cv->events_[C_BROADCAST]); |
michael@0 | 380 | } |
michael@0 | 381 | #endif |
michael@0 | 382 | #endif |