Cp_thread


Files

file  thread.h

Data Structures

struct  _cp_pooled_thread
struct  _cp_thread_pool
struct  cp_pooled_thread_scheduler
struct  _cp_pooled_thread_client_interface

Defines

#define SCHEDULER_THRESHOLD   1

Typedefs

typedef int(*) cp_thread_stop_fn (void *)
typedef CPROPS_DLL struct
_cp_pooled_thread 
cp_pooled_thread
typedef CPROPS_DLL struct
_cp_thread_pool 
cp_thread_pool
typedef int cp_pooled_thread_report_load (struct _cp_pooled_thread_client_interface *s)
typedef void cp_pooled_thread_shrink (struct _cp_pooled_thread_client_interface *)
typedef CPROPS_DLL struct
_cp_pooled_thread_client_interface 
cp_pooled_thread_client_interface

Functions

long cp_pooled_thread_get_id (cp_pooled_thread *pt)
cp_pooled_threadcp_pooled_thread_create (cp_thread_pool *owner)
int cp_pooled_thread_stop (cp_pooled_thread *pt)
void cp_pooled_thread_destroy (cp_pooled_thread *t)
int cp_pooled_thread_release (cp_pooled_thread *t)
int cp_pooled_thread_run_task (cp_pooled_thread *pt, cp_thread_action action, void *prm)
int cp_pooled_thread_run_stoppable_task (cp_pooled_thread *pt, cp_thread_action action, void *action_prm, cp_thread_stop_fn stop_fn, void *stop_prm)
void * cp_pooled_thread_run (void *prm)
int cp_thread_pool_wait (cp_thread_pool *pool)
int cp_thread_pool_stop (cp_thread_pool *pool)
cp_thread_poolcp_thread_pool_create (int min_size, int max_size)
cp_thread * cp_thread_pool_get_impl (cp_thread_pool *pool, cp_thread_action action, void *action_prm, cp_thread_stop_fn stop_fn, void *stop_prm, int block)
cp_thread * cp_thread_pool_get (cp_thread_pool *pool, cp_thread_action action, void *prm)
cp_thread * cp_thread_pool_get_stoppable (cp_thread_pool *pool, cp_thread_action action, void *action_prm, cp_thread_stop_fn stop_fn, void *stop_prm)
cp_thread * cp_thread_pool_get_nb (cp_thread_pool *pool, cp_thread_action action, void *prm)
cp_thread * cp_thread_pool_get_stoppable_nb (cp_thread_pool *pool, cp_thread_action action, void *action_prm, cp_thread_stop_fn stop_fn, void *stop_prm)
void cp_thread_pool_destroy (cp_thread_pool *pool)
int cp_thread_pool_count_available (cp_thread_pool *pool)
cp_pooled_thread_client_interfacecp_pooled_thread_client_interface_create (cp_pooled_thread_scheduler *owner, void *client, int min_threads, int max_threads, cp_pooled_thread_report_load report_load, cp_pooled_thread_shrink shrink, cp_thread_action action, void *action_prm, cp_thread_stop_fn stop_fn, void *stop_prm)
void cp_pooled_thread_client_interface_destroy (cp_pooled_thread_client_interface *client)
cp_pooled_thread_schedulercp_pooled_thread_scheduler_create (cp_thread_pool *pool)
void cp_pooled_thread_scheduler_destroy (cp_pooled_thread_scheduler *scheduler)
void cp_pooled_thread_scheduler_register_client (cp_pooled_thread_scheduler *scheduler, cp_pooled_thread_client_interface *client)
cp_pooled_thread_client_interfacechoose_random_client (cp_pooled_thread_scheduler *scheduler)
void cp_pooled_thread_client_get_nb (cp_pooled_thread_client_interface *c)
void cp_pooled_thread_client_get (cp_pooled_thread_client_interface *c)
void cp_pooled_thread_client_get_stoppable_nb (cp_pooled_thread_client_interface *c)
void cp_pooled_thread_client_get_stoppable (cp_pooled_thread_client_interface *c)
void cp_pooled_thread_client_negociate (cp_pooled_thread_client_interface *c)
CPROPS_DLL cp_pooled_threadcp_pooled_thread_create (struct _cp_thread_pool *owner)

