lwp_futex_table.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-01 Shell Init ver.
  9. */
  10. #include "lwp_futex_internal.h"
  11. static struct shared_futex_entry *_futex_hash_head;
  12. /**
  13. * @brief Add a futex to the global hash table
  14. *
  15. * @param[in] key Pointer to the shared futex key structure
  16. * @param[in] futex The futex to be added to the table
  17. *
  18. * @return rt_err_t Returns RT_EOK on success, error code on failure
  19. */
  20. rt_err_t futex_global_table_add(struct shared_futex_key *key, rt_futex_t futex)
  21. {
  22. rt_err_t rc = 0;
  23. struct shared_futex_entry *entry = &futex->entry;
  24. futex->entry.key.mobj = key->mobj;
  25. futex->entry.key.offset = key->offset;
  26. RT_UTHASH_ADD(_futex_hash_head, key, sizeof(struct shared_futex_key), entry);
  27. return rc;
  28. }
  29. /**
  30. * @brief Find a futex in the global hash table
  31. *
  32. * @param[in] key Pointer to the shared futex key structure
  33. * @param[out] futex Pointer to store the found futex
  34. *
  35. * @return rt_err_t Returns RT_EOK if found, -RT_ENOENT if not found
  36. */
  37. rt_err_t futex_global_table_find(struct shared_futex_key *key, rt_futex_t *futex)
  38. {
  39. rt_err_t rc;
  40. rt_futex_t found_futex;
  41. struct shared_futex_entry *entry;
  42. RT_UTHASH_FIND(_futex_hash_head, key, sizeof(struct shared_futex_key), entry);
  43. if (entry)
  44. {
  45. rc = RT_EOK;
  46. found_futex = rt_container_of(entry, struct rt_futex, entry);
  47. }
  48. else
  49. {
  50. rc = -RT_ENOENT;
  51. found_futex = RT_NULL;
  52. }
  53. *futex = found_futex;
  54. return rc;
  55. }
  56. /**
  57. * @brief Delete a futex from the global hash table
  58. *
  59. * @param[in] key Pointer to the shared futex key structure
  60. *
  61. * @return rt_err_t Returns RT_EOK if deleted successfully, -RT_ENOENT if not found
  62. */
  63. rt_err_t futex_global_table_delete(struct shared_futex_key *key)
  64. {
  65. rt_err_t rc;
  66. struct shared_futex_entry *entry;
  67. RT_UTHASH_FIND(_futex_hash_head, key, sizeof(struct shared_futex_key), entry);
  68. if (entry)
  69. {
  70. RT_UTHASH_DELETE(_futex_hash_head, entry);
  71. rc = RT_EOK;
  72. }
  73. else
  74. {
  75. rc = -RT_ENOENT;
  76. }
  77. return rc;
  78. }