summaryrefslogtreecommitdiffstats
path: root/src/lib/polling.c
blob: 42e9fef310dbb567a8b51449a652ae2768995131 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// $Id: polling.c,v 1.1.1.1 2013/12/11 20:49:20 bcbrock Exp $
// $Source: /afs/awd/projects/eclipz/KnowledgeBase/.cvsroot/eclipz/chips/p8/working/procedures/lib/polling.c,v $
//-----------------------------------------------------------------------------
// *! (C) Copyright International Business Machines Corp. 2013
// *! All Rights Reserved -- Property of IBM
// *! *** IBM Confidential ***
//-----------------------------------------------------------------------------

/// \file polling.c
/// \brief Library APIs for polling

#include "polling.h"

int
polling(int* o_rc,
        int (*i_condition)(void* io_arg, int* o_satisfied),
        void* io_arg,
        SsxInterval i_timeout,
        SsxInterval i_sleep)
{
    SsxTimebase start;
    int rc, pollRc, timed_out, done;

    if (SSX_ERROR_CHECK_API) {
        SSX_ERROR_IF((i_condition == 0), POLLING_ERROR);
    }
    
    start = ssx_timebase_get();
    timed_out = 0;

    do {
        pollRc = i_condition(io_arg, &done);
        if (pollRc) {
            rc = POLLING_CONDITION;
            break;
        }
        if (done) {
            rc = 0;
            break;
        }
        if (timed_out) {
            rc = POLLING_TIMEOUT;
            break;
        }
        if (i_sleep != 0) {
            rc = ssx_sleep(i_sleep);
            if (rc) {
                break;
            }
        }
        timed_out = 
            ((i_timeout != SSX_WAIT_FOREVER) &&
             ((ssx_timebase_get() - start) >= i_timeout));

    } while (1);

    if (o_rc) {
        *o_rc = pollRc;
    }

    return rc;
}


void
busy_wait(SsxInterval i_interval)
{
    SsxTimebase start;

    start = ssx_timebase_get();
    while ((ssx_timebase_get() - start) < i_interval);
}

OpenPOWER on IntegriCloud