Commit 7561dbc4 authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Make trace code thread safe.

parent 1d6f8f1d
......@@ -31,6 +31,7 @@
#include <unistd.h>
#include <inttypes.h>
#include <sys/ioctl.h>
#include <pthread.h>
static bool timespec_older(struct timespec *t1, struct timespec *t2)
......@@ -120,19 +121,24 @@ static struct trace_pkt **packet_pool;
static int packet_pool_size = 0;
static int packet_pool_cur = 0;
static pthread_mutex_t trace_pkt_mutex = PTHREAD_MUTEX_INITIALIZER;
struct trace_pkt *trace_packet_new(void)
{
struct trace_pkt *pkt;
struct trace_pkt *pkt = NULL;
pthread_mutex_lock(&trace_pkt_mutex);
if (packet_pool_cur) {
pkt = packet_pool[packet_pool_cur-1];
packet_pool_cur--;
return pkt;
}
pthread_mutex_unlock(&trace_pkt_mutex);
pkt = calloc(1, sizeof(struct trace_pkt));
pkt->data = malloc(PACKET_BUFLEN);
pkt->buflen = PACKET_BUFLEN;
if (!pkt) {
pkt = calloc(1, sizeof(struct trace_pkt));
pkt->data = malloc(PACKET_BUFLEN);
pkt->buflen = PACKET_BUFLEN;
}
return pkt;
}
......@@ -156,6 +162,8 @@ void trace_packet_put(struct trace_pkt *pkt)
return;
}
pthread_mutex_lock(&trace_pkt_mutex);
if (packet_pool_cur >= packet_pool_size) {
packet_pool_size += PACKET_POOL_CHUNK;
packet_pool = realloc(packet_pool,
......@@ -164,6 +172,8 @@ void trace_packet_put(struct trace_pkt *pkt)
packet_pool[packet_pool_cur] = pkt;
pkt->len = 0;
packet_pool_cur++;
pthread_mutex_unlock(&trace_pkt_mutex);
}
int trace_packet_list_add(struct trace_pkt *pkt,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment