Blame winpr/libwinpr/synch/sleep.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * WinPR: Windows Portable Runtime
Packit 1fb8d4
 * Synchronization Functions
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/windows.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/synch.h>
Packit 1fb8d4
Packit 1fb8d4
#ifndef _WIN32
Packit 1fb8d4
Packit 1fb8d4
#include <time.h>
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_UNISTD_H
Packit 1fb8d4
	#ifndef _XOPEN_SOURCE
Packit 1fb8d4
		#define _XOPEN_SOURCE 500
Packit 1fb8d4
	#endif
Packit 1fb8d4
#include <unistd.h>
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
VOID Sleep(DWORD dwMilliseconds)
Packit 1fb8d4
{
Packit 1fb8d4
	usleep(dwMilliseconds * 1000);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
DWORD SleepEx(DWORD dwMilliseconds, BOOL bAlertable)
Packit 1fb8d4
{
Packit 1fb8d4
	usleep(dwMilliseconds * 1000);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
VOID USleep(DWORD dwMicroseconds)
Packit 1fb8d4
{
Packit 1fb8d4
#ifndef _WIN32
Packit 1fb8d4
	usleep(dwMicroseconds);
Packit 1fb8d4
#else
Packit 1fb8d4
	static LARGE_INTEGER freq = { 0, 0 };
Packit 1fb8d4
	LARGE_INTEGER t1 = { 0, 0 };
Packit 1fb8d4
	LARGE_INTEGER t2 = { 0, 0 };
Packit 1fb8d4
Packit 1fb8d4
	QueryPerformanceCounter(&t1;;
Packit 1fb8d4
Packit 1fb8d4
	if (freq.QuadPart == 0) {
Packit 1fb8d4
		QueryPerformanceFrequency(&freq);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	// in order to save cpu cyles we use Sleep() for the large share ...
Packit 1fb8d4
	if (dwMicroseconds >= 1000) {
Packit 1fb8d4
		Sleep(dwMicroseconds/1000);
Packit 1fb8d4
	}
Packit 1fb8d4
	// ... and busy loop until all the requested micro seconds have passed
Packit 1fb8d4
	do {
Packit 1fb8d4
		QueryPerformanceCounter(&t2;;
Packit 1fb8d4
	} while (((t2.QuadPart - t1.QuadPart)*1000000)/freq.QuadPart < dwMicroseconds);
Packit 1fb8d4
#endif
Packit 1fb8d4
}