lock.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * TAP-Windows -- A kernel driver to provide virtual tap
  3. * device functionality on Windows.
  4. *
  5. * This code was inspired by the CIPE-Win32 driver by Damion K. Wilson.
  6. *
  7. * This source code is Copyright (C) 2002-2014 OpenVPN Technologies, Inc.,
  8. * and is released under the GPL version 2 (see below).
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program (see the file COPYING included with this
  21. * distribution); if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. typedef struct
  25. {
  26. volatile long count;
  27. } MUTEX;
  28. #define MUTEX_SLEEP_TIME 10000 // microseconds
  29. #define INIT_MUTEX(m) { (m)->count = 0; }
  30. #define ACQUIRE_MUTEX_BLOCKING(m) \
  31. { \
  32. while (NdisInterlockedIncrement (&((m)->count)) != 1) \
  33. { \
  34. NdisInterlockedDecrement(&((m)->count)); \
  35. NdisMSleep(MUTEX_SLEEP_TIME); \
  36. } \
  37. }
  38. #define RELEASE_MUTEX(m) \
  39. { \
  40. NdisInterlockedDecrement(&((m)->count)); \
  41. }
  42. #define ACQUIRE_MUTEX_NONBLOCKING(m, result) \
  43. { \
  44. if (NdisInterlockedIncrement (&((m)->count)) != 1) \
  45. { \
  46. NdisInterlockedDecrement(&((m)->count)); \
  47. result = FALSE; \
  48. } \
  49. else \
  50. { \
  51. result = TRUE; \
  52. } \
  53. }
  54. #define ACQUIRE_MUTEX_ADAPTIVE(m, result) \
  55. { \
  56. result = TRUE; \
  57. while (NdisInterlockedIncrement (&((m)->count)) != 1) \
  58. { \
  59. NdisInterlockedDecrement(&((m)->count)); \
  60. if (KeGetCurrentIrql () < DISPATCH_LEVEL) \
  61. NdisMSleep(MUTEX_SLEEP_TIME); \
  62. else \
  63. { \
  64. result = FALSE; \
  65. break; \
  66. } \
  67. } \
  68. }