thread: Add a function to pop and call all registered cleanup handlers

This commit is contained in:
Martin Willi
2015-04-15 14:38:43 +02:00
parent ef33f8e85d
commit d36b30803e
4 changed files with 78 additions and 0 deletions
+17
View File
@@ -399,6 +399,23 @@ void thread_cleanup_pop(bool execute)
free(handler);
}
/**
* Described in header.
*/
void thread_cleanup_popall()
{
private_thread_t *this = (private_thread_t*)thread_current();
cleanup_handler_t *handler;
while (this->cleanup_handlers->get_count(this->cleanup_handlers))
{
this->cleanup_handlers->remove_last(this->cleanup_handlers,
(void**)&handler);
handler->cleanup(handler->arg);
free(handler);
}
}
/**
* Described in header.
*/
+10
View File
@@ -123,6 +123,16 @@ void thread_cleanup_push(thread_cleanup_t cleanup, void *arg);
*/
void thread_cleanup_pop(bool execute);
/**
* Pop and execute all cleanup handlers in reverse order of registration.
*
* This function is for very special purposes only, where the caller exactly
* knows which cleanup handlers have been pushed. For regular use, a caller
* should thread_cleanup_pop() exactly the number of handlers it pushed
* using thread_cleanup_push().
*/
void thread_cleanup_popall();
/**
* Enable or disable the cancelability of the current thread. The current
* value is returned.
@@ -559,6 +559,26 @@ void thread_cleanup_pop(bool execute)
}
}
/**
* Described in header.
*/
void thread_cleanup_popall()
{
private_thread_t *this;
cleanup_t cleanup = {};
bool old;
this = get_current_thread();
while (array_count(this->cleanup))
{
old = set_leak_detective(FALSE);
array_remove(this->cleanup, -1, &cleanup);
set_leak_detective(old);
cleanup.cb(cleanup.arg);
}
}
/**
* Described in header.
*/