summaryrefslogtreecommitdiffstats
path: root/lib/libUPnP/Neptune/Source/Tests/Queue1/QueueTest1.cpp
blob: 6afa53596a63da8823c7d8f874c7f7f225fbaf29 (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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*****************************************************************
|
|      Queue Test Program 1
|
|      (c) 2008 Gilles Boccon-Gibod
|      Author: Gilles Boccon-Gibod (bok@bok.net)
|
 ****************************************************************/

/*----------------------------------------------------------------------
|       includes
+---------------------------------------------------------------------*/
#include "Neptune.h"

#if defined(WIN32) && defined(_DEBUG)
#include <crtdbg.h>
#endif

#define CHECK(x) {                                  \
    if (!(x)) {                                     \
        printf("TEST FAILED line %d\n", __LINE__);  \
        NPT_ASSERT(0);                              \
    }                                               \
}

/*----------------------------------------------------------------------
|       Item
+---------------------------------------------------------------------*/
class Item
{
public:
    typedef enum {
        MSG_INCREMENT_COUNTER,
        MSG_CHANGE_TIMEOUT,
        MSG_TERMINATE
    } Message;
    
    Item(Message msg) : m_Message(msg) {}
    
    Message m_Message;
};

/*----------------------------------------------------------------------
|       WriterThread
+---------------------------------------------------------------------*/
class WriterThread : public NPT_Thread
{
public:
    WriterThread(NPT_Queue<Item>& queue, const char* name) : 
        m_Queue(queue), m_Name(name), m_Counter(0) {}
    
    void Run() {
        NPT_Debug("WRITER %s starting ++++++++++++++++++++++++\n", (const char*)m_Name);

        for (int i=0; i<1000; i++) {
            if (i%5 == 0) {
                NPT_Debug("WRITER %s post: change timeout\n", (const char*)m_Name);
                m_Queue.Push(new Item(Item::MSG_CHANGE_TIMEOUT));
            } 
            m_Queue.Push(new Item(Item::MSG_INCREMENT_COUNTER));
            if (i%3 == 0) {
                NPT_Debug("WRITER %s sleeping\n", (const char*)m_Name);
                NPT_System::Sleep(NPT_TimeInterval(0.01f));
            }
        }

        NPT_Debug("WRITER %s terminating ----------------------\n", (const char*)m_Name);
    }
    
    NPT_Queue<Item>& m_Queue;
    NPT_String       m_Name;
    unsigned int     m_Counter;
};

/*----------------------------------------------------------------------
|       ReaderThread
+---------------------------------------------------------------------*/
class ReaderThread : public NPT_Thread
{
public:
    ReaderThread(NPT_Queue<Item>& queue, const char* name, NPT_TimeInterval sleep_time) : 
        m_Queue(queue), m_Name(name), m_Counter(0), m_NbTimeouts(0), m_SleepTime(sleep_time) {}
    
    void Run() {
        NPT_Debug("READER %s starting =====================\n", (const char*)m_Name);
        NPT_Timeout timeout = NPT_TIMEOUT_INFINITE;
        for (;;) {
            if (m_SleepTime.ToNanos() != 0) {
                NPT_Debug("READER %s sleeping...\n", (const char*)m_Name);
                NPT_System::Sleep(m_SleepTime);
                NPT_Debug("READER %s woke up!\n", (const char*)m_Name);
            }
            
            Item* item = NULL;
            NPT_Result result = m_Queue.Pop(item, timeout);
            if (NPT_SUCCEEDED(result)) {
                CHECK(item != NULL);
                Item::Message msg = item->m_Message;
                delete item;
                switch (msg) {
                    case Item::MSG_INCREMENT_COUNTER:
                        ++m_Counter;
                        NPT_Debug("READER %s new counter=%d\n", (const char*)m_Name, m_Counter);
                        break;
                        
                    case Item::MSG_CHANGE_TIMEOUT:
                        if (timeout == 0) {
                            timeout = 15;
                        } else if (timeout == 15) {
                            timeout = NPT_TIMEOUT_INFINITE;
                        } else {
                            timeout = 0;
                        }
                        NPT_Debug("READER %s new timeout=%d\n", (const char*)m_Name, timeout);
                        break;
                        
                    case Item::MSG_TERMINATE:
                        NPT_Debug("READER %s terminating #######################\n", (const char*)m_Name);
                        return;
                }
            } else {
                NPT_Debug("READER %s pop returned %d\n", (const char*)m_Name, result);
                if (timeout == 0) {
                    CHECK(result == NPT_ERROR_LIST_EMPTY);
                    NPT_System::Sleep(0.01f);
                } else if (timeout != NPT_TIMEOUT_INFINITE) {
                    CHECK(result == NPT_ERROR_TIMEOUT);
                    ++m_NbTimeouts;
                } else {
                    NPT_ASSERT(0);
                }
            }
        }
    }

    NPT_Queue<Item>& m_Queue;
    NPT_String       m_Name;
    unsigned int     m_Counter;
    unsigned int     m_NbTimeouts;
    NPT_TimeInterval m_SleepTime;
};

