William A. Rowe, Jr. wrote:
Henry,

thank you for your submission.

http://svn.apache.org/viewvc?view=rev&revision=492362

Please remember, the API is flexible until 1.3.0 (or 2.0.0) is tagged and
released, at which point the new files become subject to the (rather strict)
versioning rules.


Hi Bill,

Happy 2007! This is great, thank you for all the help to get this committed.

Attached is a small patch, it includes two modifications:

1. Fix the assert, which should only for preventing task from owner itself calling cancel.

2. Add a apr_thread_pool_task_owner_get API to retrieve the owner of the task. This is convenient and can, in many cases, eliminate the need to create a structure to use as parameters.

Cheers,
Henry

Index: include/apr_thread_pool.h
===================================================================
--- include/apr_thread_pool.h	(revision 492376)
+++ include/apr_thread_pool.h	(working copy)
@@ -226,6 +226,15 @@
  */
 APR_DECLARE(apr_size_t) apr_thread_pool_threshold_get(apr_thread_pool_t * me);
 
+/**
+ * Get owner of the task currently been executed by the thread. 
+ * @param thd The thread is executing a task 
+ * @param owner Pointer to receive owner of the task.
+ * @return APR_SUCCESS if the owner is retrieved successfully
+ */
+APR_DECLARE(apr_status_t) apr_thread_pool_task_owner_get(apr_thread_t * thd,
+                                                         void **owner);
+
 #ifdef __cplusplus
 #if 0
 {
Index: misc/apr_thread_pool.c
===================================================================
--- misc/apr_thread_pool.c	(revision 492376)
+++ misc/apr_thread_pool.c	(working copy)
@@ -233,6 +233,7 @@
         while (NULL != task && !me->terminated) {
             elt->current_owner = task->owner;
             apr_thread_mutex_unlock(me->lock);
+            apr_thread_data_set(task, "apr_thread_pool_task", NULL, t);
             task->func(t, task->param);
             apr_thread_mutex_lock(me->lock);
             APR_RING_INSERT_TAIL(me->recycled_tasks, task,
@@ -598,6 +599,10 @@
     apr_thread_mutex_lock(me->lock);
     elt = APR_RING_FIRST(me->busy_thds);
     while (elt != APR_RING_SENTINEL(me->busy_thds, apr_thread_list_elt, link)) {
+        if (elt->current_owner != owner) {
+            elt = APR_RING_NEXT(elt, link);
+            continue;
+        }
 #ifndef NDEBUG
         /* make sure the thread is not the one calling tasks_cancel */
         apr_os_thread_t *os_thread;
@@ -609,10 +614,6 @@
         assert(!apr_os_thread_equal(apr_os_thread_current(), *os_thread));
 #endif
 #endif
-        if (elt->current_owner != owner) {
-            elt = APR_RING_NEXT(elt, link);
-            continue;
-        }
         while (elt->current_owner == owner) {
             apr_thread_mutex_unlock(me->lock);
             apr_sleep(200 * 1000);
@@ -812,6 +813,26 @@
     return ov;
 }
 
+APR_DECLARE(apr_status_t) apr_thread_pool_task_owner_get(apr_thread_t * thd,
+                                                         void **owner)
+{
+    apr_status_t rv;
+    apr_thread_pool_task_t * task;
+    
+    rv = apr_thread_data_get((void**) &task, "apr_thread_pool_task", thd);
+    if (rv != APR_SUCCESS) {
+        return rv;
+    }
+
+    if (!task) {
+        *owner = NULL;
+        return APR_BADARG;
+    }
+    
+    *owner = task->owner;
+    return APR_SUCCESS;
+}
+
 #endif /* APR_HAS_THREADS */
 
 /* vim: set ts=4 sw=4 et cin tw=80: */

Reply via email to