w1146869587
2021-03-25 b741dbdd2b89eee17d47d42ccf7d08addc1881b0
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// OISemaphore.cpp : implementation file
//
 
#include "stdafx.h"
#include "OISemaphore.h"
 
#include "OIThread.h"
 
namespace OIComm {
 
/////////////////////////////////////////////////////////////////////////////
// COISemaphore
 
OISemaphoreHandle COISemaphore::GetHandle()
{
    return    m_hSem;
}
 
// Windows ---------------------------------------------------------------
#ifdef _WIN32
 
COISemaphore::COISemaphore( oi_value_t nInitVal )
{
    m_hSem    = ::CreateSemaphore( (LPSECURITY_ATTRIBUTES)NULL, nInitVal, 1, (LPCTSTR)NULL );
}
 
COISemaphore::~COISemaphore()
{
    ::CloseHandle( m_hSem );
}
 
int COISemaphore::Post()
{
    return (::ReleaseSemaphore( m_hSem, 1, (LPLONG)NULL ) != 0) ? 0 : -1;
}
 
int COISemaphore::Wait()
{
//    return (WaitForSingleObject( m_handle, INFINITE ) == WAIT_OBJECT_0) ? 0 : -1;
    return    Wait( INFINITE );
}
 
int COISemaphore::Wait( DWORD dwTimeOutMillis )
{
    return (WaitForSingleObject( m_hSem, dwTimeOutMillis ) == WAIT_OBJECT_0) ? 0 : -1;
}
 
int COISemaphore::TryWait()
{
    return -1; // %! not implemented
}
 
int COISemaphore::GetValue( int &nValue )
{
    return 0; // %! not implemented
}
 
int COISemaphore::WaitForMultipleObjects( DWORD dwCount, COISemaphore *lpSem[], bool bWaitAll, DWORD dwMilliSeconds )
{
    DWORD                dwRet;
    OISemaphoreHandle    *lpHandle    = new OISemaphoreHandle[dwCount];
 
    for ( DWORD dwIndex = 0; dwIndex < dwCount; dwIndex++ )
        lpHandle[dwIndex]    = lpSem[dwIndex]->GetHandle();
 
    dwRet    = ::WaitForMultipleObjects( dwCount, lpHandle, bWaitAll, dwMilliSeconds );
    delete [] lpHandle;
    if ( WAIT_TIMEOUT == dwRet )
        return    -1;
 
    return    (dwRet - WAIT_OBJECT_0);
}
 
// Linux ---------------------------------------------------------------
#else
 
COISemaphore::COISemaphore( oi_value_t nInitVal )
{
    sem_init( &m_hSem, 0, nInitVal );
}
 
COISemaphore::~COISemaphore()
{
    sem_destroy( &m_hSem );
}
 
int COISemaphore::Post()
{
    return sem_post( &m_hSem );
}
 
int COISemaphore::Wait()
{
    return sem_wait( &m_hSem );
}
 
int COISemaphore::Wait( DWORD dwTimeOutMillis )
{
    struct timespec    ts;
    long    nSecs;
    long    nAdd;
 
    clock_gettime( CLOCK_REALTIME, &ts );
    nSecs    = dwTimeOutMillis / 1000;
    dwTimeOutMillis    = dwTimeOutMillis % 1000;
    
    dwTimeOutMillis    = dwTimeOutMillis * 1000 * 1000 + ts.tv_nsec;
    nAdd    = dwTimeOutMillis / (1000 * 1000 * 1000);
    ts.tv_sec += (nAdd + nSecs);
    ts.tv_nsec = dwTimeOutMillis % (1000 * 1000 * 1000);
 
    return sem_timedwait( &m_hSem, &ts );
}
 
int COISemaphore::TryWait()
{
    return sem_trywait( &m_hSem );
}
 
int COISemaphore::GetValue( int &nValue )
{
    return sem_getvalue( &m_hSem, &nValue );
}
 
int COISemaphore::WaitForMultipleObjects( DWORD dwCount, COISemaphore *lpSem[], bool bWaitAll, DWORD dwMilliSeconds )
{
    int                    nRet;
    COISemaphore        sem;
    DWORD                dwIndex;
    COICriticalSection    cs;
    int                    nReleaseIndex    = -1;    // Which is first release, For WaitAll is false
    int                    nReleaseCount    = 0;    // release count, For WaitAll is true
    COIWaitForThread    *lpThread;
    COIWaitForThreadList            listThread;
    COIWaitForThreadList::iterator    it;
 
    // Create thread
    for ( dwIndex = 0; dwIndex < dwCount; dwIndex++ )
    {
        lpThread    = new COIWaitForThread( cs );
        lpThread->m_pWaitSem        = lpSem[dwIndex];
        lpThread->m_bWaitAll        = bWaitAll;
        lpThread->m_pReleaseIndex    = &nReleaseIndex;
        lpThread->m_pReleaseCount    = &nReleaseCount;
        lpThread->m_dwIndex            = dwIndex;
        lpThread->m_dwCount            = dwCount;
 
        lpThread->m_pFinishSem        = &sem;
        listThread.push_back( lpThread );
        lpThread->Start();
    } // End of for ( dwIndex = 0; dwIndex < dwCount; dwIndex++ )
 
    if ( INFINITE == dwMilliSeconds )
        nRet    = sem.Wait();
    else
        nRet    = sem.Wait( dwMilliSeconds );
    // Release thread
    it    = listThread.begin();
    for ( dwIndex = 0; dwIndex < dwCount; dwIndex++ )
    {
        lpThread    = *it;
 
        // Release sem
        lpSem[dwIndex]->Post();
        // Wait for thread end
        lpThread->Wait( 100 );
        // delete thread
        delete    lpThread;
    }
    listThread.clear();
 
    if ( 0 == nRet )
    {
        if ( bWaitAll )
            return    0;
        else
            return    nReleaseIndex;
    }
 
    return    -1;
}
 
#endif // End of #ifdef _WIN32
 
} // End of namespace OIComm