File modules/th/thread.c

  $Revision: 1.19 $

Example code: A thread.
Status: NOT REVUED, NOT TESTED
Authors: Chris Ottrey Joao Damas
Online References:

Included Files


Preprocessor definitions

String sizes

#define STR_S 63

#define STR_M 255

#define STR_L 1023

#define STR_XL 4095

#define STR_XXL 16383


Typedef th_args

typedef struct th_args th_args
struct th_args 
   { 
     void* function; 
     int sock; 
   } 

Global Variable Whois_thread_count_lock

Mutex lock. Used for synchronizing changes.

pthread_mutex_t Whois_thread_count_lock
Visible in:  modules/th/thread.c
Used in: TH_do_whois()modules/th/thread.c


Global Variable Config_thread_count_lock

pthread_mutex_t Config_thread_count_lock
Visible in:  modules/th/thread.c

Global Variable Mirror_thread_count_lock

pthread_mutex_t Mirror_thread_count_lock
Visible in:  modules/th/thread.c
Used in: TH_do_mirror()modules/th/thread.c

Global Variable Whois_thread_count

The number of threads.

int Whois_thread_count
Visible in:  modules/th/thread.c
Used in: TH_do_whois()modules/th/thread.c


Global Variable Config_thread_count

int Config_thread_count
Visible in:  modules/th/thread.c

Global Variable Mirror_thread_count

int Mirror_thread_count
Visible in:  modules/th/thread.c
Used in: TH_do_mirror()modules/th/thread.c

Global Function TH_acquire_read_lock()

  Aquire a readers lock.

Reference: "Multithreaded Programming Techniques - Prasad p.192" More:
  Author:
        ottrey
void TH_acquire_read_lock ( rw_lock_t* prw_lock )
rw_lock_t* prw_lock
Readers writers lock.
Prototyped in: modules/th/thread.h
Calls: pthread_cond_wait(), pthread_mutex_lock(), pthread_mutex_unlock()
Called by: AC_check_acl()modules/ac/access_control.c
  AC_fetch_acc()modules/ac/access_control.c
  RP_asc_search()modules/rp/rp_search.c

Global Function TH_acquire_write_lock()

  Aquire a writers lock.

Reference: "Multithreaded Programming Techniques - Prasad p.192" More:
  Author:
        ottrey
void TH_acquire_write_lock ( rw_lock_t* prw_lock )
rw_lock_t* prw_lock
Readers writers lock.
Prototyped in: modules/th/thread.h
Calls: pthread_cond_wait(), pthread_mutex_lock(), pthread_mutex_unlock()
Called by: AC_acc_load()modules/ac/access_control.c
  AC_ban_set()modules/ac/access_control.c
  AC_commit_credit()modules/ac/access_control.c
  AC_decay()modules/ac/access_control.c
  RP_tree_add()modules/rp/rp_tree.c
  RP_uni_node()modules/rp/rp_update.c

Global Function TH_do_config()

  Handle config connections.

More:
  Author:
        joao
void TH_do_config ( void* arg )
void* arg
The socket to connect to. (It has to be passed in this way for this thread routine.)
Prototyped in: modules/th/thread.h
Calls: PC_interact()modules/pc/protocol_config.c
  log_print()modules/th/thread.c
  pthread_exit(), pthread_self(), sprintf(), strcpy()
Used in: SV_start()modules/sv/server.c

Global Function TH_do_mirror()

  Handle NRTM connections.

More:
  Author:
        joao
void TH_do_mirror ( void* arg )
void* arg
The socket to connect to. (It has to be passed in this way for this thread routine.)
Prototyped in: modules/th/thread.h
Calls: PM_interact()modules/pm/protocol_mirror.c
  log_print()modules/th/thread.c
  pthread_exit(), pthread_mutex_lock(), pthread_mutex_unlock(), pthread_self(), sprintf(), strcpy()
Used in: SV_start()modules/sv/server.c
References Variables: Mirror_thread_countmodules/th/thread.c
  Mirror_thread_count_lockmodules/th/thread.c

Global Function TH_do_whois()

  Handle whois connections.

More:
  Author:
        joao
void TH_do_whois ( void* arg )
void* arg
The socket to connect to. (It has to be passed in this way for this thread routine.)
Prototyped in: modules/th/thread.h
Calls: ER_dbg_va()modules/er/er.c
  PW_interact()modules/pw/protocol_whois.c
  pthread_exit(), pthread_mutex_lock(), pthread_mutex_unlock(), pthread_self()
Used in: SV_start()modules/sv/server.c
References Variables: Whois_thread_countmodules/th/thread.c
  Whois_thread_count_lockmodules/th/thread.c

Global Function TH_get_id()

int TH_get_id ( void )
Prototyped in: modules/th/thread.h
Calls: pthread_self()
Called by: log_config()modules/pc/protocol_config.c

Global Function TH_hdl_signal()

  Handle signals.

Changes the flags: do_nrtm do_update do_whoisd
More:
  Author:
        andrei
void TH_hdl_signal ( void )
Prototyped in: modules/th/thread.h
Calls: CO_get_do_update()modules/co/constants.c
  CO_set_const()modules/co/constants.c
  SV_shutdown()modules/sv/server.c
  log_print()modules/th/thread.c
  pthread_exit(), pthread_sigmask(), sigaddset(), sigemptyset(), sigwait(), sprintf(), strcpy()

Global Function TH_init_read_write_lock()

  Initialize a readers/writers lock.

Side effect: the lock is set to open(?)
Reference: "Multithreaded Programming Techniques - Prasad p.192" More:
  Author:
        ottrey
