void
libMary_threadLocalInit ()
{
  #ifdef LIBMARY_MT_SAFE
    _libMary_main_tlocal = new (std::nothrow) LibMary_ThreadLocal;
    assert (_libMary_main_tlocal);

    #ifdef LIBMARY_PTHREAD
      {
          int const res = pthread_key_create (&_libMary_tlocal_key, tlocal_destructor);
          assert (res == 0);
      }

      {
          int const res = pthread_setspecific (_libMary_tlocal_key, _libMary_main_tlocal);
          assert (res == 0);
      }
    #else
      #ifdef LIBMARY__OLD_GTHREAD_API
        _libMary_tlocal_gprivate_dtor = g_private_new (tlocal_destructor);
        #ifndef LIBMARY_TLOCAL
          _libMary_tlocal_gprivate = g_private_new (NULL /* notify */);
        #endif
      #endif

      g_private_set (LIBMARY__TLOCAL_GPRIVATE, _libMary_main_tlocal);
    #endif
  #else
      _libMary_tlocal = new (std::nothrow) LibMary_ThreadLocal;
      assert (_libMary_tlocal);
  #endif
}
static void _osync_trace_init()
{
	const char *noprivacy;
	const char *error;
	trace = g_getenv("OSYNC_TRACE");
	if (!trace)
		return;
	
	noprivacy = g_getenv("OSYNC_NOPRIVACY");
	if (!trace_sensitive)
		trace_sensitive = g_private_new(NULL);

	if (noprivacy)
		g_private_set(trace_sensitive, GINT_TO_POINTER(1));
	else
		g_private_set(trace_sensitive, GINT_TO_POINTER(0));

	error = g_getenv("OSYNC_PRINTERROR");
	if (!print_stderr)
		print_stderr = g_private_new(NULL);

	if (error)
		g_private_set(print_stderr, GINT_TO_POINTER(1));
	else
		g_private_set(print_stderr, GINT_TO_POINTER(0));
	
	if (!g_file_test(trace, G_FILE_TEST_IS_DIR)) {
		printf("OSYNC_TRACE argument is no directory\n");
		return;
	}
}
void osync_trace_disable(void)
{
	if (!trace_disabled)
		trace_disabled = g_private_new (NULL);
	
	g_private_set(trace_disabled, GINT_TO_POINTER(1));
}
Example #4
0
/* Init the buffer cache system. This is called during vips_init.
 */
void
vips__buffer_init( void )
{
#ifdef HAVE_PRIVATE_INIT
	static GPrivate private = 
		G_PRIVATE_INIT( (GDestroyNotify) buffer_thread_destroy_notify );

	buffer_thread_key = &private;
#else
	if( !buffer_thread_key ) 
		buffer_thread_key = g_private_new( 
			(GDestroyNotify) buffer_thread_destroy_notify );
#endif

	if( buffer_cache_max_reserve < 1 )
		printf( "vips__buffer_init: buffer reserve disabled\n" );

#ifdef DEBUG
	printf( "vips__buffer_init: DEBUG enabled\n" ); 
#endif /*DEBUG*/

#ifdef DEBUG_CREATE
	printf( "vips__buffer_init: DEBUG_CREATE enabled\n" ); 
#endif /*DEBUG_CREATE*/
}
void
rb_threads_init (void)
{
	GMutex *m;

	private_is_primary_thread = g_private_new (NULL);
	g_private_set (private_is_primary_thread, GUINT_TO_POINTER (1));

	g_static_rec_mutex_init (&rb_gdk_mutex);
	gdk_threads_set_lock_functions (_threads_enter, _threads_leave);
	gdk_threads_init ();

	m = g_mutex_new ();

	g_mutex_lock (m);
	mutex_recurses = g_mutex_trylock (m);
	if (mutex_recurses)
		g_mutex_unlock (m);
	g_mutex_unlock (m);
	g_mutex_free (m);

	rb_debug ("GMutex %s recursive", mutex_recurses ? "is" : "isn't");

	/* purge useless thread-pool threads occasionally */
	g_timeout_add_seconds (30, purge_useless_threads, NULL);
}
/**
 * create the event-threads handler
 *
 * provides the event-queue that is contains the event_ops from the event-threads
 * and notifies all the idling event-threads for the new event-ops to process
 */
