static void
test_thread_stop_unused (void)
{
   GThreadPool *pool;
   guint i;
   guint limit = 100;

   /* Spawn a few threads. */
   g_thread_pool_set_max_unused_threads (-1);
   pool = g_thread_pool_new ((GFunc) g_usleep, NULL, -1, FALSE, NULL);

   for (i = 0; i < limit; i++)
     g_thread_pool_push (pool, GUINT_TO_POINTER (1000), NULL);

   DEBUG_MSG (("[unused] ===> pushed %d threads onto the idle pool",
	       limit));

   /* Wait for the threads to migrate. */
   g_usleep (G_USEC_PER_SEC);

   DEBUG_MSG (("[unused] current threads %d",
	       test_count_threads()));

   DEBUG_MSG (("[unused] stopping unused threads"));
   g_thread_pool_stop_unused_threads ();

   for (i = 0; i < 5; i++)
     {
       if (g_thread_pool_get_num_unused_threads () == 0 &&
           test_count_threads () == 0)
         break;

       DEBUG_MSG (("[unused] waiting ONE second for threads to die"));

       /* Some time for threads to die. */
       g_usleep (G_USEC_PER_SEC);
     }

   DEBUG_MSG (("[unused] stopped idle threads, %d remain, %d threads still exist",
	       g_thread_pool_get_num_unused_threads (),
	       test_count_threads ()));

   g_assert (g_thread_pool_get_num_unused_threads () == test_count_threads ());
   g_assert (g_thread_pool_get_num_unused_threads () == 0);

   g_thread_pool_set_max_unused_threads (MAX_THREADS);

   DEBUG_MSG (("[unused] cleaning up thread pool"));
   g_thread_pool_free (pool, FALSE, TRUE);
}
Example #2
0
static void sigusr1_cb(struct ev_loop *loop, ev_signal *w, int revents)
{
	struct emc_server_context *ctx = w->data;

log_printf(DEBUG_LEVEL_INFO, "INFO signal SIGUSR1 caught");
log_printf(DEBUG_LEVEL_INFO, "  TLS handshake threads : current %d / idle %d / max %d",
		g_thread_pool_get_num_threads(ctx->pool_tls_handshake),
		g_thread_pool_get_num_unused_threads(),
		g_thread_pool_get_max_threads(ctx->pool_tls_handshake) );
log_printf(DEBUG_LEVEL_INFO, "  TLS worker threads : current %d / idle %d / max %d",
		g_thread_pool_get_num_threads(ctx->pool_reader),
		g_thread_pool_get_num_unused_threads(),
		g_thread_pool_get_max_threads(ctx->pool_reader) );
}
static void
test_thread_functions (void)
{
  gint max_unused_threads;
  guint max_idle_time;

  /* This function attempts to call functions which don't need a
   * threadpool to operate to make sure no uninitialised pointers
   * accessed and no errors occur.
   */

  max_unused_threads = 3;

  DEBUG_MSG (("[funcs] Setting max unused threads to %d", 
	      max_unused_threads));
  g_thread_pool_set_max_unused_threads (max_unused_threads);

  DEBUG_MSG (("[funcs] Getting max unused threads = %d", 
	     g_thread_pool_get_max_unused_threads ()));
  g_assert (g_thread_pool_get_max_unused_threads() == max_unused_threads);

  DEBUG_MSG (("[funcs] Getting num unused threads = %d", 
	     g_thread_pool_get_num_unused_threads ()));
  g_assert (g_thread_pool_get_num_unused_threads () == 0);

  DEBUG_MSG (("[funcs] Stopping unused threads"));
  g_thread_pool_stop_unused_threads ();

  max_idle_time = 10 * G_USEC_PER_SEC;

  DEBUG_MSG (("[funcs] Setting max idle time to %d", 
	      max_idle_time));
  g_thread_pool_set_max_idle_time (max_idle_time);

  DEBUG_MSG (("[funcs] Getting max idle time = %d", 
	     g_thread_pool_get_max_idle_time ()));
  g_assert (g_thread_pool_get_max_idle_time () == max_idle_time);

  DEBUG_MSG (("[funcs] Setting max idle time to 0"));
  g_thread_pool_set_max_idle_time (0);

  DEBUG_MSG (("[funcs] Getting max idle time = %d", 
	     g_thread_pool_get_max_idle_time ()));
  g_assert (g_thread_pool_get_max_idle_time () == 0);
}
static gboolean
test_check_start_and_stop (gpointer user_data)
{
  static guint test_number = 0;
  static gboolean run_next = FALSE;
  gboolean continue_timeout = TRUE;
  gboolean quit = TRUE;

  if (test_number == 0) {
    run_next = TRUE;
    DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number)); 
  }
   
  if (run_next) {
    test_number++;

    switch (test_number) {
    case 1:
      test_thread_functions ();
      break;
    case 2:
      test_thread_stop_unused ();
      break;
    case 3:
      test_thread_pools ();   
      break;
    case 4:
      test_thread_sort (FALSE);  
      break;
    case 5:
      test_thread_sort (TRUE);  
      break;
    case 6:
      test_thread_idle_time ();   
      break;
    default:
      DEBUG_MSG (("***** END OF TESTS *****")); 
      g_main_loop_quit (main_loop);
      continue_timeout = FALSE;
      break;
    }

    run_next = FALSE;
    return TRUE;
  }

  if (test_number == 3) {
    G_LOCK (thread_counter_pools); 
    quit &= running_thread_counter <= 0;
    DEBUG_MSG (("***** POOL RUNNING THREAD COUNT:%ld", 
		running_thread_counter)); 
    G_UNLOCK (thread_counter_pools); 
  }

  if (test_number == 4 || test_number == 5) {
    G_LOCK (thread_counter_sort);
    quit &= sort_thread_counter <= 0;
    DEBUG_MSG (("***** POOL SORT THREAD COUNT:%ld", 
		sort_thread_counter)); 
    G_UNLOCK (thread_counter_sort); 
  }

  if (test_number == 6) {
    guint idle;

    idle = g_thread_pool_get_num_unused_threads ();
    quit &= idle < 1;
    DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d",
		idle, g_thread_pool_unprocessed (idle_pool)));
  }    

  if (quit) {
    run_next = TRUE;
  }

  return continue_timeout;
}