[Aggregator] Use semaphores to lock requests' list

The requests' list might be accessed by the main thread and the monitor
thread simultaneously, so we need to secure the concurrent accesses.
This commit is contained in:
Matteo Cypriani 2011-06-09 17:36:00 +02:00
parent 48fa965c54
commit d2559062e6
1 changed files with 29 additions and 2 deletions

View File

@ -12,6 +12,7 @@
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#include <confuse.h>
@ -21,7 +22,10 @@
char *program_name = NULL ;
cfg_t *cfg = NULL ; // Configuration structure
request_list *requests = NULL ; // Computed data list
sem_t lock_requests ; // Semaphore to get access to the requests
ap_list *token_aps = NULL ; // Token ring of the APs
uint_fast16_t nb_aps = 0 ; // Number of APs in the AP ring
@ -49,6 +53,9 @@ int main(int argc, char **argv)
action.sa_handler = owl_sigterm_handler ;
sigaction(SIGTERM, &action, NULL) ;
/* Set up semaphores */
sem_init(&lock_requests, 0, 1) ;
/* Create UDP socket */
listening_port = cfg_getint(cfg, "listening_port") ;
if ((sockfd = owl_create_udp_listening_socket(listening_port)) < 0)
@ -130,6 +137,8 @@ int main(int argc, char **argv)
free_request_list() ;
free_ap_list() ;
cfg_free(cfg) ; // Clean configuration
// Destroy semaphores:
sem_destroy(&lock_requests) ;
fprintf(stderr, "%s: end.\n", program_name) ;
return ret ;
@ -531,10 +540,13 @@ void* monitor_requests(void *NULL_value)
while (owl_run)
{
request_ptr = requests ;
request_prev = NULL ;
request_info_ptr = NULL ;
sem_wait(&lock_requests) ;
owl_timestamp_now(&current_time) ;
request_ptr = requests ;
while (request_ptr != NULL) // Parsing list
{
@ -666,6 +678,8 @@ void* monitor_requests(void *NULL_value)
request_ptr = request_ptr->next ;
}
sem_post(&lock_requests) ;
fflush(NULL) ;
usleep(cfg_getint(cfg, "check_interval")) ; // Wait to check again
}
@ -698,6 +712,8 @@ void got_request(owl_captured_request request)
tmp_info->next = NULL ;
/* Add it in the list */
sem_wait(&lock_requests) ;
tmp_request = requests ;
if (requests == NULL) // If the request list does not exist,
{
@ -798,6 +814,8 @@ void got_request(owl_captured_request request)
}
}
}
sem_post(&lock_requests) ;
}
@ -810,6 +828,8 @@ void free_request_list()
request_list *next_request ;
request_info_list *next_request_info ;
sem_wait(&lock_requests) ;
while (requests != NULL)
{
while (requests->info != NULL)
@ -822,6 +842,8 @@ void free_request_list()
free(requests) ;
requests = next_request ;
}
sem_post(&lock_requests) ;
}
@ -1152,19 +1174,22 @@ void free_ap_list()
*/
void print_request_list()
{
request_list *request_ptr = requests ;
request_list *request_ptr = NULL ;
request_info_list *info_ptr = NULL ;
char mobile_mac_str[OWL_ETHER_ADDR_STRLEN] ;
char
request_time_str[OWL_TIMESTAMP_STR_LEN],
start_time_str[OWL_TIMESTAMP_STR_LEN] ;
sem_wait(&lock_requests) ;
if (requests == NULL) // Empty list
{
fprintf(stderr, "No request.\n") ;
return ;
}
request_ptr = requests ;
while (request_ptr != NULL)
{
info_ptr = request_ptr->info ; // Get the sub-list pointer
@ -1199,6 +1224,8 @@ void print_request_list()
request_ptr = request_ptr->next ;
}
sem_post(&lock_requests) ;
}