forked from adobe-flash/crossbridge
-
Notifications
You must be signed in to change notification settings - Fork 35
/
pthread_mutex_test2.c
125 lines (109 loc) · 3.45 KB
/
pthread_mutex_test2.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2013 Adobe Systems Inc
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <pthread.h>
#include <stdlib.h>
#define MAXTHREAD 15
#define MAXMUTEX 20
static pthread_cond_t sCond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t sCondMutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t sMutexes[MAXMUTEX];
volatile static int sTotal[MAXMUTEX] = { 0 };
volatile static int sThreadCount = 0;
volatile static int sMutexCount;
volatile static int sCycle = 0;
static int total()
{
int result = 0, i;
for(i = 0; i < MAXMUTEX; i++)
result += sTotal[i];
return result;
}
static void *threadProc(void *arg)
{
int id = (int)arg;
unsigned seed = id;
pthread_mutex_lock(&sCondMutex);
for(;;)
{
__sync_fetch_and_add(&sCycle, 1);
pthread_cond_wait(&sCond, &sCondMutex);
if(sThreadCount < 0)
break;
if(id < sThreadCount)
{
int i;
for(i = 0; i < 4000; i++)
{
int ln = rand_r(&seed) % sMutexCount;
pthread_mutex_lock(sMutexes + ln);
sTotal[ln]++;
pthread_mutex_unlock(sMutexes + ln);
}
}
}
pthread_mutex_unlock(&sCondMutex);
return NULL;
}
static void *dummyThreadProc(void *arg)
{
return NULL;
}
int main()
{
pthread_mutex_t minit = PTHREAD_MUTEX_INITIALIZER;
pthread_t threads[MAXTHREAD];
int i, j, n;
for(j = 0; j < MAXMUTEX; j++)
sMutexes[j] = minit;
for(i = 0; i < MAXTHREAD; i++)
pthread_create(threads + i, NULL, threadProc, (void *)i);
while(sCycle < MAXTHREAD); // everyone waiting?
pthread_mutex_lock(&sCondMutex);
pthread_mutex_unlock(&sCondMutex);
for(i = 1; i <= MAXTHREAD; i++) // # of threads
for(j = 1; j <= MAXMUTEX; j++) // # of mutexes
{
int nextCycle = sCycle + MAXTHREAD;
printf("%d threads %d mutexes %d total\n", i, j, total());
sThreadCount = i;
sMutexCount = j;
pthread_cond_broadcast(&sCond);
while(sCycle != nextCycle)
{
#if 0
pthread_t dummyThread;
pthread_create(&dummyThread, NULL, dummyThreadProc, NULL);
pthread_join(dummyThread, NULL);
#endif
}
pthread_mutex_lock(&sCondMutex);
pthread_mutex_unlock(&sCondMutex);
}
sThreadCount = -1;
pthread_cond_broadcast(&sCond);
{
int k;
for(k = 0; k < MAXTHREAD; k++)
pthread_join(threads[k], NULL);
}
printf("%d total\n", total());
if(i - 1 == MAXTHREAD && j - 1 == MAXMUTEX)
printf("RESULT=PASS");
else
printf("RESULT=FAIL");
return 0;
}