summaryrefslogtreecommitdiff
path: root/regress/gnu/lib/libobjc/thread/thread.m
blob: 3610ffb2a78c21863b530840bc566989f3012be6 (plain)
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
/* Written by David Wetzel */

#include <assert.h>
#include <stdio.h>
#include <unistd.h>

#include <objc/objc.h>
#include <objc/objc-api.h>
#include <objc/Object.h>

static const int debug = 0;

@interface MyClass : Object
{
  char *myName;
}
-(void)setMyName:(const char *)n;
-(const char *)myName;
@end

@implementation MyClass
-(void)setMyName:(const char *)n
{
    size_t len;

    if (myName) {
	    if (strcmp(myName, n) != 0)
		    return;
	    objc_free(myName);
    }
    len = strlen(n) + 1;
    myName = objc_malloc(len);
    strcpy(myName, n);
}
-(char *)myName
{
    return myName;
}
-(void)start
{
	if (debug)
		printf("detached thread started!\n");
}
@end

static void
becomeMultiThreaded(void)
{
	if (debug)
		printf("becoming multithreaded!\n");
}

int
main(int argc, char *argv[])
{
	id o = [MyClass new];
	const char *c;
	objc_thread_callback cb;
	objc_thread_t rv;

	[o setMyName:"thread"];
	c = [o myName];

	if (debug)
		printf("Testing: %s\n",c);

	cb = objc_set_thread_callback(becomeMultiThreaded);
	if (debug)
		printf("Old Callback: %p\n",cb);

	rv = objc_thread_detach(@selector(start), o, nil);
	if (debug)
		printf("detach value: %p\n",rv);
	assert(rv != NULL);

	return 0;
}