pool.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2016-present, 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. #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, ZSTD_customMem customMem);
  27. /*! POOL_free() :
  28. Free a thread pool returned by POOL_create().
  29. */
  30. void POOL_free(POOL_ctx* ctx);
  31. /*! POOL_sizeof() :
  32. return memory usage of pool returned by POOL_create().
  33. */
  34. size_t POOL_sizeof(POOL_ctx* ctx);
  35. /*! POOL_function :
  36. The function type that can be added to a thread pool.
  37. */
  38. typedef void (*POOL_function)(void*);
  39. /*! POOL_add_function :
  40. The function type for a generic thread pool add function.
  41. */
  42. typedef void (*POOL_add_function)(void*, POOL_function, void*);
  43. /*! POOL_add() :
  44. Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
  45. Possibly blocks until there is room in the queue.
  46. Note : The function may be executed asynchronously, so `opaque` must live until the function has been completed.
  47. */
  48. void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
  49. /*! POOL_tryAdd() :
  50. Add the job `function(opaque)` to the thread pool if a worker is available.
  51. return immediately otherwise.
  52. @return : 1 if successful, 0 if not.
  53. */
  54. int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
  55. #if defined (__cplusplus)
  56. }
  57. #endif
  58. #endif