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.

pool.h 2.5KB

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