pool.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "zstd_deps.h"
  13. #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
  14. #include "../zstd.h"
  15. typedef struct POOL_ctx_s POOL_ctx;
  16. /*! POOL_create() :
  17. * Create a thread pool with at most `numThreads` threads.
  18. * `numThreads` must be at least 1.
  19. * The maximum number of queued jobs before blocking is `queueSize`.
  20. * @return : POOL_ctx pointer on success, else NULL.
  21. */
  22. POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);
  23. POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
  24. ZSTD_customMem customMem);
  25. /*! POOL_free() :
  26. * Free a thread pool returned by POOL_create().
  27. */
  28. void POOL_free(POOL_ctx* ctx);
  29. /*! POOL_joinJobs() :
  30. * Waits for all queued jobs to finish executing.
  31. */
  32. void POOL_joinJobs(POOL_ctx* ctx);
  33. /*! POOL_resize() :
  34. * Expands or shrinks pool's number of threads.
  35. * This is more efficient than releasing + creating a new context,
  36. * since it tries to preserve and reuse existing threads.
  37. * `numThreads` must be at least 1.
  38. * @return : 0 when resize was successful,
  39. * !0 (typically 1) if there is an error.
  40. * note : only numThreads can be resized, queueSize remains unchanged.
  41. */
  42. int POOL_resize(POOL_ctx* ctx, size_t numThreads);
  43. /*! POOL_sizeof() :
  44. * @return threadpool memory usage
  45. * note : compatible with NULL (returns 0 in this case)
  46. */
  47. size_t POOL_sizeof(const POOL_ctx* ctx);
  48. /*! POOL_function :
  49. * The function type that can be added to a thread pool.
  50. */
  51. typedef void (*POOL_function)(void*);
  52. /*! POOL_add() :
  53. * Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
  54. * Possibly blocks until there is room in the queue.
  55. * Note : The function may be executed asynchronously,
  56. * therefore, `opaque` must live until function has been completed.
  57. */
  58. void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
  59. /*! POOL_tryAdd() :
  60. * Add the job `function(opaque)` to thread pool _if_ a queue slot is available.
  61. * Returns immediately even if not (does not block).
  62. * @return : 1 if successful, 0 if not.
  63. */
  64. int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
  65. #endif