win32: re-fix the _utf8 compat function situation
[platform/upstream/glib.git] / glib / gthreadpool.c
index 9f5dc82..78684ab 100644 (file)
@@ -1,7 +1,7 @@
 /* GLIB - Library of useful routines for C programming
  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  *
- * GAsyncQueue: thread pool implementation.
+ * GThreadPool: thread pool implementation.
  * Copyright (C) 2000 Sebastian Wilhelmi; University of Karlsruhe
  *
  * This library is free software; you can redistribute it and/or
@@ -29,6 +29,7 @@
 #include "gthreadpool.h"
 
 #include "gasyncqueue.h"
+#include "gasyncqueueprivate.h"
 #include "gmain.h"
 #include "gtestutils.h"
 #include "gtimer.h"
@@ -88,8 +89,8 @@ typedef struct _GRealThreadPool GRealThreadPool;
 struct _GRealThreadPool
 {
   GThreadPool pool;
-  GAsyncQueuequeue;
-  GCond *cond;
+  GAsyncQueue *queue;
+  GCond cond;
   gint max_threads;
   gint num_threads;
   gboolean running;
@@ -109,9 +110,9 @@ static gint wakeup_thread_serial = 0;
 /* Here all unused threads are waiting  */
 static GAsyncQueue *unused_thread_queue = NULL;
 static gint unused_threads = 0;
-static gint max_unused_threads = 0;
+static gint max_unused_threads = 2;
 static gint kill_unused_threads = 0;
-static guint max_idle_time = 0;
+static guint max_idle_time = 15 * 1000;
 
 static void             g_thread_pool_queue_push_unlocked (GRealThreadPool  *pool,
                                                            gpointer          data);
@@ -162,21 +163,16 @@ g_thread_pool_wait_for_new_pool (void)
       else if (local_max_idle_time > 0)
         {
           /* If a maximal idle time is given, wait for the given time. */
-          GTimeVal end_time;
-
-          g_get_current_time (&end_time);
-          g_time_val_add (&end_time, local_max_idle_time * 1000);
-
           DEBUG_MSG (("thread %p waiting in global pool for %f seconds.",
                       g_thread_self (), local_max_idle_time / 1000.0));
 
-          pool = g_async_queue_timed_pop (unused_thread_queue, &end_time);
+          pool = g_async_queue_timeout_pop (unused_thread_queue,
+                                           local_max_idle_time * 1000);
         }
       else
         {
           /* If no maximal idle time is given, wait indefinitely. */
-          DEBUG_MSG (("thread %p waiting in global pool.",
-                      g_thread_self ()));
+          DEBUG_MSG (("thread %p waiting in global pool.", g_thread_self ()));
           pool = g_async_queue_pop (unused_thread_queue);
         }
 
@@ -260,17 +256,13 @@ g_thread_pool_wait_for_new_task (GRealThreadPool *pool)
           /* A thread will wait for new tasks for at most 1/2
            * second before going to the global pool.
            */
-          GTimeVal end_time;
-
-          g_get_current_time (&end_time);
-          g_time_val_add (&end_time, G_USEC_PER_SEC / 2);    /* 1/2 second */
-
           DEBUG_MSG (("thread %p in pool %p waits for up to a 1/2 second for task "
                       "(%d running, %d unprocessed).",
                       g_thread_self (), pool, pool->num_threads,
                       g_async_queue_length_unlocked (pool->queue)));
 
-          task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
+          task = g_async_queue_timeout_pop_unlocked (pool->queue,
+                                                    G_USEC_PER_SEC / 2);
         }
     }
   else
@@ -362,7 +354,7 @@ g_thread_pool_thread_proxy (gpointer data)
                    * immediately, inform the waiting thread of a change
                    * of the thread pool state.
                    */
-                  g_cond_broadcast (pool->cond);
+                  g_cond_broadcast (&pool->cond);
                 }
             }
 