/*----------------------------------------------------------------------
|       Test1
+---------------------------------------------------------------------*/
static void
Test1()
{
    // create a queue
    NPT_Queue<Item> queue;
    
    // create 2 writers
    NPT_Debug("creating writer 1\n");
    WriterThread writer1(queue, "1"); writer1.Start();
    NPT_System::Sleep(NPT_TimeInterval(0.3f));

    NPT_Debug("creating writer 2\n");
    WriterThread writer2(queue, "2"); writer2.Start();
    NPT_System::Sleep(NPT_TimeInterval(0.3f));

    // create 4 readers
    NPT_Debug("creating reader 1\n");
    ReaderThread reader1(queue, "1", NPT_TimeInterval(0.0f)); reader1.Start();
    NPT_System::Sleep(NPT_TimeInterval(0.3f));

    NPT_Debug("creating reader 2\n");
    ReaderThread reader2(queue, "2", NPT_TimeInterval(0.0f)); reader2.Start();
    NPT_System::Sleep(NPT_TimeInterval(0.3f));

    NPT_Debug("creating reader 3\n");
    ReaderThread reader3(queue, "3", NPT_TimeInterval(0.0f)); reader3.Start();
    NPT_System::Sleep(NPT_TimeInterval(0.3f));

    NPT_Debug("creating reader 4\n");
    ReaderThread reader4(queue, "4", NPT_TimeInterval(0.0f)); reader4.Start();
    NPT_System::Sleep(NPT_TimeInterval(0.3f));


    // wait for the writers
    NPT_Result result;
    NPT_Debug("Waiting for Writer 1 *********************\n");
    result = writer1.Wait();
    NPT_Debug("Writer 1 done *********************\n");
    CHECK(result == NPT_SUCCESS);
    NPT_Debug("Waiting for Writer 2 *********************\n");
    result = writer2.Wait();
    NPT_Debug("Writer 1 done *********************\n");

    // post 4 termination messages
    queue.Push(new Item(Item::MSG_TERMINATE));
    queue.Push(new Item(Item::MSG_TERMINATE));
    queue.Push(new Item(Item::MSG_TERMINATE));
    queue.Push(new Item(Item::MSG_TERMINATE));

    // wait for the readers
    CHECK(result == NPT_SUCCESS);
    NPT_Debug("Waiting for Reader 1 *********************\n");
    result = reader1.Wait();
    NPT_Debug("Reader 1 done *********************\n");
    CHECK(result == NPT_SUCCESS);
    NPT_Debug("Waiting for Reader 2 *********************\n");
    result = reader2.Wait();
    NPT_Debug("Reader 2 done *********************\n");
    CHECK(result == NPT_SUCCESS);
    NPT_Debug("Waiting for Reader 3 *********************\n");
    result = reader3.Wait();
    NPT_Debug("Reader 3 done *********************\n");
    CHECK(result == NPT_SUCCESS);
    NPT_Debug("Waiting for Reader 4 *********************\n");
    result = reader4.Wait();    
    NPT_Debug("Reader 4 done *********************\n");
    CHECK(result == NPT_SUCCESS);
    
    // check counters
    unsigned int total = reader1.m_Counter+reader2.m_Counter+reader3.m_Counter+reader4.m_Counter;
    CHECK(total == 2*1000);
}

/*----------------------------------------------------------------------
|       Test2
+---------------------------------------------------------------------*/
static void
Test2()
{
    // create a queue
    NPT_Queue<Item> queue;
    
    // create 2 readers
    NPT_Debug("creating reader 1\n");
    ReaderThread reader1(queue, "1", NPT_TimeInterval(0.05f)); reader1.Start();

    NPT_Debug("creating reader 2\n");
    ReaderThread reader2(queue, "2", NPT_TimeInterval(0.065f)); reader2.Start();

    for (int i=0; i<30; i++) {
        queue.Push(new Item(Item::MSG_INCREMENT_COUNTER));
    }
    queue.Push(new Item(Item::MSG_TERMINATE));
    for (int i=0; i<30; i++) {
        queue.Push(new Item(Item::MSG_INCREMENT_COUNTER));
    }
    queue.Push(new Item(Item::MSG_TERMINATE));
    
    NPT_Result result;
    NPT_Debug("Waiting for Reader 1 *********************\n");
    result = reader1.Wait();
    NPT_Debug("Reader 1 done *********************\n");
    CHECK(result == NPT_SUCCESS);
    NPT_Debug("Waiting for Reader 1 *********************\n");
    result = reader2.Wait();
    NPT_Debug("Reader 2 done *********************\n");
    CHECK(result == NPT_SUCCESS);
    
    // check counters
    unsigned int total = reader1.m_Counter+reader2.m_Counter;
    CHECK(total == 60);
}

#if defined(WIN32) && defined(_DEBUG)
static int AllocHook( int allocType, void *userData, size_t size, int blockType, 
                     long requestNumber, const unsigned char *filename, int lineNumber)
{
    (void)allocType;
    (void)userData;
    (void)size;
    (void)blockType;
    (void)requestNumber;
    (void)lineNumber;
    (void)filename;
    return 1;
}
#endif

/*----------------------------------------------------------------------
|       main
+---------------------------------------------------------------------*/
int
main(int argc, char** argv) 
{
    NPT_COMPILER_UNUSED(argc);
    NPT_COMPILER_UNUSED(argv);

#if defined(WIN32) && defined(_DEBUG)
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF    |
                   _CRTDBG_CHECK_ALWAYS_DF |
                   _CRTDBG_LEAK_CHECK_DF);
    _CrtSetAllocHook(AllocHook);
#endif

    Test1();
    Test2();
    
    NPT_Debug("- program done -\n");

    return 0;
}