static void
test_thread_idle_time ()
{
  guint limit = 50;
  guint interval = 10000;
  gint i;

  idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func, 
				 NULL, 
				 MAX_THREADS,
				 FALSE,
				 NULL);

  g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);  
  g_thread_pool_set_max_idle_time (interval); 

  g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);   
  g_assert (g_thread_pool_get_max_idle_time () == interval);

  for (i = 0; i < limit; i++) {
    g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL); 
    DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, "
		"number of threads:%d, unprocessed:%d",
		i,
		g_thread_pool_get_num_threads (idle_pool),
		g_thread_pool_unprocessed (idle_pool)));
  }

  g_timeout_add ((interval - 1000),
		 test_thread_idle_timeout, 
		 GUINT_TO_POINTER (interval));
}
Exemple #2
0
int emc_init_server(struct emc_server_context *ctx)
{
	int result;
	int max_workers;
	char *emc_data_file;

	g_thread_init(NULL);

	max_workers = emc_config_table_get_or_default_int("emc_max_workers", EMC_DEFAULT_MAX_WORKERS);
	emc_data_file = emc_config_table_get("emc_data_file");

	ctx->nuauth_directory = g_tree_new( emc_netmask_order_func );

	result = emc_parse_datafile(ctx, emc_data_file);
	if (result < 0) {
		return -1;
	}

	loop = ev_default_loop(0);

	result = emc_setup_servers(loop, ctx);
	if (result < 0) {
		return -1;
	}

	ev_signal_init(&sigint_watcher, sigint_cb, SIGINT);
	ev_signal_start(loop, &sigint_watcher);

	ev_signal_init(&sigterm_watcher, sigint_cb, SIGTERM);
	ev_signal_start(loop, &sigterm_watcher);

	ev_signal_init(&sigusr1_watcher, sigusr1_cb, SIGUSR1);
	ev_signal_start(loop, &sigusr1_watcher);

	ev_async_init(&client_ready_signal, emc_client_ready_cb);
	ev_async_start(loop, &client_ready_signal);

	ctx->continue_processing = 1;
	sigint_watcher.data = ctx;
	sigterm_watcher.data = ctx;
	sigusr1_watcher.data = ctx;
	client_ready_signal.data = ctx;

	g_thread_pool_set_max_unused_threads( (int)(max_workers/2) );

	ctx->pool_tls_handshake = g_thread_pool_new((GFunc)emc_worker_tls_handshake, NULL,
						    max_workers, FALSE,
						    NULL);
	ctx->pool_reader = g_thread_pool_new((GFunc)emc_worker_reader, NULL,
						    max_workers, FALSE,
						    NULL);

	ctx->work_queue = g_async_queue_new();

	ctx->tls_client_list_mutex = g_mutex_new();

	log_printf(DEBUG_LEVEL_DEBUG, "Max: %d", g_thread_pool_get_max_unused_threads());

	return 0;
}
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);
}
Exemple #4
0
/**
 * g_thread_pool_stop_unused_threads:
 *
 * Stops all currently unused threads. This does not change the
 * maximal number of unused threads. This function can be used to
 * regularly stop all unused threads e.g. from g_timeout_add().
 */
void
g_thread_pool_stop_unused_threads (void)
{
  guint oldval;

  oldval = g_thread_pool_get_max_unused_threads ();

  g_thread_pool_set_max_unused_threads (0);
  g_thread_pool_set_max_unused_threads (oldval);
}