Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtaso/taso.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2010-2011 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
* Phong Vo <[email protected]> *
18
* Adam Edgar <[email protected]> *
19
* Glenn Fowler <[email protected]> *
20
* *
21
***********************************************************************/
22
#pragma prototyped
23
24
#include <ast_standards.h>
25
#include <ast.h>
26
#include <aso.h>
27
28
#include "FEATURE/pthread"
29
30
#if _aso_pthread_spinlock
31
32
typedef struct Aso_spin_s
33
{
34
pthread_spinlock_t lock;
35
} Aso_spin_t;
36
37
static void*
38
aso_init_spin(void* data, const char* details)
39
{
40
Aso_spin_t* spin = (Aso_spin_t*)data;
41
42
if (spin)
43
{
44
pthread_spin_destroy(&spin->lock);
45
free(spin);
46
return 0;
47
}
48
if (!(spin = (Aso_spin_t*)malloc(sizeof(Aso_spin_t))))
49
return 0;
50
pthread_spin_init(&spin->lock, 0);
51
return spin;
52
}
53
54
static ssize_t
55
aso_lock_spin(void* data, ssize_t k, void volatile* p)
56
{
57
Aso_spin_t* spin = (Aso_spin_t*)data;
58
59
if (k > 0)
60
pthread_spin_unlock(&spin->lock);
61
else
62
pthread_spin_lock(&spin->lock);
63
return 1;
64
}
65
66
static Asometh_t aso_spin = { "spin", ASO_THREAD, aso_init_spin, aso_lock_spin };
67
68
#endif
69
70
#if _aso_pthread_mutex
71
72
typedef struct Aso_mutex_s
73
{
74
pthread_mutex_t lock;
75
} Aso_mutex_t;
76
77
static void*
78
aso_init_mutex(void* data, const char* details)
79
{
80
Aso_mutex_t* mutex = (Aso_mutex_t*)data;
81
82
if (mutex)
83
{
84
pthread_mutex_destroy(&mutex->lock);
85
free(mutex);
86
return 0;
87
}
88
if (!(mutex = (Aso_mutex_t*)malloc(sizeof(Aso_mutex_t))))
89
return 0;
90
pthread_mutex_init(&mutex->lock, 0);
91
return mutex;
92
}
93
94
static ssize_t
95
aso_lock_mutex(void* data, ssize_t k, void volatile* p)
96
{
97
Aso_mutex_t* mutex = (Aso_mutex_t*)data;
98
99
if (k > 0)
100
pthread_mutex_unlock(&mutex->lock);
101
else
102
pthread_mutex_lock(&mutex->lock);
103
return 1;
104
}
105
106
static Asometh_t aso_mutex = { "mutex", ASO_THREAD, aso_init_mutex, aso_lock_mutex };
107
108
#endif
109
110
#if _aso_pthread_spin || _aso_pthread_mutex
111
112
static Asometh_t* method[] =
113
{
114
#if _aso_pthread_spin
115
&aso_spin,
116
#endif
117
#if _aso_pthread_mutex
118
&aso_mutex,
119
#endif
120
};
121
122
#endif
123
124
/*
125
* default library asometh() intercept
126
*
127
* if type!=0 return lock method for type with name details
128
* else if name!=0 return lock method matching <name>[,<details>]
129
* else return the current lock method
130
* 0 returned on error
131
*/
132
133
Asometh_t*
134
asometh(int type, void* data)
135
{
136
#if _aso_pthread_spin || _aso_pthread_mutex
137
size_t n;
138
int i;
139
char* e;
140
Asometh_t* meth;
141
char* name;
142
143
if (type == ASO_NEXT)
144
{
145
if (!(meth = (Asometh_t*)data))
146
return method[0];
147
for (i = 0; i < elementsof(method) - 1; i++)
148
if (meth == method[i])
149
return method[i+1];
150
if (meth == method[i])
151
meth = 0;
152
return _asometh(type, meth);
153
}
154
if (type)
155
for (i = 0; i < elementsof(method); i++)
156
if (method[i]->type & type)
157
{
158
method[i]->details = (char*)data;
159
return method[i];
160
}
161
if (name = (char*)data)
162
{
163
n = (e = strchr(name, ',')) ? (e - name) : strlen(name);
164
for (i = 0; i < elementsof(method); i++)
165
if (strncmp(name, method[i]->name, n) == 0)
166
{
167
if (e)
168
method[i]->details = e + 1;
169
return method[i];
170
}
171
}
172
#endif
173
return _asometh(type, data);
174
}
175
176