1
/** gslock - Program to test GSLazyLocks.
2
Copyright (C) 2003 Free Software Foundation, Inc.
4
Written by: David Ayers <d.ayers@inode.at>
6
This file is part of the GNUstep Base Library.
8
This library is free software; you can redistribute it and/or
9
modify it under the terms of the GNU Library General Public
10
License as published by the Free Software Foundation; either
11
version 2 of the License, or (at your option) any later version.
13
This library is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
Library General Public License for more details.
18
You should have received a copy of the GNU Library General Public
19
License along with this library; if not, write to the Free
20
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
24
#include <Foundation/NSAutoreleasePool.h>
25
#include <Foundation/NSArray.h>
26
#include <Foundation/NSValue.h>
27
#include <Foundation/NSString.h>
28
#include <Foundation/NSException.h>
29
#include <Foundation/NSFileHandle.h>
30
#include <Foundation/NSThread.h>
32
#include <GNUstepBase/GSLock.h>
37
GSLazyRecursiveLock *gLock2 = nil;
39
NSConditionLock *cLock = nil;
41
volatile int counter = 0;
42
volatile int threadExitCounter;
48
for (i = 0; i < 5; i++)
49
i = ((i + 1) + (i - 1) / 2);
52
#define NUM_ITERATIONS 10000
54
@interface Tester : NSObject
55
- (void)runTest:(NSString *)ident;
56
- (void)dummy:(id)none;
57
- (void)createNewLockAt:(id)none;
59
@implementation Tester
60
- (void)dummy:(id)none
62
NSLog(@"Multithreaded:%@",[NSThread currentThread]);
64
- (void)runTest:(NSString *)ident
69
NSTimeInterval time = 0;
70
NSAutoreleasePool *pool;
73
pool = [[NSAutoreleasePool alloc] init];
75
makeMulti = ([ident isEqualToString: @"Make Multithreaded GS"]);
77
for (i = 0; i < 100; i++)
79
start = [NSDate date];
80
for (j = 0; j < NUM_ITERATIONS; j++)
89
if (makeMulti && i == 49 )
91
[NSThread detachNewThreadSelector: @selector(dummy:)
104
time += [end timeIntervalSinceDate: start];
106
NSLog(@"End (%@/%@/%@):%f ",
107
[NSThread currentThread], ident, lock, time / 100 );
114
-(void)createNewLockAt:(id)none
118
GS_INITIALIZED_LOCK(gLock1,NSLock);
119
GS_INITIALIZED_LOCK(gLock2,GSLazyRecursiveLock);
121
NSLog(@"Created locks: %@ %@", gLock1, gLock2);
123
[cLock unlockWithCondition: YES];
133
tester = [Tester new];
135
[tester runTest:@"empty"];
137
lock = [GSLazyLock new];
138
[tester runTest:@"single GS"];
140
lock = [GSLazyRecursiveLock new];
141
[tester runTest:@"single (r) GS"];
144
[tester runTest:@"single NS"];
146
lock = [NSRecursiveLock new];
147
[tester runTest:@"single (r) NS"];
149
lock = [GSLazyLock new];
150
[tester runTest:@"Make Multithreaded GS"];
152
/* We are now multithreaded. */
153
NSCAssert1 ([lock class] == [NSLock class],
154
@"Class didn't morph:%@", lock);
156
lock = [GSLazyLock new];
157
NSCAssert1 ([lock class] == [NSLock class],
158
@"Returned wrong lock:%@", lock);
159
/* These tests actually only test NS*Lock locking, but... */
160
[tester runTest:@"multi simple GS"];
162
lock = [GSLazyRecursiveLock new];
163
NSCAssert1 ([lock class] == [NSRecursiveLock class],
164
@"Returned wrong lock:%@", lock);
165
[tester runTest:@"multi simple (r) GS"];
168
[tester runTest:@"multi simple NS"];
170
lock = [NSRecursiveLock new];
171
[tester runTest:@"multi simple NS"];
173
/* Let's test locking anyway while we're at it. */
174
for (threadExitCounter = 0, i = 0; i < 3; i++)
177
ident = [NSString stringWithFormat: @"multi complex (%d)", i];
178
[NSThread detachNewThreadSelector: @selector(runTest:)
183
while (threadExitCounter < 3)
184
[NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 10.0]];
186
NSCAssert1 (counter == NUM_ITERATIONS * 1300,
187
@"Locks broken! %d", counter );
194
Tester *t = [Tester new];
196
cLock = [[NSConditionLock alloc] initWithCondition: NO];
198
[NSThread detachNewThreadSelector: @selector(createNewLockAt:)
202
[cLock lockWhenCondition: YES
203
beforeDate: [NSDate dateWithTimeIntervalSinceNow: 10.0]];
206
NSCAssert1([gLock1 isKindOfClass: [NSLock class]],
207
@"-[NSLock newLockAt:] returned %@", gLock1);
208
NSCAssert1([gLock2 isKindOfClass: [NSRecursiveLock class]],
209
@"-[GSLazyRecursiveLock newLockAt:] returned %@", gLock1);
217
NSAutoreleasePool *pool;
218
[NSAutoreleasePool enableDoubleReleaseCheck:YES];
219
pool = [[NSAutoreleasePool alloc] init];