~ubuntu-branches/ubuntu/karmic/gnustep-base/karmic

« back to all changes in this revision

Viewing changes to Testing/gslock.m

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2005-04-17 00:14:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050417001438-enf0y07c9tku85z1
Tags: 1.10.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** gslock - Program to test GSLazyLocks.
 
2
   Copyright (C) 2003 Free Software Foundation, Inc.
 
3
 
 
4
   Written by:  David Ayers  <d.ayers@inode.at>
 
5
 
 
6
   This file is part of the GNUstep Base Library.
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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.
 
21
*/
 
22
 
 
23
 
 
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>
 
31
 
 
32
#include <GNUstepBase/GSLock.h>
 
33
 
 
34
NSLock          *lock = nil;
 
35
 
 
36
NSLock              *gLock1 = nil;
 
37
GSLazyRecursiveLock *gLock2 = nil;
 
38
 
 
39
NSConditionLock     *cLock = nil;
 
40
 
 
41
volatile int counter = 0;
 
42
volatile int threadExitCounter;
 
43
 
 
44
void
 
45
wait_a_while ()
 
46
{
 
47
  volatile int i;
 
48
  for (i = 0; i < 5; i++)
 
49
    i = ((i + 1) + (i - 1) / 2);
 
50
}
 
51
 
 
52
#define NUM_ITERATIONS 10000
 
53
 
 
54
@interface Tester : NSObject
 
55
- (void)runTest:(NSString *)ident;
 
56
- (void)dummy:(id)none;
 
57
- (void)createNewLockAt:(id)none;
 
58
@end
 
59
@implementation Tester
 
60
- (void)dummy:(id)none
 
61
{
 
62
  NSLog(@"Multithreaded:%@",[NSThread currentThread]);
 
63
}
 
64
- (void)runTest:(NSString *)ident
 
65
{
 
66
  NSDate *start;
 
67
  NSDate *end;
 
68
  int i,j;
 
69
  NSTimeInterval time = 0;
 
70
  NSAutoreleasePool *pool;
 
71
  BOOL makeMulti;
 
72
 
 
73
  pool = [[NSAutoreleasePool alloc] init];
 
74
 
 
75
  makeMulti = ([ident isEqualToString: @"Make Multithreaded GS"]);
 
76
 
 
77
  for (i = 0; i < 100; i++)
 
78
    {
 
79
      start = [NSDate date];
 
80
      for (j = 0; j < NUM_ITERATIONS; j++)
 
81
        {
 
82
          volatile int temp;
 
83
 
 
84
          [lock lock];
 
85
 
 
86
          temp = counter;
 
87
          wait_a_while ();
 
88
 
 
89
          if (makeMulti && i == 49 )
 
90
            {
 
91
              [NSThread detachNewThreadSelector: @selector(dummy:)
 
92
                        toTarget: self
 
93
                        withObject: nil];
 
94
              makeMulti = NO;
 
95
            }
 
96
 
 
97
 
 
98
          counter =  temp + 1;
 
99
          wait_a_while ();
 
100
 
 
101
          [lock unlock];
 
102
        }
 
103
      end = [NSDate date];
 
104
      time += [end timeIntervalSinceDate: start];
 
105
    }
 
106
  NSLog(@"End (%@/%@/%@):%f ",
 
107
        [NSThread currentThread], ident, lock, time / 100 );
 
108
 
 
109
  threadExitCounter++;
 
110
 
 
111
  [pool release];
 
112
}
 
113
 
 
114
-(void)createNewLockAt:(id)none
 
115
{
 
116
  [cLock lock];
 
117
 
 
118
  GS_INITIALIZED_LOCK(gLock1,NSLock);
 
119
  GS_INITIALIZED_LOCK(gLock2,GSLazyRecursiveLock);
 
120
 
 
121
  NSLog(@"Created locks: %@ %@", gLock1, gLock2);
 
122
 
 
123
  [cLock unlockWithCondition: YES];
 
124
}
 
