3
|
1 |
/*
|
|
2 |
* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
/*
|
|
22 |
ransleep.c: sleep for some time period specified in milliseconds
|
|
23 |
optionally choose a random time up to the maximum time specified.
|
|
24 |
|
|
25 |
Description: Useful for delays between retries and for perturbing the
|
|
26 |
start times of tools which might cause resource starvation
|
|
27 |
if they all execute at exactly the same time.
|
|
28 |
*/
|
|
29 |
|
|
30 |
#include "../config.h"
|
|
31 |
#include <unistd.h>
|
|
32 |
#include <stdlib.h>
|
|
33 |
#include <stdio.h>
|
|
34 |
|
|
35 |
// OS specific headers
|
|
36 |
#ifdef HOST_WIN
|
|
37 |
#include <windows.h>
|
|
38 |
#else
|
|
39 |
#include <sys/types.h>
|
|
40 |
#include <sys/select.h>
|
|
41 |
#endif
|
|
42 |
|
|
43 |
int main(int argc, char *argv[])
|
|
44 |
{
|
|
45 |
|
|
46 |
srand(getpid());
|
|
47 |
int millisecs=0;
|
|
48 |
|
|
49 |
if (argc != 2)
|
|
50 |
{
|
|
51 |
fprintf(stderr,"Must supply numeric argument - maximum milliseconds to sleep\n");
|
|
52 |
exit(1);
|
|
53 |
}
|
|
54 |
|
|
55 |
millisecs = atoi(argv[1]);
|
|
56 |
|
|
57 |
|
|
58 |
if (millisecs <= 0 )
|
|
59 |
{
|
|
60 |
fprintf(stderr,"Must supply numeric argument > 0 - maximum milliseconds to sleep\n");
|
|
61 |
exit(1);
|
|
62 |
}
|
|
63 |
|
|
64 |
|
|
65 |
millisecs = rand() % millisecs;
|
|
66 |
fprintf(stderr,"random sleep for %d milliseconds\n", millisecs);
|
|
67 |
|
|
68 |
#ifndef HAS_MILLISECONDSLEEP
|
|
69 |
struct timeval wtime;
|
|
70 |
wtime.tv_sec=millisecs/1000;
|
|
71 |
wtime.tv_usec=(millisecs % 1000) * 1000;
|
|
72 |
|
|
73 |
select(0,NULL,NULL,
|
|
74 |
NULL, &wtime);
|
|
75 |
#else
|
|
76 |
Sleep(millisecs);
|
|
77 |
#endif
|
|
78 |
|
|
79 |
return 0;
|
|
80 |
}
|
|
81 |
|
|
82 |
|