1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Transmission Control Protocol (TCP)                                 */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_api.h"
29 #include "nx_tcp.h"
30 #include "tx_thread.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nx_tcp_socket_thread_suspend                       PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function suspends a thread on a TCP service within             */
46 /*    NetX.                                                               */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    suspension_list_head                  Pointer to the suspension list*/
51 /*    mutex_ptr                             Pointer to mutex to release   */
52 /*    suspend_cleanup                       Suspension cleanup routine    */
53 /*    wait_option                           Optional timeout value        */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    tx_mutex_put                          Release protection            */
62 /*    _tx_thread_system_suspend             Suspend thread                */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _nx_tcp_client_socket_bind            Client socket bind processing */
67 /*    _nx_tcp_client_socket_connect         Client socket connect         */
68 /*                                            processing                  */
69 /*    _nx_tcp_socket_disconnect             Socket disconnect processing  */
70 /*    _nx_tcp_socket_receive                Socket receive processing     */
71 /*    _nx_tcp_socket_send                   Socket send processing        */
72 /*    _nx_tcp_server_socket_accept          Server socket accept          */
73 /*                                            processing                  */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
80 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
81 /*                                            resulting in version 6.1    */
82 /*                                                                        */
83 /**************************************************************************/
_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 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)
85 {
86 
87 TX_INTERRUPT_SAVE_AREA
88 
89 TX_THREAD *thread_ptr;
90 
91 
92     /* Disable interrupts.  */
93     TX_DISABLE
94 
95     /* Pickup thread pointer.  */
96     thread_ptr =  _tx_thread_current_ptr;
97 
98     /* Setup suspension list.  */
99     if (*suspension_list_head)
100     {
101 
102         /* This list is not NULL, add current thread to the end. */
103         thread_ptr -> tx_thread_suspended_next =      *suspension_list_head;
104         thread_ptr -> tx_thread_suspended_previous =  (*suspension_list_head) -> tx_thread_suspended_previous;
105         ((*suspension_list_head) -> tx_thread_suspended_previous) -> tx_thread_suspended_next =  thread_ptr;
106         (*suspension_list_head) -> tx_thread_suspended_previous =   thread_ptr;
107     }
108     else
109     {
110 
111         /* No other threads are suspended.  Setup the head pointer and
112            just setup this threads pointers to itself.  */
113         *suspension_list_head =  thread_ptr;
114         thread_ptr -> tx_thread_suspended_next =        thread_ptr;
115         thread_ptr -> tx_thread_suspended_previous =    thread_ptr;
116     }
117 
118     /* Setup cleanup routine pointer.  */
119     thread_ptr -> tx_thread_suspend_cleanup =  suspend_cleanup;
120 
121     /* Setup cleanup information, i.e. this pool control
122        block.  */
123     thread_ptr -> tx_thread_suspend_control_block =  (void *)socket_ptr;
124 
125     /* Set the state to suspended.  */
126     thread_ptr -> tx_thread_state =  TX_TCP_IP;
127 
128     /* Set the suspending flag.  */
129     thread_ptr -> tx_thread_suspending =  TX_TRUE;
130 
131     /* Temporarily disable preemption.  */
132     _tx_thread_preempt_disable++;
133 
134     /* Save the timeout value.  */
135     thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks =  wait_option;
136 
137     /* Restore interrupts.  */
138     TX_RESTORE
139 
140     /* Release protection.  */
141     tx_mutex_put(mutex_ptr);
142 
143     /* Call actual thread suspension routine.  */
144     _tx_thread_system_suspend(thread_ptr);
145 }
146 
147