@@ -431,9 +431,10 @@ static void lsapi_close_connection(LSAPI_Request *pReq)
431
431
lsapi_close (pReq -> m_fd );
432
432
pReq -> m_fd = -1 ;
433
433
if (s_busy_workers )
434
- __sync_fetch_and_sub (s_busy_workers , 1 );
434
+ __atomic_fetch_sub (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
435
435
if (s_worker_status )
436
- __sync_lock_test_and_set (& s_worker_status -> m_state , LSAPI_STATE_IDLE );
436
+ __atomic_store_n (& s_worker_status -> m_state , LSAPI_STATE_IDLE ,
437
+ __ATOMIC_SEQ_CST );
437
438
}
438
439
439
440
@@ -1577,10 +1578,10 @@ int LSAPI_Accept_r( LSAPI_Request * pReq )
1577
1578
else
1578
1579
{
1579
1580
if (s_worker_status )
1580
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
1581
- LSAPI_STATE_CONNECTED );
1581
+ __atomic_store_n (& s_worker_status -> m_state ,
1582
+ LSAPI_STATE_CONNECTED , __ATOMIC_SEQ_CST );
1582
1583
if (s_busy_workers )
1583
- __sync_fetch_and_add (s_busy_workers , 1 );
1584
+ __atomic_fetch_add (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
1584
1585
lsapi_set_nblock ( pReq -> m_fd , 0 );
1585
1586
if (((struct sockaddr * )& achPeer )-> sa_family == AF_INET )
1586
1587
{
@@ -2870,16 +2871,18 @@ void LSAPI_reset_server_state( void )
2870
2871
++ pStatus ;
2871
2872
}
2872
2873
if (s_busy_workers )
2873
- __sync_lock_release (s_busy_workers );
2874
+ __atomic_store_n (s_busy_workers , 0 , __ATOMIC_SEQ_CST );
2874
2875
if (s_accepting_workers )
2875
- __sync_lock_release (s_accepting_workers );
2876
+ __atomic_store_n (s_accepting_workers , 0 , __ATOMIC_SEQ_CST );
2876
2877
2877
2878
}
2878
2879
2879
2880
2880
2881
static void lsapi_sigchild ( int signal )
2881
2882
{
2882
2883
int status , pid ;
2884
+ char expect_connected = LSAPI_STATE_CONNECTED ;
2885
+ char expect_accepting = LSAPI_STATE_ACCEPTING ;
2883
2886
lsapi_child_status * child_status ;
2884
2887
if (g_prefork_server == NULL )
2885
2888
return ;
@@ -2916,19 +2919,23 @@ static void lsapi_sigchild( int signal )
2916
2919
child_status = find_child_status ( pid );
2917
2920
if ( child_status )
2918
2921
{
2919
- if (__sync_bool_compare_and_swap (& child_status -> m_state ,
2920
- LSAPI_STATE_CONNECTED ,
2921
- LSAPI_STATE_IDLE ))
2922
+ if (__atomic_compare_exchange_n (& child_status -> m_state ,
2923
+ & expect_connected ,
2924
+ LSAPI_STATE_IDLE , 1 ,
2925
+ __ATOMIC_SEQ_CST ,
2926
+ __ATOMIC_SEQ_CST ))
2922
2927
{
2923
2928
if (s_busy_workers )
2924
- __sync_fetch_and_sub (s_busy_workers , 1 );
2929
+ __atomic_fetch_sub (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
2925
2930
}
2926
- else if (__sync_bool_compare_and_swap (& child_status -> m_state ,
2927
- LSAPI_STATE_ACCEPTING ,
2928
- LSAPI_STATE_IDLE ))
2931
+ else if (__atomic_compare_exchange_n (& child_status -> m_state ,
2932
+ & expect_accepting ,
2933
+ LSAPI_STATE_IDLE , 1 ,
2934
+ __ATOMIC_SEQ_CST ,
2935
+ __ATOMIC_SEQ_CST ))
2929
2936
{
2930
2937
if (s_accepting_workers )
2931
- __sync_fetch_and_sub (s_accepting_workers , 1 );
2938
+ __atomic_fetch_sub (s_accepting_workers , 1 , __ATOMIC_SEQ_CST );
2932
2939
}
2933
2940
child_status -> m_pid = 0 ;
2934
2941
-- g_prefork_server -> m_iCurChildren ;
@@ -3201,7 +3208,7 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3201
3208
{
3202
3209
int accepting = 0 ;
3203
3210
if (s_accepting_workers )
3204
- accepting = __sync_add_and_fetch (s_accepting_workers , 0 );
3211
+ accepting = __atomic_load_n (s_accepting_workers , __ATOMIC_SEQ_CST );
3205
3212
3206
3213
if (pServer -> m_iCurChildren > 0
3207
3214
&& accepting > 0 )
@@ -3267,10 +3274,10 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3267
3274
if (pthread_atfork_func )
3268
3275
(* pthread_atfork_func )(NULL , NULL , set_skip_write );
3269
3276
3270
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
3271
- LSAPI_STATE_CONNECTED );
3277
+ __atomic_store_n (& s_worker_status -> m_state ,
3278
+ LSAPI_STATE_CONNECTED , __ATOMIC_SEQ_CST );
3272
3279
if (s_busy_workers )
3273
- __sync_add_and_fetch (s_busy_workers , 1 );
3280
+ __atomic_add_fetch (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
3274
3281
lsapi_set_nblock ( pReq -> m_fd , 0 );
3275
3282
//keep it open if busy_count is used.
3276
3283
if (!s_keep_listener && s_busy_workers
@@ -3342,7 +3349,7 @@ int LSAPI_Postfork_Child(LSAPI_Request * pReq)
3342
3349
{
3343
3350
int max_children = g_prefork_server -> m_iMaxChildren ;
3344
3351
s_pid = getpid ();
3345
- __sync_lock_test_and_set (& pReq -> child_status -> m_pid , s_pid );
3352
+ __atomic_store_n (& pReq -> child_status -> m_pid , s_pid , __ATOMIC_SEQ_CST );
3346
3353
s_worker_status = pReq -> child_status ;
3347
3354
3348
3355
setsid ();
@@ -3354,10 +3361,10 @@ int LSAPI_Postfork_Child(LSAPI_Request * pReq)
3354
3361
if (pthread_atfork_func )
3355
3362
(* pthread_atfork_func )(NULL , NULL , set_skip_write );
3356
3363
3357
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
3358
- LSAPI_STATE_CONNECTED );
3364
+ __atomic_store_n (& s_worker_status -> m_state ,
3365
+ LSAPI_STATE_CONNECTED , __ATOMIC_SEQ_CST );
3359
3366
if (s_busy_workers )
3360
- __sync_add_and_fetch (s_busy_workers , 1 );
3367
+ __atomic_add_fetch (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
3361
3368
lsapi_set_nblock ( pReq -> m_fd , 0 );
3362
3369
//keep it open if busy_count is used.
3363
3370
if (!s_keep_listener && s_busy_workers
@@ -3474,7 +3481,7 @@ int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq)
3474
3481
{
3475
3482
int accepting = 0 ;
3476
3483
if (s_accepting_workers )
3477
- accepting = __sync_add_and_fetch (s_accepting_workers , 0 );
3484
+ accepting = __atomic_load_n (s_accepting_workers , __ATOMIC_SEQ_CST );
3478
3485
3479
3486
if (pServer -> m_iCurChildren > 0
3480
3487
&& accepting > 0 )
@@ -3559,7 +3566,7 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
3559
3566
}
3560
3567
else if (s_req_processed > 0 && s_max_busy_workers > 0 && s_busy_workers )
3561
3568
{
3562
- ret = __sync_fetch_and_add (s_busy_workers , 0 );
3569
+ ret = __atomic_load_n (s_busy_workers , __ATOMIC_SEQ_CST );
3563
3570
if (ret >= s_max_busy_workers )
3564
3571
{
3565
3572
send_conn_close_notification (pReq -> m_fd );
@@ -3603,19 +3610,19 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
3603
3610
if (fd == pReq -> m_fdListen )
3604
3611
{
3605
3612
if (s_worker_status )
3606
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
3607
- LSAPI_STATE_ACCEPTING );
3613
+ __atomic_store_n (& s_worker_status -> m_state ,
3614
+ LSAPI_STATE_ACCEPTING , __ATOMIC_SEQ_CST );
3608
3615
if (s_accepting_workers )
3609
- __sync_fetch_and_add (s_accepting_workers , 1 );
3616
+ __atomic_fetch_add (s_accepting_workers , 1 , __ATOMIC_SEQ_CST );
3610
3617
}
3611
3618
ret = (* g_fnSelect )(fd + 1 , & readfds , NULL , NULL , & timeout );
3612
3619
if (fd == pReq -> m_fdListen )
3613
3620
{
3614
3621
if (s_accepting_workers )
3615
- __sync_fetch_and_sub (s_accepting_workers , 1 );
3622
+ __atomic_fetch_sub (s_accepting_workers , 1 , __ATOMIC_SEQ_CST );
3616
3623
if (s_worker_status )
3617
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
3618
- LSAPI_STATE_IDLE );
3624
+ __atomic_store_n (& s_worker_status -> m_state ,
3625
+ LSAPI_STATE_IDLE , __ATOMIC_SEQ_CST );
3619
3626
}
3620
3627
3621
3628
if ( ret == 0 )
@@ -3663,10 +3670,11 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
3663
3670
if ( pReq -> m_fd != -1 )
3664
3671
{
3665
3672
if (s_worker_status )
3666
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
3667
- LSAPI_STATE_CONNECTED );
3673
+ __atomic_store_n (& s_worker_status -> m_state ,
3674
+ LSAPI_STATE_CONNECTED ,
3675
+ __ATOMIC_SEQ_CST );
3668
3676
if (s_busy_workers )
3669
- __sync_fetch_and_add (s_busy_workers , 1 );
3677
+ __atomic_fetch_add (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
3670
3678
3671
3679
fd = pReq -> m_fd ;
3672
3680
@@ -4337,5 +4345,5 @@ int LSAPI_Set_Restored_Parent_Pid(int pid)
4337
4345
4338
4346
int LSAPI_Inc_Req_Processed (int cnt )
4339
4347
{
4340
- return __sync_add_and_fetch (s_global_counter , cnt );
4348
+ return __atomic_add_fetch (s_global_counter , cnt , __ATOMIC_SEQ_CST );
4341
4349
}
0 commit comments