spinlock yield test 1
This commit is contained in:
parent
ec86950b09
commit
eca94a06b4
1 changed files with 18 additions and 8 deletions
22
common/log.h
22
common/log.h
|
@ -108,7 +108,8 @@ class LogTargetWrapper
|
||||||
// TODO: MSVC
|
// TODO: MSVC
|
||||||
//
|
//
|
||||||
static const auto dummy_init __attribute__((unused)) = [&](){
|
static const auto dummy_init __attribute__((unused)) = [&](){
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
//std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
while( _lock.test_and_set(std::memory_order_acquire) ){ std::this_thread::yield(); }
|
||||||
if(!_opened && _type == Type::File)
|
if(!_opened && _type == Type::File)
|
||||||
{
|
{
|
||||||
auto result = std::fopen(_filename.c_str(), "w");
|
auto result = std::fopen(_filename.c_str(), "w");
|
||||||
|
@ -124,25 +125,29 @@ class LogTargetWrapper
|
||||||
}
|
}
|
||||||
_opened = true;
|
_opened = true;
|
||||||
}
|
}
|
||||||
return _opened;
|
_lock.clear(std::memory_order_release);
|
||||||
|
return _opened.load();
|
||||||
}();
|
}();
|
||||||
return _handle;
|
return _handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush()
|
void flush()
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
//std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
while( _lock.test_and_set(std::memory_order_acquire) ){ std::this_thread::yield(); }
|
||||||
if(_opened && _type != Type::Invalid && _handle)
|
if(_opened && _type != Type::Invalid && _handle)
|
||||||
{
|
{
|
||||||
std::fflush(_handle);
|
std::fflush(_handle);
|
||||||
}
|
}
|
||||||
|
_lock.clear(std::memory_order_release);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex _mutex;
|
//std::mutex _mutex;
|
||||||
|
std::atomic_flag _lock = ATOMIC_FLAG_INIT;
|
||||||
Type _type {Type::Invalid};
|
Type _type {Type::Invalid};
|
||||||
std::string _filename;
|
std::string _filename;
|
||||||
bool _opened {false};
|
std::atomic<bool> _opened {false};
|
||||||
std::atomic<FILE*> _handle {nullptr};
|
std::atomic<FILE*> _handle {nullptr};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -346,12 +351,13 @@ class LogStateWrapper
|
||||||
|
|
||||||
bool log_param_single_parse_impl(const std::string & param)
|
bool log_param_single_parse_impl(const std::string & param)
|
||||||
{
|
{
|
||||||
|
#ifdef LOG_WITH_TEST
|
||||||
if ( param == "--log-test")
|
if ( param == "--log-test")
|
||||||
{
|
{
|
||||||
log_test();
|
log_test();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
if ( param == "--log-disable")
|
if ( param == "--log-disable")
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
|
@ -804,6 +810,7 @@ inline FILE *log_set_target_impl(FILE *target) { return log_handler2_impl(true,
|
||||||
inline FILE *log_handler() { return log_handler1_impl(); }
|
inline FILE *log_handler() { return log_handler1_impl(); }
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef LOG_WITH_TEST
|
||||||
inline void log_test()
|
inline void log_test()
|
||||||
{
|
{
|
||||||
auto log_default_logfile = log_set_target(log_filename_generator("llama","log"));
|
auto log_default_logfile = log_set_target(log_filename_generator("llama","log"));
|
||||||
|
@ -847,6 +854,7 @@ inline void log_test()
|
||||||
LOGLN("22 Hello msvc LOGLN with (%d)(%s) arguments\n", 1, "test")
|
LOGLN("22 Hello msvc LOGLN with (%d)(%s) arguments\n", 1, "test")
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#define log_param_single_parse(param) LogStateWrapper::log_param_single_parse(param)
|
#define log_param_single_parse(param) LogStateWrapper::log_param_single_parse(param)
|
||||||
#define log_param_pair_parse(...) LogStateWrapper::log_param_pair_parse(__VA_ARGS__)
|
#define log_param_pair_parse(...) LogStateWrapper::log_param_pair_parse(__VA_ARGS__)
|
||||||
|
@ -858,7 +866,9 @@ inline void log_print_usage()
|
||||||
printf(" -h, --help show this help message and exit\n");*/
|
printf(" -h, --help show this help message and exit\n");*/
|
||||||
/* spacing
|
/* spacing
|
||||||
printf("__-param----------------Description\n");*/
|
printf("__-param----------------Description\n");*/
|
||||||
|
#ifdef LOG_WITH_TEST
|
||||||
printf(" --log-test Run simple logging test\n");
|
printf(" --log-test Run simple logging test\n");
|
||||||
|
#endif
|
||||||
printf(" --log-disable Disable trace logs\n");
|
printf(" --log-disable Disable trace logs\n");
|
||||||
printf(" --log-enable Enable trace logs\n");
|
printf(" --log-enable Enable trace logs\n");
|
||||||
printf(" --log-file Specify a log filename (without extension)\n");
|
printf(" --log-file Specify a log filename (without extension)\n");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue