sync.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2023 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Common utilities for synchronization.
  18. */
  19. #ifndef KERN_SYNC_H
  20. #define KERN_SYNC_H
  21. #include <stdint.h>
  22. #include <string.h>
  23. #include <kern/hash.h>
  24. #include <kern/task.h>
  25. struct vm_map;
  26. struct vm_object;
  27. union sync_key
  28. {
  29. struct
  30. {
  31. struct vm_map *map;
  32. uintptr_t addr;
  33. } local;
  34. struct
  35. {
  36. struct vm_object *object;
  37. alignas (sizeof (uintptr_t)) uint64_t offset;
  38. } shared;
  39. struct
  40. {
  41. uintptr_t all[1 + sizeof (uint64_t) / sizeof (uintptr_t)];
  42. } both;
  43. };
  44. static_assert (sizeof (((union sync_key *)0)->both) >=
  45. sizeof (((union sync_key *)0)->local) &&
  46. sizeof (((union sync_key *)0)->both) >=
  47. sizeof (((union sync_key *)0)->shared),
  48. "invalid layout for sync_key");
  49. static inline bool
  50. sync_key_eq (const union sync_key *x, const union sync_key *y)
  51. {
  52. for (size_t i = 0; i < ARRAY_SIZE (x->both.all); ++i)
  53. if (x->both.all[i] != y->both.all[i])
  54. return (false);
  55. return (true);
  56. }
  57. static inline uint32_t
  58. sync_key_hash (const union sync_key *x)
  59. {
  60. uint32_t ret = 0;
  61. for (size_t i = 0; i < ARRAY_SIZE (x->both.all); ++i)
  62. ret = hash_mix (ret, hash_uptr (x->both.all[i]));
  63. return (ret);
  64. }
  65. static inline void
  66. sync_key_clear (union sync_key *key)
  67. {
  68. for (size_t i = 0; i < ARRAY_SIZE (key->both.all); ++i)
  69. key->both.all[i] = 0;
  70. }
  71. static inline bool
  72. sync_key_isclear (const union sync_key *key)
  73. {
  74. for (size_t i = 0; i < ARRAY_SIZE (key->both.all); ++i)
  75. if (key->both.all[i] != 0)
  76. return (false);
  77. return (true);
  78. }
  79. static inline void
  80. sync_key_init (union sync_key *key, const void *ptr)
  81. {
  82. sync_key_clear (key);
  83. key->local.addr = (uintptr_t)ptr;
  84. }
  85. static inline void
  86. sync_key_local_init (union sync_key *key, const void *ptr, struct vm_map *map)
  87. {
  88. sync_key_init (key, ptr);
  89. key->local.map = map;
  90. }
  91. static inline void
  92. sync_key_shared_init (union sync_key *key, struct vm_object *obj, uint64_t off)
  93. {
  94. sync_key_clear (key);
  95. key->shared.object = obj;
  96. key->shared.offset = off;
  97. }
  98. #endif