@@ -442,9 +442,10 @@ static void lsapi_close_connection(LSAPI_Request *pReq)
442
442
lsapi_close (pReq -> m_fd );
443
443
pReq -> m_fd = -1 ;
444
444
if (s_busy_workers )
445
- __sync_fetch_and_sub (s_busy_workers , 1 );
445
+ __atomic_fetch_sub (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
446
446
if (s_worker_status )
447
- __sync_lock_test_and_set (& s_worker_status -> m_state , LSAPI_STATE_IDLE );
447
+ __atomic_store_n (& s_worker_status -> m_state , LSAPI_STATE_IDLE ,
448
+ __ATOMIC_SEQ_CST );
448
449
}
449
450
450
451
@@ -1588,10 +1589,10 @@ int LSAPI_Accept_r( LSAPI_Request * pReq )
1588
1589
else
1589
1590
{
1590
1591
if (s_worker_status )
1591
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
1592
- LSAPI_STATE_CONNECTED );
1592
+ __atomic_store_n (& s_worker_status -> m_state ,
1593
+ LSAPI_STATE_CONNECTED , __ATOMIC_SEQ_CST );
1593
1594
if (s_busy_workers )
1594
- __sync_fetch_and_add (s_busy_workers , 1 );
1595
+ __atomic_fetch_add (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
1595
1596
lsapi_set_nblock ( pReq -> m_fd , 0 );
1596
1597
if (((struct sockaddr * )& achPeer )-> sa_family == AF_INET )
1597
1598
{
@@ -2919,16 +2920,18 @@ void LSAPI_reset_server_state( void )
2919
2920
++ pStatus ;
2920
2921
}
2921
2922
if (s_busy_workers )
2922
- __sync_lock_release (s_busy_workers );
2923
+ __atomic_store_n (s_busy_workers , 0 , __ATOMIC_SEQ_CST );
2923
2924
if (s_accepting_workers )
2924
- __sync_lock_release (s_accepting_workers );
2925
+ __atomic_store_n (s_accepting_workers , 0 , __ATOMIC_SEQ_CST );
2925
2926
2926
2927
}
2927
2928
2928
2929
2929
2930
static void lsapi_sigchild ( int signal )
2930
2931
{
2931
2932
int status , pid ;
2933
+ char expect_connected = LSAPI_STATE_CONNECTED ;
2934
+ char expect_accepting = LSAPI_STATE_ACCEPTING ;
2932
2935
lsapi_child_status * child_status ;
2933
2936
if (g_prefork_server == NULL )
2934
2937
return ;
@@ -2965,19 +2968,23 @@ static void lsapi_sigchild( int signal )
2965
2968
child_status = find_child_status ( pid );
2966
2969
if ( child_status )
2967
2970
{
2968
- if (__sync_bool_compare_and_swap (& child_status -> m_state ,
2969
- LSAPI_STATE_CONNECTED ,
2970
- LSAPI_STATE_IDLE ))
2971
+ if (__atomic_compare_exchange_n (& child_status -> m_state ,
2972
+ & expect_connected ,
2973
+ LSAPI_STATE_IDLE , 1 ,
2974
+ __ATOMIC_SEQ_CST ,
2975
+ __ATOMIC_SEQ_CST ))
2971
2976
{
2972
2977
if (s_busy_workers )
2973
- __sync_fetch_and_sub (s_busy_workers , 1 );
2978
+ __atomic_fetch_sub (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
2974
2979
}
2975
- else if (__sync_bool_compare_and_swap (& child_status -> m_state ,
2976
- LSAPI_STATE_ACCEPTING ,
2977
- LSAPI_STATE_IDLE ))
2980
+ else if (__atomic_compare_exchange_n (& child_status -> m_state ,
2981
+ & expect_accepting ,
2982
+ LSAPI_STATE_IDLE , 1 ,
2983
+ __ATOMIC_SEQ_CST ,
2984
+ __ATOMIC_SEQ_CST ))
2978
2985
{
2979
2986
if (s_accepting_workers )
2980
- __sync_fetch_and_sub (s_accepting_workers , 1 );
2987
+ __atomic_fetch_sub (s_accepting_workers , 1 , __ATOMIC_SEQ_CST );
2981
2988
}
2982
2989
child_status -> m_pid = 0 ;
2983
2990
-- g_prefork_server -> m_iCurChildren ;
@@ -3250,7 +3257,7 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3250
3257
{
3251
3258
int accepting = 0 ;
3252
3259
if (s_accepting_workers )
3253
- accepting = __sync_add_and_fetch (s_accepting_workers , 0 );
3260
+ accepting = __atomic_load_n (s_accepting_workers , __ATOMIC_SEQ_CST );
3254
3261
3255
3262
if (pServer -> m_iCurChildren > 0
3256
3263
&& accepting > 0 )
@@ -3316,10 +3323,10 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3316
3323
if (pthread_atfork_func )
3317
3324
(* pthread_atfork_func )(NULL , NULL , set_skip_write );
3318
3325
3319
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
3320
- LSAPI_STATE_CONNECTED );
3326
+ __atomic_store_n (& s_worker_status -> m_state ,
3327
+ LSAPI_STATE_CONNECTED , __ATOMIC_SEQ_CST );
3321
3328
if (s_busy_workers )
3322
- __sync_add_and_fetch (s_busy_workers , 1 );
3329
+ __atomic_add_fetch (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
3323
3330
lsapi_set_nblock ( pReq -> m_fd , 0 );
3324
3331
//keep it open if busy_count is used.
3325
3332
if (!s_keep_listener && s_busy_workers
@@ -3391,7 +3398,7 @@ int LSAPI_Postfork_Child(LSAPI_Request * pReq)
3391
3398
{
3392
3399
int max_children = g_prefork_server -> m_iMaxChildren ;
3393
3400
s_pid = getpid ();
3394
- __sync_lock_test_and_set (& pReq -> child_status -> m_pid , s_pid );
3401
+ __atomic_store_n (& pReq -> child_status -> m_pid , s_pid , __ATOMIC_SEQ_CST );
3395
3402
s_worker_status = pReq -> child_status ;
3396
3403
3397
3404
setsid ();
@@ -3403,10 +3410,10 @@ int LSAPI_Postfork_Child(LSAPI_Request * pReq)
3403
3410
if (pthread_atfork_func )
3404
3411
(* pthread_atfork_func )(NULL , NULL , set_skip_write );
3405
3412
3406
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
3407
- LSAPI_STATE_CONNECTED );
3413
+ __atomic_store_n (& s_worker_status -> m_state ,
3414
+ LSAPI_STATE_CONNECTED , __ATOMIC_SEQ_CST );
3408
3415
if (s_busy_workers )
3409
- __sync_add_and_fetch (s_busy_workers , 1 );
3416
+ __atomic_add_fetch (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
3410
3417
lsapi_set_nblock ( pReq -> m_fd , 0 );
3411
3418
//keep it open if busy_count is used.
3412
3419
if (!s_keep_listener && s_busy_workers
@@ -3523,7 +3530,7 @@ int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq)
3523
3530
{
3524
3531
int accepting = 0 ;
3525
3532
if (s_accepting_workers )
3526
- accepting = __sync_add_and_fetch (s_accepting_workers , 0 );
3533
+ accepting = __atomic_load_n (s_accepting_workers , __ATOMIC_SEQ_CST );
3527
3534
3528
3535
if (pServer -> m_iCurChildren > 0
3529
3536
&& accepting > 0 )
@@ -3608,7 +3615,7 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
3608
3615
}
3609
3616
else if (s_req_processed > 0 && s_max_busy_workers > 0 && s_busy_workers )
3610
3617
{
3611
- ret = __sync_fetch_and_add (s_busy_workers , 0 );
3618
+ ret = __atomic_load_n (s_busy_workers , __ATOMIC_SEQ_CST );
3612
3619
if (ret >= s_max_busy_workers )
3613
3620
{
3614
3621
send_conn_close_notification (pReq -> m_fd );
@@ -3652,19 +3659,19 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
3652
3659
if (fd == pReq -> m_fdListen )
3653
3660
{
3654
3661
if (s_worker_status )
3655
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
3656
- LSAPI_STATE_ACCEPTING );
3662
+ __atomic_store_n (& s_worker_status -> m_state ,
3663
+ LSAPI_STATE_ACCEPTING , __ATOMIC_SEQ_CST );
3657
3664
if (s_accepting_workers )
3658
- __sync_fetch_and_add (s_accepting_workers , 1 );
3665
+ __atomic_fetch_add (s_accepting_workers , 1 , __ATOMIC_SEQ_CST );
3659
3666
}
3660
3667
ret = (* g_fnSelect )(fd + 1 , & readfds , NULL , NULL , & timeout );
3661
3668
if (fd == pReq -> m_fdListen )
3662
3669
{
3663
3670
if (s_accepting_workers )
3664
- __sync_fetch_and_sub (s_accepting_workers , 1 );
3671
+ __atomic_fetch_sub (s_accepting_workers , 1 , __ATOMIC_SEQ_CST );
3665
3672
if (s_worker_status )
3666
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
3667
- LSAPI_STATE_IDLE );
3673
+ __atomic_store_n (& s_worker_status -> m_state ,
3674
+ LSAPI_STATE_IDLE , __ATOMIC_SEQ_CST );
3668
3675
}
3669
3676
3670
3677
if ( ret == 0 )
@@ -3712,10 +3719,11 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
3712
3719
if ( pReq -> m_fd != -1 )
3713
3720
{
3714
3721
if (s_worker_status )
3715
- __sync_lock_test_and_set (& s_worker_status -> m_state ,
3716
- LSAPI_STATE_CONNECTED );
3722
+ __atomic_store_n (& s_worker_status -> m_state ,
3723
+ LSAPI_STATE_CONNECTED ,
3724
+ __ATOMIC_SEQ_CST );
3717
3725
if (s_busy_workers )
3718
- __sync_fetch_and_add (s_busy_workers , 1 );
3726
+ __atomic_fetch_add (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
3719
3727
3720
3728
fd = pReq -> m_fd ;
3721
3729
@@ -4386,5 +4394,5 @@ int LSAPI_Set_Restored_Parent_Pid(int pid)
4386
4394
4387
4395
int LSAPI_Inc_Req_Processed (int cnt )
4388
4396
{
4389
- return __sync_add_and_fetch (s_global_counter , cnt );
4397
+ return __atomic_add_fetch (s_global_counter , cnt , __ATOMIC_SEQ_CST );
4390
4398
}
0 commit comments