summaryrefslogtreecommitdiffstats
path: root/nci/jni/CondVar.cpp
blob: 115634431e7de5a16437ab827a6cc7a176432778 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*****************************************************************************
**
**  Name:           CondVar.cpp
**
**  Description:    Encapsulate a condition variable for thread synchronization.
**
**  Copyright (c) 2012, Broadcom Corp., All Rights Reserved.
**  Proprietary and confidential.
**
*****************************************************************************/

#include "CondVar.h"
#include "NfcJniUtil.h"
#include <errno.h>


/*******************************************************************************
**
** Function:        CondVar
**
** Description:     Initialize member variables.
**
** Returns:         None.
**
*******************************************************************************/
CondVar::CondVar ()
{
    memset (&mCondition, 0, sizeof(mCondition));
    int const res = pthread_cond_init (&mCondition, NULL);
    if (res)
    {
        ALOGE ("CondVar::CondVar: fail init; error=0x%X", res);
    }
}


/*******************************************************************************
**
** Function:        ~CondVar
**
** Description:     Cleanup all resources.
**
** Returns:         None.
**
*******************************************************************************/
CondVar::~CondVar ()
{
    int const res = pthread_cond_destroy (&mCondition);
    if (res)
    {
        ALOGE ("CondVar::~CondVar: fail destroy; error=0x%X", res);
    }
}


/*******************************************************************************
**
** Function:        wait
**
** Description:     Block the caller and wait for a condition.
**
** Returns:         None.
**
*******************************************************************************/
void CondVar::wait (Mutex& mutex)
{
    int const res = pthread_cond_wait (&mCondition, mutex.nativeHandle());
    if (res)
    {
        ALOGE ("CondVar::wait: fail wait; error=0x%X", res);
    }
}


/*******************************************************************************
**
** Function:        wait
**
** Description:     Block the caller and wait for a condition.
**                  millisec: Timeout in milliseconds.
**
** Returns:         True if wait is successful; false if timeout occurs.
**
*******************************************************************************/
bool CondVar::wait (Mutex& mutex, long millisec)
{
    bool retVal = false;
    struct timespec absoluteTime;

    if (clock_gettime (CLOCK_MONOTONIC, &absoluteTime) == -1)
    {
        ALOGE ("CondVar::wait: fail get time; errno=0x%X", errno);
    }
    else
    {
        absoluteTime.tv_sec += millisec / 1000;
        long ns = absoluteTime.tv_nsec + ((millisec % 1000) * 1000000);
        if (ns > 1000000000)
        {
            absoluteTime.tv_sec++;
            absoluteTime.tv_nsec = ns - 1000000000;
        }
        else
            absoluteTime.tv_nsec = ns;
    }

    //pthread_cond_timedwait_monotonic_np() is an Android-specific function
    //declared in /development/ndk/platforms/android-9/include/pthread.h;
    //it uses monotonic clock.
    //the standard pthread_cond_timedwait() uses realtime clock.
    int waitResult = pthread_cond_timedwait_monotonic_np (&mCondition, mutex.nativeHandle(), &absoluteTime);
    if ((waitResult != 0) && (waitResult != ETIMEDOUT))
        ALOGE ("CondVar::wait: fail timed wait; error=0x%X", waitResult);
    retVal = (waitResult == 0); //waited successfully
    return retVal;
}


/*******************************************************************************
**
** Function:        notifyOne
**
** Description:     Unblock the waiting thread.
**
** Returns:         None.
**
*******************************************************************************/
void CondVar::notifyOne ()
{
    int const res = pthread_cond_signal (&mCondition);
    if (res)
    {
        ALOGE ("CondVar::notifyOne: fail signal; error=0x%X", res);
    }
}