Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/aso/tthread.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1999-2012 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "terror.h"
21
22
#include <pthread.h>
23
#include <sys/time.h>
24
25
#ifndef elementsof
26
#define elementsof(x) (sizeof(x)/sizeof(x[0]))
27
#endif
28
#ifndef integralof
29
#define integralof(x) (((char*)(x))-((char*)0))
30
#endif
31
32
#define N_OBJS 1000000
33
#define N_THREADS 8
34
35
typedef struct _obj_s
36
{ struct _obj_s* next;
37
int value;
38
} Obj_t;
39
40
static Obj_t* Objlist; /* list of objects */
41
42
static unsigned int Asolock;
43
44
static unsigned int N_free;
45
46
static void* consumer(void* arg)
47
{
48
Obj_t* obj;
49
unsigned int id = (unsigned int)integralof(arg);
50
51
while (1)
52
{ asolock(&Asolock, id, ASO_SPINLOCK);
53
54
if((obj = Objlist))
55
{ Objlist = obj->next;
56
free(obj);
57
N_free += 1;
58
}
59
60
asolock(&Asolock, id, ASO_UNLOCK);
61
62
if(!obj)
63
return NULL;
64
}
65
}
66
67
tmain()
68
{
69
int i, m, type;
70
Asometh_t *meth;
71
Obj_t *obj;
72
char *lockid;
73
pthread_t thread[N_THREADS];
74
struct timeval tv1, tv2;
75
76
topts();
77
type = Tstall ? ASO_THREAD : ASO_INTRINSIC;
78
lockid = tstfile("aso", 0);
79
meth = 0;
80
while (meth = asometh(ASO_NEXT, meth))
81
{
82
if (!(meth->type & type))
83
continue;
84
if (asoinit(lockid, meth, 0))
85
{
86
twarn("%s method initialization failed", meth->name);
87
continue;
88
}
89
tinfo("testing %s method with %d threads", meth->name, N_THREADS);
90
Asolock = 0;
91
/* create object list */
92
for (i = 0; i < N_OBJS; i++)
93
{ if(!(obj = malloc(sizeof(Obj_t))) )
94
terror("malloc failed");
95
obj->value = i;
96
obj->next = Objlist;
97
Objlist = obj;
98
}
99
100
/* time before starting the threads... */
101
gettimeofday(&tv1, NULL);
102
103
N_free = 0;
104
for(i = 0; i < N_THREADS; ++i)
105
pthread_create(&thread[i], NULL, consumer, (char*)0 + i + 1);
106
107
for(i = 0; i < N_THREADS; ++i)
108
pthread_join(thread[i], NULL);
109
110
/* time after threads finished... */
111
gettimeofday(&tv2, NULL);
112
113
if(N_free != N_OBJS)
114
terror("%s method free() call error -- expected %d, got %d", meth->name, N_OBJS, N_free);
115
116
if (tv1.tv_usec > tv2.tv_usec)
117
{
118
tv2.tv_sec--;
119
tv2.tv_usec += 1000000;
120
}
121
122
tinfo("%s method elapsed time %ld.%lds", meth->name,
123
tv2.tv_sec - tv1.tv_sec, tv2.tv_usec - tv1.tv_usec);
124
}
125
126
texit(0);
127
}
128
129