1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23#include <stdio.h>  // fprintf(), NULL
24#include <stdlib.h> // exit(), EXIT_SUCCESS
25#include <pthread.h>
26
27#include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
28
29__attribute__((weak))
30void foobar() {
31}
32
33__thread int a;
34__thread int b = 5;
35__thread static int c;
36__thread static int d = 5;
37
38
39static void* work(void* arg)
40{
41  uintptr_t offset = (uintptr_t)arg;
42	//fprintf(stderr, "self=%p, &a=%p\n", pthread_self(), get_a());
43  void* result = malloc(10);
44	if ( a != 0 ) {
45		FAIL("tlv-basic: a non-zero on slow-path");
46		exit(0);
47	}
48	if ( b != 5 ) {
49		FAIL("tlv-basic: b not five");
50		exit(0);
51	}
52
53	if ( c != 0 ) {
54		FAIL("tlv-basic: c non-zero");
55		exit(0);
56	}
57
58	if ( d != 5 ) {
59		FAIL("tlv-basic: gd not five");
60		exit(0);
61	}
62
63  for(int i=0; i < 10000; ++i) {
64    a = 3 + offset + i;
65    b = 7 + offset + i;
66    c = 11 + offset + i;
67    d = 13 + offset + i;
68    foobar();
69    if ( a != 3 + offset + i ) {
70      FAIL("tlv-basic: a not three");
71      exit(0);
72    }
73    if ( b != 7 + offset + i ) {
74      FAIL("tlv-basic: b not seven");
75      exit(0);
76    }
77    if ( c != 11 + offset + i ) {
78      FAIL("tlv-basic: c not eleven");
79      exit(0);
80    }
81    if ( d != 13 + offset + i ) {
82      FAIL("tlv-basic: d not thirteen");
83      exit(0);
84    }
85  }
86
87	return result;
88}
89
90int main()
91{
92	pthread_t worker1;
93	if ( pthread_create(&worker1, NULL, work, (void*)1) != 0 ) {
94		FAIL("pthread_create failed");
95		exit(0);
96	}
97
98	pthread_t worker2;
99	if ( pthread_create(&worker2, NULL, work, (void*)10) != 0 ) {
100		FAIL("pthread_create failed");
101		exit(0);
102	}
103
104	pthread_t worker3;
105	if ( pthread_create(&worker3, NULL, work, (void*)100) != 0 ) {
106		FAIL("pthread_create failed");
107		exit(0);
108	}
109
110	void* result;
111	//fprintf(stderr, "waiting for worker 1\n");
112	pthread_join(worker1, &result);
113	//fprintf(stderr, "waiting for worker 2\n");
114	pthread_join(worker2, &result);
115	//fprintf(stderr, "waiting for worker 3\n");
116	pthread_join(worker3, &result);
117
118	work(NULL);
119
120	PASS("tlv-basic");
121	return EXIT_SUCCESS;
122}
123