-
Notifications
You must be signed in to change notification settings - Fork 24
/
async.c
230 lines (203 loc) · 6.01 KB
/
async.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* PANDAseq -- Assemble paired FASTQ Illumina reads and strip the region between amplification primers.
Copyright (C) 2013 Andre Masella
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#ifdef HAVE_PTHREAD
# include <pthread.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "pandaseq.h"
#include "misc.h"
#ifdef HAVE_PTHREAD
struct seq_data {
panda_seq_identifier id;
panda_qual forward[MAX_LEN];
size_t forward_length;
panda_qual reverse[MAX_LEN];
size_t reverse_length;
struct seq_data *next;
};
struct async_data {
MANAGED_MEMBER(
PandaNextSeq,
next);
pthread_t reader;
pthread_cond_t is_ready;
pthread_cond_t has_free;
volatile bool done;
pthread_key_t in_flight;
pthread_mutex_t free_mutex;
pthread_mutex_t ready_mutex;
struct seq_data *ready;
struct seq_data *free;
};
static bool async_next_seq(
panda_seq_identifier *id,
panda_qual **forward,
size_t *forward_length,
panda_qual **reverse,
size_t *reverse_length,
struct async_data *data) {
struct seq_data *seq = NULL;
*forward = NULL;
*reverse = NULL;
*forward_length = 0;
*reverse_length = 0;
seq = pthread_getspecific(data->in_flight);
if (seq != NULL) {
pthread_mutex_lock(&data->free_mutex);
seq->next = data->free;
data->free = seq;
pthread_setspecific(data->in_flight, NULL);
pthread_cond_signal(&data->has_free);
pthread_mutex_unlock(&data->free_mutex);
}
pthread_mutex_lock(&data->ready_mutex);
do {
seq = data->ready;
if (seq != NULL) {
data->ready = seq->next;
pthread_mutex_unlock(&data->ready_mutex);
pthread_setspecific(data->in_flight, seq);
*forward = seq->forward;
*forward_length = seq->forward_length;
*reverse = seq->reverse;
*reverse_length = seq->reverse_length;
*id = seq->id;
seq->next = NULL;
return true;
}
if (data->done) {
pthread_mutex_unlock(&data->ready_mutex);
return false;
}
} while (pthread_cond_wait(&data->is_ready, &data->ready_mutex) == 0);
pthread_mutex_unlock(&data->ready_mutex);
return false;
}
static void *async_thread(
struct async_data *data) {
while (true) {
struct seq_data *seq;
pthread_mutex_lock(&data->free_mutex);
while ((seq = data->free) == NULL) {
if (data->done || pthread_cond_wait(&data->has_free, &data->free_mutex) != 0 || data->done) {
data->done = true;
pthread_mutex_unlock(&data->free_mutex);
pthread_mutex_lock(&data->ready_mutex);
pthread_cond_broadcast(&data->is_ready);
pthread_mutex_unlock(&data->ready_mutex);
pthread_exit(NULL);
}
}
data->free = NULL;
pthread_mutex_unlock(&data->free_mutex);
while (seq != NULL) {
const panda_qual *forward;
const panda_qual *reverse;
if (data->next(&seq->id, &forward, &seq->forward_length, &reverse, &seq->reverse_length, data->next_data)) {
struct seq_data *next;
memcpy(seq->forward, forward, seq->forward_length * sizeof(panda_qual));
memcpy(seq->reverse, reverse, seq->reverse_length * sizeof(panda_qual));
next = seq->next;
pthread_mutex_lock(&data->ready_mutex);
seq->next = data->ready;
data->ready = seq;
pthread_cond_broadcast(&data->is_ready);
pthread_mutex_unlock(&data->ready_mutex);
seq = next;
} else {
struct seq_data *next;
data->done = true;
for (; seq != NULL; seq = next) {
next = seq->next;
free(seq);
}
pthread_mutex_lock(&data->ready_mutex);
pthread_cond_broadcast(&data->is_ready);
pthread_mutex_unlock(&data->ready_mutex);
pthread_exit(NULL);
}
}
}
}
static void async_destroy(
struct async_data *data) {
struct seq_data *seq;
struct seq_data *temp;
data->done = true;
pthread_mutex_lock(&data->free_mutex);
pthread_cond_broadcast(&data->has_free);
pthread_mutex_unlock(&data->free_mutex);
pthread_join(data->reader, NULL);
pthread_cond_destroy(&data->is_ready);
pthread_cond_destroy(&data->has_free);
pthread_mutex_destroy(&data->free_mutex);
pthread_mutex_destroy(&data->ready_mutex);
pthread_key_delete(data->in_flight);
DESTROY_MEMBER(data, next);
for (seq = data->ready; seq != NULL; seq = (temp = seq->next, free(seq), temp)) ;
for (seq = data->free; seq != NULL; seq = (temp = seq->next, free(seq), temp)) ;
free(data);
}
PandaNextSeq panda_create_async_reader(
PandaNextSeq next,
void *next_data,
PandaDestroy next_destroy,
size_t length,
void **user_data,
PandaDestroy *destroy) {
struct async_data *data;
if (length < 2) {
*user_data = next_data;
*destroy = next_destroy;
return next;
}
data = malloc(sizeof(struct async_data));
data->done = false;
data->next = next;
data->next_data = next_data;
data->next_destroy = next_destroy;
pthread_cond_init(&data->is_ready, NULL);
pthread_cond_init(&data->has_free, NULL);
pthread_mutex_init(&data->free_mutex, NULL);
pthread_mutex_init(&data->ready_mutex, NULL);
pthread_key_create(&data->in_flight, free);
data->ready = NULL;
data->free = NULL;
length *= 4;
for (; length > 0; length--) {
struct seq_data *seq = malloc(sizeof(struct seq_data));
seq->next = data->free;
data->free = seq;
}
pthread_create(&data->reader, NULL, (void *(*)(void *)) &async_thread, data);
*user_data = data;
*destroy = (PandaDestroy) async_destroy;
return (PandaNextSeq) async_next_seq;
}
#else
PandaNextSeq panda_create_async_reader(
PandaNextSeq next,
void *next_data,
PandaDestroy next_destroy,
size_t length,
void **user_data,
PandaDestroy *destroy) {
(void) length;
*user_data = next_data;
*destroy = next_destroy;
return next;
}
#endif