1
- /* $OpenBSD: parser.c,v 1.156 2025/06/18 09:04:51 tb Exp $ */
1
+ /* $OpenBSD: parser.c,v 1.157 2025/06/20 05:00:01 claudio Exp $ */
2
2
/*
3
3
* Copyright (c) 2019 Claudio Jeker <[email protected] >
4
4
* Copyright (c) 2019 Kristaps Dzonsons <[email protected] >
@@ -51,6 +51,8 @@ static struct ibufqueue *globalmsgq;
51
51
static pthread_mutex_t globalmsgq_mtx = PTHREAD_MUTEX_INITIALIZER ;
52
52
static pthread_cond_t globalmsgq_cond = PTHREAD_COND_INITIALIZER ;
53
53
54
+ static volatile int quit ;
55
+
54
56
struct parse_repo {
55
57
RB_ENTRY (parse_repo ) entry ;
56
58
char * path ;
@@ -1075,9 +1077,9 @@ parse_worker(void *arg)
1075
1077
if ((myq = ibufq_new ()) == NULL )
1076
1078
err (1 , "ibufqueue_new" );
1077
1079
1078
- for (;; ) {
1080
+ while (! quit ) {
1079
1081
pthread_mutex_lock (& globalq_mtx );
1080
- while (TAILQ_EMPTY (& globalq ))
1082
+ while (TAILQ_EMPTY (& globalq ) && ! quit )
1081
1083
pthread_cond_wait (& globalq_cond , & globalq_mtx );
1082
1084
n = 0 ;
1083
1085
while ((entp = TAILQ_FIRST (& globalq )) != NULL ) {
@@ -1100,10 +1102,10 @@ parse_worker(void *arg)
1100
1102
}
1101
1103
}
1102
1104
1103
-
1104
1105
X509_STORE_CTX_free (ctx );
1105
1106
BN_CTX_free (bn_ctx );
1106
1107
ibufq_free (myq );
1108
+ return NULL ;
1107
1109
}
1108
1110
1109
1111
static void *
@@ -1115,11 +1117,12 @@ parse_writer(void *arg)
1115
1117
if ((myq = msgbuf_new ()) == NULL )
1116
1118
err (1 , NULL );
1117
1119
pfd .fd = * (int * )arg ;
1118
- for (;; ) {
1120
+ while (! quit ) {
1119
1121
if (msgbuf_queuelen (myq ) == 0 ) {
1120
1122
pthread_mutex_lock (& globalmsgq_mtx );
1121
- while (ibufq_queuelen (globalmsgq ) == 0 )
1122
- pthread_cond_wait (& globalmsgq_cond , & globalmsgq_mtx );
1123
+ while (ibufq_queuelen (globalmsgq ) == 0 && !quit )
1124
+ pthread_cond_wait (& globalmsgq_cond ,
1125
+ & globalmsgq_mtx );
1123
1126
/* enqueue messages to local msgbuf */
1124
1127
msgbuf_concat (myq , globalmsgq );
1125
1128
pthread_mutex_unlock (& globalmsgq_mtx );
@@ -1137,8 +1140,10 @@ parse_writer(void *arg)
1137
1140
errx (1 , "poll: bad descriptor" );
1138
1141
1139
1142
/* If the parent closes, return immediately. */
1140
- if ((pfd .revents & POLLHUP ))
1143
+ if ((pfd .revents & POLLHUP )) {
1144
+ quit = 1 ;
1141
1145
break ;
1146
+ }
1142
1147
1143
1148
if (pfd .revents & POLLOUT ) {
1144
1149
if (msgbuf_write (pfd .fd , myq ) == -1 ) {
@@ -1152,6 +1157,7 @@ parse_writer(void *arg)
1152
1157
}
1153
1158
}
1154
1159
1160
+ msgbuf_free (myq );
1155
1161
return NULL ;
1156
1162
}
1157
1163
@@ -1170,7 +1176,7 @@ proc_parser(int fd, int nthreads)
1170
1176
struct msgbuf * inbufq ;
1171
1177
struct entity * entp ;
1172
1178
struct ibuf * b ;
1173
- pthread_t wthread , dummy ;
1179
+ pthread_t writer , * workers ;
1174
1180
int i ;
1175
1181
1176
1182
/* Only allow access to the cache directory. */
@@ -1191,12 +1197,15 @@ proc_parser(int fd, int nthreads)
1191
1197
NULL )
1192
1198
err (1 , NULL );
1193
1199
1194
- pthread_create (& wthread , NULL , & parse_writer , & fd );
1200
+ if ((workers = calloc (nthreads , sizeof (* workers ))) == NULL )
1201
+ err (1 , NULL );
1202
+
1203
+ pthread_create (& writer , NULL , & parse_writer , & fd );
1195
1204
for (i = 0 ; i < nthreads ; i ++ )
1196
- pthread_create (& dummy , NULL , & parse_worker , NULL );
1205
+ pthread_create (& workers [ i ] , NULL , & parse_worker , NULL );
1197
1206
1198
1207
pfd .fd = fd ;
1199
- for (;; ) {
1208
+ while (! quit ) {
1200
1209
pfd .events = POLLIN ;
1201
1210
if (poll (& pfd , 1 , INFTIM ) == -1 ) {
1202
1211
if (errno == EINTR )
@@ -1207,8 +1216,10 @@ proc_parser(int fd, int nthreads)
1207
1216
errx (1 , "poll: bad descriptor" );
1208
1217
1209
1218
/* If the parent closes, return immediately. */
1210
- if ((pfd .revents & POLLHUP ))
1219
+ if ((pfd .revents & POLLHUP )) {
1220
+ quit = 1 ;
1211
1221
break ;
1222
+ }
1212
1223
1213
1224
if ((pfd .revents & POLLIN )) {
1214
1225
switch (ibuf_read (fd , inbufq )) {
@@ -1243,13 +1254,25 @@ proc_parser(int fd, int nthreads)
1243
1254
}
1244
1255
}
1245
1256
1257
+ /* signal all threads */
1258
+ pthread_cond_broadcast (& globalq_cond );
1259
+ pthread_cond_broadcast (& globalmsgq_cond );
1260
+
1246
1261
pthread_mutex_lock (& globalq_mtx );
1247
1262
while ((entp = TAILQ_FIRST (& globalq )) != NULL ) {
1248
1263
TAILQ_REMOVE (& globalq , entp , entries );
1249
1264
entity_free (entp );
1250
1265
}
1251
1266
pthread_mutex_unlock (& globalq_mtx );
1252
1267
1268
+ if (pthread_join (writer , NULL ) != 0 )
1269
+ errx (1 , "pthread_join writer" );
1270
+ for (i = 0 ; i < nthreads ; i ++ ) {
1271
+ if (pthread_join (workers [i ], NULL ) != 0 )
1272
+ errx (1 , "pthread_join worker %d" , i );
1273
+ }
1274
+ free (workers ); /* karl marx */
1275
+
1253
1276
auth_tree_free (& auths );
1254
1277
crl_tree_free (& crlt );
1255
1278
0 commit comments