$Revision: 1.19 $
Example code: A thread.
Status: NOT REVUED, NOT TESTED
Authors: Chris Ottrey
Joao Damas
- Online References:
-
Included Files
- #include "include/socket.h"
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <netdb.h>
- #include <signal.h>
- #include <stdio.h>
- #include <include/iproutines.h>
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 struct th_args th_args
struct th_args |
|
{ |
|
void* function; |
|
int sock; |
|
} |
|
Mutex lock. Used for synchronizing changes.
pthread_mutex_t Whois_thread_count_lock
pthread_mutex_t Config_thread_count_lock
pthread_mutex_t Mirror_thread_count_lock
The number of threads.
int Whois_thread_count
int Config_thread_count
int Mirror_thread_count
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.
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.
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.)
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.)
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.)
int TH_get_id ( void )
Handle signals.
Changes the flags:
do_nrtm
do_update
do_whoisd
More:
Author:
andrei
void TH_hdl_signal ( void )
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.
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.
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.
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;
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;
void TH_run2 ( void* function(void*) )
char* TH_to_string ( void )
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 )
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 )
static void log_print ( const char* arg )
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.