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));
}
示例#2
0
文件: fm.c 项目: gilir/libfm-debian
gboolean fm_init(FmConfig* config)
{
    char* path;

#ifdef ENABLE_NLS
    bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
#endif

	g_thread_init(NULL);
	g_thread_pool_set_max_idle_time(10000); /* is 10 sec enough? */

    if(config)
        fm_config = (FmConfig*)g_object_ref(config);
    else
    {
        /* create default config object */
        fm_config = fm_config_new();
        fm_config_load_from_file(fm_config, NULL);
    }

	fm_path_init();
    fm_icon_init();
    fm_monitor_init();
    fm_file_info_init();

    /* override gnome-terminal */
    path = g_strconcat(PACKAGE_LIB_DIR ":", g_getenv("PATH"), NULL);
    g_setenv("PATH", path, TRUE);
    g_free(path);

    fm_qdata_id = g_quark_from_static_string("fm_qdata_id");

    return TRUE;
}
示例#3
0
void
ToolsCorePool_Init(ToolsAppCtx *ctx)
{
   gint maxThreads;
   GError *err = NULL;

   ToolsServiceProperty prop = { TOOLS_CORE_PROP_TPOOL };

   gState.funcs.submit = ToolsCorePoolSubmit;
   gState.funcs.cancel = ToolsCorePoolCancel;
   gState.funcs.start = ToolsCorePoolStart;
   gState.ctx = ctx;

   maxThreads = g_key_file_get_integer(ctx->config, ctx->name,
                                       "pool.maxThreads", &err);
   if (err != NULL) {
      maxThreads = DEFAULT_MAX_THREADS;
      g_clear_error(&err);
   }

   if (maxThreads > 0) {
      gState.pool = g_thread_pool_new(ToolsCorePoolRunWorker,
                                      NULL, maxThreads, FALSE, &err);
      if (err == NULL) {
#if GLIB_CHECK_VERSION(2, 10, 0)
         gint maxIdleTime;
         gint maxUnused;

         maxIdleTime = g_key_file_get_integer(ctx->config, ctx->name,
                                              "pool.maxIdleTime", &err);
         if (err != NULL || maxIdleTime <= 0) {
            maxIdleTime = DEFAULT_MAX_IDLE_TIME;
            g_clear_error(&err);
         }

         maxUnused = g_key_file_get_integer(ctx->config, ctx->name,
                                            "pool.maxUnusedThreads", &err);
         if (err != NULL || maxUnused < 0) {
            maxUnused = DEFAULT_MAX_UNUSED_THREADS;
            g_clear_error(&err);
         }

         g_thread_pool_set_max_idle_time(maxIdleTime);
         g_thread_pool_set_max_unused_threads(maxUnused);
#endif
      } else {
         g_warning("error initializing thread pool, running single threaded: %s",
                   err->message);
         g_clear_error(&err);
      }
   }

   gState.active = TRUE;
   gState.lock = g_mutex_new();
   gState.threads = g_ptr_array_new();
   gState.workQueue = g_queue_new();

   ToolsCoreService_RegisterProperty(ctx->serviceObj, &prop);
   g_object_set(ctx->serviceObj, TOOLS_CORE_PROP_TPOOL, &gState.funcs, NULL);
}
示例#4
0
文件: fm.c 项目: YustasSwamp/libfm
/**
 * fm_init
 * @config: (allow-none): configuration file data
 *
 * Initializes libfm data. This API should be always called before any
 * other Libfm function is called. It is idempotent.
 *
 * Returns: %FALSE in case of duplicate call.
 *
 * Since: 0.1.0
 */
gboolean fm_init(FmConfig* config)
{
#if GLIB_CHECK_VERSION(2, 30, 0)
    if (g_atomic_int_add(&init_done, 1) != 0)
#else
    if (g_atomic_int_exchange_and_add(&init_done, 1) != 0)
#endif
        return FALSE; /* duplicate call */

#ifdef ENABLE_NLS
    bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
#endif

#if !GLIB_CHECK_VERSION(2, 36, 0)
    g_type_init();
#endif
#if !GLIB_CHECK_VERSION(2, 32, 0)
    g_thread_init(NULL);
#endif
    g_thread_pool_set_max_idle_time(10000); /* is 10 sec enough? */

    if(config)
        fm_config = (FmConfig*)g_object_ref(config);
    else
    {
        /* create default config object */
        fm_config = fm_config_new();
        fm_config_load_from_file(fm_config, NULL);
    }

#ifdef USE_UDISKS
    /* extension point should be added before any other GIO monitor call
       otherwise it will be ignored by GIO because GIO initializes it once */
    _fm_udisks_init();
#endif

    _fm_file_init();
    _fm_path_init();
    _fm_icon_init();
    _fm_monitor_init();
    _fm_mime_type_init();
    _fm_file_info_init(); /* should be called only after _fm_mime_type_init() */
    _fm_folder_init();
    _fm_archiver_init();
    _fm_thumbnailer_init(); // must be called after mime-types are initialized
    _fm_thumbnail_loader_init();
    _fm_terminal_init(); /* should be called after config initialization */
    _fm_templates_init();
    _fm_folder_config_init();

#ifdef HAVE_ACTIONS
	/* generated by vala */
    _fm_file_actions_init();
#endif

    fm_qdata_id = g_quark_from_static_string("fm_qdata_id");

    return TRUE;
}
示例#5
0
static gpointer
init_scheduler (gpointer arg)
{
  if (job_thread_pool == NULL)
    {
      /* TODO: thread_pool_new can fail */
      job_thread_pool = g_thread_pool_new (io_job_thread,
					   NULL,
					   10,
					   FALSE,
					   NULL);
      if (job_thread_pool != NULL)
	{
	  g_thread_pool_set_sort_function (job_thread_pool,
					   g_io_job_compare,
					   NULL);
	  /* It's kinda weird that this is a global setting
	   * instead of per threadpool. However, we really
	   * want to cache some threads, but not keep around
	   * those threads forever. */
	  g_thread_pool_set_max_idle_time (15 * 1000);
	  g_thread_pool_set_max_unused_threads (2);
	}
    }
  return NULL;
}
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);
}
示例#7
0
文件: pool.c 项目: danielfm/bttracker
GThreadPool *
bt_new_request_processor_pool(bt_config_t *config)
{
  syslog(LOG_DEBUG, "Creating thread pool with %d workers", config->thread_max);

  g_thread_pool_set_max_idle_time(config->thread_max_idle_time * 1000);
  return g_thread_pool_new(bt_request_processor, config,
                           config->thread_max, true, NULL);
}