/***************************************************************************
 * Copyright (C) 2026 Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * AI Disclosure: This file was largely AI-generated by Codex (gpt 5.4).
 * The AI-generated portions may be considered public domain (CC0-1.0)
 * and not subject to the project's licence. The human contributor has
 * reviewed and verified that the code is correct.
 *
 * SPDX-License-Identifier: MIT and CC0-1.0
 **************************************************************************/

#define TX_SOURCE_CODE
#define TX_THREAD_SMP_SOURCE_CODE

#include "tx_api.h"
#include "tx_thread.h"

UINT  _tx_thread_interrupt_disable(void)
{
UINT    previous_value;

    previous_value =  _tx_thread_interrupt_control(TX_INT_DISABLE);
    return(previous_value);
}


VOID  _tx_thread_interrupt_restore(UINT previous_posture)
{
    previous_posture =  _tx_thread_interrupt_control(previous_posture);
    TX_PARAMETER_NOT_USED(previous_posture);
}


UINT  _tx_thread_interrupt_control(UINT new_posture)
{
UINT        old_posture;
TX_THREAD   *thread_ptr;
DWORD       thread_id;
UINT        core;

    _tx_win32_critical_section_obtain(&_tx_win32_critical_section);

#ifdef TX_WIN32_DEBUG_ENABLE
    if (new_posture == TX_INT_ENABLE)
    {
        _tx_win32_debug_entry_insert("RESTORE", __FILE__, __LINE__);
    }
    else
    {
        _tx_win32_debug_entry_insert("DISABLE", __FILE__, __LINE__);
    }
#endif

    thread_id =  GetCurrentThreadId();
    core =       _tx_thread_smp_core_get();
    thread_ptr = _tx_thread_current_ptr[core];

    if ((_tx_win32_threadx_thread != 0) &&
        ((thread_ptr == TX_NULL) || (thread_ptr -> tx_thread_win32_thread_id != thread_id)))
    {
        _tx_win32_critical_section_release_all(&_tx_win32_critical_section);
        ExitThread(0);
    }

    if (_tx_win32_critical_section.tx_win32_critical_section_nested_count == 1U)
    {
        old_posture =  TX_INT_ENABLE;
    }
    else
    {
        old_posture =  TX_INT_DISABLE;
    }

    if (_tx_thread_system_state[core] != 0UL)
    {
        if (new_posture == TX_INT_ENABLE)
        {
            _tx_win32_global_int_disabled_flag =  TX_FALSE;
            _tx_win32_critical_section_release_all(&_tx_win32_critical_section);
        }
        else if (new_posture == TX_INT_DISABLE)
        {
            _tx_win32_global_int_disabled_flag =  TX_TRUE;
        }
    }
    else if (thread_ptr != TX_NULL)
    {
        if (new_posture == TX_INT_ENABLE)
        {
            thread_ptr -> tx_thread_win32_int_disabled_flag =  TX_FALSE;
            _tx_win32_critical_section_release_all(&_tx_win32_critical_section);
        }
        else if (new_posture == TX_INT_DISABLE)
        {
            thread_ptr -> tx_thread_win32_int_disabled_flag =  TX_TRUE;
        }
    }

    return(old_posture);
}