Variables

__BEGIN_DECLS typedef void *(*) cp_thread_action (void *)

Typedef Documentation

typedef CPROPS_DLL struct _cp_pooled_thread cp_pooled_thread

cp_pooled_thread is a thread that lives in a thread_pool. The struct holds synchronization elements used to control the thread and actuation settings, i.e. a thread function and a parameter to be passed for the next starting thread. The pool initializes a bunch of threads and sets them in 'wait'.

When a client requests threads from the pool, the next available thread is signalled out of 'wait', and runs the thread function requested by the client (see cp_thread_pool_get_impl). When the client thread function is done, the cp_pooled_thread returns to the thread pool and becomes available to pool clients. The cp_pooled_thread only exits when the pool exits, unless explicitly stopped (eg pthread_exit) by client code.

typedef CPROPS_DLL struct _cp_pooled_thread_client_interface cp_pooled_thread_client_interface

cp_pooled_thread_client_interface acts as the link to the cp_pooled_thread_scheduler for clients that require a variable number of threads. This interface holds 3 functions pointers that must be supplied by a client:
report_load - should return the number of open requests the client has to handle shrink - will be called by the framework to stop one client thread action - the thread function for this client

typedef CPROPS_DLL struct _cp_thread_pool cp_thread_pool

cp_thread_pool holds a list of free threads (in wait mode). The list grows up to max_size, after which subsequent calls to cp_thread_pool_get will block, and calls to cp_thread_pool_get_nb will return NULL - until clients return their threads to the pool.

typedef int(*) cp_thread_stop_fn(void *)

a stop function for client threads

Definition at line 32 of file thread.h.


Function Documentation

CPROPS_DLL void cp_pooled_thread_client_get ( cp_pooled_thread_client_interface c  ) 

convenience to abstract cp_thread_pool based implementation, see cp_pooled_thread_get and cp_pooled_thread_get_nb

Definition at line 550 of file thread.c.

References cp_thread_pool_get().

CPROPS_DLL void cp_pooled_thread_client_get_nb ( cp_pooled_thread_client_interface c  ) 

convenience to abstract cp_thread_pool based implementation, see cp_pooled_thread_get and cp_pooled_thread_get_nb

Definition at line 544 of file thread.c.

References cp_thread_pool_get_nb().

CPROPS_DLL void cp_pooled_thread_client_get_stoppable ( cp_pooled_thread_client_interface c  ) 

convenience to abstract cp_thread_pool based implementation, see cp_pooled_thread_get and cp_pooled_thread_get_nb

Definition at line 562 of file thread.c.

References cp_thread_pool_get_stoppable().

CPROPS_DLL void cp_pooled_thread_client_get_stoppable_nb ( cp_pooled_thread_client_interface c  ) 

convenience to abstract cp_thread_pool based implementation, see cp_pooled_thread_get and cp_pooled_thread_get_nb

Definition at line 556 of file thread.c.

References cp_thread_pool_get_stoppable_nb().

CPROPS_DLL cp_pooled_thread_client_interface * cp_pooled_thread_client_interface_create ( cp_pooled_thread_scheduler owner,
void *  client,
int  min_threads,
int  max_threads,
cp_pooled_thread_report_load  report_load,
cp_pooled_thread_shrink  shrink,
cp_thread_action  action,
void *  action_prm,
cp_thread_stop_fn  stop_fn,
void *  stop_prm 
)

cp_pooled_thread_client_interface constructor

Definition at line 457 of file thread.c.

References cp_fatal(), CP_MEMORY_ALLOCATION_FAILURE, and cp_pooled_thread_scheduler_register_client().

CPROPS_DLL void cp_pooled_thread_client_interface_destroy ( cp_pooled_thread_client_interface client  ) 

cp_pooled_thread_client_interface destructor

