static gpointer test_g_static_private_thread (gpointer data) { guint number = GPOINTER_TO_INT (data); guint i; guint *private1, *private2; for (i = 0; i < 10; i++) { number = number * 11 + 1; /* A very simple and bad RNG ;-) */ private1 = g_static_private_get (&test_g_static_private_private1); if (!private1 || number % 7 > 3) { private1 = test_g_static_private_constructor (); g_static_private_set (&test_g_static_private_private1, private1, test_g_static_private_destructor); } *private1 = number; private2 = g_static_private_get (&test_g_static_private_private2); if (!private2 || number % 13 > 5) { private2 = test_g_static_private_constructor (); g_static_private_set (&test_g_static_private_private2, private2, test_g_static_private_destructor); } *private2 = number * 2; g_usleep (G_USEC_PER_SEC / 5); g_assert (number == *private1); g_assert (number * 2 == *private2); } g_mutex_lock (&test_g_static_private_mutex); test_g_static_private_ready++; g_mutex_unlock (&test_g_static_private_mutex); /* Busy wait is not nice but that's just a test */ while (test_g_static_private_ready != 0) g_usleep (G_USEC_PER_SEC / 5); for (i = 0; i < 10; i++) { private2 = g_static_private_get (&test_g_static_private_private2); number = number * 11 + 1; /* A very simple and bad RNG ;-) */ if (!private2 || number % 13 > 5) { private2 = test_g_static_private_constructor (); g_static_private_set (&test_g_static_private_private2, private2, test_g_static_private_destructor); } *private2 = number * 2; g_usleep (G_USEC_PER_SEC / 5); g_assert (number * 2 == *private2); } return GINT_TO_POINTER (GPOINTER_TO_INT (data) * 3); }
const char * guid_to_string(const GncGUID * guid) { #ifdef G_THREADS_ENABLED #ifndef HAVE_GLIB_2_32 static GStaticPrivate guid_buffer_key = G_STATIC_PRIVATE_INIT; gchar *string; string = static_cast<gchar*>(g_static_private_get (&guid_buffer_key)); if (string == NULL) { string = static_cast<gchar*>(malloc(GUID_ENCODING_LENGTH + 1)); g_static_private_set (&guid_buffer_key, string, g_free); } #else static GPrivate guid_buffer_key = G_PRIVATE_INIT(g_free); gchar *string; string = static_cast<char*>(g_private_get (&guid_buffer_key)); if (string == NULL) { string = static_cast<char*>(malloc(GUID_ENCODING_LENGTH + 1)); g_private_set (&guid_buffer_key, string); } #endif #else static char string[64]; #endif encode_md5_data(guid->data, string); string[GUID_ENCODING_LENGTH] = '\0'; return string; }
/** * z_policy_thread_release: * @self: this * * Releases reference information acquired by z_policy_thread_acquire. */ void z_policy_thread_release(ZPolicyThread *self) { self->used = FALSE; PyEval_ReleaseThread(self->thread); g_static_private_set(&policy_thread, NULL, NULL); }
static ca_context* ca_context_get_default() { // This allows us to avoid race conditions with freeing the context by handing that // responsibility to Glib, and still use one context at a time static GStaticPrivate ctx_static_private = G_STATIC_PRIVATE_INIT; ca_context* ctx = (ca_context*) g_static_private_get(&ctx_static_private); if (ctx) { return ctx; } ca_context_create(&ctx); if (!ctx) { return nullptr; } g_static_private_set(&ctx_static_private, ctx, (GDestroyNotify) ca_context_destroy); GtkSettings* settings = gtk_settings_get_default(); if (g_object_class_find_property(G_OBJECT_GET_CLASS(settings), "gtk-sound-theme-name")) { gchar* sound_theme_name = nullptr; g_object_get(settings, "gtk-sound-theme-name", &sound_theme_name, NULL); if (sound_theme_name) { ca_context_change_props(ctx, "canberra.xdg-theme.name", sound_theme_name, NULL); g_free(sound_theme_name); } } nsCOMPtr<nsIStringBundleService> bundleService = mozilla::services::GetStringBundleService(); if (bundleService) { nsCOMPtr<nsIStringBundle> brandingBundle; bundleService->CreateBundle("chrome://branding/locale/brand.properties", getter_AddRefs(brandingBundle)); if (brandingBundle) { nsAutoString wbrand; brandingBundle->GetStringFromName(NS_LITERAL_STRING("brandShortName").get(), getter_Copies(wbrand)); NS_ConvertUTF16toUTF8 brand(wbrand); ca_context_change_props(ctx, "application.name", brand.get(), NULL); } } nsCOMPtr<nsIXULAppInfo> appInfo = do_GetService("@mozilla.org/xre/app-info;1"); if (appInfo) { nsAutoCString version; appInfo->GetVersion(version); ca_context_change_props(ctx, "application.version", version.get(), NULL); } ca_context_change_props(ctx, "application.icon_name", MOZ_APP_NAME, NULL); return ctx; }
static gpointer mbb_task_work(struct mbb_task *task) { struct mbb_task_hook *hook = task->hook; g_static_private_set(&task_key, task, (GDestroyNotify) mbb_task_free); mbb_log("started"); if (hook->init != NULL) { if (! hook->init(task->data)) { mbb_log("init failed"); return NULL; } } while (hook->work(task->data)) { if (task_poll(task) == FALSE) break; } if (hook->fini != NULL) hook->fini(task->data); if (! task->cancel) mbb_log("complete"); return NULL; }
Worker* worker_getPrivate() { /* reference the global shadow engine */ Engine* engine = shadow_engine; /* get current thread's private worker object */ Worker* worker = g_static_private_get(engine_getWorkerKey(engine)); /* todo: should we use g_once here instead? */ if(!worker) { worker = _worker_new(engine); g_static_private_set(engine_getWorkerKey(engine), worker, worker_free); gboolean* preloadIsReady = g_new(gboolean, 1); *preloadIsReady = TRUE; g_static_private_set(engine_getPreloadKey(engine), preloadIsReady, g_free); } MAGIC_ASSERT(worker); return worker; }
static gpointer thread_func (gpointer nil) { /* wait for main thread to reach its g_cond_wait call */ g_mutex_lock (mutex); g_static_private_set (&sp, &sp, notify); g_cond_broadcast (cond); g_mutex_unlock (mutex); return nil; }
/** * g_cancellable_push_current: * @cancellable: a #GCancellable object * * Pushes @cancellable onto the cancellable stack. The current * cancllable can then be recieved using g_cancellable_get_current(). * * This is useful when implementing cancellable operations in * code that does not allow you to pass down the cancellable object. * * This is typically called automatically by e.g. #GFile operations, * so you rarely have to call this yourself. **/ void g_cancellable_push_current (GCancellable *cancellable) { GSList *l; g_return_if_fail (cancellable != NULL); l = g_static_private_get (¤t_cancellable); l = g_slist_prepend (l, cancellable); g_static_private_set (¤t_cancellable, l, NULL); }
/** * g_cancellable_pop_current: * @cancellable: a #GCancellable object * * Pops @cancellable off the cancellable stack (verifying that @cancellable * is on the top of the stack). **/ void g_cancellable_pop_current (GCancellable *cancellable) { GSList *l; l = g_static_private_get (¤t_cancellable); g_return_if_fail (l != NULL); g_return_if_fail (l->data == cancellable); l = g_slist_delete_link (l, l); g_static_private_set (¤t_cancellable, l, NULL); }
static MsgContext * msg_get_context(void) { MsgContext *context; context = g_static_private_get(&msg_context_private); if (!context) { context = g_new0(MsgContext, 1); g_static_private_set(&msg_context_private, context, g_free); } return context; }
void scallionpreload_init(GModule* handle) { ScallionPreloadWorker* worker = _scallionpreload_newWorker(handle); /* lookup all our required symbols in this worker's module, asserting success */ g_assert(g_module_symbol(handle, TOR_LIB_PREFIX "tor_open_socket", (gpointer*)&(worker->a))); g_assert(g_module_symbol(handle, TOR_LIB_PREFIX "tor_gettimeofday", (gpointer*)&(worker->b))); g_assert(g_module_symbol(handle, TOR_LIB_PREFIX "spawn_func", (gpointer*)&(worker->d))); g_assert(g_module_symbol(handle, TOR_LIB_PREFIX "rep_hist_bandwidth_assess", (gpointer*)&(worker->e))); g_assert(g_module_symbol(handle, TOR_LIB_PREFIX "router_get_advertised_bandwidth_capped", (gpointer*)&(worker->f))); g_assert(g_module_symbol(handle, TOR_LIB_PREFIX "event_base_loopexit", (gpointer*)&(worker->g))); g_assert(g_module_symbol(handle, TOR_LIB_PREFIX "crypto_global_cleanup", (gpointer*)&(worker->h))); g_static_private_set(&scallionWorkerKey, worker, g_free); }
/* * authldap_connect() * * initializes the connection for authentication. * * returns 0 on success, -1 on failure */ static int authldap_connect(void) { int version = 0; LDAP *_ldap_conn = NULL; int ret; if (! g_thread_supported()) g_thread_init(NULL); g_once(&ldap_conn_once, authldap_once, NULL); switch (_ldap_cfg.version_int) { case 3: version = LDAP_VERSION3; if (strlen(_ldap_cfg.uri)) { TRACE(TRACE_DEBUG, "connecting to ldap server on [%s] version [%d]", _ldap_cfg.uri, _ldap_cfg.version_int); if ((ret = ldap_initialize(&_ldap_conn, _ldap_cfg.uri) != LDAP_SUCCESS)) TRACE(TRACE_WARNING, "ldap_initialize() failed %d", ret); } else { char *uri = g_strdup_printf("ldap://%s:%d", _ldap_cfg.hostname, _ldap_cfg.port_int); TRACE(TRACE_DEBUG, "connecting to ldap server on [%s] version [%d]", uri, _ldap_cfg.version_int); if ((ret = ldap_initialize(&_ldap_conn, uri)) != LDAP_SUCCESS) TRACE(TRACE_EMERG, "ldap_initialize() failed [%d]", ret); g_free(uri); } break; case 2: version = LDAP_VERSION2; /* fall through... */ default: if (!version) { TRACE(TRACE_WARNING, "Unsupported LDAP version [%d] requested." " Default to LDAP version 3.", _ldap_cfg.version_int); version = LDAP_VERSION3; } TRACE(TRACE_DEBUG, "connecting to ldap server on [%s] : [%d] version [%d]", _ldap_cfg.hostname, _ldap_cfg.port_int, _ldap_cfg.version_int); _ldap_conn = ldap_init(_ldap_cfg.hostname, _ldap_cfg.port_int); break; } ldap_set_option(_ldap_conn, LDAP_OPT_PROTOCOL_VERSION, &version); /* Turn off referrals */ if (strncasecmp(_ldap_cfg.referrals, "no", 2) == 0) ldap_set_option(_ldap_conn, LDAP_OPT_REFERRALS, 0); g_static_private_set(&ldap_conn_key, _ldap_conn, (GDestroyNotify)authldap_free); return auth_ldap_bind(); }
static gpointer test_thread_func (gpointer user_data) { TestThreadData *data; data = user_data; g_static_private_set (&test_thread_data, data, NULL); do_something_very_slow (); clutter_threads_add_idle_full (G_PRIORITY_DEFAULT + 30, test_thread_done_idle, data, NULL); return NULL; }
static vlog_data_t * get_vlog_data(void) { vlog_data_t *rv; rv = g_static_private_get(&vlog_private); if (!rv) { rv = g_malloc(sizeof(*rv)); if (rv) { memset(rv, 0, sizeof(*rv)); rv->data = g_malloc(1024); if (rv->data) rv->len = 1024; else rv->len = 0; g_static_private_set(&vlog_private, rv, vlog_data_destroy); } } return rv; }
/** * z_policy_thread_acquire: * @self: this * * Acquires and stores reference information about the current thread. * self->thread is the PyThreadState (context on the Python-side), * policy_thread is the thread-private ZPolicyThread* (context on the C-side). */ void z_policy_thread_acquire(ZPolicyThread *self) { z_policy_thread_wait(self); g_static_private_set(&policy_thread, self, NULL); PyEval_AcquireThread(self->thread); /* NOTE: this is currently a warning, but it'd probably make sense to * actually exclude parallel execution in the same thread by using a mutex * in ZPolicyThread. However as this is a risky change at 3.1.x, x >= 14 I * only added a warning here. */ if (self->used) { #if 0 z_log(NULL, CORE_ERROR, 0, "Internal error, ZPolicyThread reused, dumping core & continuing;"); z_coredump_create(); #endif } self->used = TRUE; }
void bluesky_profile_set(BlueSkyProfile *profile) { g_static_private_set(&per_thread_profile, profile, NULL); }
static int _setup_recv_parts (lcm_udpm_t *lcm) { g_static_rec_mutex_lock(&lcm->mutex); // some thread synchronization code to ensure that only one thread sets up the // receive thread, and that all threads entering this function after the thread // setup begins wait for it to finish. if(lcm->creating_read_thread) { // check if this thread is the one creating the receive thread. // If so, just return. if(g_static_private_get(&CREATE_READ_THREAD_PKEY)) { g_static_rec_mutex_unlock(&lcm->mutex); return 0; } // ugly bit with two mutexes because we can't use a GStaticRecMutex with a GCond g_mutex_lock(lcm->create_read_thread_mutex); g_static_rec_mutex_unlock(&lcm->mutex); // wait for the thread creating the read thread to finish while(lcm->creating_read_thread) { g_cond_wait(lcm->create_read_thread_cond, lcm->create_read_thread_mutex); } g_mutex_unlock(lcm->create_read_thread_mutex); g_static_rec_mutex_lock(&lcm->mutex); // if we've gotten here, then either the read thread is created, or it // was not possible to do so. Figure out which happened, and return. int result = lcm->thread_created ? 0 : -1; g_static_rec_mutex_unlock(&lcm->mutex); return result; } else if(lcm->thread_created) { g_static_rec_mutex_unlock(&lcm->mutex); return 0; } // no other thread is trying to create the read thread right now. claim that task. lcm->creating_read_thread = 1; lcm->create_read_thread_mutex = g_mutex_new(); lcm->create_read_thread_cond = g_cond_new(); // mark this thread as the one creating the read thread g_static_private_set(&CREATE_READ_THREAD_PKEY, GINT_TO_POINTER(1), NULL); dbg (DBG_LCM, "allocating resources for receiving messages\n"); // allocate the fragment buffer hashtable lcm->frag_bufs = lcm_frag_buf_store_new(MAX_FRAG_BUF_TOTAL_SIZE, MAX_NUM_FRAG_BUFS); // allocate multicast socket lcm->recvfd = socket (AF_INET, SOCK_DGRAM, 0); if (lcm->recvfd < 0) { perror ("allocating LCM recv socket"); goto setup_recv_thread_fail; } struct sockaddr_in addr; memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = lcm->params.mc_port; // allow other applications on the local machine to also bind to this // multicast address and port int opt=1; dbg (DBG_LCM, "LCM: setting SO_REUSEADDR\n"); if (setsockopt (lcm->recvfd, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof (opt)) < 0) { perror ("setsockopt (SOL_SOCKET, SO_REUSEADDR)"); goto setup_recv_thread_fail; } #ifdef USE_REUSEPORT /* Mac OS and FreeBSD require the REUSEPORT option in addition * to REUSEADDR or it won't let multiple processes bind to the * same port, even if they are using multicast. */ dbg (DBG_LCM, "LCM: setting SO_REUSEPORT\n"); if (setsockopt (lcm->recvfd, SOL_SOCKET, SO_REUSEPORT, (char*)&opt, sizeof (opt)) < 0) { perror ("setsockopt (SOL_SOCKET, SO_REUSEPORT)"); goto setup_recv_thread_fail; } #endif #if 0 // set loopback option so that packets sent out on the multicast socket // are also delivered to it unsigned char lo_opt = 1; dbg (DBG_LCM, "LCM: setting multicast loopback option\n"); status = setsockopt (lcm->recvfd, IPPROTO_IP, IP_MULTICAST_LOOP, &lo_opt, sizeof (lo_opt)); if (status < 0) { perror ("setting multicast loopback"); return -1; } #endif #ifdef WIN32 // Windows has small (8k) buffer by default // Increase it to a default reasonable amount int recv_buf_size = 2048 * 1024; setsockopt(lcm->recvfd, SOL_SOCKET, SO_RCVBUF, (char*)&recv_buf_size, sizeof(recv_buf_size)); #endif // debugging... how big is the receive buffer? unsigned int retsize = sizeof (int); getsockopt (lcm->recvfd, SOL_SOCKET, SO_RCVBUF, (char*)&lcm->kernel_rbuf_sz, (socklen_t *) &retsize); dbg (DBG_LCM, "LCM: receive buffer is %d bytes\n", lcm->kernel_rbuf_sz); if (lcm->params.recv_buf_size) { if (setsockopt (lcm->recvfd, SOL_SOCKET, SO_RCVBUF, (char *) &lcm->params.recv_buf_size, sizeof (lcm->params.recv_buf_size)) < 0) { perror ("setsockopt(SOL_SOCKET, SO_RCVBUF)"); fprintf (stderr, "Warning: Unable to set recv buffer size\n"); } getsockopt (lcm->recvfd, SOL_SOCKET, SO_RCVBUF, (char*)&lcm->kernel_rbuf_sz, (socklen_t *) &retsize); dbg (DBG_LCM, "LCM: receive buffer is %d bytes\n", lcm->kernel_rbuf_sz); if (lcm->params.recv_buf_size > lcm->kernel_rbuf_sz) { g_warning ("LCM UDP receive buffer size (%d) \n" " is smaller than reqested (%d). " "For more info:\n" " http://lcm-proj.github.io/multicast_setup.html\n", lcm->kernel_rbuf_sz, lcm->params.recv_buf_size); } } /* Enable per-packet timestamping by the kernel, if available */ #ifdef SO_TIMESTAMP opt = 1; setsockopt (lcm->recvfd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof (opt)); #endif if (bind (lcm->recvfd, (struct sockaddr*)&addr, sizeof (addr)) < 0) { perror ("bind"); goto setup_recv_thread_fail; } struct ip_mreq mreq; mreq.imr_multiaddr = lcm->params.mc_addr; mreq.imr_interface.s_addr = INADDR_ANY; // join the multicast group dbg (DBG_LCM, "LCM: joining multicast group\n"); if (setsockopt (lcm->recvfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mreq, sizeof (mreq)) < 0) { perror ("setsockopt (IPPROTO_IP, IP_ADD_MEMBERSHIP)"); goto setup_recv_thread_fail; } lcm->inbufs_empty = lcm_buf_queue_new (); lcm->inbufs_filled = lcm_buf_queue_new (); lcm->ringbuf = lcm_ringbuf_new (LCM_RINGBUF_SIZE); int i; for (i = 0; i < LCM_DEFAULT_RECV_BUFS; i++) { /* We don't set the receive buffer's data pointer yet because it * will be taken from the ringbuffer at receive time. */ lcm_buf_t * lcmb = (lcm_buf_t *) calloc (1, sizeof (lcm_buf_t)); lcm_buf_enqueue (lcm->inbufs_empty, lcmb); } // setup a pipe for notifying the reader thread when to quit if(0 != lcm_internal_pipe_create(lcm->thread_msg_pipe)) { perror(__FILE__ " pipe(setup)"); goto setup_recv_thread_fail; } fcntl (lcm->thread_msg_pipe[1], F_SETFL, O_NONBLOCK); /* Start the reader thread */ lcm->read_thread = g_thread_create (recv_thread, lcm, TRUE, NULL); if (!lcm->read_thread) { fprintf (stderr, "Error: LCM failed to start reader thread\n"); goto setup_recv_thread_fail; } lcm->thread_created = 1; g_static_rec_mutex_unlock(&lcm->mutex); // conduct a self-test just to make sure everything is working. dbg (DBG_LCM, "LCM: conducting self test\n"); int self_test_results = udpm_self_test(lcm); g_static_rec_mutex_lock(&lcm->mutex); if (0 == self_test_results) { dbg (DBG_LCM, "LCM: self test successful\n"); } else { // self test failed. destroy the read thread fprintf (stderr, "LCM self test failed!!\n" "Check your routing tables and firewall settings\n"); _destroy_recv_parts (lcm); } // notify threads waiting for the read thread to be created g_mutex_lock(lcm->create_read_thread_mutex); lcm->creating_read_thread = 0; g_cond_broadcast(lcm->create_read_thread_cond); g_mutex_unlock(lcm->create_read_thread_mutex); g_static_rec_mutex_unlock(&lcm->mutex); return self_test_results; setup_recv_thread_fail: _destroy_recv_parts (lcm); g_static_rec_mutex_unlock(&lcm->mutex); return -1; }
static inline void g_module_set_error_unduped (gchar *error) { g_static_private_set (&module_error_private, error, g_free); errno = 0; }