diff options
Diffstat (limited to 'src/core/support/cpu_posix.c')
-rw-r--r-- | src/core/support/cpu_posix.c | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/core/support/cpu_posix.c b/src/core/support/cpu_posix.c index 1844e9a622..99484e37fb 100644 --- a/src/core/support/cpu_posix.c +++ b/src/core/support/cpu_posix.c @@ -42,6 +42,8 @@ #include <grpc/support/log.h> #include <grpc/support/sync.h> +static __thread char magic_thread_local; + static int ncpus = 0; static void init_ncpus() { @@ -58,10 +60,18 @@ unsigned gpr_cpu_num_cores(void) { return ncpus; } +/* This is a cheap, but good enough, pointer hash for sharding things: */ +static size_t shard_ptr(const void *info) { + size_t x = (size_t)info; + return ((x >> 4) ^ (x >> 9) ^ (x >> 14)) % gpr_cpu_num_cores(); +} + unsigned gpr_cpu_current_cpu(void) { /* NOTE: there's no way I know to return the actual cpu index portably... - most code that's using this is using it to shard across work queues though */ - return 0; + most code that's using this is using it to shard across work queues though, + so here we use thread identity instead to achieve a similar though not + identical effect */ + return shard_ptr(&magic_thread_local); } -#endif /* GPR_CPU_LINUX */ +#endif /* GPR_CPU_POSIX */ |