xref: /dpdk/lib/eal/unix/eal_unix_timer.c (revision 72b452c5f2599f970f47fd17d3e8e5d60bfebe7a)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright 2020 Mellanox Technologies, Ltd
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
5*72b452c5SDmitry Kozlyuk #include <errno.h>
699a2dd95SBruce Richardson #include <time.h>
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson #include <rte_cycles.h>
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson void
rte_delay_us_sleep(unsigned int us)1199a2dd95SBruce Richardson rte_delay_us_sleep(unsigned int us)
1299a2dd95SBruce Richardson {
1399a2dd95SBruce Richardson 	struct timespec wait[2];
1499a2dd95SBruce Richardson 	int ind = 0;
1599a2dd95SBruce Richardson 
1699a2dd95SBruce Richardson 	wait[0].tv_sec = 0;
1799a2dd95SBruce Richardson 	if (us >= US_PER_S) {
1899a2dd95SBruce Richardson 		wait[0].tv_sec = us / US_PER_S;
1999a2dd95SBruce Richardson 		us -= wait[0].tv_sec * US_PER_S;
2099a2dd95SBruce Richardson 	}
2199a2dd95SBruce Richardson 	wait[0].tv_nsec = 1000 * us;
2299a2dd95SBruce Richardson 
2399a2dd95SBruce Richardson 	while (nanosleep(&wait[ind], &wait[1 - ind]) && errno == EINTR) {
2499a2dd95SBruce Richardson 		/*
2599a2dd95SBruce Richardson 		 * Sleep was interrupted. Flip the index, so the 'remainder'
2699a2dd95SBruce Richardson 		 * will become the 'request' for a next call.
2799a2dd95SBruce Richardson 		 */
2899a2dd95SBruce Richardson 		ind = 1 - ind;
2999a2dd95SBruce Richardson 	}
3099a2dd95SBruce Richardson }
31