dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: AGPL-3.0+ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 5 | * |
| 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 | |
| 33 | using namespace std; |
| 34 | |
| 35 | |
| 36 | InterthreadQueue<int> gQ; |
| 37 | InterthreadMap<int,int> gMap; |
| 38 | |
Pau Espin Pedrol | 93d9b11 | 2018-01-09 18:41:48 +0100 | [diff] [blame] | 39 | int q_last_read_val = -1; |
| 40 | int q_last_write_val; |
| 41 | int m_last_read_val; |
| 42 | int m_last_write_val; |
| 43 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 44 | void* qWriter(void*) |
| 45 | { |
| 46 | int *p; |
| 47 | for (int i=0; i<20; i++) { |
| 48 | p = new int; |
| 49 | *p = i; |
Pau Espin Pedrol | 93d9b11 | 2018-01-09 18:41:48 +0100 | [diff] [blame] | 50 | CERR("queue write " << *p); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 51 | gQ.write(p); |
Pau Espin Pedrol | 93d9b11 | 2018-01-09 18:41:48 +0100 | [diff] [blame] | 52 | q_last_write_val = i; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 53 | if (random()%2) sleep(1); |
| 54 | } |
| 55 | p = new int; |
| 56 | *p = -1; |
| 57 | gQ.write(p); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | void* qReader(void*) |
| 62 | { |
| 63 | bool done = false; |
| 64 | while (!done) { |
| 65 | int *p = gQ.read(); |
Pau Espin Pedrol | 93d9b11 | 2018-01-09 18:41:48 +0100 | [diff] [blame] | 66 | 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 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 74 | delete p; |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | void* mapWriter(void*) |
| 81 | { |
| 82 | int *p; |
| 83 | for (int i=0; i<20; i++) { |
| 84 | p = new int; |
| 85 | *p = i; |
Pau Espin Pedrol | 93d9b11 | 2018-01-09 18:41:48 +0100 | [diff] [blame] | 86 | CERR("map write " << *p); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 87 | gMap.write(i,p); |
Pau Espin Pedrol | 93d9b11 | 2018-01-09 18:41:48 +0100 | [diff] [blame] | 88 | m_last_write_val = i; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 89 | if (random()%2) sleep(1); |
| 90 | } |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | void* mapReader(void*) |
| 95 | { |
| 96 | for (int i=0; i<20; i++) { |
| 97 | int *p = gMap.read(i); |
Pau Espin Pedrol | 93d9b11 | 2018-01-09 18:41:48 +0100 | [diff] [blame] | 98 | CERR("map read " << *p); |
| 99 | assert(*p == i); |
| 100 | m_last_read_val = *p; |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 101 | // InterthreadMap will delete the pointers |
| 102 | // delete p; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 103 | } |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | int 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 Pedrol | 93d9b11 | 2018-01-09 18:41:48 +0100 | [diff] [blame] | 128 | |
| 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"); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
| 138 | // vim: ts=4 sw=4 |