obs-scripting-callback.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <callback/calldata.h>
  16. #include <util/threading.h>
  17. #include <util/bmem.h>
  18. #include "obs-scripting-internal.h"
  19. extern pthread_mutex_t detach_mutex;
  20. extern struct script_callback *detached_callbacks;
  21. struct script_callback {
  22. struct script_callback *next;
  23. struct script_callback **p_prev_next;
  24. void (*on_remove)(void *p_cb);
  25. obs_script_t *script;
  26. calldata_t extra;
  27. volatile bool removed;
  28. };
  29. static inline bool script_callback_removed(struct script_callback *cb)
  30. {
  31. return os_atomic_load_bool(&cb->removed);
  32. }
  33. static inline void *add_script_callback(struct script_callback **first,
  34. obs_script_t *script, size_t extra_size)
  35. {
  36. struct script_callback *cb = bzalloc(sizeof(*cb) + extra_size);
  37. cb->script = script;
  38. struct script_callback *next = *first;
  39. cb->next = next;
  40. cb->p_prev_next = first;
  41. if (next)
  42. next->p_prev_next = &cb->next;
  43. *first = cb;
  44. return cb;
  45. }
  46. static inline void remove_script_callback(struct script_callback *cb)
  47. {
  48. os_atomic_set_bool(&cb->removed, true);
  49. struct script_callback *next = cb->next;
  50. if (next)
  51. next->p_prev_next = cb->p_prev_next;
  52. *cb->p_prev_next = cb->next;
  53. pthread_mutex_lock(&detach_mutex);
  54. next = detached_callbacks;
  55. cb->next = next;
  56. if (next)
  57. next->p_prev_next = &cb->next;
  58. cb->p_prev_next = &detached_callbacks;
  59. detached_callbacks = cb;
  60. pthread_mutex_unlock(&detach_mutex);
  61. if (cb->on_remove)
  62. cb->on_remove(cb);
  63. }
  64. static inline void just_free_script_callback(struct script_callback *cb)
  65. {
  66. calldata_free(&cb->extra);
  67. bfree(cb);
  68. }
  69. static inline void free_script_callback(struct script_callback *cb)
  70. {
  71. pthread_mutex_lock(&detach_mutex);
  72. struct script_callback *next = cb->next;
  73. if (next)
  74. next->p_prev_next = cb->p_prev_next;
  75. *cb->p_prev_next = cb->next;
  76. pthread_mutex_unlock(&detach_mutex);
  77. just_free_script_callback(cb);
  78. }