@@ -411,14 +403,15 @@ g_thread_pool_start_thread (GRealThreadPool  *pool,
 
   if (!success)
     {
-      GError *local_error = NULL;
+      GThread *thread;
 
       /* No thread was found, we have to start a new one */
-      if (!g_thread_create (g_thread_pool_thread_proxy, pool, FALSE, &local_error))
-        {
-          g_propagate_error (error, local_error);
-          return FALSE;
-        }
+      thread = g_thread_try_new ("pool", g_thread_pool_thread_proxy, pool, error);
+
+      if (thread == NULL)
+        return FALSE;
+
+      g_thread_unref (thread);
     }
 
   /* See comment in g_thread_pool_thread_proxy as to why this is done
@@ -477,7 +470,6 @@ g_thread_pool_new (GFunc      func,
   g_return_val_if_fail (func, NULL);
   g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
   g_return_val_if_fail (max_threads >= -1, NULL);
-  g_return_val_if_fail (g_thread_supported (), NULL);
 
   retval = g_new (GRealThreadPool, 1);
 
@@ -485,10 +477,12 @@ g_thread_pool_new (GFunc      func,
   retval->pool.user_data = user_data;
   retval->pool.exclusive = exclusive;
   retval->queue = g_async_queue_new ();
-  retval->cond = NULL;
+  g_cond_init (&retval->cond);
   retval->max_threads = max_threads;
   retval->num_threads = 0;
   retval->running = TRUE;
+  retval->immediate = FALSE;
+  retval->waiting = FALSE;
   retval->sort_func = NULL;
   retval->sort_user_data = NULL;
 
@@ -524,12 +518,13 @@ g_thread_pool_new (GFunc      func,
  * @data: a new task for @pool
  * @error: return location for error, or %NULL
  *
- * Inserts @data into the list of tasks to be executed by @pool. When
- * the number of currently running threads is lower than the maximal
- * allowed number of threads, a new thread is started (or reused) with
- * the properties given to g_thread_pool_new (). Otherwise @data stays
- * in the queue until a thread in this pool finishes its previous task
- * and processes @data.
+ * Inserts @data into the list of tasks to be executed by @pool.
+ *
+ * When the number of currently running threads is lower than the
+ * maximal allowed number of threads, a new thread is started (or
+ * reused) with the properties given to g_thread_pool_new().
+ * Otherwise, @data stays in the queue until a thread in this pool
+ * finishes its previous task and processes @data.
  *
  * @error can be %NULL to ignore errors, or non-%NULL to report
  * errors. An error can only occur when a new thread couldn't be
@@ -735,14 +730,14 @@ g_thread_pool_unprocessed (GThreadPool *pool)
  *
  * If @immediate is %TRUE, no new task is processed for @pool.
  * Otherwise @pool is not freed before the last task is processed.
- * Note however, that no thread of this pool is interrupted, while
+ * Note however, that no thread of this pool is interrupted while
  * processing a task. Instead at least all still running threads
  * can finish their tasks before the @pool is freed.
  *
  * If @wait_ is %TRUE, the functions does not return before all
  * tasks to be processed (dependent on @immediate, whether all
- * or only the currently running) are ready. Otherwise the function
- * returns immediately.
+ * or only the currently running) are ready.
+ * Otherwise the function returns immediately.
  *
  * After calling this function @pool must not be used anymore.
  */
@@ -773,11 +768,9 @@ g_thread_pool_free (GThreadPool *pool,
 
   if (wait_)
     {
-      real->cond = g_cond_new ();
-
       while (g_async_queue_length_unlocked (real->queue) != -real->num_threads &&
              !(immediate && real->num_threads == 0))
-        g_cond_wait (real->cond, _g_async_queue_get_mutex (real->queue));
+        g_cond_wait (&real->cond, _g_async_queue_get_mutex (real->queue));
     }
 
   if (immediate || g_async_queue_length_unlocked (real->queue) == -real->num_threads)
@@ -809,9 +802,7 @@ g_thread_pool_free_internal (GRealThreadPool* pool)
   g_return_if_fail (pool->num_threads == 0);
 
   g_async_queue_unref (pool->queue);
-
-  if (pool->cond)
-    g_cond_free (pool->cond);
+  g_cond_clear (&pool->cond);
 
   g_free (pool);
 }
@@ -838,6 +829,8 @@ g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool)
  * Sets the maximal number of unused threads to @max_threads.
  * If @max_threads is -1, no limit is imposed on the number
  * of unused threads.
+ *
+ * The default value is 2.
  */
 void
 g_thread_pool_set_max_unused_threads (gint max_threads)
@@ -973,8 +966,7 @@ g_thread_pool_set_sort_function (GThreadPool      *pool,
  *
  * By setting @interval to 0, idle threads will not be stopped.
  *
- * This function makes use of g_async_queue_timed_pop () using
- * @interval.
+ * The default value is 15000 (15 seconds).
  *
  * Since: 2.10
  */