2023-02-27 13:40:20 +01:00
|
|
|
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
|
2022-09-19 12:39:02 +02:00
|
|
|
* SPDX-License-Identifier: MIT */
|
2022-01-11 15:47:17 +01:00
|
|
|
|
2023-03-22 17:06:52 +01:00
|
|
|
#define _GNU_SOURCE /* Only here */
|
2022-01-11 15:47:17 +01:00
|
|
|
|
2023-03-22 17:06:52 +01:00
|
|
|
#include "compat.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2022-01-11 15:47:17 +01:00
|
|
|
|
|
|
|
/* Define gettid for older glibc versions (below 2.30) */
|
2022-01-11 19:08:37 +01:00
|
|
|
#if defined(__GLIBC__)
|
|
|
|
#if !__GLIBC_PREREQ(2, 30)
|
2023-03-22 16:01:55 +01:00
|
|
|
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
2023-03-22 17:06:52 +01:00
|
|
|
static pid_t
|
2022-09-29 15:34:44 +02:00
|
|
|
gettid(void)
|
2022-01-11 15:47:17 +01:00
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
return (pid_t) syscall(SYS_gettid);
|
2022-01-11 15:47:17 +01:00
|
|
|
}
|
2023-03-22 16:01:55 +01:00
|
|
|
|
2022-01-11 19:08:37 +01:00
|
|
|
#endif /* !__GLIBC_PREREQ(2, 30) */
|
|
|
|
#endif /* defined(__GLIBC__) */
|
2022-01-11 15:47:17 +01:00
|
|
|
|
2023-03-22 17:06:52 +01:00
|
|
|
pid_t
|
|
|
|
get_tid(void)
|
|
|
|
{
|
|
|
|
return gettid();
|
|
|
|
}
|
2023-03-22 16:01:55 +01:00
|
|
|
|
2023-03-22 17:06:52 +01:00
|
|
|
int
|
2023-03-22 16:01:55 +01:00
|
|
|
sleep_us(long usec)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (usec < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ts.tv_sec = usec / 1000000L;
|
|
|
|
ts.tv_nsec = (usec % 1000000L) * 1000L;
|
|
|
|
|
|
|
|
do {
|
|
|
|
res = nanosleep(&ts, &ts);
|
|
|
|
} while (res && errno == EINTR);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|