Debian Codegraph @

libcthreads/libcthreads_repeating_thread.c

raw symbols SCIP
1 /*
2 * Repeating thread functions
3 *
4 * Copyright (C) 2012-2024, Joachim Metz <[email protected]>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23 #include <memory.h>
24 #include <types.h>
25
26 #include <errno.h>
27
28 #if defined( _MSC_VER ) && defined( WINAPI ) && ( WINVER >= 0x0602 )
29 #include <Processthreadsapi.h>
30 #include <Synchapi.h>
31 #endif
32
33 #if defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
34 #include <pthread.h>
35 #endif
36
37 #include "libcthreads_condition.h"
38 #include "libcthreads_definitions.h"
39 #include "libcthreads_libcerror.h"
40 #include "libcthreads_mutex.h"
41 #include "libcthreads_repeating_thread.h"
42 #include "libcthreads_thread_attributes.h"
43 #include "libcthreads_types.h"
44
45 #if !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT )
46
47 #if defined( WINAPI )
48
49 /* Start function helper function for WINAPI
50 * Returns 0 if successful or 1 on error
51 */
52 DWORD WINAPI libcthreads_repeating_thread_start_function_helper(
53 void *arguments )
54 {
55 libcthreads_internal_repeating_thread_t *internal_repeating_thread = NULL;
56 DWORD result = 0;
57 int start_function_result = 0;
58
59 if( arguments != NULL )
60 {
61 internal_repeating_thread = (libcthreads_internal_repeating_thread_t *) arguments;
62
63 if( ( internal_repeating_thread != NULL )
64 && ( internal_repeating_thread->start_function != NULL ) )
65 {
66 internal_repeating_thread->start_function_result = 1;
67
68 do
69 {
70 libcthreads_mutex_grab(
71 internal_repeating_thread->condition_mutex,
72 NULL );
73
74 /* Check for exit status in case of the situation where the thread
75 * is created after the join has been called
76 */
77 while( internal_repeating_thread->status != LIBCTHREADS_STATUS_EXIT )
78 {
79 libcthreads_condition_wait(
80 internal_repeating_thread->status_condition,
81 internal_repeating_thread->condition_mutex,
82 NULL );
83 }
84 libcthreads_mutex_release(
85 internal_repeating_thread->condition_mutex,
86 NULL );
87
88 start_function_result = internal_repeating_thread->start_function(
89 internal_repeating_thread->start_function_arguments );
90
91 if( ( start_function_result != 1 )
92 && ( internal_repeating_thread->start_function_result == 1 ) )
93 {
94 internal_repeating_thread->start_function_result = start_function_result;
95 }
96 }
97 while( internal_repeating_thread->status != LIBCTHREADS_STATUS_EXIT );
98
99 if( internal_repeating_thread->start_function_result != 1 )
100 {
101 result = 1;
102 }
103 }
104 }
105 ExitThread(
106 result );
107 }
108
109 #elif defined( HAVE_PTHREAD_H )
110
111 /* Start function helper function for pthread
112 * Returns a pointer to the start function result if successful or NULL on error
113 */
114 void *libcthreads_repeating_thread_start_function_helper(
115 void *arguments )
116 {
117 libcthreads_internal_repeating_thread_t *internal_repeating_thread = NULL;
118 void *result = NULL;
119 int start_function_result = 0;
120
121 if( arguments != NULL )
122 {
123 internal_repeating_thread = (libcthreads_internal_repeating_thread_t *) arguments;
124
125 if( ( internal_repeating_thread != NULL )
126 && ( internal_repeating_thread->start_function != NULL ) )
127 {
128 internal_repeating_thread->start_function_result = 1;
129
130 do
131 {
132 libcthreads_mutex_grab(
133 internal_repeating_thread->condition_mutex,
134 NULL );
135
136 /* Check for exit status in case of the situation where the thread
137 * is created after the join has been called
138 */
139 while( internal_repeating_thread->status != LIBCTHREADS_STATUS_EXIT )
140 {
141 libcthreads_condition_wait(
142 internal_repeating_thread->status_condition,
143 internal_repeating_thread->condition_mutex,
144 NULL );
145 }
146 libcthreads_mutex_release(
147 internal_repeating_thread->condition_mutex,
148 NULL );
149
150 start_function_result = internal_repeating_thread->start_function(
151 internal_repeating_thread->start_function_arguments );
152
153 if( ( start_function_result != 1 )
154 && ( internal_repeating_thread->start_function_result == 1 ) )
155 {
156 internal_repeating_thread->start_function_result = start_function_result;
157 }
158 }
159 while( internal_repeating_thread->status != LIBCTHREADS_STATUS_EXIT );
160
161 result = (void *) &( internal_repeating_thread->start_function_result );
162 }
163 }
164 pthread_exit(
165 result );
166 }
167
168 #endif /* defined( WINAPI ) else defined( HAVE_PTHREAD_H ) */
169
170 /* Creates a repeating thread
171 * Make sure the value repeating_thread is referencing, is set to NULL
172 *
173 * The start_function should return 1 if successful and -1 on error
174 * Returns 1 if successful or -1 on error
175 */
176 int libcthreads_repeating_thread_create(
177 libcthreads_repeating_thread_t **repeating_thread,
178 const libcthreads_thread_attributes_t *thread_attributes,
179 int (*start_function)(
180 void *arguments ),
181 void *start_function_arguments,
182 libcerror_error_t **error )
183 {
184 libcthreads_internal_repeating_thread_t *internal_repeating_thread = NULL;
185 static char *function = "libcthreads_repeating_thread_create";
186
187 #if defined( WINAPI )
188 SECURITY_ATTRIBUTES *security_attributes = NULL;
189 DWORD error_code = 0;
190
191 #elif defined( HAVE_PTHREAD_H )
192 pthread_attr_t *attributes = NULL;
193 int pthread_result = 0;
194 #endif
195
196 if( repeating_thread == NULL )
197 {
198 libcerror_error_set(
199 error,
200 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
201 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
202 "%s: invalid repeating thread.",
203 function );
204
205 return( -1 );
206 }
207 if( *repeating_thread != NULL )
208 {
209 libcerror_error_set(
210 error,
211 LIBCERROR_ERROR_DOMAIN_RUNTIME,
212 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
213 "%s: invalid repeating thread value already set.",
214 function );
215
216 return( -1 );
217 }
218 if( start_function == NULL )
219 {
220 libcerror_error_set(
221 error,
222 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
223 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
224 "%s: invalid start function.",
225 function );
226
227 return( -1 );
228 }
229 internal_repeating_thread = memory_allocate_structure(
230 libcthreads_internal_repeating_thread_t );
231
232 if( internal_repeating_thread == NULL )
233 {
234 libcerror_error_set(
235 error,
236 LIBCERROR_ERROR_DOMAIN_MEMORY,
237 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
238 "%s: unable to create repeating thread.",
239 function );
240
241 goto on_error;
242 }
243 if( memory_set(
244 internal_repeating_thread,
245 0,
246 sizeof( libcthreads_internal_repeating_thread_t ) ) == NULL )
247 {
248 libcerror_error_set(
249 error,
250 LIBCERROR_ERROR_DOMAIN_MEMORY,
251 LIBCERROR_MEMORY_ERROR_SET_FAILED,
252 "%s: unable to clear repeating thread.",
253 function );
254
255 memory_free(
256 internal_repeating_thread );
257
258 return( -1 );
259 }
260 if( libcthreads_mutex_initialize(
261 &( internal_repeating_thread->condition_mutex ),
262 error ) != 1 )
263 {
264 libcerror_error_set(
265 error,
266 LIBCERROR_ERROR_DOMAIN_RUNTIME,
267 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
268 "%s: unable to create condition mutex.",
269 function );
270
271 goto on_error;
272 }
273 if( libcthreads_condition_initialize(
274 &( internal_repeating_thread->status_condition ),
275 error ) != 1 )
276 {
277 libcerror_error_set(
278 error,
279 LIBCERROR_ERROR_DOMAIN_RUNTIME,
280 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
281 "%s: unable to create status condition.",
282 function );
283
284 goto on_error;
285 }
286 internal_repeating_thread->start_function = start_function;
287 internal_repeating_thread->start_function_arguments = start_function_arguments;
288
289 #if defined( WINAPI )
290 if( thread_attributes != NULL )
291 {
292 security_attributes = &( ( (libcthreads_internal_thread_attributes_t *) thread_attributes )->security_attributes );
293 }
294 internal_repeating_thread->thread_handle = CreateThread(
295 security_attributes,
296 0, /* stack size */
297 &libcthreads_repeating_thread_start_function_helper,
298 (void *) internal_repeating_thread,
299 0, /* creation flags */
300 &( internal_repeating_thread->thread_identifier ) );
301
302 if( internal_repeating_thread->thread_handle == NULL )
303 {
304 error_code = GetLastError();
305
306 libcerror_system_set_error(
307 error,
308 error_code,
309 LIBCERROR_ERROR_DOMAIN_RUNTIME,
310 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
311 "%s: unable to create thread handle.",
312 function );
313
314 goto on_error;
315 }
316 #elif defined( HAVE_PTHREAD_H )
317 if( thread_attributes != NULL )
318 {
319 attributes = &( ( (libcthreads_internal_thread_attributes_t *) thread_attributes )->attributes );
320 }
321 pthread_result = pthread_create(
322 &( internal_repeating_thread->thread ),
323 attributes,
324 &libcthreads_repeating_thread_start_function_helper,
325 (void *) internal_repeating_thread );
326
327 switch( pthread_result )
328 {
329 case 0:
330 break;
331
332 case EAGAIN:
333 libcerror_error_set(
334 error,
335 LIBCERROR_ERROR_DOMAIN_RUNTIME,
336 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
337 "%s: unable to create thread with error: Insufficient resources.",
338 function );
339
340 goto on_error;
341
342 default:
343 libcerror_system_set_error(
344 error,
345 pthread_result,
346 LIBCERROR_ERROR_DOMAIN_RUNTIME,
347 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
348 "%s: unable to create thread.",
349 function );
350
351 goto on_error;
352 }
353 #endif
354 *repeating_thread = (libcthreads_repeating_thread_t *) internal_repeating_thread;
355
356 return( 1 );
357
358 on_error:
359 if( internal_repeating_thread != NULL )
360 {
361 if( internal_repeating_thread->status_condition != NULL )
362 {
363 libcthreads_condition_free(
364 &( internal_repeating_thread->status_condition ),
365 NULL );
366 }
367 if( internal_repeating_thread->condition_mutex != NULL )
368 {
369 libcthreads_mutex_free(
370 &( internal_repeating_thread->condition_mutex ),
371 NULL );
372 }
373 memory_free(
374 internal_repeating_thread );
375 }
376 return( -1 );
377 }
378
379 /* Gives the thread a push
380 * Returns 1 if successful or -1 on error
381 */
382 int libcthreads_repeating_thread_push(
383 libcthreads_repeating_thread_t *repeating_thread,
384 libcerror_error_t **error )
385 {
386 libcthreads_internal_repeating_thread_t *internal_repeating_thread = NULL;
387 static char *function = "libcthreads_repeating_thread_push";
388 int result = 1;
389
390 if( repeating_thread == NULL )
391 {
392 libcerror_error_set(
393 error,
394 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
395 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
396 "%s: invalid repeating thread.",
397 function );
398
399 return( -1 );
400 }
401 internal_repeating_thread = (libcthreads_internal_repeating_thread_t *) repeating_thread;
402
403 /* TODO some work */
404
405 if( libcthreads_mutex_grab(
406 internal_repeating_thread->condition_mutex,
407 error ) != 1 )
408 {
409 libcerror_error_set(
410 error,
411 LIBCERROR_ERROR_DOMAIN_RUNTIME,
412 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
413 "%s: unable to grab condition mutex.",
414 function );
415
416 return( -1 );
417 }
418 if( libcthreads_condition_signal(
419 internal_repeating_thread->status_condition,
420 error ) != 1 )
421 {
422 libcerror_error_set(
423 error,
424 LIBCERROR_ERROR_DOMAIN_RUNTIME,
425 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
426 "%s: unable to signal status condition.",
427 function );
428
429 result = -1;
430 }
431 if( libcthreads_mutex_release(
432 internal_repeating_thread->condition_mutex,
433 error ) != 1 )
434 {
435 libcerror_error_set(
436 error,
437 LIBCERROR_ERROR_DOMAIN_RUNTIME,
438 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
439 "%s: unable to release condition mutex.",
440 function );
441
442 return( -1 );
443 }
444 return( result );
445 }
446
447 /* Joins the current with a specified repeating thread
448 * The the thread is freed after join
449 * Returns 1 if successful or -1 on error
450 */
451 int libcthreads_repeating_thread_join(
452 libcthreads_repeating_thread_t **repeating_thread,
453 libcerror_error_t **error )
454 {
455 libcthreads_internal_repeating_thread_t *internal_repeating_thread = NULL;
456 static char *function = "libcthreads_repeating_thread_join";
457 int result = 1;
458
459 #if defined( WINAPI )
460 DWORD error_code = 0;
461 DWORD wait_status = 0;
462
463 #elif defined( HAVE_PTHREAD_H )
464 int *thread_return_value = NULL;
465 int pthread_result = 0;
466 #endif
467
468 if( repeating_thread == NULL )
469 {
470 libcerror_error_set(
471 error,
472 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
473 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
474 "%s: invalid repeating thread.",
475 function );
476
477 return( -1 );
478 }
479 if( *repeating_thread == NULL )
480 {
481 libcerror_error_set(
482 error,
483 LIBCERROR_ERROR_DOMAIN_RUNTIME,
484 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
485 "%s: missing repeating thread value.",
486 function );
487
488 return( -1 );
489 }
490 internal_repeating_thread = (libcthreads_internal_repeating_thread_t *) *repeating_thread;
491 *repeating_thread = NULL;
492
493 if( libcthreads_mutex_grab(
494 internal_repeating_thread->condition_mutex,
495 error ) != 1 )
496 {
497 libcerror_error_set(
498 error,
499 LIBCERROR_ERROR_DOMAIN_RUNTIME,
500 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
501 "%s: unable to grab condition mutex.",
502 function );
503
504 return( -1 );
505 }
506 internal_repeating_thread->status = LIBCTHREADS_STATUS_EXIT;
507
508 if( libcthreads_condition_signal(
509 internal_repeating_thread->status_condition,
510 error ) != 1 )
511 {
512 libcerror_error_set(
513 error,
514 LIBCERROR_ERROR_DOMAIN_RUNTIME,
515 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
516 "%s: unable to signal status condition.",
517 function );
518
519 result = -1;
520 }
521 if( libcthreads_mutex_release(
522 internal_repeating_thread->condition_mutex,
523 error ) != 1 )
524 {
525 libcerror_error_set(
526 error,
527 LIBCERROR_ERROR_DOMAIN_RUNTIME,
528 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
529 "%s: unable to release condition mutex.",
530 function );
531
532 return( -1 );
533 }
534 if( result != 1 )
535 {
536 return( -1 );
537 }
538 #if defined( WINAPI )
539 wait_status = WaitForSingleObject(
540 internal_repeating_thread->thread_handle,
541 INFINITE );
542
543 if( wait_status == WAIT_FAILED )
544 {
545 error_code = GetLastError();
546
547 libcerror_system_set_error(
548 error,
549 error_code,
550 LIBCERROR_ERROR_DOMAIN_RUNTIME,
551 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
552 "%s: wait for thread failed.",
553 function );
554
555 result = -1;
556 }
557
558 #elif defined( HAVE_PTHREAD_H )
559 pthread_result = pthread_join(
560 internal_repeating_thread->thread,
561 (void **) &thread_return_value );
562
563 if( pthread_result == EDEADLK )
564 {
565 libcerror_error_set(
566 error,
567 LIBCERROR_ERROR_DOMAIN_RUNTIME,
568 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
569 "%s: unable to join thread with error: Deadlock condition detected.",
570 function );
571
572 result = -1;
573 }
574 else if( pthread_result != 0 )
575 {
576 libcerror_system_set_error(
577 error,
578 result,
579 LIBCERROR_ERROR_DOMAIN_RUNTIME,
580 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
581 "%s: unable to join thread.",
582 function );
583
584 result = -1;
585 }
586 else if( ( thread_return_value == NULL )
587 || ( thread_return_value != &( internal_repeating_thread->start_function_result ) ) )
588 {
589 libcerror_error_set(
590 error,
591 LIBCERROR_ERROR_DOMAIN_RUNTIME,
592 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
593 "%s: invalid thread return value.",
594 function );
595
596 result = -1;
597 }
598 else if( *thread_return_value != 1 )
599 {
600 libcerror_error_set(
601 error,
602 LIBCERROR_ERROR_DOMAIN_RUNTIME,
603 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
604 "%s: thread returned an error status.",
605 function,
606 *thread_return_value );
607
608 result = -1;
609 }
610 #endif
611 if( libcthreads_condition_free(
612 &( internal_repeating_thread->status_condition ),
613 error ) != 1 )
614 {
615 libcerror_error_set(
616 error,
617 LIBCERROR_ERROR_DOMAIN_RUNTIME,
618 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
619 "%s: unable to free status condition.",
620 function );
621
622 result = -1;
623 }
624 if( libcthreads_mutex_free(
625 &( internal_repeating_thread->condition_mutex ),
626 error ) != 1 )
627 {
628 libcerror_error_set(
629 error,
630 LIBCERROR_ERROR_DOMAIN_RUNTIME,
631 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
632 "%s: unable to free condition mutex.",
633 function );
634
635 result = -1;
636 }
637 memory_free(
638 internal_repeating_thread );
639
640 return( result );
641 }
642
643 #endif /* !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT ) */
644