1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   Transmission Control Protocol (TCP)                                 */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "nx_api.h"
28 #include "nx_tcp.h"
29 #include "tx_thread.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_tcp_socket_thread_resume                        PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function resumes a thread suspended on a TCP service within    */
45 /*    NetX.                                                               */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    thread_ptr                            Pointer to thread to resume   */
50 /*    status                                Return status                 */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _tx_thread_system_resume              Resume suspended thread       */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    _nx_tcp_client_socket_unbind          Client socket unbind          */
63 /*                                            processing                  */
64 /*    _nx_tcp_socket_connection_reset       Received reset processing     */
65 /*    _nx_tcp_socket_state_closing          Socket state closing          */
66 /*                                            processing                  */
67 /*    _nx_tcp_socket_state_data_check       Socket data check processing  */
68 /*    _nx_tcp_socket_state_fin_wait2        Socket FIN wait-2 processing  */
69 /*    _nx_tcp_socket_state_fin_wait1        Socket FIN wait-1 processing  */
70 /*    _nx_tcp_socket_state_last_ack         Socket last ack processing    */
71 /*    _nx_tcp_socket_state_syn_received     Socket SYN received           */
72 /*                                            processing                  */
73 /*    _nx_tcp_socket_state_syn_sent         Socket SYN sent processing    */
74 /*    _nx_tcp_socket_state_transmit_check   Socket transmit check         */
75 /*                                            processing                  */
76 /*                                                                        */
77 /*  RELEASE HISTORY                                                       */
78 /*                                                                        */
79 /*    DATE              NAME                      DESCRIPTION             */
80 /*                                                                        */
81 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
82 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
83 /*                                            resulting in version 6.1    */
84 /*                                                                        */
85 /**************************************************************************/
_nx_tcp_socket_thread_resume(TX_THREAD ** suspension_list_head,UINT status)86 VOID  _nx_tcp_socket_thread_resume(TX_THREAD **suspension_list_head, UINT status)
87 {
88 
89 TX_INTERRUPT_SAVE_AREA
90 
91 TX_THREAD *thread_ptr;
92 
93 
94     /* Disable interrupts.  */
95     TX_DISABLE
96 
97     /* Pickup the thread pointer.  */
98     thread_ptr =  *suspension_list_head;
99 
100     /* Determine if there still is a thread suspended.  */
101     if (thread_ptr)
102     {
103 
104         /* Determine if there are anymore threads on the suspension list.  */
105         if (thread_ptr == thread_ptr -> tx_thread_suspended_next)
106         {
107 
108             /* Only this thread is on the suspension list.  Simply set the
109                list head to NULL to reflect an empty suspension list.  */
110             *suspension_list_head =  TX_NULL;
111         }
112         else
113         {
114 
115             /* More than one thread is on the suspension list, we need to
116                adjust the link pointers and move the next entry to the
117                front of the list.  */
118             *suspension_list_head =  thread_ptr -> tx_thread_suspended_next;
119 
120             /* Update the links of the adjacent threads.  */
121             (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
122                 thread_ptr -> tx_thread_suspended_previous;
123             (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
124                 thread_ptr -> tx_thread_suspended_next;
125         }
126 
127         /* Prepare for resumption of the thread.  */
128 
129         /* Clear cleanup routine to avoid timeout.  */
130         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
131 
132         /* Temporarily disable preemption.  */
133         _tx_thread_preempt_disable++;
134 
135         /* Restore interrupts.  */
136         TX_RESTORE
137 
138         /* Put return status into the thread control block.  */
139         thread_ptr -> tx_thread_suspend_status =  status;
140 
141         /* Resume thread.  */
142         _tx_thread_system_resume(thread_ptr);
143     }
144     else
145     {
146 
147         /* Nothing was suspended.  Simply restore interrupts.  */
148         TX_RESTORE
149     }
150 }
151 
152