File: doubleLocking.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (95 lines) | stat: -rw-r--r-- 2,995 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#import <Foundation/Foundation.h>
#import "Testing.h"
#import "ObjectTesting.h"

int main()
{
  NSAutoreleasePool	*arp = [NSAutoreleasePool new];
  NSFileManager		*mgr;
  NSString		*helpers;
  NSString		*command;
  NSTask		*task;
  NSPipe		*ePipe;
  NSFileHandle		*hdl;
  NSData		*data;
  NSString		*string;
  NSLock 		*lock = nil;
  unsigned		count;

  mgr = [NSFileManager defaultManager];
  helpers = [mgr currentDirectoryPath];
  helpers = [helpers stringByAppendingPathComponent: @"Helpers"];
  helpers = [helpers stringByAppendingPathComponent: @"obj"];

  command = [helpers stringByAppendingPathComponent: @"doubleNSLock"];
  task = AUTORELEASE([[NSTask alloc] init]);
  ePipe = [NSPipe pipe];
  [task setLaunchPath: command];
  [task setStandardError: ePipe]; 
  hdl = [ePipe fileHandleForReading];
  [task launch];
  for (count = 0; count < 10 && [task isRunning]; count++)
    {
      [NSThread sleepForTimeInterval: 1.0];
    }
  data = [hdl availableData];
  NSLog(@"Data was %*.*s", [data length], [data length], [data bytes]);
  string = AUTORELEASE([[NSString alloc]
    initWithData: data encoding: NSISOLatin1StringEncoding]);
  PASS([string rangeOfString: @"deadlock"].length > 0,
    "NSLock reported deadlock as expected");
  if (NO == testPassed)
    {
      PASS(count == 10, "NSLock seems to have deadlocked as expected")
     [task terminate];
    }
  [task waitUntilExit];

  command = [helpers stringByAppendingPathComponent: @"doubleNSConditionLock"];
  task = AUTORELEASE([[NSTask alloc] init]);
  ePipe = [NSPipe pipe];
  [task setLaunchPath: command];
  [task setStandardError: ePipe]; 
  hdl = [ePipe fileHandleForReading];
  [task launch];
  for (count = 0; count < 10 && [task isRunning]; count++)
    {
      [NSThread sleepForTimeInterval: 1.0];
    }
  data = [hdl availableData];
  NSLog(@"Data was %*.*s", [data length], [data length], [data bytes]);
  string = AUTORELEASE([[NSString alloc]
    initWithData: data encoding: NSISOLatin1StringEncoding]);
  PASS([string rangeOfString: @"deadlock"].length > 0,
    "NSConditionLock reported deadlock as expected");
  if (NO == testPassed)
    {
      PASS(count == 10, "NSConditionLock seems to have deadlocked as expected")
      [task terminate];
    }
  [task waitUntilExit];

  lock = AUTORELEASE([NSRecursiveLock new]);
  [lock lock];
  [lock lock];
  [lock unlock];
  [lock unlock];

  lock = AUTORELEASE([NSLock new]);
  PASS([lock tryLock] == YES, "NSLock can tryLock");
  PASS([lock tryLock] == NO, "NSLock says NO for recursive tryLock");
  [lock unlock];

  lock = AUTORELEASE([NSConditionLock new]);
  PASS([lock tryLock] == YES, "NSConditionLock can tryLock");
  PASS([lock tryLock] == NO, "NSConditionLock says NO for recursive tryLock");
  [lock unlock];

  lock = AUTORELEASE([NSRecursiveLock new]);
  PASS([lock tryLock] == YES, "NSRecursiveLock can tryLock");
  PASS([lock tryLock] == YES, "NSRecursiveLock says YES for recursive tryLock");
  [lock unlock];

  [arp release];
  return 0;
}