@@ -49,13 +49,13 @@ typedef struct http_sconn {
49
49
nni_http_handler * handler ; // set if we deferred to read body
50
50
nni_http_handler * release ; // set if we dispatched handler
51
51
bool close ;
52
- bool closed ;
53
52
bool finished ;
54
53
nni_aio * cbaio ;
55
54
nni_aio * rxaio ;
56
55
nni_aio * txaio ;
57
56
nni_aio * txdataio ;
58
57
nni_reap_node reap ;
58
+ nni_atomic_flag closed ;
59
59
} http_sconn ;
60
60
61
61
typedef struct http_error {
@@ -310,16 +310,15 @@ http_sc_reap(void *arg)
310
310
}
311
311
312
312
static void
313
- http_sc_close_locked (http_sconn * sc )
313
+ http_sconn_close (http_sconn * sc )
314
314
{
315
315
nni_http_conn * conn ;
316
316
317
- if (sc -> closed ) {
317
+ if (nni_atomic_flag_test_and_set ( & sc -> closed ) ) {
318
318
return ;
319
319
}
320
320
NNI_ASSERT (!sc -> finished );
321
321
322
- sc -> closed = true;
323
322
nni_aio_close (sc -> rxaio );
324
323
nni_aio_close (sc -> txaio );
325
324
nni_aio_close (sc -> txdataio );
@@ -331,17 +330,6 @@ http_sc_close_locked(http_sconn *sc)
331
330
nni_reap (& http_sc_reap_list , sc );
332
331
}
333
332
334
- static void
335
- http_sconn_close (http_sconn * sc )
336
- {
337
- nni_http_server * s ;
338
- s = sc -> server ;
339
-
340
- nni_mtx_lock (& s -> mtx );
341
- http_sc_close_locked (sc );
342
- nni_mtx_unlock (& s -> mtx );
343
- }
344
-
345
333
static void
346
334
http_sconn_txdatdone (void * arg )
347
335
{
@@ -987,13 +975,16 @@ nni_http_server_init(nni_http_server **serverp, const nng_url *url)
987
975
988
976
nni_mtx_lock (& http_servers_lk );
989
977
NNI_LIST_FOREACH (& http_servers , s ) {
978
+ nni_mtx_lock (& s -> mtx );
990
979
if ((!s -> closed ) && (url -> u_port == s -> port ) &&
991
980
(strcmp (url -> u_hostname , s -> hostname ) == 0 )) {
992
981
* serverp = s ;
993
982
s -> refcnt ++ ;
983
+ nni_mtx_unlock (& s -> mtx );
994
984
nni_mtx_unlock (& http_servers_lk );
995
985
return (0 );
996
986
}
987
+ nni_mtx_unlock (& s -> mtx );
997
988
}
998
989
999
990
// We didn't find a server, try to make a new one.
@@ -1065,7 +1056,7 @@ http_server_stop(nni_http_server *s)
1065
1056
// Stopping the server is a hard stop -- it aborts any work
1066
1057
// being done by clients. (No graceful shutdown).
1067
1058
NNI_LIST_FOREACH (& s -> conns , sc ) {
1068
- http_sc_close_locked (sc );
1059
+ http_sconn_close (sc );
1069
1060
}
1070
1061
}
1071
1062
0 commit comments