Introduce walk_func

This commit is contained in:
Nils O. Selåsdal
2012-11-12 21:21:57 +01:00
parent 5a7bc1b078
commit ac83766abc
2 changed files with 51 additions and 14 deletions
+29 -8
View File
@@ -149,8 +149,33 @@ int uc_thread_queue_get(struct uc_threadqueue *queue, const struct timespec *tim
return 0;
}
static void uc_thread_queue_walk_unlocled(struct uc_threadqueue *queue, uc_thread_queue_walk_func walk_func)
{
struct uc_threadmsg *p;
struct uc_threadmsg *next;
for(p = queue->first; p; p = next) {
next = p->next;
walk_func(p);
}
}
int uc_thread_queue_walk(struct uc_threadqueue *queue, uc_thread_queue_walk_func walk_func)
{
if (queue == NULL || !walk_func) {
return EINVAL;
}
pthread_mutex_lock(&queue->mutex);
uc_thread_queue_walk_unlocled(queue, walk_func);
pthread_mutex_unlock(&queue->mutex);
return 0;
}
//maybe caller should supply a callback for cleaning the elements ?
int uc_thread_queue_cleanup(struct uc_threadqueue *queue, int freedata)
int uc_thread_queue_cleanup(struct uc_threadqueue *queue, uc_thread_queue_walk_func free_func)
{
int rc;
@@ -172,13 +197,8 @@ int uc_thread_queue_cleanup(struct uc_threadqueue *queue, int freedata)
return EBUSY;
}
if(freedata) {
struct uc_threadmsg *p;
struct uc_threadmsg *next;
for(p = queue->first; p; p = next) {
next = p->next;
free(p);
}
if(free_func) {
uc_thread_queue_walk_unlocled(queue, free_func);
}
pthread_mutex_unlock(&queue->mutex);
@@ -190,6 +210,7 @@ int uc_thread_queue_cleanup(struct uc_threadqueue *queue, int freedata)
}
long thread_queue_length(struct uc_threadqueue *queue)
{
long length;