chassis_event_threads_t *chassis_event_threads_new() {
	chassis_event_threads_t *threads;

	tls_event_base_key = g_private_new(NULL);

	threads = g_new0(chassis_event_threads_t, 1);

	/* create the ping-fds
	 *
	 * the event-thread write a byte to the ping-pipe to trigger a fd-event when
	 * something is available in the event-async-queues
	 */
	if (0 != evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, threads->event_notify_fds)) {
		int err;
#ifdef WIN32
		err = WSAGetLastError();
#else
		err = errno;
#endif
		g_error("%s: evutil_socketpair() failed: %s (%d)", 
				G_STRLOC,
				g_strerror(err),
				err);
	}
	threads->event_threads = g_ptr_array_new();
	threads->event_queue = g_async_queue_new();

	return threads;
}
Example #7
0
void
_g_messages_thread_init_nomessage (void)
{
  g_messages_lock = g_mutex_new ();
  g_log_depth = g_private_new (NULL);
  g_messages_prefixed_init ();
  _g_debug_init ();
}
Example #8
0
/* Init the buffer cache system.
 */
void
im__buffer_init( void )
{
#ifdef HAVE_THREADS
	if( !thread_buffer_cache_key ) 
		thread_buffer_cache_key = g_private_new( 
			(GDestroyNotify) buffer_cache_free );
#endif /*HAVE_THREADS*/
}
static void
mate_vfs_thread_init (void)
{
	private_is_primary_thread = g_private_new (NULL);
	g_private_set (private_is_primary_thread, GUINT_TO_POINTER (1));
	
	_mate_vfs_module_callback_private_init ();
	
	_mate_vfs_async_job_map_init ();
	_mate_vfs_job_queue_init ();
}
Example #10
0
void os_mime_type_init(void)
{
	if (!init_done) {
		private_key = g_private_new(magic_destroy_notify);

		// this is to pre-load the magic file for the current thread
		// to avoid a dead-lock for the first scanned file
		get_private_magic();

		init_done = 1;
	}
}
Example #11
0
/* Init the buffer cache system.
 */
void
vips__buffer_init( void )
{
#ifdef HAVE_PRIVATE_INIT
	static GPrivate private = 
		G_PRIVATE_INIT( (GDestroyNotify) buffer_cache_free );

	thread_buffer_cache_key = &private;
#else
	if( !thread_buffer_cache_key ) 
		thread_buffer_cache_key = g_private_new( 
			(GDestroyNotify) buffer_cache_free );
#endif
}
Example #12
0
static void
vips__thread_profile_init( void )
{
#ifdef HAVE_PRIVATE_INIT
	static GPrivate private = 
		G_PRIVATE_INIT( (GDestroyNotify) vips__thread_profile_init_cb );

	vips_thread_profile_key = &private;
#else
	if( !vips_thread_profile_key ) 
		vips_thread_profile_key = g_private_new( 
			(GDestroyNotify) vips__thread_profile_init_cb );
#endif
}
Example #13
0
/* Init the buffer cache system.
 */
void
vips__buffer_init( void )
{
#ifdef HAVE_PRIVATE_INIT
	static GPrivate private = 
		G_PRIVATE_INIT( (GDestroyNotify) vips__buffer_init_cb );

	buffer_thread_key = &private;
#else
	if( !buffer_thread_key ) 
		buffer_thread_key = g_private_new( 
			(GDestroyNotify) vips__buffer_init_cb );
#endif

	if( buffer_cache_max_reserve < 1 )
		printf( "vips__buffer_init: buffer reserve disabled\n" );
}
Example #14
0
/**
 * This function should be called after specifying various threading
 * parameters using z_thread_*() functions and _before_ creating any threads
 * using z_thread_new().
 **/
