Skip to content

Commit

Permalink
more pthread fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok committed Oct 18, 2018
1 parent 6c72911 commit 5cdaf98
Showing 1 changed file with 15 additions and 41 deletions.
56 changes: 15 additions & 41 deletions src/modules/rlm_stats/rlm_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,10 @@ RCSID("$Id$")
* statistics.
*/

#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#define PTHREAD_MUTEX_LOCK pthread_mutex_lock
#define PTHREAD_MUTEX_UNLOCK pthread_mutex_unlock

#else
#define PTHREAD_MUTEX_LOCK
#define PTHREAD_MUTEX_UNLOCK
#endif

typedef struct rlm_stats_t {
#ifdef HAVE_PTHREAD_H
pthread_mutex_t mutex;
#endif

fr_dict_attr_t const *type_da; //!< FreeRADIUS-Stats4-Type
fr_dict_attr_t const *ipv4_da; //!< FreeRADIUS-Stats4-IPv4-Address
fr_dict_attr_t const *ipv6_da; //!< FreeRADIUS-Stats4-IPv6-Address
Expand All @@ -74,14 +63,10 @@ typedef struct rlm_stats_thread_t {

fr_time_t last_manage; //!< when we deleted old things

#ifdef HAVE_PTHREAD_H
pthread_mutex_t src_mutex;
#endif
rbtree_t *src; //!< stats by source

#ifdef HAVE_PTHREAD_H
pthread_mutex_t dst_mutex;
#endif
rbtree_t *dst; //!< stats by destination

uint64_t stats[FR_MAX_PACKET_CODE];
Expand Down Expand Up @@ -117,9 +102,7 @@ static void coalesce(uint64_t final_stats[FR_MAX_PACKET_CODE], rlm_stats_thread_
{
rlm_stats_data_t *stats;
rlm_stats_thread_t *other;
#ifdef HAVE_PTHREAD_H
pthread_mutex_t *mutex;
#endif
rbtree_t **tree;
uint64_t local_stats[FR_MAX_PACKET_CODE];

Expand Down Expand Up @@ -148,17 +131,15 @@ static void coalesce(uint64_t final_stats[FR_MAX_PACKET_CODE], rlm_stats_thread_
if (other == t) continue;

tree = (rbtree_t **) (((uint8_t *) other) + tree_offset);
#ifdef HAVE_PTHREAD_H
mutex = (pthread_mutex_t *) (((uint8_t *) other) + mutex_offset);
#endif
PTHREAD_MUTEX_LOCK(mutex);
pthread_mutex_lock(mutex);
stats = rbtree_finddata(*tree, mydata);
if (!stats) {
PTHREAD_MUTEX_UNLOCK(mutex);
pthread_mutex_unlock(mutex);
continue;
}
memcpy(&local_stats, stats->stats, sizeof(stats->stats));
PTHREAD_MUTEX_UNLOCK(mutex);
pthread_mutex_unlock(mutex);

for (i = 0; i < FR_MAX_PACKET_CODE; i++) {
final_stats[i] += local_stats[i];
Expand Down Expand Up @@ -210,9 +191,9 @@ static rlm_rcode_t CC_HINT(nonnull) mod_stats(void *instance, void *thread, REQU
stats->ipaddr = request->packet->src_ipaddr;
stats->created = request->async->recv_time;

PTHREAD_MUTEX_LOCK(&t->src_mutex);
pthread_mutex_lock(&t->src_mutex);
(void) rbtree_insert(t->src, stats);
PTHREAD_MUTEX_UNLOCK(&t->src_mutex);
pthread_mutex_unlock(&t->src_mutex);
}

stats->last_packet = request->async->recv_time;
Expand All @@ -230,9 +211,9 @@ static rlm_rcode_t CC_HINT(nonnull) mod_stats(void *instance, void *thread, REQU
stats->ipaddr = request->packet->dst_ipaddr;
stats->created = request->async->recv_time;

PTHREAD_MUTEX_LOCK(&t->dst_mutex);
pthread_mutex_lock(&t->dst_mutex);
(void) rbtree_insert(t->dst, stats);
PTHREAD_MUTEX_UNLOCK(&t->dst_mutex);
pthread_mutex_unlock(&t->dst_mutex);
}

stats->last_packet = request->async->recv_time;
Expand All @@ -249,12 +230,12 @@ static rlm_rcode_t CC_HINT(nonnull) mod_stats(void *instance, void *thread, REQU

t->last_global_update = request->async->recv_time;

PTHREAD_MUTEX_LOCK(&inst->mutex);
pthread_mutex_lock(&inst->mutex);
for (i = 0; i < FR_MAX_PACKET_CODE; i++) {
inst->stats[i] += t->stats[i];
t->stats[i] = 0;
}
PTHREAD_MUTEX_UNLOCK(&inst->mutex);
pthread_mutex_unlock(&inst->mutex);

return RLM_MODULE_UPDATED;
}
Expand Down Expand Up @@ -290,13 +271,13 @@ static rlm_rcode_t CC_HINT(nonnull) mod_stats(void *instance, void *thread, REQU
*
* The copy helps minimize mutex contention.
*/
PTHREAD_MUTEX_LOCK(&inst->mutex);
pthread_mutex_lock(&inst->mutex);
for (i = 0; i < FR_MAX_PACKET_CODE; i++) {
inst->stats[i] += t->stats[i];
t->stats[i] = 0;
}
memcpy(&local_stats, inst->stats, sizeof(inst->stats));
PTHREAD_MUTEX_UNLOCK(&inst->mutex);
pthread_mutex_unlock(&inst->mutex);
vp = NULL;
break;

Expand Down Expand Up @@ -379,16 +360,14 @@ static int mod_thread_instantiate(UNUSED CONF_SECTION const *cs, void *instance,

t->inst = inst;

#ifdef HAVE_PTHREAD_H
pthread_mutex_init(&t->src_mutex, NULL);
pthread_mutex_init(&t->dst_mutex, NULL);
#endif
t->src = rbtree_talloc_create(t, data_cmp, rlm_stats_data_t, NULL, RBTREE_FLAG_NONE);
t->dst = rbtree_talloc_create(t, data_cmp, rlm_stats_data_t, NULL, RBTREE_FLAG_NONE);

PTHREAD_MUTEX_LOCK(&inst->mutex);
pthread_mutex_lock(&inst->mutex);
fr_dlist_insert_head(&inst->list, t);
PTHREAD_MUTEX_UNLOCK(&inst->mutex);
pthread_mutex_unlock(&inst->mutex);

return 0;
}
Expand All @@ -403,12 +382,12 @@ static int mod_thread_detach(UNUSED fr_event_list_t *el, void *thread)
rlm_stats_t *inst = t->inst;
int i;

PTHREAD_MUTEX_LOCK(&inst->mutex);
pthread_mutex_lock(&inst->mutex);
for (i = 0; i < FR_MAX_PACKET_CODE; i++) {
inst->stats[i] += t->stats[i];
}
fr_dlist_remove(&inst->list, t);
PTHREAD_MUTEX_UNLOCK(&inst->mutex);
pthread_mutex_unlock(&inst->mutex);

return 0;
}
Expand All @@ -417,10 +396,7 @@ static int mod_instantiate(void *instance, UNUSED CONF_SECTION *conf)
{
rlm_stats_t *inst = instance;

#ifdef HAVE_PTHREAD_H
pthread_mutex_init(&inst->mutex, NULL);
#endif

fr_dlist_init(&inst->list, rlm_stats_thread_t, entry);

return 0;
Expand All @@ -434,9 +410,7 @@ static int mod_detach(void *instance)
{
rlm_stats_t *inst = talloc_get_type_abort(instance, rlm_stats_t);

#ifdef HAVE_PTHREAD_H
pthread_mutex_destroy(&inst->mutex);
#endif

/* free things here */
return 0;
Expand Down

0 comments on commit 5cdaf98

Please sign in to comment.