mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-03-31 12:53:07 +02:00
Add g_get_num_processors()
Based on a patch from John Cupitt <jcupitt@gmail.com> Useful for thread pools which should scale to number of processors. See https://bugzilla.gnome.org/show_bug.cgi?id=687223 https://bugzilla.gnome.org/show_bug.cgi?id=614930
This commit is contained in:
parent
ed5accf16c
commit
2149b29468
@ -650,6 +650,9 @@ g_pointer_bit_lock
|
|||||||
g_pointer_bit_trylock
|
g_pointer_bit_trylock
|
||||||
g_pointer_bit_unlock
|
g_pointer_bit_unlock
|
||||||
|
|
||||||
|
<SUBSECTION>
|
||||||
|
g_get_num_processors
|
||||||
|
|
||||||
<SUBSECTION Private>
|
<SUBSECTION Private>
|
||||||
G_LOCK_NAME
|
G_LOCK_NAME
|
||||||
atexit
|
atexit
|
||||||
|
@ -179,6 +179,7 @@ g_completion_new
|
|||||||
g_completion_remove_items
|
g_completion_remove_items
|
||||||
g_completion_set_compare
|
g_completion_set_compare
|
||||||
g_get_filename_charsets
|
g_get_filename_charsets
|
||||||
|
g_get_num_processors
|
||||||
g_convert
|
g_convert
|
||||||
g_convert_error_quark
|
g_convert_error_quark
|
||||||
g_convert_with_fallback
|
g_convert_with_fallback
|
||||||
|
@ -1007,5 +1007,61 @@ g_thread_self (void)
|
|||||||
return (GThread*) thread;
|
return (GThread*) thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_get_num_processors:
|
||||||
|
*
|
||||||
|
* Determine the approximate number of threads that the system will
|
||||||
|
* schedule simultaneously for this process. This is intended to be
|
||||||
|
* used as a parameter to g_thread_pool_new() for CPU bound tasks and
|
||||||
|
* similar cases.
|
||||||
|
*
|
||||||
|
* Returns: Number of schedulable threads, always greater than 0
|
||||||
|
*
|
||||||
|
* Since: 2.36
|
||||||
|
*/
|
||||||
|
guint
|
||||||
|
g_get_num_processors (void)
|
||||||
|
{
|
||||||
|
#ifdef G_OS_WIN32
|
||||||
|
DWORD_PTR process_cpus;
|
||||||
|
DWORD_PTR system_cpus;
|
||||||
|
|
||||||
|
if (GetProcessAffinityMask (GetCurrentProcess (),
|
||||||
|
&process_cpus, &system_cpus))
|
||||||
|
{
|
||||||
|
unsigned int count;
|
||||||
|
|
||||||
|
for (count = 0; process_cpus != 0; process_cpus >>= 1)
|
||||||
|
if (process_cpus & 1)
|
||||||
|
count++;
|
||||||
|
|
||||||
|
if (count > 0)
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
#elif defined(HAVE_UNISTD_H) && defined(_SC_NPROCESSORS_ONLN)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
|
||||||
|
count = sysconf (_SC_NPROCESSORS_ONLN);
|
||||||
|
if (count > 0)
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
#elif defined HW_NCPU
|
||||||
|
{
|
||||||
|
int mib[2], count = 0;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
mib[0] = CTL_HW;
|
||||||
|
mib[1] = HW_NCPU;
|
||||||
|
len = sizeof(count);
|
||||||
|
|
||||||
|
if (sysctl (mib, 2, &count, &len, NULL, 0) == 0 && count > 0)
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return 1; /* Fallback */
|
||||||
|
}
|
||||||
|
|
||||||
/* Epilogue {{{1 */
|
/* Epilogue {{{1 */
|
||||||
/* vim: set foldmethod=marker: */
|
/* vim: set foldmethod=marker: */
|
||||||
|
@ -249,6 +249,9 @@ void g_once_init_leave (volatile void *location,
|
|||||||
(g_once_init_leave((location), (gsize) (result)))
|
(g_once_init_leave((location), (gsize) (result)))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_36
|
||||||
|
guint g_get_num_processors (void);
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
#endif /* __G_THREAD_H__ */
|
#endif /* __G_THREAD_H__ */
|
||||||
|
@ -26,8 +26,6 @@
|
|||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define N_THREADS (20)
|
|
||||||
|
|
||||||
static char *echo_prog_path;
|
static char *echo_prog_path;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -35,8 +33,12 @@ multithreaded_test_run (GThreadFunc function)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
GPtrArray *threads = g_ptr_array_new ();
|
GPtrArray *threads = g_ptr_array_new ();
|
||||||
|
guint n_threads;
|
||||||
|
|
||||||
for (i = 0; i < N_THREADS; i++)
|
/* Limit to 64, otherwise we may hit file descriptor limits and such */
|
||||||
|
n_threads = MIN (g_get_num_processors () * 2, 64);
|
||||||
|
|
||||||
|
for (i = 0; i < n_threads; i++)
|
||||||
{
|
{
|
||||||
GThread *thread;
|
GThread *thread;
|
||||||
|
|
||||||
@ -44,7 +46,7 @@ multithreaded_test_run (GThreadFunc function)
|
|||||||
g_ptr_array_add (threads, thread);
|
g_ptr_array_add (threads, thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < N_THREADS; i++)
|
for (i = 0; i < n_threads; i++)
|
||||||
{
|
{
|
||||||
gpointer ret;
|
gpointer ret;
|
||||||
ret = g_thread_join (g_ptr_array_index (threads, i));
|
ret = g_thread_join (g_ptr_array_index (threads, i));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user