-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2017-6346.cpp
182 lines (140 loc) · 3.6 KB
/
2017-6346.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <sys/types.h>
#include <pthread.h>
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <atomic>
#include <iostream>
#include <time.h>
//#define MUTEX_FOR_CORRECT_EXE_SEQUENCE
#ifdef MUTEX_FOR_CORRECT_EXE_SEQUENCE
pthread_mutex_t lock_for_correct_exe;
#endif
// #define TEST_TIME
#define PACKET_FANOUT_ROLLOVER 3
#define PACKET_FANOUT_FLAG_ROLLOVER 0x1000
#define EINVAL 22
#define ENOMEM 12
#define ENOSPC 28
#define u8 unsigned char
#define u16 unsigned short
pthread_mutex_t fanout_mutex;
typedef struct {
std::atomic_int counter;
} atomic_t;
typedef atomic_t atomic_long_t;
struct sock{
int num;
};
struct packet_rollover {
int sock;
atomic_long_t num;
};
struct packet_sock {
struct sock sk;
struct packet_rollover *rollover;
};
struct pthread_args{
struct sock* sk;
u16 type_flags;
int thread_id;
};
static struct packet_sock *pkt_sk(struct sock *sk)
{
return (struct packet_sock *)sk;
}
static void *kzalloc(unsigned size)
{
void *p = memalign(8, size);
if (!p)
return p;
memset(p, 0, size);
return p;
}
static void kfree(void *p)
{
if (p)
{
free(p);
}
}
void atomic_long_set(atomic_long_t* n, int v){
n->counter = v;
}
static int fanout_add(struct sock *sk, u16 type_flags, int thread_id)
{
struct packet_sock *po = pkt_sk(sk);
u8 type = type_flags & 0xff;
int err;
if (type == PACKET_FANOUT_ROLLOVER ||
(type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
po->rollover = (packet_rollover*)kzalloc(sizeof(*po->rollover));
if (!po->rollover)
return -ENOMEM;
printf("thread %d kzalloc, po->rollover = %p\n", thread_id, po->rollover);
atomic_long_set(&po->rollover->num, 0);
printf("thread %d change.\n", thread_id);
}
pthread_mutex_lock(&fanout_mutex);
err = -EINVAL;
pthread_mutex_unlock(&fanout_mutex);
if (err) {
kfree(po->rollover);
printf("thread %d free.\n", thread_id);
po->rollover = NULL;
}
#ifdef MUTEX_FOR_CORRECT_EXE_SEQUENCE
if(thread_id == 2)
pthread_mutex_unlock(&lock_for_correct_exe);
#endif
return err;
}
void* thread_func(void* args){
struct sock *sk = ((pthread_args*)args)->sk;
u16 type_flags = ((pthread_args*)args)->type_flags;
int id = ((pthread_args*)args)->thread_id;
if(id == 1)
#ifdef MUTEX_FOR_CORRECT_EXE_SEQUENCE
pthread_mutex_lock(&lock_for_correct_exe);
#else
usleep(500);
#endif
fanout_add(sk, type_flags, id);
return NULL;
}
int main()
{
#ifdef TEST_TIME
static double run_time_begin;
static double run_time_end;
static double run_time_total;
run_time_begin = clock();
#endif
#ifdef MUTEX_FOR_CORRECT_EXE_SEQUENCE
pthread_mutex_init(&lock_for_correct_exe, NULL);
pthread_mutex_lock(&lock_for_correct_exe);
#endif
struct packet_sock pktsk;
pthread_t t1, t2;
struct pthread_args args_one, args_two;
args_one.sk = &(pktsk.sk);
args_one.type_flags = 3;
args_one.thread_id = 1;
args_two.sk = &(pktsk.sk);
args_two.type_flags = 3;
args_two.thread_id = 2;
pthread_mutex_init(&fanout_mutex, NULL);
pthread_create(&t1, NULL, thread_func, &args_one);
pthread_create(&t2, NULL, thread_func, &args_two);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&fanout_mutex);
printf("\nprogram-successful-exit\n");
#ifdef TEST_TIME
run_time_end = clock();
run_time_total = run_time_end - run_time_begin;
printf("test-the-total-time: %.3lf\n", (double)(run_time_total/CLOCKS_PER_SEC)*1000);
#endif
return 0;
}