]> git.ipfire.org Git - people/ms/strongswan.git/commitdiff
thread: Add a function to pop and call all registered cleanup handlers
authorMartin Willi <martin@revosec.ch>
Tue, 14 Apr 2015 06:59:01 +0000 (08:59 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 15 Apr 2015 12:38:43 +0000 (14:38 +0200)
src/libstrongswan/tests/suites/test_threading.c
src/libstrongswan/threading/thread.c
src/libstrongswan/threading/thread.h
src/libstrongswan/threading/windows/thread.c

index 55a4cd797a11327280d4ac6fb6e5e3c5823fd1e7..9a9fdd8e9ec52836d9ae75d9681038bc317e300b 100644 (file)
@@ -1517,6 +1517,36 @@ START_TEST(test_cleanup_pop)
 }
 END_TEST
 
+static void *cleanup_popall_run(void *data)
+{
+       thread_cleanup_push(cleanup3, data);
+       thread_cleanup_push(cleanup2, data);
+       thread_cleanup_push(cleanup1, data);
+
+       thread_cleanup_popall();
+       return NULL;
+}
+
+START_TEST(test_cleanup_popall)
+{
+       thread_t *threads[THREADS];
+       uintptr_t values[THREADS];
+       int i;
+
+       for (i = 0; i < THREADS; i++)
+       {
+               values[i] = 1;
+               threads[i] = thread_create(cleanup_popall_run, &values[i]);
+       }
+       for (i = 0; i < THREADS; i++)
+       {
+               threads[i]->join(threads[i]);
+               ck_assert_int_eq(values[i], 4);
+       }
+}
+END_TEST
+
+
 static thread_value_t *tls[10];
 
 static void *tls_run(void *data)
@@ -1697,6 +1727,7 @@ Suite *threading_suite_create()
        tcase_add_test(tc, test_cleanup_exit);
        tcase_add_test(tc, test_cleanup_cancel);
        tcase_add_test(tc, test_cleanup_pop);
+       tcase_add_test(tc, test_cleanup_popall);
        suite_add_tcase(s, tc);
 
        tc = tcase_create("thread local storage");
index 91a2d345d4159c85b1bb03f3d257aa1564ba80e6..7a243e8262b69dca0ad58369310f019c682b3bc9 100644 (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.
  */
index 38275541e3b2ed6d0b532939bebfa902963eeeb3..c24772839505604ef54d3226e91ce14f6210170c 100644 (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.
index e76758f8c3c925144f7fdf53f9634d172d567a42..610524722d0e69a734bdde319d1ebd5d6f8d362a 100644 (file)
@@ -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.
  */