pthread_mutex_t masters_mtx = PTHREAD_MUTEX_INITIALIZER;
static struct list *masters;
+static void thread_free(struct thread_master *master, struct thread *thread);
/* CLI start ---------------------------------------------------------------- */
static unsigned int cpu_record_hash_key(struct cpu_thread_history *a)
/* Move thread to unuse list. */
static void thread_add_unuse(struct thread_master *m, struct thread *thread)
{
+ pthread_mutex_t mtxc = thread->mtx;
+
assert(m != NULL && thread != NULL);
assert(thread->next == NULL);
assert(thread->prev == NULL);
memset(thread, 0, sizeof(struct thread));
thread->type = THREAD_UNUSED;
- if (m->unuse.count < THREAD_UNUSED_DEPTH)
+ /* Restore the thread mutex context. */
+ thread->mtx = mtxc;
+
+ if (m->unuse.count < THREAD_UNUSED_DEPTH) {
thread_list_add(&m->unuse, thread);
- else
- XFREE(MTYPE_THREAD, thread);
+ return;
+ }
+
+ thread_free(m, thread);
}
/* Free all unused thread. */
for (t = list->head; t; t = next) {
next = t->next;
- XFREE(MTYPE_THREAD, t);
+ thread_free(m, t);
list->count--;
- m->alloc--;
}
}
t = thread_array[index];
if (t) {
thread_array[index] = NULL;
- XFREE(MTYPE_THREAD, t);
- m->alloc--;
+ thread_free(m, t);
}
}
XFREE(MTYPE_THREAD_POLL, thread_array);
int i;
for (i = 0; i < queue->size; i++)
- XFREE(MTYPE_THREAD, queue->array[i]);
+ thread_free(m, queue->array[i]);
- m->alloc -= queue->size;
pqueue_delete(queue);
}
{
struct thread *t;
while ((t = thread_trim_head(&m->unuse)) != NULL) {
- pthread_mutex_destroy(&t->mtx);
- XFREE(MTYPE_THREAD, t);
+ thread_free(m, t);
}
}
pthread_mutex_unlock(&m->mtx);
return thread;
}
+static void thread_free(struct thread_master *master, struct thread *thread)
+{
+ /* Update statistics. */
+ assert(master->alloc > 0);
+ master->alloc--;
+
+ /* Free allocated resources. */
+ pthread_mutex_destroy(&thread->mtx);
+ XFREE(MTYPE_THREAD, thread);
+}
+
static int fd_poll(struct thread_master *m, struct pollfd *pfds, nfds_t pfdsize,
nfds_t count, const struct timeval *timer_wait)
{