int main (gint argc, gchar * argv[]) { g_thread_init (NULL); g_thread_create_full (thread1, NULL, 0x200000, FALSE, TRUE, G_THREAD_PRIORITY_NORMAL, NULL); g_thread_create_full (thread2, NULL, 0x200000, FALSE, TRUE, G_THREAD_PRIORITY_NORMAL, NULL); g_usleep (G_MAXLONG); return 0; }
/** * iris_thread_new: * @exclusive: the thread is exclusive * * Createa a new #IrisThread instance that can be used to queue work items * to be processed on the thread. * * If @exclusive, then the thread will not yield to the scheduler and * therefore will not participate in scheduler thread balancing. * * Return value: the newly created #IrisThread instance */ IrisThread* iris_thread_new (gboolean exclusive) { IrisThread *thread; iris_debug (IRIS_DEBUG_THREAD); #if LINUX #else pthread_once (&my_thread_once, _pthread_init); #endif thread = g_slice_new0 (IrisThread); thread->exclusive = exclusive; thread->queue = g_async_queue_new (); thread->mutex = g_mutex_new (); thread->thread = g_thread_create_full ((GThreadFunc)iris_thread_worker, thread, 0, /* stack size */ FALSE, /* joinable */ FALSE, /* system thread */ G_THREAD_PRIORITY_NORMAL, NULL); thread->scheduler = NULL; return thread; }
int s52ais_initAIS(void) { g_print("s52ais_initAIS() .. start\n"); // so all occurence of _ais_list are mutex'ed // (but this one is useless - but what if android restart main()!) GMUTEXLOCK(&_ais_list_mutex); if (NULL == _ais_list) { _ais_list = g_array_new(FALSE, TRUE, sizeof(_ais_t)); } else { g_print("s52ais:s52ais_initAIS(): bizzard case where we are restarting a running process!!\n"); GMUTEXUNLOCK(&_ais_list_mutex); return TRUE; } GMUTEXUNLOCK(&_ais_list_mutex); #if 0 // NOTE: on Ubuntu, GPSD is started at boot-time if (TRUE != _startGPSD()) return FALSE; #endif #ifdef WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #endif #ifdef S52_USE_DBUS _initDBus(); #endif #ifdef S52_USE_SOCK _s52_connection = _s52_init_sock(_localhost, S52_PORT); if (NULL == _s52_connection) { g_print("s52ais_initAIS() .. _s52_init_sock() failed \n"); return FALSE; } #endif // no thread needed in standalone #if !defined(S52AIS_STANDALONE) // not joinable - gps done will not wait #ifdef S52_USE_ANDROID _gpsClientThread = g_thread_create_full(_gpsdClientStart, NULL, 0, FALSE, TRUE, G_THREAD_PRIORITY_NORMAL, NULL); #else _gpsClientThread = g_thread_new("threadName", _gpsdClientStart, NULL); #endif #endif // setup timer g_get_current_time(&_timeTick); return TRUE; }
/** * g_thread_create: * @func: a function to execute in the new thread * @data: an argument to supply to the new thread * @joinable: should this thread be joinable? * @error: return location for error, or %NULL * * This function creates a new thread. * * The new thread executes the function @func with the argument @data. * If the thread was created successfully, it is returned. * * @error can be %NULL to ignore errors, or non-%NULL to report errors. * The error is set, if and only if the function returns %NULL. * * This function returns a reference to the created thread only if * @joinable is %TRUE. In that case, you must free this reference by * calling g_thread_unref() or g_thread_join(). If @joinable is %FALSE * then you should probably not touch the return value. * * Returns: the new #GThread on success * * Deprecated:2.32: Use g_thread_new() instead */ GThread * g_thread_create (GThreadFunc func, gpointer data, gboolean joinable, GError **error) { return g_thread_create_full (func, data, 0, joinable, 0, 0, error); }
void verve_env_init (VerveEnv *env) { env->paths = NULL; env->binaries = NULL; /* Spawn the thread used to load the command completion data */ env->load_thread = g_thread_create_full (verve_env_load_thread, env, 0, TRUE, FALSE, G_THREAD_PRIORITY_LOW, NULL); }
GThread * create_worker_thread(GThreadFunc func, gpointer data, gboolean joinable, GError **error) { GThread *h; WorkerThreadParams *p; p = g_new0(WorkerThreadParams, 1); p->func = func; p->data = data; h = g_thread_create_full(worker_thread_func, p, 128 * 1024, joinable, TRUE, G_THREAD_PRIORITY_NORMAL, error); if (!h) { g_free(p); return NULL; } return h; }
/** * Allocate and initialize a Zorp thread identified by a name, and * using the given thread function. * * @param[in] name name to identify this thread with * @param[in] func thread function * @param[in] arg pointer to pass to thread function * * @returns TRUE to indicate success **/ gboolean z_thread_new(gchar *name, GThreadFunc func, gpointer arg) { ZThread *self = g_new0(ZThread, 1); GError *error = NULL; static gint thread_id = 1; self->thread_id = thread_id++; self->func = func; self->arg = arg; strncpy(self->name, name, sizeof(self->name) - 1); g_async_queue_lock(queue); if (num_threads >= max_threads) { /*LOG This message reports that the maximal thread limit is reached. Try to increase the maximal thread limit. */ z_log(NULL, CORE_ERROR, 3, "Too many running threads, waiting for one to become free; num_threads='%d', max_threads='%d'", num_threads, max_threads); g_async_queue_push_unlocked(queue, self); g_async_queue_unlock(queue); } else { num_threads++; g_async_queue_ref_unlocked(queue); g_async_queue_unlock(queue); if (!g_thread_create_full(z_thread_func, self, max_stack_size, FALSE, TRUE, G_THREAD_PRIORITY_NORMAL, &error)) { /*LOG This message indicates that creating a new thread failed. It is likely that the system is running low on some resources or some limit is reached. */ z_log(NULL, CORE_ERROR, 2, "Error starting new thread; error='%s'", error->message); g_async_queue_lock(queue); num_threads--; g_async_queue_unlock(queue); return FALSE; } } return TRUE; }
static int create_thread(os_handler_t *handler, int priority, void (*startup)(void *data), void *data) { GThread *t; t = g_thread_create_full(startup, data, 0, FALSE, FALSE, priority, NULL); if (!t) return ENOMEM; return 0; }
static void link_exec_set_io_thread (gpointer data, gboolean immediate) { GError *error = NULL; gboolean to_io_thread = TRUE; if (link_is_io_in_thread) return; link_lock (); g_mutex_lock (link_cmd_queue_lock); link_is_io_in_thread = TRUE; link_thread_context = g_main_context_new (); link_thread_loop = g_main_loop_new (link_thread_context, TRUE); link_connections_move_io_T (to_io_thread); link_servers_move_io_T (to_io_thread); if (link_pipe (link_wakeup_fds) < 0) g_error ("Can't create CORBA main-thread wakeup pipe"); link_main_source = link_source_create_watch (link_thread_context, LINK_WAKEUP_POLL, NULL, (G_IO_IN | G_IO_PRI), link_mainloop_handle_input, NULL); link_io_thread = g_thread_create_full (link_io_thread_fn, NULL, 256 * 1024, TRUE, FALSE, G_THREAD_PRIORITY_NORMAL, &error); if (!link_io_thread || error) g_error ("Failed to create linc worker thread"); g_main_loop_quit (link_loop); g_mutex_unlock (link_cmd_queue_lock); link_unlock (); }
static gboolean on_startup ( G_GNUC_UNUSED gpointer data ) { struct pgm_addrinfo_t* res = NULL; pgm_error_t* pgm_err = NULL; sa_family_t sa_family = AF_UNSPEC; g_message ("startup."); /* parse network parameter into transport address structure */ if (!pgm_getaddrinfo (g_network, NULL, &res, &pgm_err)) { g_error ("parsing network parameter: %s", pgm_err->message); goto err_abort; } sa_family = res->ai_send_addrs[0].gsr_group.ss_family; if (g_udp_encap_port) { g_message ("create PGM/UDP socket."); if (!pgm_socket (&g_sock, sa_family, SOCK_SEQPACKET, IPPROTO_UDP, &pgm_err)) { g_error ("socket: %s", pgm_err->message); goto err_abort; } pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_UDP_ENCAP_UCAST_PORT, &g_udp_encap_port, sizeof(g_udp_encap_port)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_UDP_ENCAP_MCAST_PORT, &g_udp_encap_port, sizeof(g_udp_encap_port)); } else { g_message ("create PGM/IP socket."); if (!pgm_socket (&g_sock, sa_family, SOCK_SEQPACKET, IPPROTO_PGM, &pgm_err)) { g_error ("socket: %s", pgm_err->message); goto err_abort; } } /* Use RFC 2113 tagging for PGM Router Assist */ const int no_router_assist = 0; pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_IP_ROUTER_ALERT, &no_router_assist, sizeof(no_router_assist)); pgm_drop_superuser(); /* set PGM parameters */ const int recv_only = 1, passive = 0, peer_expiry = pgm_secs (300), spmr_expiry = pgm_msecs (250), nak_bo_ivl = pgm_msecs (50), nak_rpt_ivl = pgm_secs (2), nak_rdata_ivl = pgm_secs (2), nak_data_retries = 50, nak_ncf_retries = 50; pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_RECV_ONLY, &recv_only, sizeof(recv_only)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_PASSIVE, &passive, sizeof(passive)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_MTU, &g_max_tpdu, sizeof(g_max_tpdu)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_RXW_SQNS, &g_sqns, sizeof(g_sqns)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_PEER_EXPIRY, &peer_expiry, sizeof(peer_expiry)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_SPMR_EXPIRY, &spmr_expiry, sizeof(spmr_expiry)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_NAK_BO_IVL, &nak_bo_ivl, sizeof(nak_bo_ivl)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_NAK_RPT_IVL, &nak_rpt_ivl, sizeof(nak_rpt_ivl)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_NAK_RDATA_IVL, &nak_rdata_ivl, sizeof(nak_rdata_ivl)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_NAK_DATA_RETRIES, &nak_data_retries, sizeof(nak_data_retries)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_NAK_NCF_RETRIES, &nak_ncf_retries, sizeof(nak_ncf_retries)); /* create global session identifier */ struct pgm_sockaddr_t addr; memset (&addr, 0, sizeof(addr)); addr.sa_port = g_port ? g_port : DEFAULT_DATA_DESTINATION_PORT; addr.sa_addr.sport = DEFAULT_DATA_SOURCE_PORT; if (!pgm_gsi_create_from_hostname (&addr.sa_addr.gsi, &pgm_err)) { g_error ("creating GSI: %s", pgm_err->message); goto err_abort; } /* assign socket to specified address */ struct pgm_interface_req_t if_req; memset (&if_req, 0, sizeof(if_req)); if_req.ir_interface = res->ai_recv_addrs[0].gsr_interface; memcpy (&if_req.ir_address, &res->ai_send_addrs[0].gsr_addr, sizeof(struct sockaddr_storage)); if (!pgm_bind3 (g_sock, &addr, sizeof(addr), &if_req, sizeof(if_req), /* tx interface */ &if_req, sizeof(if_req), /* rx interface */ &pgm_err)) { g_error ("binding PGM socket: %s", pgm_err->message); goto err_abort; } /* join IP multicast groups */ for (unsigned i = 0; i < res->ai_recv_addrs_len; i++) pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_JOIN_GROUP, &res->ai_recv_addrs[i], sizeof(struct pgm_group_source_req)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_SEND_GROUP, &res->ai_send_addrs[0], sizeof(struct pgm_group_source_req)); pgm_freeaddrinfo (res); /* set IP parameters */ const int nonblocking = 1, multicast_loop = g_multicast_loop ? 1 : 0, multicast_hops = 16, dscp = 0x2e << 2; /* Expedited Forwarding PHB for network elements, no ECN. */ pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_MULTICAST_LOOP, &multicast_loop, sizeof(multicast_loop)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_MULTICAST_HOPS, &multicast_hops, sizeof(multicast_hops)); if (AF_INET6 != sa_family) pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_TOS, &dscp, sizeof(dscp)); pgm_setsockopt (g_sock, IPPROTO_PGM, PGM_NOBLOCK, &nonblocking, sizeof(nonblocking)); if (!pgm_connect (g_sock, &pgm_err)) { g_error ("connecting PGM socket: %s", pgm_err->message); goto err_abort; } /* create receiver thread */ GError* glib_err = NULL; g_thread = g_thread_create_full (receiver_thread, g_sock, 0, TRUE, TRUE, G_THREAD_PRIORITY_HIGH, &glib_err); if (!g_thread) { g_error ("g_thread_create_full failed errno %i: \"%s\"", glib_err->code, glib_err->message); g_error_free (glib_err); goto err_abort; } /* period timer to indicate some form of life */ // TODO: Gnome 2.14: replace with g_timeout_add_seconds() g_timeout_add(10 * 1000, (GSourceFunc)on_mark, NULL); g_message ("startup complete."); return FALSE; err_abort: if (NULL != g_sock) { pgm_close (g_sock, FALSE); g_sock = NULL; } if (NULL != res) { pgm_freeaddrinfo (res); res = NULL; } if (NULL != pgm_err) { pgm_error_free (pgm_err); pgm_err = NULL; } g_main_loop_quit (g_loop); return FALSE; }
static gboolean on_startup ( G_GNUC_UNUSED gpointer data ) { g_message ("startup."); g_message ("create transport."); pgm_gsi_t gsi; int e = pgm_create_md5_gsi (&gsi); g_assert (e == 0); struct group_source_req recv_gsr, send_gsr; gsize recv_len = 1; e = pgm_if_parse_transport (g_network, AF_UNSPEC, &recv_gsr, &recv_len, &send_gsr); g_assert (e == 0); g_assert (recv_len == 1); if (g_source[0]) { ((struct sockaddr_in*)&recv_gsr.gsr_source)->sin_addr.s_addr = inet_addr(g_source); } if (g_udp_encap_port) { ((struct sockaddr_in*)&send_gsr.gsr_group)->sin_port = g_htons (g_udp_encap_port); ((struct sockaddr_in*)&recv_gsr.gsr_group)->sin_port = g_htons (g_udp_encap_port); } pgm_transport_list = NULL; e = pgm_transport_create (&g_transport, &gsi, 0, g_port, &recv_gsr, 1, &send_gsr); g_assert (e == 0); pgm_transport_set_recv_only (g_transport, FALSE); pgm_transport_set_max_tpdu (g_transport, g_max_tpdu); pgm_transport_set_rxw_sqns (g_transport, g_sqns); pgm_transport_set_multicast_loop (g_transport, g_multicast_loop); pgm_transport_set_hops (g_transport, 16); pgm_transport_set_peer_expiry (g_transport, pgm_secs(300)); pgm_transport_set_spmr_expiry (g_transport, pgm_msecs(250)); pgm_transport_set_nak_bo_ivl (g_transport, pgm_msecs(50)); pgm_transport_set_nak_rpt_ivl (g_transport, pgm_secs(2)); pgm_transport_set_nak_rdata_ivl (g_transport, pgm_secs(2)); pgm_transport_set_nak_data_retries (g_transport, 50); pgm_transport_set_nak_ncf_retries (g_transport, 50); e = pgm_transport_bind (g_transport); if (e < 0) { if (e == -1) g_critical ("pgm_transport_bind failed errno %i: \"%s\"", errno, strerror(errno)); else if (e == -2) g_critical ("pgm_transport_bind failed h_errno %i: \"%s\"", h_errno, hstrerror(h_errno)); else g_critical ("pgm_transport_bind failed e %i", e); g_main_loop_quit(g_loop); return FALSE; } g_assert (e == 0); /* create receiver thread */ GError* err; g_thread = g_thread_create_full (receiver_thread, g_transport, 0, TRUE, TRUE, G_THREAD_PRIORITY_HIGH, &err); if (!g_thread) { g_critical ("g_thread_create_full failed errno %i: \"%s\"", err->code, err->message); g_main_loop_quit(g_loop); return FALSE; } /* period timer to indicate some form of life */ // TODO: Gnome 2.14: replace with g_timeout_add_seconds() g_timeout_add(10 * 1000, (GSourceFunc)on_mark, NULL); g_message ("startup complete."); return FALSE; }
int main (int argc, char *argv[]) { guint seed32, *seedp = NULL; gboolean ccounters = FALSE, use_memchunks = FALSE; guint n_threads = 1; const gchar *mode = "slab allocator + magazine cache", *emode = " "; if (argc > 1) n_threads = g_ascii_strtoull (argv[1], NULL, 10); if (argc > 2) { guint i, l = strlen (argv[2]); for (i = 0; i < l; i++) switch (argv[2][i]) { case 'G': /* GLib mode */ g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, FALSE); g_slice_set_config (G_SLICE_CONFIG_BYPASS_MAGAZINES, FALSE); mode = "slab allocator + magazine cache"; break; case 'S': /* slab mode */ g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, FALSE); g_slice_set_config (G_SLICE_CONFIG_BYPASS_MAGAZINES, TRUE); mode = "slab allocator"; break; case 'M': /* malloc mode */ g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, TRUE); mode = "system malloc"; break; case 'O': /* old memchunks */ use_memchunks = TRUE; mode = "old memchunks"; break; case 'f': /* eager freeing */ g_slice_set_config (G_SLICE_CONFIG_WORKING_SET_MSECS, 0); clean_memchunks = TRUE; emode = " with eager freeing"; break; case 'c': /* print contention counters */ ccounters = TRUE; break; case '~': want_corruption = TRUE; /* force occasional corruption */ break; default: usage(); return 1; } } if (argc > 3) prime_size = g_ascii_strtoull (argv[3], NULL, 10); if (argc > 4) { seed32 = g_ascii_strtoull (argv[4], NULL, 10); seedp = &seed32; } g_thread_init (NULL); if (argc <= 1) usage(); gchar strseed[64] = "<random>"; if (seedp) g_snprintf (strseed, 64, "%u", *seedp); g_print ("Starting %d threads allocating random blocks <= %u bytes with seed=%s using %s%s\n", n_threads, prime_size, strseed, mode, emode); GThread *threads[n_threads]; guint i; if (!use_memchunks) for (i = 0; i < n_threads; i++) threads[i] = g_thread_create_full (test_sliced_mem_thread, seedp, 0, TRUE, FALSE, 0, NULL); else { old_mem_chunks_init(); for (i = 0; i < n_threads; i++) threads[i] = g_thread_create_full (test_memchunk_thread, seedp, 0, TRUE, FALSE, 0, NULL); } for (i = 0; i < n_threads; i++) g_thread_join (threads[i]); if (ccounters) { guint n, n_chunks = g_slice_get_config (G_SLICE_CONFIG_CHUNK_SIZES); g_print (" ChunkSize | MagazineSize | Contention\n"); for (i = 0; i < n_chunks; i++) { gint64 *vals = g_slice_get_config_state (G_SLICE_CONFIG_CONTENTION_COUNTER, i, &n); g_print (" %9llu | %9llu | %9llu\n", vals[0], vals[2], vals[1]); g_free (vals); } } else g_print ("Done.\n"); return 0; }