proc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "../util/darray.h"
  17. #include "../util/threading.h"
  18. #include "decl.h"
  19. #include "proc.h"
  20. struct proc_info {
  21. struct decl_info func;
  22. void *data;
  23. proc_handler_proc_t callback;
  24. };
  25. static inline void proc_info_free(struct proc_info *pi)
  26. {
  27. decl_info_free(&pi->func);
  28. }
  29. struct proc_handler {
  30. /* TODO: replace with hash table lookup? */
  31. pthread_mutex_t mutex;
  32. DARRAY(struct proc_info) procs;
  33. };
  34. static struct proc_info *getproc(proc_handler_t *handler, const char *name)
  35. {
  36. for (size_t i = 0; i < handler->procs.num; i++) {
  37. struct proc_info *info = handler->procs.array + i;
  38. if (strcmp(info->func.name, name) == 0) {
  39. return info;
  40. }
  41. }
  42. return NULL;
  43. }
  44. /* ------------------------------------------------------------------------- */
  45. proc_handler_t *proc_handler_create(void)
  46. {
  47. struct proc_handler *handler = bmalloc(sizeof(struct proc_handler));
  48. if (pthread_mutex_init_recursive(&handler->mutex) != 0) {
  49. blog(LOG_ERROR, "Couldn't create proc_handler mutex");
  50. bfree(handler);
  51. return NULL;
  52. }
  53. da_init(handler->procs);
  54. return handler;
  55. }
  56. void proc_handler_destroy(proc_handler_t *handler)
  57. {
  58. if (!handler)
  59. return;
  60. for (size_t i = 0; i < handler->procs.num; i++)
  61. proc_info_free(handler->procs.array + i);
  62. da_free(handler->procs);
  63. pthread_mutex_destroy(&handler->mutex);
  64. bfree(handler);
  65. }
  66. void proc_handler_add(proc_handler_t *handler, const char *decl_string, proc_handler_proc_t proc, void *data)
  67. {
  68. if (!handler)
  69. return;
  70. struct proc_info pi;
  71. memset(&pi, 0, sizeof(struct proc_info));
  72. if (!parse_decl_string(&pi.func, decl_string)) {
  73. blog(LOG_ERROR, "Function declaration invalid: %s", decl_string);
  74. return;
  75. }
  76. pi.callback = proc;
  77. pi.data = data;
  78. pthread_mutex_lock(&handler->mutex);
  79. struct proc_info *existing = getproc(handler, pi.func.name);
  80. if (existing) {
  81. blog(LOG_WARNING, "Procedure '%s' already exists", pi.func.name);
  82. proc_info_free(&pi);
  83. } else {
  84. da_push_back(handler->procs, &pi);
  85. }
  86. pthread_mutex_unlock(&handler->mutex);
  87. }
  88. bool proc_handler_call(proc_handler_t *handler, const char *name, calldata_t *params)
  89. {
  90. if (!handler)
  91. return false;
  92. pthread_mutex_lock(&handler->mutex);
  93. struct proc_info *info = getproc(handler, name);
  94. struct proc_info info_copy;
  95. if (info)
  96. info_copy = *info;
  97. pthread_mutex_unlock(&handler->mutex);
  98. if (!info)
  99. return false;
  100. info_copy.callback(info_copy.data, params);
  101. return true;
  102. }