Skip to content

Commit

Permalink
replaced rwlock mutex, for speed
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsaou committed May 7, 2016
1 parent ab6eb9e commit 97d70cf
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions iprange.c
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,9 @@ static int ipset_load_binary_v10(FILE *fp, ipset *ips, int first_line_missing) {
}

static void ipset_save_binary_v10(ipset *ips) {
// it is crucial not to generate any output
// if the ipset is empty:
// the caller may do 'test -s file' to check it
if(!ips->entries) return;

fprintf(stdout, BINARY_HEADER_V10);
Expand Down Expand Up @@ -1257,17 +1260,23 @@ static unsigned long dns_replies_failed;

static pthread_mutex_t dns_mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t dns_cond = PTHREAD_COND_INITIALIZER;
static pthread_rwlock_t dns_requests_rwlock = PTHREAD_RWLOCK_INITIALIZER;
static pthread_rwlock_t dns_replies_rwlock = PTHREAD_RWLOCK_INITIALIZER;
static pthread_mutex_t dns_requests_mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t dns_replies_mut = PTHREAD_MUTEX_INITIALIZER;

void dns_lock_requests(void) { pthread_rwlock_wrlock(&dns_requests_rwlock); }
void dns_unlock_requests(void) { pthread_rwlock_unlock(&dns_requests_rwlock); }
void dns_lock_replies(void) { pthread_rwlock_wrlock(&dns_replies_rwlock); }
void dns_unlock_replies(void) { pthread_rwlock_unlock(&dns_replies_rwlock); }

static void *dns_thread_resolve(void *ptr);
void dns_lock_requests(void) { pthread_mutex_lock(&dns_requests_mut); }
void dns_unlock_requests(void) { pthread_mutex_unlock(&dns_requests_mut); }
void dns_lock_replies(void) { pthread_mutex_lock(&dns_replies_mut); }
void dns_unlock_replies(void) { pthread_mutex_unlock(&dns_replies_mut); }

// the threads waiting for requests
void dns_thread_wait_for_requests(void) {
pthread_mutex_lock(&dns_mut);
while(!dns_requests)
pthread_cond_wait(&dns_cond, &dns_mut);
pthread_mutex_unlock(&dns_mut);
}

// the master signals the threads for new requests
static void dns_signal_threads(void)
{
/* signal the childs we have a new request for them */
Expand All @@ -1276,6 +1285,9 @@ static void dns_signal_threads(void)
pthread_mutex_unlock(&dns_mut);
}


static void *dns_thread_resolve(void *ptr);

/* ----------------------------------------------------------------------------
* dns_request_add()
*
Expand Down Expand Up @@ -1430,10 +1442,7 @@ static DNSREQ *dns_request_get(void)
if(ret) return ret;
}

pthread_mutex_lock(&dns_mut);
while(!dns_requests)
pthread_cond_wait(&dns_cond, &dns_mut);
pthread_mutex_unlock(&dns_mut);
dns_thread_wait_for_requests();
}

return ret;
Expand Down Expand Up @@ -1908,7 +1917,7 @@ static void ipset_print(ipset *ips, int print) {
return;
}

if(unlikely(debug)) fprintf(stderr, "%s: Printing %s having %lu entries, %lu unique IPs\n", PROG, ips->filename, ips->entries, ips->unique_ips);
if(unlikely(debug)) fprintf(stderr, "%s: Printing %s with %lu ranges, %lu unique IPs\n", PROG, ips->filename, ips->entries, ips->unique_ips);

switch(print) {
case PRINT_CIDR:
Expand Down

4 comments on commit 97d70cf

@enihcam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is nice. Could you release 1.0.3 so distro can adopt it?

@ktsaou
Copy link
Member Author

@ktsaou ktsaou commented on 97d70cf Oct 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I'll do this weekend...
I had the impression I already did...

@ktsaou
Copy link
Member Author

@ktsaou ktsaou commented on 97d70cf Oct 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I released it. Thanks!

@enihcam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I am already using it. :)

Please sign in to comment.