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_suspend                       PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function suspends a thread on a TCP service within             */
45 /*    NetX.                                                               */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    suspension_list_head                  Pointer to the suspension list*/
50 /*    mutex_ptr                             Pointer to mutex to release   */
51 /*    suspend_cleanup                       Suspension cleanup routine    */
52 /*    wait_option                           Optional timeout value        */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    tx_mutex_put                          Release protection            */
61 /*    _tx_thread_system_suspend             Suspend thread                */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    _nx_tcp_client_socket_bind            Client socket bind processing */
66 /*    _nx_tcp_client_socket_connect         Client socket connect         */
67 /*                                            processing                  */
68 /*    _nx_tcp_socket_disconnect             Socket disconnect processing  */
69 /*    _nx_tcp_socket_receive                Socket receive processing     */
70 /*    _nx_tcp_socket_send                   Socket send processing        */
71 /*    _nx_tcp_server_socket_accept          Server socket accept          */
72 /*                                            processing                  */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
79 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
80 /*                                            resulting in version 6.1    */
81 /*                                                                        */
82 /**************************************************************************/
_nx_tcp_socket_thread_suspend(TX_THREAD ** suspension_list_head,VOID (* suspend_cleanup)(TX_THREAD * NX_CLEANUP_PARAMETER),NX_TCP_SOCKET * socket_ptr,TX_MUTEX * mutex_ptr,ULONG wait_option)83 VOID  _nx_tcp_socket_thread_suspend(TX_THREAD **suspension_list_head, VOID (*suspend_cleanup)(TX_THREAD * NX_CLEANUP_PARAMETER), NX_TCP_SOCKET *socket_ptr, TX_MUTEX *mutex_ptr, ULONG wait_option)
84 {
85 
86 TX_INTERRUPT_SAVE_AREA
87 
88 TX_THREAD *thread_ptr;
89 
90 
91     /* Disable interrupts.  */
92     TX_DISABLE
93 
94     /* Pickup thread pointer.  */
95     thread_ptr =  _tx_thread_current_ptr;
96 
97     /* Setup suspension list.  */
98     if (*suspension_list_head)
99     {
100 
101         /* This list is not NULL, add current thread to the end. */
102         thread_ptr -> tx_thread_suspended_next =      *suspension_list_head;
103         thread_ptr -> tx_thread_suspended_previous =  (*suspension_list_head) -> tx_thread_suspended_previous;
104         ((*suspension_list_head) -> tx_thread_suspended_previous) -> tx_thread_suspended_next =  thread_ptr;
105         (*suspension_list_head) -> tx_thread_suspended_previous =   thread_ptr;
106     }
107     else
108     {
109 
110         /* No other threads are suspended.  Setup the head pointer and
111            just setup this threads pointers to itself.  */
112         *suspension_list_head =  thread_ptr;
113         thread_ptr -> tx_thread_suspended_next =        thread_ptr;
114         thread_ptr -> tx_thread_suspended_previous =    thread_ptr;
115     }
116 
117     /* Setup cleanup routine pointer.  */
118     thread_ptr -> tx_thread_suspend_cleanup =  suspend_cleanup;
119 
120     /* Setup cleanup information, i.e. this pool control
121        block.  */
122     thread_ptr -> tx_thread_suspend_control_block =  (void *)socket_ptr;
123 
124     /* Set the state to suspended.  */
125     thread_ptr -> tx_thread_state =  TX_TCP_IP;
126 
127     /* Set the suspending flag.  */
128     thread_ptr -> tx_thread_suspending =  TX_TRUE;
129 
130     /* Temporarily disable preemption.  */
131     _tx_thread_preempt_disable++;
132 
133     /* Save the timeout value.  */
134     thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks =  wait_option;
135 
136     /* Restore interrupts.  */
137     TX_RESTORE
138 
139     /* Release protection.  */
140     tx_mutex_put(mutex_ptr);
141 
142     /* Call actual thread suspension routine.  */
143     _tx_thread_system_suspend(thread_ptr);
144 }
145 
146