mutex.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Linux 2.6.32 and later Kernel module for VMware MVP Hypervisor Support
  3. *
  4. * Copyright (C) 2010-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; see the file COPYING. If not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #line 5
  20. /**
  21. * @file
  22. *
  23. * @brief Common mutex definitions.
  24. */
  25. #ifndef _MUTEX_H
  26. #define _MUTEX_H
  27. #define INCLUDE_ALLOW_MVPD
  28. #define INCLUDE_ALLOW_VMX
  29. #define INCLUDE_ALLOW_MODULE
  30. #define INCLUDE_ALLOW_MONITOR
  31. #define INCLUDE_ALLOW_GPL
  32. #include "include_check.h"
  33. #define MUTEX_CVAR_MAX 2 /**< maximum number of condition variables */
  34. /**< supported on a given mutex */
  35. typedef enum MutexMode MutexMode;
  36. typedef struct HKWaitQ HKWaitQ;
  37. typedef struct Mutex Mutex;
  38. /**
  39. * @brief modes for locking
  40. */
  41. enum MutexMode {
  42. MutexModeSH = 1, /**< minimum value that can be saved in low */
  43. /**< 16 bits of 'state', ie, it won't allow */
  44. /**< any other EXs without overflowing. */
  45. /**< it also will block if there are already */
  46. /**< 0xFFFF other shared accesses, but it */
  47. /**< should be of little consequence. */
  48. MutexModeEX = 0xFFFF /**< maximum value that can be saved in low */
  49. /**< 16 bits of 'state', ie, it won't allow */
  50. /**< any other EXs or SHs in there without */
  51. /**< overflowing, thus causing a block. */
  52. };
  53. #include "atomic.h"
  54. typedef union Mutex_State {
  55. uint32 state; /**< for atomic setting/reading */
  56. struct {
  57. uint16 mode; /**< the sum of mode values of MutexMode */
  58. uint16 blck; /**< The number of threads blocked */
  59. };
  60. } Mutex_State;
  61. /**
  62. * @brief shareable mutex struct.
  63. */
  64. struct Mutex {
  65. HKVA mtxHKVA; /**< mutex's host kernel virtual address */
  66. AtmUInt32 state; /**< low 16 bits: # of shared accessors */
  67. /**< or FFFF if granted exclusive */
  68. /**< high 16 bits: # of blocked threads */
  69. AtmUInt32 waiters; /**< number of threads on all condWaitQs */
  70. /**< ... increment only with mutex locked EX */
  71. /**< ... decrement any time */
  72. AtmUInt32 blocked; /**< number times blocked (stats only) */
  73. HKVA lockWaitQ; /**< threads blocked for mutex to be unlocked */
  74. HKVA cvarWaitQs[MUTEX_CVAR_MAX]; /**< condition variables */
  75. /*
  76. * Padding to keep binary compatibility @see{MVP-1876}
  77. * These padding bytes can be used for debugging.
  78. */
  79. int line;
  80. int lineUnl;
  81. uint32 pad3;
  82. uint32 pad4;
  83. uint32 pad5;
  84. uint32 pad6;
  85. };
  86. #define Mutex_Lock(a, b) Mutex_LockLine(a, b, __FILE__, __LINE__)
  87. #define Mutex_Unlock(a, b) Mutex_UnlockLine(a, b, __LINE__)
  88. #define Mutex_UnlSleep(a, b, c) Mutex_UnlSleepLine(a, b, c, __FILE__, __LINE__)
  89. #define Mutex_UnlSleepTest(a, b, c, d, e) \
  90. Mutex_UnlSleepTestLine(a, b, c, d, e, __FILE__, __LINE__)
  91. int Mutex_LockLine(Mutex *mutex, MutexMode mode, const char *file, int line);
  92. void Mutex_UnlockLine(Mutex *mutex, MutexMode mode, int line);
  93. int
  94. Mutex_UnlSleepLine(Mutex *mutex,
  95. MutexMode mode,
  96. uint32 cvi,
  97. const char *file,
  98. int line);
  99. int
  100. Mutex_UnlSleepTestLine(Mutex *mutex,
  101. MutexMode mode,
  102. uint32 cvi,
  103. AtmUInt32 *test,
  104. uint32 mask,
  105. const char *file,
  106. int line);
  107. void Mutex_UnlWake(Mutex *mutex, MutexMode mode, uint32 cvi, _Bool all);
  108. #endif