summaryrefslogtreecommitdiff
path: root/ports/linux/gnu/src/tx_thread_context_save.c
blob: c98943fa919022de8d00d19eaddceba9ba4790c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation 
 * 
 * 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.
 * 
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */ 
/** ThreadX Component                                                     */ 
/**                                                                       */
/**   Thread                                                              */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


#define TX_SOURCE_CODE


/* Include necessary system files.  */

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


/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _tx_thread_context_save                             Linux/GNU       */ 
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */ 
/*    This function saves the context of an executing thread in the       */ 
/*    beginning of interrupt processing.  The function also ensures that  */ 
/*    the system stack is used upon return to the calling ISR.            */ 
/*                                                                        */ 
/*  INPUT                                                                 */ 
/*                                                                        */ 
/*    None                                                                */ 
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    None                                                                */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*    _tx_linux_debug_entry_insert                                        */ 
/*    tx_linux_mutex_lock                                                 */ 
/*    _tx_linux_thread_suspend                                            */ 
/*    tx_linux_mutex_unlock                                               */ 
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    ISRs                                                                */ 
/*                                                                        */ 
/*  RELEASE HISTORY                                                       */ 
/*                                                                        */ 
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  09-30-2020     William E. Lamie         Initial Version 6.1           */
/*                                                                        */
/**************************************************************************/
VOID   _tx_thread_context_save(VOID)
{

    /* Debug entry.  */
    _tx_linux_debug_entry_insert("CONTEXT_SAVE", __FILE__, __LINE__);

    /* Lock mutex to ensure other threads are not playing with
       the core ThreadX data structures.  */
    tx_linux_mutex_lock(_tx_linux_mutex);

    /* If an application thread is running, suspend it to simulate preemption. */
    if ((_tx_thread_current_ptr) && (_tx_thread_system_state == 0))
    {

        /* Debug entry.  */
        _tx_linux_debug_entry_insert("CONTEXT_SAVE-suspend_thread", __FILE__, __LINE__);

        /* Yes, this is the first interrupt and an application thread is running...
           suspend it!  */
        _tx_linux_thread_suspend(_tx_thread_current_ptr -> tx_thread_linux_thread_id);

        /* Indicate that this thread was suspended asynchronously.  */
        _tx_thread_current_ptr -> tx_thread_linux_suspension_type =  1;
    }

    /* Increment the nested interrupt condition.  */
    _tx_thread_system_state++;

    /* Unlock linux mutex. */
    tx_linux_mutex_unlock(_tx_linux_mutex);
}