File: lazy_thread.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 (69 lines) | stat: -rw-r--r-- 1,933 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
#import "ObjectTesting.h"
#import <Foundation/NSThread.h>

#if defined(_WIN32)
#include <process.h>
#else
#include <pthread.h>
#endif

static NSThread *threadResult = nil;

#if defined(_WIN32)
unsigned int __stdcall
#else
void *
#endif
thread(void *ignored)
{
  threadResult = [NSThread currentThread];
  return 0;
}

int main(void)
{
  NSAutoreleasePool *arp = [NSAutoreleasePool new];
  
#if defined(_WIN32)
  HANDLE thr;
  thr = (HANDLE)_beginthreadex(NULL, 0, thread, NULL, 0, NULL);
  WaitForSingleObject(thr, INFINITE);
  CloseHandle(thr);
#else
  pthread_t thr;
  pthread_create(&thr, NULL, thread, NULL);
  pthread_join(thr, NULL);
#endif
  PASS(threadResult != 0, "NSThread lazily created from native thread");
  testHopeful = YES;
  PASS((threadResult != 0) && (threadResult != [NSThread mainThread]),
    "Spawned thread is not main thread");

#if defined(_WIN32)
  thr = (HANDLE)_beginthreadex(NULL, 0, thread, NULL, 0, NULL);
  WaitForSingleObject(thr, INFINITE);
  CloseHandle(thr);
#else
  pthread_create(&thr, NULL, thread, NULL);
  pthread_join(thr, NULL);
#endif
  PASS(threadResult != 0, "NSThread lazily created from native thread");
  PASS((threadResult != 0) && (threadResult != [NSThread mainThread]),
    "Spawned thread is not main thread");

  NSThread *t = [NSThread currentThread];
  [t setName: @"xxxtestxxx"];
  NSLog(@"Thread description is '%@'", t);
  NSRange r = [[t description] rangeOfString: @"name = xxxtestxxx"];
  PASS(r.length > 0, "thread description contains name");
  
  PASS([NSThread threadPriority] == 0.5, "default thread priority is 0.5");
  PASS([NSThread setThreadPriority:0.8], "change thread priority to 0.8");
  PASS([NSThread threadPriority] == 0.8, "thread priority was changed to 0.8");
  PASS([NSThread setThreadPriority:0.2], "change thread priority to 0.2");
  PASS([NSThread threadPriority] == 0.2, "thread priority was changed to 0.2");
  
  DESTROY(arp);
  return 0;
}