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