@@ -431,9 +431,10 @@ static void lsapi_close_connection(LSAPI_Request *pReq)
431431 lsapi_close (pReq -> m_fd );
432432 pReq -> m_fd = -1 ;
433433 if (s_busy_workers )
434- __sync_fetch_and_sub (s_busy_workers , 1 );
434+ __atomic_fetch_sub (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
435435 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 );
437438}
438439
439440
@@ -1577,10 +1578,10 @@ int LSAPI_Accept_r( LSAPI_Request * pReq )
15771578 else
15781579 {
15791580 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 );
15821583 if (s_busy_workers )
1583- __sync_fetch_and_add (s_busy_workers , 1 );
1584+ __atomic_fetch_add (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
15841585 lsapi_set_nblock ( pReq -> m_fd , 0 );
15851586 if (((struct sockaddr * )& achPeer )-> sa_family == AF_INET )
15861587 {
@@ -2870,16 +2871,18 @@ void LSAPI_reset_server_state( void )
28702871 ++ pStatus ;
28712872 }
28722873 if (s_busy_workers )
2873- __sync_lock_release (s_busy_workers );
2874+ __atomic_store_n (s_busy_workers , 0 , __ATOMIC_SEQ_CST );
28742875 if (s_accepting_workers )
2875- __sync_lock_release (s_accepting_workers );
2876+ __atomic_store_n (s_accepting_workers , 0 , __ATOMIC_SEQ_CST );
28762877
28772878}
28782879
28792880
28802881static void lsapi_sigchild ( int signal )
28812882{
28822883 int status , pid ;
2884+ char expect_connected = LSAPI_STATE_CONNECTED ;
2885+ char expect_accepting = LSAPI_STATE_ACCEPTING ;
28832886 lsapi_child_status * child_status ;
28842887 if (g_prefork_server == NULL )
28852888 return ;
@@ -2916,19 +2919,23 @@ static void lsapi_sigchild( int signal )
29162919 child_status = find_child_status ( pid );
29172920 if ( child_status )
29182921 {
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 ))
29222927 {
29232928 if (s_busy_workers )
2924- __sync_fetch_and_sub (s_busy_workers , 1 );
2929+ __atomic_fetch_sub (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
29252930 }
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 ))
29292936 {
29302937 if (s_accepting_workers )
2931- __sync_fetch_and_sub (s_accepting_workers , 1 );
2938+ __atomic_fetch_sub (s_accepting_workers , 1 , __ATOMIC_SEQ_CST );
29322939 }
29332940 child_status -> m_pid = 0 ;
29342941 -- g_prefork_server -> m_iCurChildren ;
@@ -3201,7 +3208,7 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
32013208 {
32023209 int accepting = 0 ;
32033210 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 );
32053212
32063213 if (pServer -> m_iCurChildren > 0
32073214 && accepting > 0 )
@@ -3267,10 +3274,10 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
32673274 if (pthread_atfork_func )
32683275 (* pthread_atfork_func )(NULL , NULL , set_skip_write );
32693276
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 );
32723279 if (s_busy_workers )
3273- __sync_add_and_fetch (s_busy_workers , 1 );
3280+ __atomic_add_fetch (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
32743281 lsapi_set_nblock ( pReq -> m_fd , 0 );
32753282 //keep it open if busy_count is used.
32763283 if (!s_keep_listener && s_busy_workers
@@ -3342,7 +3349,7 @@ int LSAPI_Postfork_Child(LSAPI_Request * pReq)
33423349{
33433350 int max_children = g_prefork_server -> m_iMaxChildren ;
33443351 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 );
33463353 s_worker_status = pReq -> child_status ;
33473354
33483355 setsid ();
@@ -3354,10 +3361,10 @@ int LSAPI_Postfork_Child(LSAPI_Request * pReq)
33543361 if (pthread_atfork_func )
33553362 (* pthread_atfork_func )(NULL , NULL , set_skip_write );
33563363
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 );
33593366 if (s_busy_workers )
3360- __sync_add_and_fetch (s_busy_workers , 1 );
3367+ __atomic_add_fetch (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
33613368 lsapi_set_nblock ( pReq -> m_fd , 0 );
33623369 //keep it open if busy_count is used.
33633370 if (!s_keep_listener && s_busy_workers
@@ -3474,7 +3481,7 @@ int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq)
34743481 {
34753482 int accepting = 0 ;
34763483 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 );
34783485
34793486 if (pServer -> m_iCurChildren > 0
34803487 && accepting > 0 )
@@ -3559,7 +3566,7 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
35593566 }
35603567 else if (s_req_processed > 0 && s_max_busy_workers > 0 && s_busy_workers )
35613568 {
3562- ret = __sync_fetch_and_add (s_busy_workers , 0 );
3569+ ret = __atomic_load_n (s_busy_workers , __ATOMIC_SEQ_CST );
35633570 if (ret >= s_max_busy_workers )
35643571 {
35653572 send_conn_close_notification (pReq -> m_fd );
@@ -3603,19 +3610,19 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
36033610 if (fd == pReq -> m_fdListen )
36043611 {
36053612 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 );
36083615 if (s_accepting_workers )
3609- __sync_fetch_and_add (s_accepting_workers , 1 );
3616+ __atomic_fetch_add (s_accepting_workers , 1 , __ATOMIC_SEQ_CST );
36103617 }
36113618 ret = (* g_fnSelect )(fd + 1 , & readfds , NULL , NULL , & timeout );
36123619 if (fd == pReq -> m_fdListen )
36133620 {
36143621 if (s_accepting_workers )
3615- __sync_fetch_and_sub (s_accepting_workers , 1 );
3622+ __atomic_fetch_sub (s_accepting_workers , 1 , __ATOMIC_SEQ_CST );
36163623 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 );
36193626 }
36203627
36213628 if ( ret == 0 )
@@ -3663,10 +3670,11 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
36633670 if ( pReq -> m_fd != -1 )
36643671 {
36653672 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 );
36683676 if (s_busy_workers )
3669- __sync_fetch_and_add (s_busy_workers , 1 );
3677+ __atomic_fetch_add (s_busy_workers , 1 , __ATOMIC_SEQ_CST );
36703678
36713679 fd = pReq -> m_fd ;
36723680
@@ -4337,5 +4345,5 @@ int LSAPI_Set_Restored_Parent_Pid(int pid)
43374345
43384346int LSAPI_Inc_Req_Processed (int cnt )
43394347{
4340- return __sync_add_and_fetch (s_global_counter , cnt );
4348+ return __atomic_add_fetch (s_global_counter , cnt , __ATOMIC_SEQ_CST );
43414349}
0 commit comments