Hdevthread.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*****************************************************************************
  2. * Hdevthread.h
  3. *****************************************************************************
  4. *
  5. * Project: HALCON/C
  6. * Description: generic HALCON/C thread interface for hdevelop's HALCON/C
  7. * export (for internal use only, signatures and functionality
  8. * may change )
  9. *
  10. * (c) 2014-2022 by MVTec Software GmbH
  11. * www.mvtec.com
  12. *
  13. ****************************************************************************/
  14. #ifndef HDEVTHREAD_H
  15. #define HDEVTHREAD_H
  16. #if defined(__cplusplus)
  17. extern "C" {
  18. #endif
  19. #include "HalconCDefs.h"
  20. #include "halconc/Hvector.h"
  21. typedef Hlong Hdevthread;
  22. typedef Hlong Hdevthread_context;
  23. #define HDEVTHREAD_UNDEF 0
  24. #define HDEVTHREAD_CONTEXT_UNDEF 0
  25. /**************************************************************************
  26. * HALCON/C Hdevthread interface
  27. * This interface is for HDevelop C-Export only!
  28. **************************************************************************/
  29. /* A thread context marks for the started thread the period the father
  30. * (=caller) thread stays within the function in which the thread was
  31. * started. This period is equivalent to the period the output parameters of
  32. * procedure started within a thread are valid. I.e., a thread has to be
  33. * joined in the function where it was started if the output parameters
  34. * should be used.
  35. * The procedure that is starting a thread has to call create_hdevthread_context
  36. * after entering (at least before starting the first thread).
  37. * clear_hdevthread_contex must be called before leaving the calling procedure,
  38. * but at least after the last join call within this procedure (if present).
  39. */
  40. LIntExport Herror
  41. create_hdevthread_context(Hdevthread_context* H_RESTRICT context_handle);
  42. LIntExport Herror clear_hdevthread_context(Hdevthread_context context_handle);
  43. /* create_hdevthread allocates and initializes a HALCON/C thread handle
  44. * 'hthread_handle'. This handle will hold the input and output parameters
  45. * of the procedure that should be called as thread. Therefore, the basic
  46. * address of this procedure is passed in 'proc' (arbitrary sequence of
  47. * input/output HALCON/C types, return type void). Accordingly, the total
  48. * number of input object parameters (iop_num), output object parameters
  49. * (oop_num), input vector parameters (ivp_num), output vector parameters
  50. * (ovp_num), input control parameters (icp_num) and output control
  51. * parameters (ocp_num) of the procedure is passed. The thread instance will
  52. * be owned by the started thread and needn't be cleared by the calling
  53. * thread. */
  54. LIntExport Herror create_hdevthread(Hdevthread_context const context_handle,
  55. void const* proc, Hlong ip_num,
  56. Hlong op_num,
  57. Hdevthread* H_RESTRICT hthread_handle);
  58. /* exit thread is called at the end of the thread's execution and clears
  59. * the hthread handle. */
  60. LIntExport Herror exit_hdevthread(Hdevthread hthread_handle);
  61. /* clear_hdevthread frees all data hold by the hthread handle. Generally,
  62. * it will be called by exit_hdevthread implicitly and needn't be called
  63. * explicitly. */
  64. LIntExport Herror clear_hdevthread(Hdevthread hthread_handle);
  65. /* -------------------------------------------------------------------
  66. * Functions to set the input/output parameters of the procedure 'proc'
  67. * within the father thread before starting the new (child) thread:
  68. * -------------------------------------------------------------------*/
  69. /* set_hdevthread_iop sets a input object parameter of the procedure that should
  70. * be started as a thread. The object parameter
  71. * is copied (lazy copy). parpos denotes the x-th occurrence of an input
  72. * object parameter within the signature of proc, starting with 0.*/
  73. LIntExport Herror set_hdevthread_iop(Hdevthread hthread_handle, Hlong parpos,
  74. Hobject const obj);
  75. /* set_hdevthread_oop sets a reference on a output object parameter of the
  76. * procedure that should be started as a thread. parpos denotes the x-th
  77. * occurrence of an output object parameter within the signature of proc,
  78. * starting with 0.*/
  79. LIntExport Herror bind_hdevthread_oop(Hdevthread hthread_handle, Hlong parpos,
  80. bool glb_flag, Hobject* obj);
  81. /* set_hdevthread_ivp sets a input vector parameter (Hvector) of the procedure
  82. * that should be started as a thread. The input parameter is copied
  83. * (deep copy). parpos denotes the x-th occurrence of the input vector
  84. * parameter within the signature of proc, starting with 0.*/
  85. LIntExport Herror set_hdevthread_ivp(Hdevthread hthread_handle, Hlong parpos,
  86. Hvector const vector);
  87. /* set_hdevthread_ovp sets the reference on a output vector parameter (Hvector*)
  88. * of the procedure that should be started as a thread. If a sub vector
  89. * should be set, the reference of the original vector is passed in vector,
  90. * the indices of the sub vector are passed in vec_idx.
  91. * parpos denotes the x-th occurrence of the output vector
  92. * parameter within the signature of proc, starting with 0.*/
  93. LIntExport Herror bind_hdevthread_ovp(Hdevthread hthread_handle, Hlong parpos,
  94. bool glb_flag, Hvector* vector,
  95. Htuple const vec_idx);
  96. /* set_hdevthread_icp sets a input control parameter (Htuple) of the procedure
  97. * that should be started as a thread. The control parameter is copied
  98. * (deep copy). parpos denotes the x-th occurrence of the input
  99. * control parameter within the signature of proc, starting with 0.*/
  100. LIntExport Herror set_hdevthread_icp(Hdevthread hthread_handle, Hlong parpos,
  101. Htuple const tpl);
  102. /* bind_hdevthread_ocp sets a reference on a output control parameter (Htuple*)
  103. * of the procedure that should be started as a thread.
  104. * parpos denotes the x-th
  105. * occurrence of an output control parameter within the signature of proc,
  106. * starting with 0.*/
  107. LIntExport Herror bind_hdevthread_ocp(Hdevthread hthread_handle, Hlong parpos,
  108. bool glb_flag, Htuple* H_RESTRICT tpl);
  109. /* -------------------------------------------------------------------
  110. * Functions to set the input/output parameters of the procedure 'proc'
  111. * within the child thread:
  112. * -------------------------------------------------------------------*/
  113. /* get_hdevthread_iop returns a reference on the copied object set with
  114. * set_hdevthread_iop. */
  115. LIntExport Herror get_hdevthread_iop(Hdevthread const hthread_handle,
  116. Hlong parpos, Hobject* H_RESTRICT* obj);
  117. /* overwrite_hdevthread_oop writes back the output object parameter on the
  118. * reference passed in set_hdevthread_oop. */
  119. LIntExport Herror store_hdevthread_oop(Hdevthread hthread_handle, Hlong parpos,
  120. Hobject const obj);
  121. /* get_hdevthread_ivp returns a reference on the copied vector parameter
  122. * set with set_hdevthread_ivp. */
  123. LIntExport Herror get_hdevthread_ivp(Hdevthread const hthread_handle,
  124. Hlong parpos, Hvector* H_RESTRICT* vec);
  125. /* store_hdevthread_ovp writes back the output (sub) vector parameter
  126. * (generated with create_hdevthread_ovp) on the reference passed in
  127. * set_hdevthread_ovp. */
  128. LIntExport Herror store_hdevthread_ovp(Hdevthread hthread_handle, Hlong parpos,
  129. Hvector const vector);
  130. /* get_hdevthread_icp returns a reference on the copied tuple control parameter
  131. * set with set_hdevthread_icp. */
  132. LIntExport Herror get_hdevthread_icp(Hdevthread const hthread_handle,
  133. Hlong parpos, Htuple* H_RESTRICT* tpl);
  134. /* store_hdevthread_ocp writes back the output control parameter on the
  135. * reference passed in set_hdevthread_ocp. */
  136. LIntExport Herror store_hdevthread_ocp(Hdevthread hthread_handle, Hlong parpos,
  137. Htuple const tpl);
  138. /* start_hdevthread is called by the father thread to start the thread after
  139. * the parameters are set. Contrary to par_start_hdevthread, start_hdevthread
  140. * can be used when no thread has to join with the started thread. No thread id
  141. * is returned, therefore.
  142. * Attention: Different to HDevelop, the child thread must not run anymore
  143. * when exiting the program or unloading the HALCON library. Violating this
  144. * rule might lead to a segmentation fault on exit/unload,
  145. * especially on unix systems. */
  146. LIntExport Herror start_hdevthread(Hdevthread const hthread_handle);
  147. /* par_start_hdevthread is called by the father thread to start the thread
  148. * after the parameters are set. Contrary to start_thread, a thread id
  149. * (par_handle) is returned that can be used by par_join to wait for the
  150. * started thread exiting. Attention: Different to HDevelop, the child thread
  151. * must not run anymore when exiting the program or unloading the HALCON
  152. * library. Violating this rule might lead to a segmentation fault on
  153. * exit/unload, especially on unix systems. */
  154. LIntExport Herror par_start_hdevthread(Hdevthread const hthread_handle,
  155. Htuple* H_RESTRICT par_handle);
  156. /* call_hdevthread_proc calls the procedure specified in create_hdevthread
  157. * synchronously. This must be used for synchronous procedure calls, when
  158. * the procedure was started as thread with the same output parameters within
  159. * the same hthread context. */
  160. LIntExport Herror call_hdevthread_proc(Hdevthread const hthread_handle);
  161. /* T_par_join_hdevthread waits until all threads addressed by their thread id
  162. * in par_handle finished. */
  163. LIntExport Herror T_par_join_hdevthread(Htuple const par_handles);
  164. #if defined(__cplusplus)
  165. }
  166. #endif
  167. #endif