Commit ba9a4df1 authored by Etienne Renault's avatar Etienne Renault

bricks: update excluding C++17 features

* spot/bricks/brick-assert,
spot/bricks/brick-bitlevel,
spot/bricks/brick-hashset,
spot/bricks/brick-shmem: Here.
parent 33887fae
......@@ -58,21 +58,21 @@
#define ASSERT_EQ_IDX(i, x, y) static_cast< decltype(i, x, y, void(0)) >(0)
#endif
/* you must #include <brick-string.h> to use ASSERT_UNREACHABLE_F */
/* you must #include <brick-string> to use ASSERT_UNREACHABLE_F */
#define UNREACHABLE_F(...) ::brick::_assert::assert_die_fn( \
BRICK_LOCWRAP( BRICK_LOCATION( brick::string::fmtf(__VA_ARGS__) ) ) )
#define UNREACHABLE(x) ::brick::_assert::assert_die_fn( \
BRICK_LOCWRAP( BRICK_LOCATION( x ) ) )
#define UNREACHABLE(...) ::brick::_assert::assert_die_fn( \
BRICK_LOCWRAP( BRICK_LOCATION( ::brick::_assert::fmt( __VA_ARGS__ ) ) ) )
#define UNREACHABLE_() ::brick::_assert::assert_die_fn( \
BRICK_LOCWRAP( BRICK_LOCATION( "an unreachable location" ) ) )
#define NOT_IMPLEMENTED() ::brick::_assert::assert_die_fn( \
BRICK_LOCWRAP( BRICK_LOCATION( "a missing implementation" ) ) )
#ifdef _MSC_VER
#define DIVINE_UNUSED
#define UNUSED
#define noexcept
#else
#define DIVINE_UNUSED __attribute__((unused))
#define UNUSED __attribute__((unused))
#endif
#ifndef BRICK_ASSERT_H
......@@ -139,7 +139,7 @@ struct AssertFailed : std::exception
(*this) << ":\n " << expected << " " << l.stmt;
}
const char *what() const noexcept override { return str.c_str(); }
const char *what() const noexcept { return str.c_str(); }
};
static inline void format( AssertFailed & ) {}
......@@ -196,9 +196,9 @@ inline void assert_die_fn( Location l )
} \
}
ASSERT_FN(eq, ==, !=)
ASSERT_FN(leq, <=, >)
ASSERT_FN(lt, <, >=)
ASSERT_FN(eq, ==, !=);
ASSERT_FN(leq, <=, >);
ASSERT_FN(lt, <, >=);
template< typename Location, typename X >
void assert_pred_fn( Location l, X x, bool p )
......@@ -221,6 +221,10 @@ void assert_neq_fn( Location l, X x, Y y )
throw f;
}
inline std::string fmt( const char *str ) { return str; }
inline std::string fmt( std::string str ) { return str; }
// another overload of fmt is exported by brick-string and it allows more complex formating
}
}
......
......@@ -28,7 +28,7 @@
#include <limits>
#ifdef __linux
#include <asm/byteorder.h>
#include <sys/types.h>
#include <byteswap.h>
#elif !defined LITTLE_ENDIAN // if defined _WIN32
#define BYTE_ORDER 1234
......@@ -55,6 +55,28 @@ constexpr inline T1 downalign( T1 v, T2 a ) {
return v - (v % T1(a));
}
template< typename F, typename T >
void maybe_bitcast( F f, T &t )
{
ASSERT( sizeof( T ) == sizeof( F ) );
memcpy( &t, &f, sizeof( T ) );
}
template< typename F, typename T >
void bitcast( F f, T &t )
{
//static_assert( sizeof( F ) == sizeof( T ) );
maybe_bitcast( f, t );
}
template< typename T, typename F >
T bitcast( F f )
{
T t;
bitcast( f, t );
return t;
}
namespace compiletime {
template< typename T >
......@@ -89,10 +111,7 @@ struct bvpair
constexpr bvpair( L l, H h = 0 ) : low( l ), high( h ) {}
constexpr bvpair() = default;
explicit constexpr operator bool() const { return low || high; }
#if ((__GNUC__ >= 4 && __GNUC_MINOR__ > 9) || (__clang_major__ == 3 && __clang_minor__ >= 6))
constexpr
#endif
bvpair operator<<( int s ) const
constexpr bvpair operator<<( int s ) const
{
int rem = 8 * sizeof( low ) - s;
int unshift = std::max( rem, 0 );
......@@ -100,10 +119,7 @@ struct bvpair
H carry = ( low & ~ones< L >( unshift ) ) >> unshift;
return bvpair( low << s, ( high << s ) | ( carry << shift ) );
}
#if ((__GNUC__ >= 4 && __GNUC_MINOR__ > 9) || (__clang_major__ == 3 && __clang_minor__ >= 6))
constexpr
#endif
bvpair operator>>( int s ) const
constexpr bvpair operator>>( int s ) const
{
int rem = 8 * sizeof( low ) - s;
int unshift = std::max( rem, 0 );
......@@ -133,22 +149,45 @@ template< int i > using bitvec = typename _bitvec< i >::T;
namespace {
uint32_t mixdown( uint64_t i ) /* due to Thomas Wang */
template< typename T >
union Cast
{
bitvec< sizeof( T ) * 8 > r;
T c;
Cast( T c ) : c( c ) {}
Cast( bitvec< sizeof( T ) * 8 > r ) : r( r ) {}
};
template< typename T >
bitvec< sizeof( T ) * 8 > raw( T t )
{
i = (~i) + (i << 18);
i = i ^ (i >> 31);
i = i * 21;
i = i ^ (i >> 11);
i = i + (i << 6);
i = i ^ (i >> 22);
return i;
Cast< T > c( t );
return c.r;
}
__attribute__((unused)) uint32_t mixdown( uint32_t a, uint32_t b )
template< typename T >
T unraw( bitvec< sizeof( T ) * 8 > r )
{
return mixdown( ( uint64_t( a ) << 32 ) | uint64_t( b ) );
Cast< T > c( r );
return c.c;
}
// uint32_t mixdown( uint64_t i ) /* due to Thomas Wang */
// {
// i = (~i) + (i << 18);
// i = i ^ (i >> 31);
// i = i * 21;
// i = i ^ (i >> 11);
// i = i + (i << 6);
// i = i ^ (i >> 22);
// return i;
// }
// uint32_t mixdown( uint32_t a, uint32_t b )
// {
// return mixdown( ( uint64_t( a ) << 32 ) | uint64_t( b ) );
// }
}
/*
......@@ -367,13 +406,13 @@ struct BitField
return *this; \
}
OP(+=)
OP(-=)
OP(*=)
OP(/=)
OP(%=)
OP(|=)
OP(&=)
OP(+=);
OP(-=);
OP(*=);
OP(/=);
OP(%=);
OP(|=);
OP(&=);
#undef OP
};
};
......@@ -654,7 +693,8 @@ struct BitTupleTest {
ASSERT( !get< 0 >( bt ).word() );
}
TEST(assign) {
TEST(assign) /* TODO fails in DIVINE */
{
bitlevel::BitTuple<
BitField< bool, 1 >,
BitField< int, 6 >,
......
This diff is collapsed.
......@@ -42,10 +42,10 @@
#include <iostream>
#include <typeinfo>
#include <mutex>
#include <atomic>
#include <thread>
#include <mutex>
#include <future>
#include <unistd.h> // alarm
#include <vector>
......@@ -54,18 +54,21 @@
#define BRICK_SHMEM_H
#ifndef BRICKS_CACHELINE
#if (__GNUC__ >= 7) // Please new Versions of GCC
#define BRICKS_CACHELINE 16
#else
#define BRICKS_CACHELINE 64
#endif
#endif
namespace brick {
namespace shmem {
struct ThreadBase
{
virtual void start() = 0;
virtual void stop() = 0;
virtual ~ThreadBase() {}
};
template< typename T >
struct Thread : T
struct Thread : T, ThreadBase
{
std::unique_ptr< std::thread > _thread;
bool _start_on_move; // :-(
......@@ -89,9 +92,9 @@ struct Thread : T
start();
}
virtual void start() noexcept
virtual void start()
{
_thread.reset( new std::thread( [this]() noexcept { this->main(); } ) );
_thread.reset( new std::thread( [this]() { this->main(); } ) );
}
virtual void stop()
......@@ -117,13 +120,6 @@ struct Thread : T
_thread.reset();
}
}
const Thread& operator=(const Thread& other)
{
std::cerr << "FIXME Added by us (Spot) to avoid compilation warnings\n";
std::cerr << " Should not pass here.\n";
return other;
}
};
template< typename T >
......@@ -178,13 +174,13 @@ struct AsyncLoop : Thread< LoopWrapper< T > >
stop(); /* call the correct stop(), with interrupt() */
}
void start() noexcept override
void start()
{
this->_interrupted.store( false, std::memory_order_relaxed );
Super::start();
}
void stop() override
void stop()
{
this->interrupt();
Super::stop();
......@@ -192,19 +188,22 @@ struct AsyncLoop : Thread< LoopWrapper< T > >
};
template< typename L >
auto async_loop( L &&l ) -> AsyncLoop< LambdaWrapper< L > >&&
using AsyncLambdaLoop = AsyncLoop< LambdaWrapper< L > >;
template< typename L >
auto async_loop( L &&l )
{
AsyncLoop< LambdaWrapper< L > > al( std::forward< L >( l ) );
AsyncLambdaLoop< L > al( std::forward< L >( l ) );
al._start_on_move = true;
return std::move( al );
}
template< typename L >
auto thread( L &&l ) -> Thread< LambdaWrapper< L > >
auto thread( L &&l )
{
Thread< LambdaWrapper< L > > thr( std::forward< L >( l ) );
thr._start_on_move = true;
return thr;
return std::move( thr );
}
template< typename T >
......@@ -213,7 +212,7 @@ struct ThreadSet : std::vector< Thread< T > >
template< typename... Args >
ThreadSet( Args&&... args ) : std::vector< Thread< T > >( std::forward< Args >( args )... ) {}
void start() noexcept { for ( auto &t : *this ) t.start(); }
void start() { for ( auto &t : *this ) t.start(); }
void join() { for ( auto &t : *this ) t.join(); }
};
......@@ -638,6 +637,41 @@ struct Chunked
template< typename T >
using SharedQueue = Chunked< LockedQueue, T >;
namespace
{
using steady_time = std::chrono::time_point< std::chrono::steady_clock >;
// steady_time later( int ms )
// {
// return std::chrono::steady_clock::now() + std::chrono::milliseconds( ms );
// }
template< typename I, typename F >
std::future_status wait( I b, I e, F cleanup,
steady_time until /*= 0 later( 500 )*/ ) try
{
int total = 0, ready = 0;
for ( auto i = b; i != e; ++i )
{
if ( !i->valid() ) continue;
++ total;
if ( i->wait_until( until ) == std::future_status::ready )
{
++ ready;
i->get(); /* throw any exceptions */
}
}
if ( total == ready )
{
cleanup();
return std::future_status::ready;
}
return std::future_status::timeout;
} catch ( ... ) { cleanup(); throw; };
}
}
namespace t_shmem {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment