forked from adobe-flash/crossbridge
-
Notifications
You must be signed in to change notification settings - Fork 35
/
pthread_suspend.c
executable file
·122 lines (102 loc) · 2.93 KB
/
pthread_suspend.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
// 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 <stdio.h>
#include <pthread.h>
#ifdef __AVM2__
#include <AS3/AVM2.h>
#else
#define pthread_suspend_np pthread_suspend
#define pthread_resume_np pthread_continue
#endif
int successes = 0;
int EXPECTED_SUCCESSES = 6;
static void delaySome()
{
#ifdef __AVM2__
unsigned dummyLock = 1;
avm2_msleep(&dummyLock, &dummyLock, 300);
#else
sleep(1);
#endif
}
static volatile int counter = 0;
int counter_saved = 0;
void *threadProc(void *arg)
{
for(;;)
{
__sync_fetch_and_add(&counter, 1);
pthread_testcancel();
}
return 0;
}
int main(int argc, char **argv)
{
pthread_t thread;
if( counter == 0 ){
++successes;
}else{
printf("FAIL: pre-create counter problem\n");
return 0;
}
pthread_create(&thread, NULL, threadProc, NULL);
if( counter == 0 ){
++successes;
}else{
printf("FAIL: post-create counter problem\n");
return 0;
}
delaySome();
delaySome();
if( counter > 0 ){
++successes;
}else{
printf("FAIL: counter problem after initial delays\n");
return 0;
}
pthread_suspend_np(thread);
counter_saved = counter;
delaySome();
delaySome();
if( counter == counter_saved ){
++successes;
}else{
printf("FAIL: counter != counter_saved when suspended\n");
return 0;
}
pthread_resume_np(thread);
if( counter == counter_saved ){
++successes;
}else{
printf("FAIL: counter != counter_saved when resuming\n");
return 0;
}
delaySome();
delaySome();
if( counter > counter_saved ){
++successes;
}else{
printf("FAIL: counter problem after resuming and waiting\n");
return 0;
}
if( successes == EXPECTED_SUCCESSES ){
printf("RESULT=PASS\n");
}else{
printf("RESULT=FAIL\n");
}
return 0;
}