forked from stephentu/silo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.cc
35 lines (30 loc) · 789 Bytes
/
core.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <unistd.h>
#include "amd64.h"
#include "core.h"
#include "util.h"
using namespace std;
using namespace util;
int
coreid::allocate_contiguous_aligned_block(unsigned n, unsigned alignment)
{
retry:
unsigned current = g_core_count.load(memory_order_acquire);
const unsigned rounded = slow_round_up(current, alignment);
const unsigned replace = rounded + n;
if (unlikely(replace > NMaxCores))
return -1;
if (!g_core_count.compare_exchange_strong(current, replace, memory_order_acq_rel)) {
nop_pause();
goto retry;
}
return rounded;
}
unsigned
coreid::num_cpus_online()
{
const long nprocs = sysconf(_SC_NPROCESSORS_ONLN);
ALWAYS_ASSERT(nprocs >= 1);
return nprocs;
}
__thread int coreid::tl_core_id = -1;
atomic<unsigned> coreid::g_core_count(0);