sbsv2/raptor/util/ransleep/ransleep.c
author Richard Taylor <richard.i.taylor@nokia.com>
Wed, 14 Jul 2010 14:50:01 +0100
changeset 616 24e4ef208cca
parent 590 360bd6b35136
permissions -rw-r--r--
sbs version 2.14.2

/*
* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of the License "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/




/* 
   ransleep.c: sleep for some time period specified in milliseconds
               optionally choose a random time up to the maximum time specified.

   Description: Useful for delays between retries and for perturbing the
	        start times of tools which might cause resource starvation 
		if they all execute at exactly the same time.
*/

#include "../config.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

// OS specific headers
#ifdef HOST_WIN
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/select.h>
#endif

int main(int argc, char *argv[])
{

	srand(getpid());
	int millisecs=0;

	if (argc != 2)
	{
		fprintf(stderr,"Must supply numeric argument - maximum milliseconds to sleep\n");
		exit(1);
	}

	millisecs = atoi(argv[1]);


	if (millisecs <= 0 )
	{
		fprintf(stderr,"Must supply numeric argument > 0 - maximum milliseconds to sleep\n");
		exit(1);
	}


	millisecs = rand() % millisecs;
	fprintf(stderr,"random sleep for %d milliseconds\n", millisecs);

	#ifndef HAS_MILLISECONDSLEEP
	struct timeval wtime;
	wtime.tv_sec=millisecs/1000;
	wtime.tv_usec=(millisecs % 1000) * 1000;

	select(0,NULL,NULL,
                  NULL, &wtime);
	#else
	Sleep(millisecs);
	#endif

	return 0;
}