Definition at line 491 of file thread.c.

CPROPS_DLL void cp_pooled_thread_client_negociate ( cp_pooled_thread_client_interface c  ) 

threads should call negociate when a change in the number of threads a client owns is required. Two possible scheduling approaches are -

(1) centralized: Clients report their load factor to the thread manager. The thread manager grants requests for new threads to clients with higher loads first.

(2) distributed: clients negociate with other clients requesting a thread based on their load factors. The client with the lower load factor releases a thread to the client with the higher load factor.

The distributed approach saves some bookkeeping over head in the thread manager, reduces the number steps involved in acquiring a new thread or releasing an unused one, and makes a dedicated synchronization thread unnecessary.

In the current implementation, the scheduler will randomly choose one other client to negociate with. If the load factors are different enough, one thread will be switched to the busier client.

Definition at line 618 of file thread.c.

References choose_random_client(), cp_thread_pool_get_nb(), DEBUGMSG, and SCHEDULER_THRESHOLD.

CPROPS_DLL cp_pooled_thread* cp_pooled_thread_create ( struct _cp_thread_pool owner  ) 

thread constructor function

CPROPS_DLL void cp_pooled_thread_destroy ( cp_pooled_thread t  ) 

thread destructor

Definition at line 113 of file thread.c.

References DEBUGMSG.

Referenced by cp_pooled_thread_run(), and cp_thread_pool_create().

CPROPS_DLL long cp_pooled_thread_get_id ( cp_pooled_thread thread  ) 

return a thread id for this thread

Definition at line 22 of file thread.c.

Referenced by cp_pooled_thread_create().

CPROPS_DLL void * cp_pooled_thread_run ( void *  prm  ) 

framework thread function

Definition at line 169 of file thread.c.

References cp_pooled_thread_destroy(), and DEBUGMSG.

Referenced by cp_pooled_thread_create().

CPROPS_DLL int cp_pooled_thread_run_stoppable_task ( cp_pooled_thread pt,
cp_thread_action  action,
void *  action_prm,
cp_thread_stop_fn  stop_fn,
void *  stop_prm 
)

perform action with stop function

Definition at line 158 of file thread.c.

References cp_pooled_thread_run_task().

Referenced by cp_thread_pool_get_impl().

CPROPS_DLL int cp_pooled_thread_run_task ( cp_pooled_thread pt,
cp_thread_action  action,
void *  prm 
)

sets the action and prm for this thread, then invokes 'action'

Definition at line 131 of file thread.c.

References cp_error(), CP_INVALID_FUNCTION_POINTER, and DEBUGMSG.

Referenced by cp_pooled_thread_run_stoppable_task().

CPROPS_DLL cp_pooled_thread_scheduler * cp_pooled_thread_scheduler_create ( cp_thread_pool pool  ) 

cp_pooled_thread_scheduler constructor

Definition at line 496 of file thread.c.

References cp_pooled_thread_scheduler::client_list, cp_fatal(), CP_MEMORY_ALLOCATION_FAILURE, and cp_pooled_thread_scheduler::pool.

CPROPS_DLL void cp_pooled_thread_scheduler_destroy ( cp_pooled_thread_scheduler scheduler  ) 

cp_pooled_thread_scheduler destructor

Definition at line 515 of file thread.c.

References cp_pooled_thread_scheduler::client_list.

CPROPS_DLL void cp_pooled_thread_scheduler_register_client ( cp_pooled_thread_scheduler scheduler,
cp_pooled_thread_client_interface client 
)

register client as a client of this scheduler

Definition at line 522 of file thread.c.

References cp_pooled_thread_scheduler::client_list.

Referenced by cp_pooled_thread_client_interface_create().

CPROPS_DLL int cp_pooled_thread_stop ( cp_pooled_thread t  ) 

signal a thread to stop

Definition at line 90 of file thread.c.

Referenced by cp_thread_pool_stop().

CPROPS_DLL int cp_thread_pool_count_available ( cp_thread_pool pool  ) 