125
@end
 
126
 
 
127
void
 
128
test_lazyLocks()
 
129
{
 
130
  Tester *tester;
 
131
  int i;
 
132
 
 
133
  tester = [Tester new];
 
134
 
 
135
  [tester runTest:@"empty"];
 
136
 
 
137
  lock = [GSLazyLock new];
 
138
  [tester runTest:@"single GS"];
 
139
 
 
140
  lock = [GSLazyRecursiveLock new];
 
141
  [tester runTest:@"single (r) GS"];
 
142
 
 
143
  lock = [NSLock new];
 
144
  [tester runTest:@"single NS"];
 
145
 
 
146
  lock = [NSRecursiveLock new];
 
147
  [tester runTest:@"single (r) NS"];
 
148
 
 
149
  lock = [GSLazyLock new];
 
150
  [tester runTest:@"Make Multithreaded GS"];
 
151
 
 
152
  /* We are now multithreaded.  */
 
153
  NSCAssert1 ([lock class] == [NSLock class],
 
154
              @"Class didn't morph:%@", lock);
 
155
 
 
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"];
 
161
 
 
162
  lock = [GSLazyRecursiveLock new];
 
163
  NSCAssert1 ([lock class] == [NSRecursiveLock class],
 
164
              @"Returned wrong lock:%@", lock);
 
165
  [tester runTest:@"multi simple (r) GS"];
 
166
 
 
167
  lock = [NSLock new];
 
168
  [tester runTest:@"multi simple NS"];
 
169
 
 
170
  lock = [NSRecursiveLock new];
 
171
  [tester runTest:@"multi simple NS"];
 
172
 
 
173
  /* Let's test locking anyway while we're at it. */
 
174
  for (threadExitCounter = 0, i = 0; i < 3; i++)
 
175
    {
 
176
      NSString *ident;
 
177
      ident = [NSString stringWithFormat: @"multi complex (%d)", i];
 
178
      [NSThread detachNewThreadSelector: @selector(runTest:)
 
179
                toTarget: tester
 
180
                withObject: ident];
 
181
    }
 
182
 
 
183
  while (threadExitCounter < 3)
 
184
    [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 10.0]];
 
185
 
 
186
  NSCAssert1 (counter == NUM_ITERATIONS * 1300,
 
187
              @"Locks broken! %d", counter );
 
188
 
 
189
}
 
190
 
 
191
void
 
192
test_newLockAt(void)
 
193
{
 
194
  Tester *t = [Tester new];
 
195
 
 
196
  cLock = [[NSConditionLock alloc] initWithCondition: NO];
 
197
 
 
198
  [NSThread detachNewThreadSelector: @selector(createNewLockAt:)
 
199
            toTarget: t
 
200
            withObject: nil];
 
201
 
 
202
  [cLock lockWhenCondition: YES
 
203
         beforeDate: [NSDate dateWithTimeIntervalSinceNow: 10.0]];
 
204
  [cLock unlock];
 
205
 
 
206
  NSCAssert1([gLock1 isKindOfClass: [NSLock class]],
 
207
             @"-[NSLock newLockAt:] returned %@", gLock1);
 
208
  NSCAssert1([gLock2 isKindOfClass: [NSRecursiveLock class]],
 
209
             @"-[GSLazyRecursiveLock newLockAt:] returned %@", gLock1);
 
210
 
 
211
}
 
212
 
 
213
 
 
214
int
 
215
main()
 
216
{
 
217
  NSAutoreleasePool *pool;
 
218
  [NSAutoreleasePool enableDoubleReleaseCheck:YES];
 
219
  pool = [[NSAutoreleasePool alloc] init];
 
220
 
 
221
  test_lazyLocks();
 
222
  test_newLockAt();
 
223
 
 
224
  [pool release];
 
225
 
 
226
  exit(0);
 
227
}