void TH_init_read_write_lock ( rw_lock_t* prw_lock )
rw_lock_t* prw_lock
Readers writers lock.
Prototyped in: modules/th/thread.h
Calls: pthread_cond_init(), pthread_mutex_init()
Called by: RX_tree_cre()modules/rx/rx_tree.c

Global Function TH_release_read_lock()

  Release a readers lock.

Reference: "Multithreaded Programming Techniques - Prasad p.192" More:
  Author:
        ottrey
void TH_release_read_lock ( rw_lock_t* prw_lock )
rw_lock_t* prw_lock
Readers writers lock.
Prototyped in: modules/th/thread.h
Calls: pthread_cond_signal(), pthread_mutex_lock(), pthread_mutex_unlock()
Called by: AC_check_acl()modules/ac/access_control.c
  AC_fetch_acc()modules/ac/access_control.c
  RP_asc_search()modules/rp/rp_search.c

Global Function TH_release_write_lock()

  Release a writers lock.

Reference: "Multithreaded Programming Techniques - Prasad p.192" More:
  Author:
        ottrey
void TH_release_write_lock ( rw_lock_t* prw_lock )
rw_lock_t* prw_lock
Readers writers lock.
Prototyped in: modules/th/thread.h
Calls: pthread_cond_broadcast(), pthread_mutex_lock(), pthread_mutex_unlock()
Called by: AC_acc_load()modules/ac/access_control.c
  AC_ban_set()modules/ac/access_control.c
  AC_commit_credit()modules/ac/access_control.c
  AC_decay()modules/ac/access_control.c
  RP_sql_load_attr_space()modules/rp/rp_load.c
  RP_uni_node()modules/rp/rp_update.c

Global Function TH_run()

  This is the routine that creates the main threads. 

More:
  Author:
        ottrey
	joao
void TH_run ( int sock, void* do_function(void*) )
int sock
The socket to connect to. void * do_function The function to call for each type of service
void* do_function(void*)
&nbs;
Prototyped in: modules/th/thread.h
Calls: fprintf(), pthread_attr_init(), pthread_attr_setdetachstate(), pthread_create(), wr_real_calloc()
Called by: SV_start()modules/sv/server.c
References Functions: main_thread()modules/th/thread.c

Global Function TH_run1()

  This is the routine that creates 1 main thread. 

More:
  Author:
        ottrey
	joao
	andrei
void TH_run1 ( int sock, void* do_function(void*) )
int sock
The socket to listen to. void * do_function The function to call for each type of service
void* do_function(void*)
&nbs;
Prototyped in: modules/th/thread.h
Calls: pthread_attr_init(), pthread_attr_setdetachstate(), pthread_create()
Called by: SV_start()modules/sv/server.c

Global Function TH_run2()

void TH_run2 ( void* function(void*) )
Prototyped in: modules/th/thread.h
Calls: pthread_attr_init(), pthread_attr_setdetachstate(), pthread_create()
Called by: SV_start()modules/sv/server.c

Global Function TH_to_string()

char* TH_to_string ( void )
Prototyped in: modules/th/thread.h
Calls: fprintf(), pthread_self(), sprintf(), strcat(), strcpy(), strlen(), wr_real_malloc()
Called by: show_thread()modules/pc/protocol_config.c

Global Function TH_watchdog()

  This is the routine that creates a watchdog thread. 

The watchdog will cancel (pthread_cancel()) the calling thread in case the socket is closed by the client (its read-half is closed). The calling thread should make necessaruy preparations when calling the watchdog:
- the socket should be connected - cancellation points and cleanup routines should be defined
In case the connection is closed by the calling thread itself, the watchdog just exits and no action against the calling thread is performed.
wd_args - a pointer to wd_args_t structure containing data about socket and thread ID
More:
  Author:
        ottrey
	joao
	andrei
void TH_watchdog ( wd_args_t* wd_args )
Prototyped in: modules/th/thread.h
Calls: pthread_attr_init(), pthread_attr_setdetachstate(), pthread_create()
References Functions: do_watchdog()modules/th/thread.c

Local Function do_watchdog()

The watchdog thread itself

The watchdog thread makes select() on the connected socket waiting until it becomes readable. If this happens as a result of some input, it'll simply dump it. Otherwise, this indicates that the client has closed the connection. In this case watchdog will cancel (pthread_cancel()) the whois thread (which in its turn will kill (mysql_kill()) mysql thread as part of its cleanup routine).
More:
Author:
      andrei
static void do_watchdog ( void* arg )
Prototyped in: modules/th/thread.c
Calls: memset(), pthread_cancel(), pthread_exit(), read(), select()
Used in: TH_watchdog()modules/th/thread.c

Local Function log_print()

static void log_print ( const char* arg )
Calls: CO_get_thread_logfile()modules/co/constants.c
  CO_get_thread_logging()modules/co/constants.c
  fclose(), fopen(), fprintf(), printf(), strcmp()
Called by: TH_do_config()modules/th/thread.c
  TH_do_mirror()modules/th/thread.c
  TH_hdl_signal()modules/th/thread.c

Local Function main_thread()

  Waits for an incoming connection on the and spawns a new thread to handle it.

More:
  Author:
        ottrey
	joao
	andrei (do_server)
static void* main_thread ( void* arg )
void* arg
Pointer to a struct containing the socket to talk to the client and the function to call depending on the incoming connection.
Calls: CO_get_do_server()modules/co/constants.c
  ER_dbg_va()modules/er/er.c
  SK_accept_connection()modules/sk/socket.c
  pthread_attr_init(), pthread_attr_setdetachstate(), pthread_create()
Used in: TH_run()modules/th/thread.c