blob: 462df08eec4003d1445c2f49c631caf7ef60f0db [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02004* SPDX-License-Identifier: AGPL-3.0+
dburgess82c46ff2011-10-07 02:40:51 +00005*
6* This software is distributed under the terms of the GNU Affero Public License.
7* See the COPYING file in the main directory for details.
8*
9* This use of this software may be subject to additional restrictions.
10* See the LEGAL file in the main directory for details.
11
12 This program is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Affero General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Affero General Public License for more details.
21
22 You should have received a copy of the GNU Affero General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25*/
26
27
28
29#include "Threads.h"
30#include "Interthread.h"
31#include <iostream>
32
33using namespace std;
34
35
36InterthreadQueue<int> gQ;
37InterthreadMap<int,int> gMap;
38
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010039int q_last_read_val = -1;
40int q_last_write_val;
41int m_last_read_val;
42int m_last_write_val;
43
dburgess82c46ff2011-10-07 02:40:51 +000044void* qWriter(void*)
45{
46 int *p;
47 for (int i=0; i<20; i++) {
48 p = new int;
49 *p = i;
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010050 CERR("queue write " << *p);
dburgess82c46ff2011-10-07 02:40:51 +000051 gQ.write(p);
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010052 q_last_write_val = i;
dburgess82c46ff2011-10-07 02:40:51 +000053 if (random()%2) sleep(1);
54 }
55 p = new int;
56 *p = -1;
57 gQ.write(p);
58 return NULL;
59}
60
61void* qReader(void*)
62{
63 bool done = false;
64 while (!done) {
65 int *p = gQ.read();
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010066 CERR("queue read " << *p);
67 if (*p<0) {
68 assert(q_last_read_val == 19 && *p == -1);
69 done = true;
70 } else {
71 assert(q_last_read_val == *p - 1);
72 q_last_read_val = *p;
73 }
dburgess82c46ff2011-10-07 02:40:51 +000074 delete p;
75 }
76 return NULL;
77}
78
79
80void* mapWriter(void*)
81{
82 int *p;
83 for (int i=0; i<20; i++) {
84 p = new int;
85 *p = i;
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010086 CERR("map write " << *p);
dburgess82c46ff2011-10-07 02:40:51 +000087 gMap.write(i,p);
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010088 m_last_write_val = i;
dburgess82c46ff2011-10-07 02:40:51 +000089 if (random()%2) sleep(1);
90 }
91 return NULL;
92}
93
94void* mapReader(void*)
95{
96 for (int i=0; i<20; i++) {
97 int *p = gMap.read(i);
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +010098 CERR("map read " << *p);
99 assert(*p == i);
100 m_last_read_val = *p;
kurtis.heimerl5a872472013-05-31 21:47:25 +0000101 // InterthreadMap will delete the pointers
102 // delete p;
dburgess82c46ff2011-10-07 02:40:51 +0000103 }
104 return NULL;
105}
106
107
108
109
110
111
112int main(int argc, char *argv[])
113{
114 Thread qReaderThread;
115 qReaderThread.start(qReader,NULL);
116 Thread mapReaderThread;
117 mapReaderThread.start(mapReader,NULL);
118
119 Thread qWriterThread;
120 qWriterThread.start(qWriter,NULL);
121 Thread mapWriterThread;
122 mapWriterThread.start(mapWriter,NULL);
123
124 qReaderThread.join();
125 qWriterThread.join();
126 mapReaderThread.join();
127 mapWriterThread.join();
Pau Espin Pedrol93d9b112018-01-09 18:41:48 +0100128
129 assert(q_last_write_val == 19);
130 assert(q_last_read_val == 19);
131 assert(m_last_write_val == 19);
132 assert(m_last_read_val == 19);
133
134 printf("Done\n");
dburgess82c46ff2011-10-07 02:40:51 +0000135}
136
137
138// vim: ts=4 sw=4