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