00001 #ifndef _CP_THREAD_H
00002 #define _CP_THREAD_H
00003
00014 #include "common.h"
00015
00016 __BEGIN_DECLS
00017
00018 #include "config.h"
00019
00020
00021
00022 #include "collection.h"
00023 #include "linked_list.h"
00024 #include "hashlist.h"
00025 #include "vector.h"
00026
00028 typedef void *(*cp_thread_action) (void *);
00029
00030
00032 typedef int (*cp_thread_stop_fn)(void *);
00033
00047 typedef CPROPS_DLL struct _cp_pooled_thread
00048 {
00049 long id;
00050 struct _cp_thread_pool *owner;
00051 cp_thread *worker;
00052 cp_thread_action action;
00053 void *action_prm;
00054 cp_thread_stop_fn stop_fn;
00055 void *stop_prm;
00056
00057 cp_mutex *suspend_lock;
00058 cp_cond *suspend_cond;
00059
00060 int done;
00061 int wait;
00062 } cp_pooled_thread;
00063
00065 CPROPS_DLL
00066 long cp_pooled_thread_get_id(cp_pooled_thread *thread);
00067
00069 CPROPS_DLL
00070 cp_pooled_thread *cp_pooled_thread_create(struct _cp_thread_pool *owner);
00071
00073 CPROPS_DLL
00074 void cp_pooled_thread_destroy(cp_pooled_thread *t);
00075
00077 CPROPS_DLL
00078 int cp_pooled_thread_stop(cp_pooled_thread *t);
00079
00081 CPROPS_DLL
00082 long cp_pooled_thread_get_id(cp_pooled_thread *t);
00083
00085 CPROPS_DLL
00086 int cp_pooled_thread_run_task(cp_pooled_thread *pt,
00087 cp_thread_action action,
00088 void *prm);
00089
00091 CPROPS_DLL
00092 int cp_pooled_thread_run_stoppable_task(cp_pooled_thread *pt,
00093 cp_thread_action action,
00094 void *action_prm,
00095 cp_thread_stop_fn stop_fn,
00096 void *stop_prm);
00097
00099 CPROPS_DLL
00100 void *cp_pooled_thread_run(void *prm);
00101
00102
00109 typedef CPROPS_DLL struct _cp_thread_pool
00110 {
00111 int size;
00112
00113 int min_size;
00114 int max_size;
00115
00116 int running;
00117
00118 cp_mutex *pool_lock;
00119 cp_cond *pool_cond;
00120
00121 cp_list *free_pool;
00122 cp_hashlist *in_use;
00123 } cp_thread_pool;
00124
00126 CPROPS_DLL
00127 cp_thread_pool *cp_thread_pool_create(int min_size, int max_size);
00128
00130 CPROPS_DLL
00131 void cp_thread_pool_destroy(cp_thread_pool *pool);
00132
00134 CPROPS_DLL
00135 int cp_thread_pool_wait(cp_thread_pool *pool);
00136
00138 CPROPS_DLL
00139 int cp_thread_pool_stop(cp_thread_pool *pool);
00140
00145 CPROPS_DLL
00146 cp_thread *cp_thread_pool_get(cp_thread_pool *pool,
00147 cp_thread_action action,
00148 void *prm);
00149
00154 CPROPS_DLL
00155 cp_thread *cp_thread_pool_get_stoppable(cp_thread_pool *pool,
00156 cp_thread_action action,
00157 void *action_prm,
00158 cp_thread_stop_fn stop_fn,
00159 void *stop_prm);
00160
00165 CPROPS_DLL
00166 cp_thread *cp_thread_pool_get_nb(cp_thread_pool *pool,
00167 cp_thread_action action,
00168 void *prm);
00173 CPROPS_DLL
00174 cp_thread *cp_thread_pool_get_stoppable_nb(cp_thread_pool *pool,
00175 cp_thread_action action,
00176 void *action_prm,
00177 cp_thread_stop_fn stop_fn,
00178 void *stop_prm);
00179
00181 CPROPS_DLL
00182 int cp_thread_pool_count_available(cp_thread_pool *pool);
00183
00184
00185
00186
00187
00188
00189
00190
00239 typedef CPROPS_DLL struct cp_pooled_thread_scheduler
00240 {
00241 cp_thread_pool *pool;
00242 cp_vector *client_list;
00243 int bypass;
00244 } cp_pooled_thread_scheduler;
00245
00246 CPROPS_DLL struct _cp_pooled_thread_client_interface;
00247
00248 typedef int (*cp_pooled_thread_report_load)
00249 (struct _cp_pooled_thread_client_interface *s);
00250 typedef void (*cp_pooled_thread_shrink)
00251 (struct _cp_pooled_thread_client_interface *);
00252
00263 typedef CPROPS_DLL struct _cp_pooled_thread_client_interface
00264 {
00265 int max;
00266 int min;
00267
00268 int count;
00269
00270 void *client;
00271 cp_pooled_thread_scheduler *owner;
00272 cp_pooled_thread_report_load report_load;
00273 cp_pooled_thread_shrink shrink;
00274 cp_thread_action action;
00275 void *action_prm;
00276 cp_thread_stop_fn stop_fn;
00277 void *stop_prm;
00278 } cp_pooled_thread_client_interface;
00279
00281 CPROPS_DLL
00282 cp_pooled_thread_client_interface *
00283 cp_pooled_thread_client_interface_create
00284 (cp_pooled_thread_scheduler *owner,
00285 void *client,
00286 int min_threads,
00287 int max_threads,
00288 cp_pooled_thread_report_load report_load,
00289 cp_pooled_thread_shrink shrink,
00290 cp_thread_action action,
00291 void *action_prm,
00292 cp_thread_stop_fn stop_fn,
00293 void *stop_prm);
00294
00296 CPROPS_DLL
00297 void cp_pooled_thread_client_interface_destroy
00298 (cp_pooled_thread_client_interface *client);
00299
00324 CPROPS_DLL
00325 void cp_pooled_thread_client_negociate(cp_pooled_thread_client_interface *c);
00326
00328 CPROPS_DLL
00329 cp_pooled_thread_scheduler *cp_pooled_thread_scheduler_create(cp_thread_pool *pool);
00330
00332 CPROPS_DLL
00333 void cp_pooled_thread_scheduler_destroy(cp_pooled_thread_scheduler *scheduler);
00334
00336 CPROPS_DLL
00337 void cp_pooled_thread_scheduler_register_client
00338 (cp_pooled_thread_scheduler *scheduler,
00339 cp_pooled_thread_client_interface *client);
00340
00345 CPROPS_DLL
00346 void cp_pooled_thread_client_get(cp_pooled_thread_client_interface *c);
00347
00352 CPROPS_DLL
00353 void cp_pooled_thread_client_get_stoppable(cp_pooled_thread_client_interface *c);
00354
00359 CPROPS_DLL
00360 void cp_pooled_thread_client_get_nb(cp_pooled_thread_client_interface *c);
00361
00366 CPROPS_DLL
00367 void cp_pooled_thread_client_get_stoppable_nb(cp_pooled_thread_client_interface *c);
00368
00369 __END_DECLS
00370
00373 #endif
00374