benchmark silo added
[c11concurrency-benchmarks.git] / silo / core.cc
1 #include <unistd.h>
2
3 #include "amd64.h"
4 #include "core.h"
5 #include "util.h"
6
7 using namespace std;
8 using namespace util;
9
10 int
11 coreid::allocate_contiguous_aligned_block(unsigned n, unsigned alignment)
12 {
13 retry:
14   unsigned current = g_core_count.load(memory_order_acquire);
15   const unsigned rounded = slow_round_up(current, alignment);
16   const unsigned replace = rounded + n;
17   if (unlikely(replace > NMaxCores))
18     return -1;
19   if (!g_core_count.compare_exchange_strong(current, replace, memory_order_acq_rel)) {
20     nop_pause();
21     goto retry;
22   }
23   return rounded;
24 }
25
26 unsigned
27 coreid::num_cpus_online()
28 {
29   const long nprocs = sysconf(_SC_NPROCESSORS_ONLN);
30   ALWAYS_ASSERT(nprocs >= 1);
31   return nprocs;
32 }
33
34 __thread int coreid::tl_core_id = -1;
35 atomic<unsigned> coreid::g_core_count(0);