3
A brief file description
5
@section license License
7
Licensed to the Apache Software Foundation (ASF) under one
8
or more contributor license agreements. See the NOTICE file
9
distributed with this work for additional information
10
regarding copyright ownership. The ASF licenses this file
11
to you under the Apache License, Version 2.0 (the
12
"License"); you may not use this file except in compliance
13
with the License. You may obtain a copy of the License at
15
http://www.apache.org/licenses/LICENSE-2.0
17
Unless required by applicable law or agreed to in writing, software
18
distributed under the License is distributed on an "AS IS" BASIS,
19
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
See the License for the specific language governing permissions and
21
limitations under the License.
26
////////////////////////////////////////////////////////////////////////////
28
// 1) Enable the following #include.
29
// Visual C++ does not allow this under #ifdef
30
// #include "stdafx.h"
32
// 2) Create a new project within the existing TS project
33
// which is a "WIN32 Console Application"
34
// with the "Simple Application" option.
35
// 3) Replace C/C++ Preprocessor definitions with traffic_server
37
// 4) Replace C/C++ Additional includes with traffic server
38
// definitions. Prepend "..\proxy" to directories in proxy.
40
// 5) Replace C/C++ project option "/MLd" with "/MTd"
41
// 6) Add libts as dependency.
42
////////////////////////////////////////////////////////////////////////////
49
#include "ink_atomic.h"
50
#include "ink_queue.h"
51
#include "ink_thread.h"
52
#include "ink_unused.h" /* MAGIC_EDITING_TAG */
55
#ifndef LONG_ATOMICLIST_TEST
57
#define MAX_ALIST_TEST 10
58
#define MAX_ALIST_ARRAY 100000
59
InkAtomicList al[MAX_ALIST_TEST];
60
void *al_test[MAX_ALIST_TEST][MAX_ALIST_ARRAY];
61
volatile int al_done = 0;
66
int me = (int) (uintptr_t) ame;
68
for (k = 0; k < MAX_ALIST_ARRAY; k++)
69
ink_atomiclist_push(&al[k % MAX_ALIST_TEST], &al_test[me][k]);
71
for (j = 0; j < 1000000; j++)
72
if ((x = ink_atomiclist_pop(&al[me])))
73
ink_atomiclist_push(&al[rand() % MAX_ALIST_TEST], x);
74
ink_atomic_increment((int *) &al_done, 1);
77
#endif // !LONG_ATOMICLIST_TEST
79
#ifdef LONG_ATOMICLIST_TEST
80
/************************************************************************/
81
#define MAX_ATOMIC_LISTS (4 * 1024)
82
#define MAX_ITEMS_PER_LIST (1 * 1024)
83
#define MAX_TEST_THREADS 64
84
static InkAtomicList alists[MAX_ATOMIC_LISTS];
85
struct listItem *items[MAX_ATOMIC_LISTS * MAX_ITEMS_PER_LIST];
103
struct listItem *plistItem;
105
for (ali = 0; ali < MAX_ATOMIC_LISTS; ali++)
106
ink_atomiclist_init(&alists[ali], "alist", ((char *) &l.link - (char *) &l));
108
for (ali = 0; ali < MAX_ATOMIC_LISTS; ali++) {
109
for (j = 0; j < MAX_ITEMS_PER_LIST; j++) {
110
plistItem = (struct listItem *) malloc(sizeof(struct listItem));
111
items[ali + j] = plistItem;
112
plistItem->data1 = ali + j;
113
plistItem->data2 = ali + rand();
115
plistItem->data3 = j + rand();
116
plistItem->data4 = ali + j + rand();
117
plistItem->check = (plistItem->data1 ^ plistItem->data2 ^ plistItem->data3 ^ plistItem->data4);
118
ink_atomiclist_push(&alists[ali], plistItem);
127
struct listItem *pli;
128
struct listItem *pli_next;
136
l = &alists[(me + rand()) % MAX_ATOMIC_LISTS];
138
pli = (struct listItem *) ink_atomiclist_popall(l);
142
// Place listItems into random queues
144
assert((pli->data1 ^ pli->data2 ^ pli->data3 ^ pli->data4) == pli->check);
145
pli_next = (struct listItem *) pli->link;
147
ink_atomiclist_push(&alists[(me + rand()) % MAX_ATOMIC_LISTS], (void *) pli);
151
poll(0, 0, 10); // 10 msec delay
152
if ((iterations % 100) == 0)
157
/************************************************************************/
158
#endif // LONG_ATOMICLIST_TEST
161
main(int argc, const char *argv[])
163
#ifndef LONG_ATOMICLIST_TEST
164
int32_t m = 1, n = 100;
165
//int64 lm = 1LL, ln = 100LL;
166
const char* m2 = "hello";
169
printf("sizeof(int32_t)==%d sizeof(void *)==%d\n", (int)sizeof(int32_t), (int)sizeof(void *));
172
printf("CAS: %d == 1 then 2\n", m);
173
n = ink_atomic_cas(&m, 1, 2);
174
printf("changed to: %d, result=%s\n", m, n ? "true" : "false");
176
printf("CAS: %d == 1 then 3\n", m);
177
n = ink_atomic_cas(&m, 1, 3);
178
printf("changed to: %d, result=%s\n", m, n ? "true" : "false");
180
printf("CAS pointer: '%s' == 'hello' then 'new'\n", m2);
181
n = ink_atomic_cas_ptr((pvvoidp) &m2, (char *) "hello", (char *) "new");
182
printf("changed to: %s, result=%s\n", m2, n ? (char *) "true" : (char *) "false");
184
printf("CAS pointer: '%s' == 'hello' then 'new2'\n", m2);
185
n = ink_atomic_cas_ptr((pvvoidp)&m2, (char*)m2, (char *) "new2");
186
printf("changed to: %s, result=%s\n", m2, n ? "true" : "false");
189
printf("Atomic Inc of %d\n", n);
190
m = ink_atomic_increment((int *) &n, 1);
191
printf("changed to: %d, result=%d\n", n, m);
194
printf("Atomic Fetch-and-Add 2 to pointer to '%s'\n", m2);
195
n2 = (char *)ink_atomic_increment_ptr((pvvoidp)&m2, 2);
196
printf("changed to: %s, result=%s\n", m2, n2);
198
printf("Testing atomic lists\n");
202
printf("sizeof(al_test) = %d\n", (int)sizeof(al_test));
203
memset(&al_test[0][0], 0, sizeof(al_test));
204
for (ali = 0; ali < MAX_ALIST_TEST; ali++)
205
ink_atomiclist_init(&al[ali], "foo", 0);
206
for (ali = 0; ali < MAX_ALIST_TEST; ali++) {
210
pthread_attr_init(&attr);
211
#if !defined(freebsd)
212
pthread_attr_setstacksize(&attr, 1024 * 1024);
214
ink_assert(pthread_create(&tid, &attr, testalist, (void *) ali) == 0);
216
while (al_done != MAX_ALIST_TEST)
219
#endif // !LONG_ATOMICLIST_TEST
221
#ifdef LONG_ATOMICLIST_TEST
222
printf("Testing atomic lists (long version)\n");
227
for (id = 0; id < MAX_TEST_THREADS; id++) {
228
assert(thr_create(NULL, 0, cycle_data, (void *) id, THR_NEW_LWP, NULL) == 0);
232
poll(0, 0, 10); // 10 msec delay
234
#endif // LONG_ATOMICLIST_TEST