* Copyright (c) 2022 Huawei Technologies Co.,Ltd.
*
* CBB is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*
* cm_spinlock.c
*
*
* IDENTIFICATION
* src/cm_concurrency/cm_spinlock.c
*
* -------------------------------------------------------------------------
*/
#include "cm_spinlock.h"
#include "cm_date.h"
#ifdef WIN32
__declspec(thread) uint64 g_tls_spin_sleeps = 0;
#else
__thread uint64 g_tls_spin_sleeps = 0;
#endif
static uint32 g_spin_sleep_time_nsec = 200000;
void cm_spin_sleep_and_stat(spin_statis_t *stat)
{
uint64 usecs;
timeval_t tv_begin, tv_end;
(void)cm_gettimeofday(&tv_begin);
#ifndef WIN32
cm_spin_sleep_ex(g_spin_sleep_time_nsec);
#else
cm_spin_sleep();
#endif
(void)cm_gettimeofday(&tv_end);
usecs = TIMEVAL_DIFF_US(&tv_begin, &tv_end);
if (stat != NULL) {
stat->wait_usecs = usecs;
}
g_tls_spin_sleeps += usecs;
}
void cm_spin_sleep_and_stat2(uint32 ms)
{
uint64 usecs;
timeval_t tv_begin, tv_end;
(void)cm_gettimeofday(&tv_begin);
cm_sleep(ms);
(void)cm_gettimeofday(&tv_end);
usecs = TIMEVAL_DIFF_US(&tv_begin, &tv_end);
g_tls_spin_sleeps += usecs;
}
uint64 cm_total_spin_usecs(void)
{
return g_tls_spin_sleeps;
}
void cm_set_spin_sleep_time(uint32 sleep_time_nanosecs)
{
g_spin_sleep_time_nsec = sleep_time_nanosecs;
}