returns the number of available threads in the pool.

Definition at line 442 of file thread.c.

References cp_list_item_count().

CPROPS_DLL cp_thread_pool * cp_thread_pool_create ( int  min_size,
int  max_size 
)

cp_thread_pool constructor

Definition at line 262 of file thread.c.

References cp_error(), cp_fatal(), cp_hash_compare_long(), cp_hash_long(), cp_hashlist_create, cp_hashlist_destroy_custom(), cp_list_append(), cp_list_create(), cp_list_destroy_custom(), CP_MEMORY_ALLOCATION_FAILURE, cp_pooled_thread_create(), cp_pooled_thread_destroy(), and CP_THREAD_CREATION_FAILURE.

Referenced by cp_httpclient_ctl_create(), and cp_socket_listen().

CPROPS_DLL void cp_thread_pool_destroy ( cp_thread_pool pool  ) 

cp_thread_pool destructor

Definition at line 420 of file thread.c.

References cp_hashlist_destroy(), cp_list_destroy(), cp_thread_pool_stop(), and DEBUGMSG.

Referenced by cp_httpclient_ctl_destroy(), and cp_socket_delete().

CPROPS_DLL cp_thread * cp_thread_pool_get ( cp_thread_pool pool,
cp_thread_action  action,
void *  prm 
)

request a thread from the pool. If no threads are available, this function will block until a thread becomes available.

Definition at line 388 of file thread.c.

References cp_thread_pool_get_impl().

Referenced by cp_pooled_thread_client_get(), and cp_socket_select_threadpool_impl().

CPROPS_DLL cp_thread * cp_thread_pool_get_nb ( cp_thread_pool pool,
cp_thread_action  action,
void *  prm 
)

request a thread from the pool - non-blocking version. Returns a pointer to the requested thread if one is available or NULL if the pool is empty.

Definition at line 404 of file thread.c.

References cp_thread_pool_get_impl().

Referenced by cp_pooled_thread_client_get_nb(), and cp_pooled_thread_client_negociate().

CPROPS_DLL cp_thread * cp_thread_pool_get_stoppable ( cp_thread_pool pool,
cp_thread_action  action,
void *  action_prm,
cp_thread_stop_fn  stop_fn,
void *  stop_prm 
)

request a thread from the pool. If no threads are available, this function will block until a thread becomes available.

Definition at line 395 of file thread.c.

References cp_thread_pool_get_impl().

Referenced by cp_pooled_thread_client_get_stoppable().

CPROPS_DLL cp_thread * cp_thread_pool_get_stoppable_nb ( cp_thread_pool pool,
cp_thread_action  action,
void *  action_prm,
cp_thread_stop_fn  stop_fn,
void *  stop_prm 
)

request a thread from the pool - non-blocking version. Returns a pointer to the requested thread if one is available or NULL if the pool is empty.

Definition at line 411 of file thread.c.

References cp_thread_pool_get_impl().

Referenced by cp_pooled_thread_client_get_stoppable_nb().

CPROPS_DLL int cp_thread_pool_stop ( cp_thread_pool pool  ) 

signal all threads in this pool to stop

Definition at line 241 of file thread.c.

References COLLECTION_LOCK_READ, cp_hashlist_create_iterator(), cp_hashlist_iterator_destroy(), cp_hashlist_iterator_next_value(), cp_list_create_iterator(), cp_list_iterator_destroy(), cp_list_iterator_next(), and cp_pooled_thread_stop().

Referenced by cp_httpclient_ctl_destroy(), cp_socket_delete(), and cp_thread_pool_destroy().

CPROPS_DLL int cp_thread_pool_wait ( cp_thread_pool pool  ) 

wait for threads to finish processing client requests

Definition at line 225 of file thread.c.

References cp_hashlist_item_count().


Variable Documentation

__BEGIN_DECLS typedef void*(*) cp_thread_action(void *)

a thread function

Definition at line 28 of file thread.h.


Generated on Mon Dec 5 23:00:23 2011 for cprops by  doxygen 1.4.7