void
z_thread_init(void)
{
#if HAVE_SETRLIMIT
  struct rlimit limit;

  /* NOTE: in addition to specifying the stack size where possible, we set
   * the stack limit, which sets per-thread stack limit for threadpools in
   * which case we are unable to set the stack limit any other way */

  memset(&limit, 0, sizeof(limit));
  limit.rlim_cur = max_stack_size;
  limit.rlim_max = max_stack_size;

  setrlimit(RLIMIT_STACK, &limit);
#endif

  g_thread_init(NULL);
  queue = g_async_queue_new();
  current_thread = g_private_new(NULL);
}
TrgClient *trg_client_new(void)
{
    TrgClient *tc = g_object_new(TRG_TYPE_CLIENT, NULL);
    TrgClientPrivate *priv = tc->priv;
    TrgPrefs *prefs = priv->prefs = trg_prefs_new();

    trg_prefs_load(prefs);

    priv->configMutex = g_mutex_new();
    priv->tlsKey = g_private_new(NULL);
    priv->seedRatioLimited = FALSE;
    priv->seedRatioLimit = 0.00;

    priv->pool = g_thread_pool_new((GFunc) dispatch_async_threadfunc, tc,
                                   DISPATCH_POOL_SIZE, TRUE, NULL);

    tr_formatter_size_init(disk_K, _(disk_K_str), _(disk_M_str),
                           _(disk_G_str), _(disk_T_str));
    tr_formatter_speed_init(speed_K, _(speed_K_str), _(speed_M_str),
                            _(speed_G_str), _(speed_T_str));

    return tc;
}
Example #16
0
File: authsrv.c Project: regit/nufw
/**
 * Initialize all data:
 *   - Create different queues:
 *      - tls_push_queue: read in push_worker() ;
 *      - connections_queue: read in search_and_fill() ;
 *      - localid_auth_queue: read in localid_auth().
 *   - Create hash table ::conn_list
 *   - Init. modules: init_modules_system(), load_modules()
 *   - Init. periods: init_periods()
 *   - Init. cache: init_acl_cache() and init_user_cache() (if enabled)
 *   - Create thread pools:
 *      - ip_authentication_workers with external_ip_auth() (if enabled) ;
 *      - acl_checkers with acl_check_and_decide() ;
 *      - user_loggers with real_log_user_packet() ;
 *      - user_session_loggers with log_user_session_thread() ;
 *      - decisions_workers with decisions_queue_work().
 *   - Create threads:
 *      - tls_pusher with push_worker() ;
 *      - search_and_fill_worker with search_and_fill() ;
 *      - localid_auth_thread with localid_auth() (if needed) ;
 *      - tls_auth_servers with tls_user_start_servers() ;
 *      - tls_nufw_servers with tls_nufw_start_servers() ;
 *      - limited_connections_handler with limited_connection_handler().
 *
 * Other queue, threads, etc. are created elsewhere:
 *      - in tls_user_init(): tls_sasl_worker thread pool, tls_sasl_connect().
 */
