You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

async_session.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef RSPAMD_ASYNC_SESSION_H
  17. #define RSPAMD_ASYNC_SESSION_H
  18. #include "config.h"
  19. #include "mem_pool.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. struct rspamd_async_event;
  24. struct rspamd_async_session;
  25. typedef void (*event_finalizer_t)(gpointer ud);
  26. typedef gboolean (*session_finalizer_t)(gpointer user_data);
  27. /**
  28. * Make new async session
  29. * @param pool pool to alloc memory from
  30. * @param fin a callback called when no events are found in session
  31. * @param restore a callback is called to restore processing of session
  32. * @param cleanup a callback called when session is forcefully destroyed
  33. * @param user_data abstract user data
  34. * @return
  35. */
  36. struct rspamd_async_session *rspamd_session_create(rspamd_mempool_t *pool,
  37. session_finalizer_t fin, event_finalizer_t restore,
  38. event_finalizer_t cleanup, gpointer user_data);
  39. /**
  40. * Insert new event to the session
  41. * @param session session object
  42. * @param fin finalizer callback
  43. * @param user_data abstract user_data
  44. * @param forced unused
  45. */
  46. struct rspamd_async_event *
  47. rspamd_session_add_event_full(struct rspamd_async_session *session,
  48. event_finalizer_t fin,
  49. gpointer user_data,
  50. const char *subsystem,
  51. const char *event_source);
  52. #define rspamd_session_add_event(session, fin, user_data, subsystem) \
  53. rspamd_session_add_event_full(session, fin, user_data, subsystem, G_STRLOC)
  54. /**
  55. * Remove normal event
  56. * @param session session object
  57. * @param fin final callback
  58. * @param ud user data object
  59. */
  60. void rspamd_session_remove_event_full(struct rspamd_async_session *session,
  61. event_finalizer_t fin,
  62. gpointer ud,
  63. const char *event_source);
  64. #define rspamd_session_remove_event(session, fin, user_data) \
  65. rspamd_session_remove_event_full(session, fin, user_data, G_STRLOC)
  66. /**
  67. * Must be called at the end of session, it calls fin functions for all non-forced callbacks
  68. * @return true if the whole session was destroyed and false if there are forced events
  69. */
  70. gboolean rspamd_session_destroy(struct rspamd_async_session *session);
  71. /**
  72. * Try to remove all events pending
  73. */
  74. void rspamd_session_cleanup(struct rspamd_async_session *session, bool forced_cleanup);
  75. /**
  76. * Returns mempool associated with async session
  77. * @param session
  78. * @return
  79. */
  80. rspamd_mempool_t *rspamd_session_mempool(struct rspamd_async_session *session);
  81. /**
  82. * Check session for events pending and call fin callback if no events are pending
  83. * @param session session object
  84. * @return TRUE if session has pending events
  85. */
  86. gboolean rspamd_session_pending(struct rspamd_async_session *session);
  87. /**
  88. * Returns number of events pending
  89. * @param session
  90. * @return
  91. */
  92. unsigned int rspamd_session_events_pending(struct rspamd_async_session *session);
  93. /**
  94. * Returns TRUE if an async session is currently destroying
  95. * @param s
  96. * @return
  97. */
  98. gboolean rspamd_session_blocked(struct rspamd_async_session *s);
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif /*RSPAMD_ASYNC_SESSION_H*/