threadpool.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Thread eval for VIPS.
  2. *
  3. * 29/9/99 JC
  4. * - from thread.h
  5. * 17/3/10
  6. * - from threadgroup
  7. * - rework with a simpler distributed work allocation model
  8. */
  9. /*
  10. This file is part of VIPS.
  11. VIPS is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU Lesser General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU Lesser General Public License for more details.
  19. You should have received a copy of the GNU Lesser General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. 02110-1301 USA
  23. */
  24. /*
  25. These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
  26. */
  27. #ifndef VIPS_THREADPOOL_H
  28. #define VIPS_THREADPOOL_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif /*__cplusplus*/
  32. #include <vips/semaphore.h>
  33. /* Per-thread state. Allocate functions can use these members to
  34. * communicate with work functions.
  35. */
  36. #define VIPS_TYPE_THREAD_STATE (vips_thread_state_get_type())
  37. #define VIPS_THREAD_STATE( obj ) \
  38. (G_TYPE_CHECK_INSTANCE_CAST( (obj), \
  39. VIPS_TYPE_THREAD_STATE, VipsThreadState ))
  40. #define VIPS_THREAD_STATE_CLASS( klass ) \
  41. (G_TYPE_CHECK_CLASS_CAST( (klass), \
  42. VIPS_TYPE_THREAD_STATE, VipsThreadStateClass))
  43. #define VIPS_IS_THREAD_STATE( obj ) \
  44. (G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_THREAD_STATE ))
  45. #define VIPS_IS_THREAD_STATE_CLASS( klass ) \
  46. (G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_THREAD_STATE ))
  47. #define VIPS_THREAD_STATE_GET_CLASS( obj ) \
  48. (G_TYPE_INSTANCE_GET_CLASS( (obj), \
  49. VIPS_TYPE_THREAD_STATE, VipsThreadStateClass ))
  50. typedef struct _VipsThreadState {
  51. VipsObject parent_object;
  52. /*< public >*/
  53. /* Image we run on.
  54. */
  55. VipsImage *im;
  56. /* This region is created and destroyed by the threadpool for the
  57. * use of the worker.
  58. */
  59. VipsRegion *reg;
  60. /* Neither used nor set, do what you like with them.
  61. */
  62. VipsRect pos;
  63. int x, y;
  64. /* Set in work to get the allocate to signal stop.
  65. */
  66. gboolean stop;
  67. /* The client data passed to the enclosing vips_threadpool_run().
  68. */
  69. void *a;
  70. /* Set in allocate to stall this thread for a moment. Handy for
  71. * debugging race conditions.
  72. */
  73. gboolean stall;
  74. } VipsThreadState;
  75. typedef struct _VipsThreadStateClass {
  76. VipsObjectClass parent_class;
  77. /*< public >*/
  78. } VipsThreadStateClass;
  79. void *vips_thread_state_set( VipsObject *object, void *a, void *b );
  80. /* Don't put spaces around void here, it breaks gtk-doc.
  81. */
  82. GType vips_thread_state_get_type(void);
  83. VipsThreadState *vips_thread_state_new( VipsImage *im, void *a );
  84. /* Constructor for per-thread state.
  85. */
  86. typedef VipsThreadState *(*VipsThreadStartFn)( VipsImage *im, void *a );
  87. /* A work allocate function. This is run single-threaded by a worker to
  88. * set up a new work unit.
  89. * Return non-zero for errors. Set *stop for "no more work to do"
  90. */
  91. typedef int (*VipsThreadpoolAllocateFn)( VipsThreadState *state,
  92. void *a, gboolean *stop );
  93. /* A work function. This does a unit of work (eg. processing a tile or
  94. * whatever). Return non-zero for errors.
  95. */
  96. typedef int (*VipsThreadpoolWorkFn)( VipsThreadState *state, void *a );
  97. /* A progress function. This is run by the main thread once for every
  98. * allocation. Return an error to kill computation early.
  99. */
  100. typedef int (*VipsThreadpoolProgressFn)( void *a );
  101. int vips_threadpool_run( VipsImage *im,
  102. VipsThreadStartFn start,
  103. VipsThreadpoolAllocateFn allocate,
  104. VipsThreadpoolWorkFn work,
  105. VipsThreadpoolProgressFn progress,
  106. void *a );
  107. void vips_get_tile_size( VipsImage *im,
  108. int *tile_width, int *tile_height, int *n_lines );
  109. #ifdef __cplusplus
  110. }
  111. #endif /*__cplusplus*/
  112. #endif /*VIPS_THREADPOOL_H*/