void init_nuauthdata()
{
	block_thread_pools();
	nuauthdatas->tls_push_queue = g_async_queue_new();
	if (!nuauthdatas->tls_push_queue)
		exit(EXIT_FAILURE);

	/* initialize packets list */
	conn_list = g_hash_table_new_full((GHashFunc) hash_connection,
					  (GEqualFunc) tracking_equal,
					  NULL, (GDestroyNotify)
					  free_connection);

	/* async queue initialisation */
	nuauthdatas->connections_queue = g_async_queue_new();
	if (!nuauthdatas->connections_queue)
		exit(EXIT_FAILURE);

	init_protocol_extension(nuauthdatas);

	/* init and load modules */
	init_modules_system();
	if (!load_modules())
		exit(EXIT_FAILURE);

	/* init periods */
	nuauthconf->periods = init_periods(nuauthconf);

	if (nuauthconf->acl_cache)
		init_acl_cache();

	/* create user cache thread */
	if (nuauthconf->user_cache)
		init_user_cache();

	start_all_thread_pools();

	null_message = g_new0(struct cache_message, 1);
	null_queue_datas = g_new0(gchar, 1);

	/* init private datas for pool thread */
	nuauthdatas->aclqueue =
	    g_private_new((GDestroyNotify) g_async_queue_unref);
	nuauthdatas->userqueue =
	    g_private_new((GDestroyNotify) g_async_queue_unref);

	g_static_mutex_init(&insert_mutex);
	/* create thread for search_and_fill thread */
	log_message(VERBOSE_DEBUG, DEBUG_AREA_MAIN,
		    "Creating search_and_fill thread");
	thread_new(&nuauthdatas->search_and_fill_worker,
		      "search&fill", search_and_fill);

	if (nuauthconf->push && nuauthconf->hello_authentication) {
		log_message(VERBOSE_DEBUG, DEBUG_AREA_MAIN,
			    "Creating hello mode authentication thread");
		nuauthdatas->localid_auth_queue = g_async_queue_new();
		thread_new(&nuauthdatas->localid_auth_thread,
			      "localid", localid_auth);
	}

#ifdef BUILD_NUAUTH_COMMAND
	if (nuauthconf->use_command_server) {
		log_message(VERBOSE_DEBUG, DEBUG_AREA_MAIN,
			    "Creating command thread");
		thread_new(&nuauthdatas->command_thread,
			      "command", command_server);
	}
#endif

	/* create thread for client request sender */
	thread_new(&nuauthdatas->tls_pusher, "tls pusher", push_worker);

	/* create TLS authentication server threads (auth + nufw) */
	tls_common_init();

	log_message(VERBOSE_DEBUG, DEBUG_AREA_MAIN,
		    "Creating tls authentication server threads");
	tls_user_start_servers(nuauthdatas->tls_auth_servers);

	log_message(VERBOSE_DEBUG, DEBUG_AREA_MAIN,
		    "Creating tls nufw server threads");

	tls_nufw_start_servers(nuauthdatas->tls_nufw_servers);

	log_message(INFO, DEBUG_AREA_MAIN, "Threads system started");
	release_thread_pools();
	nuauthdatas->is_starting = FALSE;
}
SchroAsync *
schro_async_new (int n_threads,
                 SchroAsyncScheduleFunc schedule,
                 SchroAsyncCompleteFunc complete, void *closure)
{
    SchroAsync *async;
    int i;

    if (n_threads == 0) {
        char *s;

        s = getenv ("SCHRO_THREADS");
        if (s && s[0]) {
            char *end;
            int n;
            n = strtoul (s, &end, 0);
            if (end[0] == 0) {
                n_threads = n;
            }
        }
        if (n_threads == 0) {
#if defined(_WIN32)
            const char *s = getenv ("NUMBER_OF_PROCESSORS");
            if (s) {
                n_threads = atoi (s);
            }
#elif defined(__APPLE__)
            {
                int mib[] = { CTL_HW, HW_NCPU };
                size_t dataSize = sizeof (int);

                if (sysctl (mib, 2, &n_threads, &dataSize, NULL, 0)) {
                    n_threads = 0;
                }
            }
#else
            n_threads = sysconf (_SC_NPROCESSORS_CONF);
#endif
        }
        if (n_threads == 0) {
            n_threads = 1;
        }
    }
    async = schro_malloc0 (sizeof (SchroAsync));

    SCHRO_DEBUG ("%d", n_threads);
    async->n_threads = n_threads;
    async->threads = schro_malloc0 (sizeof (SchroThread) * (n_threads + 1));

    async->stop = RUNNING;
    async->schedule = schedule;
    async->schedule_closure = closure;
    async->complete = complete;

    async->mutex = g_mutex_new ();
    async->app_cond = g_cond_new ();
    async->thread_cond = g_cond_new ();

    if (!domain_key_inited) {
        domain_key = g_private_new (NULL);
        domain_key_inited = TRUE;
    }

    g_mutex_lock (async->mutex);

    for (i = 0; i < n_threads; i++) {
        SchroThread *thread = async->threads + i;
        GError *error = NULL;

        thread->async = async;
        thread->index = i;
        thread->exec_domain = SCHRO_EXEC_DOMAIN_CPU;
        async->threads[i].thread = g_thread_create (schro_thread_main,
                                   async->threads + i, TRUE, &error);
        g_mutex_lock (async->mutex);
    }
    g_mutex_unlock (async->mutex);

    return async;
}
void osync_trace(OSyncTraceType type, const char *message, ...)
{
#ifdef OPENSYNC_TRACE
	va_list arglist;
	char *buffer = NULL;
	int tabs = 0;
	unsigned long int id = 0;
#ifdef _WIN32
	int pid = 0;
	char tmp_buf[1024];
#else
	pid_t pid = 0;
#endif
	char *logfile = NULL;
	GString *tabstr = NULL;
	int i = 0;
	GTimeVal curtime;
	char *logmessage = NULL;
	GError *error = NULL;
	GIOChannel *chan = NULL;
	gsize writen;
	const char *endline = NULL;
	
	if (!g_thread_supported ()) g_thread_init (NULL);
	
	if (!trace_disabled || !g_private_get(trace_disabled)) {
		_osync_trace_init();
		osync_trace_enable();
	}
	
	if (GPOINTER_TO_INT(g_private_get(trace_disabled)))
		return;
	
	if (!current_tabs)
		current_tabs = g_private_new (NULL);
	else
		tabs = GPOINTER_TO_INT(g_private_get(current_tabs));
	
#ifdef _WIN32
	pid = _getpid();
	endline = "\r\n";
#else
	pid = getpid();
	endline = "\n";
#endif
	id = (unsigned long int)g_thread_self();
	logfile = g_strdup_printf("%s%cThread%lu-%i.log", trace, G_DIR_SEPARATOR, id, pid);
	
	va_start(arglist, message);
	
#ifdef _WIN32
	vsnprintf(tmp_buf, 1024, message, arglist);
	buffer = g_strdup(tmp_buf);
#else
	buffer = g_strdup_vprintf(message, arglist);
#endif
	
	tabstr = g_string_new("");
	for (i = 0; i < tabs; i++) {
		tabstr = g_string_append(tabstr, "\t");
	}

	g_get_current_time(&curtime);
	switch (type) {
	case TRACE_ENTRY:
		logmessage = g_strdup_printf("[%li.%06li]\t%s>>>>>>>  %s%s", curtime.tv_sec, curtime.tv_usec, tabstr->str, buffer, endline);
		tabs++;
		break;
	case TRACE_INTERNAL:
		logmessage = g_strdup_printf("[%li.%06li]\t%s%s%s", curtime.tv_sec, curtime.tv_usec, tabstr->str, buffer, endline);
		break;
	case TRACE_SENSITIVE:
		if (GPOINTER_TO_INT(g_private_get(trace_sensitive)))
			logmessage = g_strdup_printf("[%li.%06li]\t%s[SENSITIVE] %s%s", curtime.tv_sec, curtime.tv_usec, tabstr->str, buffer, endline);
		else
			logmessage = g_strdup_printf("[%li.%06li]\t%s[SENSITIVE CONTENT HIDDEN]%s", curtime.tv_sec, curtime.tv_usec, tabstr->str, endline);
		break;
	case TRACE_EXIT:
		logmessage = g_strdup_printf("[%li.%06li]%s<<<<<<<  %s%s", curtime.tv_sec, curtime.tv_usec, tabstr->str, buffer, endline);
		tabs--;
		if (tabs < 0)
			tabs = 0;
		break;
	case TRACE_EXIT_ERROR:
		logmessage = g_strdup_printf("[%li.%06li]%s<--- ERROR --- %s%s", curtime.tv_sec, curtime.tv_usec, tabstr->str, buffer, endline);
		tabs--;
		if (tabs < 0)
			tabs = 0;

		if (print_stderr)
			fprintf(stderr, "EXIT_ERROR: %s\n", buffer);
		break;
	case TRACE_ERROR:
		logmessage = g_strdup_printf("[%li.%06li]%sERROR: %s%s", curtime.tv_sec, curtime.tv_usec, tabstr->str, buffer, endline);

		if (print_stderr)
			fprintf(stderr, "ERROR: %s\n", buffer);

		break;
	}
	g_free(buffer);
	g_private_set(current_tabs, GINT_TO_POINTER(tabs));
	va_end(arglist);
	
	g_string_free(tabstr, TRUE);
	
	chan = g_io_channel_new_file(logfile, "a", &error);
	if (!chan) {
		printf("unable to open %s for writing: %s\n", logfile, error->message);
		return;
	}
	
	g_io_channel_set_encoding(chan, NULL, NULL);
	if (g_io_channel_write_chars(chan, logmessage, strlen(logmessage), &writen, NULL) != G_IO_STATUS_NORMAL) {
		printf("unable to write trace to %s\n", logfile);
	} else
		g_io_channel_flush(chan, NULL);

	g_io_channel_shutdown(chan, TRUE, NULL);
	g_io_channel_unref(chan);
	g_free(logmessage);
	g_free(logfile);
	
#endif /* OPENSYNC_TRACE */
}
Example #19
0
static void
TLSCreate(TLSItem * pItem)
{
    *pItem = g_private_new(g_free);
}