| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2017 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <smooth.h> |
| 14 | #include <smooth/main.h> |
| 15 | |
| 16 | #include <boca.h> |
| 17 | |
| 18 | #include <time.h> |
| 19 | |
| 20 | using namespace smooth; |
| 21 | using namespace smooth::Threads; |
| 22 | |
| 23 | using namespace BoCA; |
| 24 | |
| 25 | /* This tests thread safety of the PictureData store. |
| 26 | */ |
| 27 | static int srandCount = 0; |
| 28 | |
| 29 | static const Int numThreads = 4; |
| 30 | static const Int numPictures = 100; |
| 31 | static const Int bufferSize = 100; |
| 32 | |
| 33 | Void Worker() |
| 34 | { |
| 35 | srand(time(0) + srandCount++); |
| 36 | |
| 37 | /* Allocate picture data. |
| 38 | */ |
| 39 | Array<PictureData *> pictures; |
| 40 | Buffer<UnsignedByte> buffer(bufferSize); |
| 41 | |
| 42 | for (Int i = 0; i < numPictures; i++) |
| 43 | { |
| 44 | for (Int i = 0; i < bufferSize; i++) buffer[i] = rand() % 256; |
| 45 | |
| 46 | PictureData *picture = new PictureData(); |
| 47 | |
| 48 | picture->Set(buffer, buffer.Size()); |
| 49 | |
| 50 | pictures.Add(picture); |
| 51 | } |
| 52 | |
| 53 | /* Do some stuff. |
| 54 | */ |
| 55 | for (Int i = 0; i < 100000; i++) |
| 56 | { |
| 57 | Int n = rand() % numPictures; |
| 58 | PictureData *picture = pictures.GetNth(n); |
| 59 | |
| 60 | picture->Size(); |
| 61 | |
| 62 | if (*picture != *pictures.GetNth(rand() % numPictures)) |
| 63 | { |
| 64 | for (Int i = 0; i < bufferSize; i++) buffer[i] = ((const Buffer<UnsignedByte> &) *picture)[i]; |
| 65 | for (Int i = 0; i < bufferSize; i++) buffer[i] = ((const UnsignedByte *) *picture)[i]; |
| 66 | |
| 67 | delete picture; |
| 68 | |
| 69 | pictures.RemoveNth(n); |
| 70 | |
| 71 | picture = new PictureData(); |
| 72 | picture->Set(buffer, buffer.Size()); |
| 73 | |
| 74 | pictures.Add(picture); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* Free picture data. |
| 79 | */ |
| 80 | foreach (PictureData *picture, pictures) delete picture; |
| 81 | |
| 82 | pictures.RemoveAll(); |
| 83 | } |
| 84 | |
| 85 | /* Start threads and wait. |
| 86 | */ |
| 87 | Int S::Main() |
| 88 | { |
| 89 | Array<Thread *> threads; |
| 90 | |
| 91 | for (Int i = 0; i < numThreads; i++) threads.Add(NonBlocking0<Void>(&Worker).Call()); |
| 92 | for (Int i = 0; i < numThreads; i++) threads.GetNth(i)->Wait(); |
| 93 | for (Int i = 0; i < numThreads; i++) delete threads.GetNth(i); |
| 94 | |
| 95 | threads.RemoveAll(); |
| 96 | |
| 97 | return 0; |
| 98 | } |