polar-objc 5.44
An Objective-C runtime library
Loading...
Searching...
No Matches
polar-thread-message.c
1/* Internal API: inter-thread message 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_thread_message ************************************************************************************************
18// Private =============================================================================================================
19
20// This variable is initialized in polar_thread_message_get_type()
21static polar_thread_message_class class_polar_thread_message;
22
23// This variable is initialized in _polar_thread_message_class_init()
24static polar_runtime_object_class *superclass_polar_thread_message = NULL;
25
26// ---------------------------------------------------------------------------------------------------------------------
27
28POLAR_FUNCTION_INTERNAL polar_thread_message *
29_polar_thread_message_init( polar_thread_message *self )
30{
31 // Chain up first
32 self = (polar_thread_message *)
33 POLAR_RUNTIME_OBJECT_CLASS(superclass_polar_thread_message)->init( (polar_runtime_object *)self );
34
35 if( self != NULL )
36 {
37 self->count_references = 1;
38 }
39
40 return self;
41}
42
43POLAR_FUNCTION_INTERNAL polar_runtime_object *
44_polar_thread_message_finalize( polar_thread_message *self )
45{
46 if( objc_atomic_decrement_intptr(&self->count_references) != 0 )
47 return (polar_runtime_object *)self;
48
49 // Chain up
50 return POLAR_RUNTIME_OBJECT_CLASS(superclass_polar_thread_message)->finalize( (polar_runtime_object *)self );
51}
52
53POLAR_FUNCTION_INTERNAL void
54_polar_thread_message_deallocate( polar_thread_message *self )
55{
56 size_t size_message_total;
57
58 if( objc_atomic_compare_exchange_intptr(&self->count_references, 0, -1) == NO )
59 return;
60
61 size_message_total = polar_runtime_object_get_size( (polar_runtime_object *)self );
62 if( self->size_message_data > OBJC_SIZEOF_POINTER )
63 size_message_total += self->size_message_data;
64
65 polar_memory_free(self, size_message_total);
66}
67
68POLAR_FUNCTION_INTERNAL void
69_polar_thread_message_class_init( polar_thread_message_class *self )
70{
71 // Acquire our superclass
72 superclass_polar_thread_message = POLAR_RUNTIME_OBJECT_CLASS(self)->class_superclass;
73
74 // Establish our virtual methods
75 ( (polar_runtime_object_class *)self )->init = (polar_func_runtime_object_lifecycle)_polar_thread_message_init;
76
77 ( (polar_runtime_object_class *)self )->finalize =
78 (polar_func_runtime_object_lifecycle)_polar_thread_message_finalize;
79
80 ( (polar_runtime_object_class *)self )->deallocate =
81 (polar_func_runtime_object_deallocate)_polar_thread_message_deallocate;
82}
83
84static struct polar_internal_type_info pti_polar_thread_message =
85{
86 .name_type = "polar_thread_message",
87 .size_type_instance = sizeof(polar_thread_message),
88 .size_type_class = sizeof(polar_thread_message_class),
89 .type_class_init = (polar_func_runtime_object_class_lifecycle)_polar_thread_message_class_init
90};
91
92// Public ==============================================================================================================
93
94POLAR_FUNCTION_INTERNAL OBJC_FUNCTION_CONSTANT polar_internal_type
95polar_thread_message_get_type( void )
96{
97 static polar_internal_type pt_polar_thread_message = POLAR_INTERNAL_TYPE_INVALID;
98
99 if( pt_polar_thread_message != POLAR_INTERNAL_TYPE_INVALID )
100 return pt_polar_thread_message;
101
102 pt_polar_thread_message = polar_internal_type_init( POLAR_TYPE_LIST_NODE, &class_polar_thread_message,
103 &pti_polar_thread_message );
104
105 return pt_polar_thread_message;
106}
107
108POLAR_FUNCTION_INTERNAL polar_thread_message *
109polar_thread_message_new( uintptr_t message_subject, size_t size_message_data, void *message_data )
110{
111 polar_thread_message *result;
112 size_t size_message_total;
113
114 size_message_total = sizeof(*result);
115 if( size_message_data > OBJC_SIZEOF_POINTER )
116 size_message_total += size_message_data;
117
118 result = (polar_thread_message *)polar_memory_allocate(size_message_total);
119 result = (polar_thread_message *)polar_runtime_object_preallocated_init(result, POLAR_TYPE_THREAD_MESSAGE, NO);
120
121 result->subject_message = message_subject;
122 result->size_message_data = size_message_data;
123
124 if( size_message_data > OBJC_SIZEOF_POINTER )
125 {
126 result->message_data = &result[1];
127 objc_memcpy(result->message_data, message_data, size_message_data);
128 }
129
130 else
131 result->message_data = message_data;
132
133 return result;
134}
135
136POLAR_FUNCTION_INTERNAL polar_thread_message *
137polar_thread_message_preallocated_init( void *p_message, size_t size_message, uintptr_t message_subject,
138 size_t size_message_data, void *message_data )
139{
140 polar_thread_message *result;
141
142 assert( p_message != NULL );
143 assert( size_message >= sizeof(*result) );
144
145 result = (polar_thread_message *)polar_runtime_object_preallocated_init(p_message, POLAR_TYPE_THREAD_MESSAGE, YES);
146
147 result->subject_message = message_subject;
148 result->count_references = -( MAXINT >> 1 );
149 result->size_message_data = size_message_data;
150 result->message_data = message_data;
151
152 return result;
153}
Definition polar-internal-type.h:34
Definition polar-runtime-object.h:36
Definition polar-runtime-object.h:31
Definition polar-thread-message.h:36
Definition polar-thread-message.h:25