polar-objc 5.44
An Objective-C runtime library
Loading...
Searching...
No Matches
polar-sync-table-node.c
1/* Internal API: sync table node type for polar-objc.
2 Copyright (C) 2022-2025 Michael Malicoat <[email protected]>
3
4 This file is part of polar-objc.
5
6 polar-objc is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
8
9 polar-objc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 details.
12
13 You should have received a copy of the GNU General Public License along with this program; see the file LICENSE. If
14 not, see <http://www.gnu.org/licenses/>.
15*/
16
17// polar_sync_table_node ***********************************************************************************************
18// Private =============================================================================================================
19
20// This variable is initialized in polar_sync_table_node_get_type()
21static polar_sync_table_node_class class_polar_sync_table_node;
22
23// This variable is initialized in _polar_sync_table_node_class_init()
24static polar_runtime_object_class *superclass_polar_sync_table_node = NULL;
25
26// ---------------------------------------------------------------------------------------------------------------------
27
28POLAR_FUNCTION_INTERNAL polar_runtime_object *
29_polar_sync_table_node_finalize( polar_sync_table_node *self )
30{
31 polar_list_node_iterator iter_waiting_requests;
32 polar_thread_message *request_current;
33 polar_thread_message_queue *request_sender;
34
35 /* Release any threads waiting on the target object with an error code, so they don't block forever. The error code
36 was already set as the default return value by polar_runtime_thread_sync_begin() or
37 polar_runtime_thread_sync_end(), so we don't need to modify the content of the message.
38 */
39 if( self->request_waiting_first != NULL )
40 {
41 polar_runtime_object_preallocated_init( &iter_waiting_requests, POLAR_TYPE_LIST_NODE_ITERATOR, YES );
42 polar_list_node_iterator_set_target( &iter_waiting_requests, (polar_list_node *)(self->request_waiting_first) );
43
44 while( polar_iterator_get_next((polar_iterator *)(&iter_waiting_requests), (void **)(&request_current)) )
45 {
46 // Return to sender
47 request_sender = request_current->sender_message;
48 request_current->sender_message = request_current->recipient_message;
49 request_current->recipient_message = NULL;
50
51 polar_thread_message_send(request_current, request_sender);
52 }
53
54 polar_runtime_object_preallocated_finalize( &iter_waiting_requests );
55
56 self->request_waiting_first = NULL;
57 self->request_waiting_last = NULL;
58 }
59
60 // Chain up
61 return POLAR_RUNTIME_OBJECT_CLASS(superclass_polar_sync_table_node)->finalize( (polar_runtime_object *)self );
62}
63
64POLAR_FUNCTION_INTERNAL void
65_polar_sync_table_node_class_init( polar_sync_table_node_class *self )
66{
67 // Acquire our superclass
68 superclass_polar_sync_table_node = POLAR_RUNTIME_OBJECT_CLASS(self)->class_superclass;
69
70 // Establish our virtual methods
71 ( (polar_runtime_object_class *)self )->finalize =
72 (polar_func_runtime_object_lifecycle)_polar_sync_table_node_finalize;
73}
74
75static struct polar_internal_type_info pti_polar_sync_table_node =
76{
77 .name_type = "polar_sync_table_node",
78 .size_type_instance = sizeof(polar_sync_table_node),
79 .size_type_class = sizeof(polar_sync_table_node_class),
80 .type_class_init = (polar_func_runtime_object_class_lifecycle)_polar_sync_table_node_class_init
81};
82
83// Public ==============================================================================================================
84
85POLAR_FUNCTION_INTERNAL OBJC_FUNCTION_CONSTANT polar_internal_type
86polar_sync_table_node_get_type( void )
87{
88 static polar_internal_type pt_polar_sync_table_node = POLAR_INTERNAL_TYPE_INVALID;
89
90 if( pt_polar_sync_table_node != POLAR_INTERNAL_TYPE_INVALID )
91 return pt_polar_sync_table_node;
92
93 pt_polar_sync_table_node = polar_internal_type_init( POLAR_TYPE_HASH_TABLE_NODE_POINTER_KEY,
94 &class_polar_sync_table_node,
95 &pti_polar_sync_table_node );
96
97 return pt_polar_sync_table_node;
98}
99
100POLAR_FUNCTION_INTERNAL void
101polar_sync_table_node_push_request_last( polar_sync_table_node *self, polar_thread_message *service_request )
102{
103 assert( POLAR_IS_SYNC_TABLE_NODE(self) );
104 assert( POLAR_IS_THREAD_MESSAGE(service_request) );
105
106 if( self->request_waiting_last != NULL )
107 ( (polar_list_node *)self->request_waiting_last )->node_next = (polar_list_node *)service_request;
108
109 self->request_waiting_last = service_request;
110
111 if( self->request_waiting_first != NULL )
112 ;
113
114 else
115 self->request_waiting_first = service_request;
116}
117
118POLAR_FUNCTION_INTERNAL polar_thread_message *
119polar_sync_table_node_pop_request_first( polar_sync_table_node *self )
120{
121 polar_thread_message *result;
122
123 assert( POLAR_IS_SYNC_TABLE_NODE(self) );
124
125 result = (polar_thread_message *)
126 polar_list_node_list_pop_first( (polar_list_node **)(&self->request_waiting_first) );
127
128 if( self->request_waiting_first != NULL )
129 ;
130
131 else
132 self->request_waiting_last = NULL;
133
134 return result;
135}
Definition polar-internal-type.h:34
Definition polar-iterator.h:25
Definition polar-list-node-iterator.h:25
Definition polar-list-node.h:25
Definition polar-runtime-object.h:36
Definition polar-runtime-object.h:31
Definition polar-sync-table-node.h:33
Definition polar-sync-table-node.h:24
Definition polar-thread-message-queue.h:23
Definition polar-thread-message.h:25