pool.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. #ifndef POOL_H
  11. #define POOL_H
  12. #if defined (__cplusplus)
  13. extern "C" {
  14. #endif
  15. #include "zstd_deps.h"
  16. #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
  17. #include "../zstd.h"
  18. typedef struct POOL_ctx_s POOL_ctx;
  19. /*! POOL_create() :
  20. * Create a thread pool with at most `numThreads` threads.
  21. * `numThreads` must be at least 1.
  22. * The maximum number of queued jobs before blocking is `queueSize`.
  23. * @return : POOL_ctx pointer on success, else NULL.
  24. */
  25. POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);
  26. POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
  27. ZSTD_customMem customMem);
  28. /*! POOL_free() :
  29. * Free a thread pool returned by POOL_create().
  30. */
  31. void POOL_free(POOL_ctx* ctx);
  32. /*! POOL_joinJobs() :
  33. * Waits for all queued jobs to finish executing.
  34. */
  35. void POOL_joinJobs(POOL_ctx* ctx);
  36. /*! POOL_resize() :
  37. * Expands or shrinks pool's number of threads.
  38. * This is more efficient than releasing + creating a new context,
  39. * since it tries to preserve and re-use existing threads.
  40. * `numThreads` must be at least 1.
  41. * @return : 0 when resize was successful,
  42. * !0 (typically 1) if there is an error.
  43. * note : only numThreads can be resized, queueSize remains unchanged.
  44. */
  45. int POOL_resize(POOL_ctx* ctx, size_t numThreads);
  46. /*! POOL_sizeof() :
  47. * @return threadpool memory usage
  48. * note : compatible with NULL (returns 0 in this case)
  49. */
  50. size_t POOL_sizeof(const POOL_ctx* ctx);
  51. /*! POOL_function :
  52. * The function type that can be added to a thread pool.
  53. */
  54. typedef void (*POOL_function)(void*);
  55. /*! POOL_add() :
  56. * Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
  57. * Possibly blocks until there is room in the queue.
  58. * Note : The function may be executed asynchronously,
  59. * therefore, `opaque` must live until function has been completed.
  60. */
  61. void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
  62. /*! POOL_tryAdd() :
  63. * Add the job `function(opaque)` to thread pool _if_ a queue slot is available.
  64. * Returns immediately even if not (does not block).
  65. * @return : 1 if successful, 0 if not.
  66. */
  67. int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
  68. #if defined (__cplusplus)
  69. }
  70. #endif
  71. #endif