Debian Codegraph @

libcthreads/libcthreads_read_write_lock.c

raw symbols SCIP
1 /*
2 * Read/Write lock 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 <Synchapi.h>
30 #endif
31
32 #if defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
33 #include <pthread.h>
34 #endif
35
36 #include "libcthreads_libcerror.h"
37 #include "libcthreads_read_write_lock.h"
38 #include "libcthreads_types.h"
39
40 #if !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT )
41
42 /* Creates a read/write lock
43 * Make sure the value read_write_lock is referencing, is set to NULL
44 * Returns 1 if successful or -1 on error
45 */
46 int libcthreads_read_write_lock_initialize(
47 libcthreads_read_write_lock_t **read_write_lock,
48 libcerror_error_t **error )
49 {
50 libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL;
51 static char *function = "libcthreads_read_write_lock_initialize";
52
53 #if defined( WINAPI ) && ( WINVER >= 0x0400 ) && ( WINVER < 0x0600 )
54 DWORD error_code = 0;
55
56 #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
57 int pthread_result = 0;
58 #endif
59
60 if( read_write_lock == NULL )
61 {
62 libcerror_error_set(
63 error,
64 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
65 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
66 "%s: invalid read/write lock.",
67 function );
68
69 return( -1 );
70 }
71 if( *read_write_lock != NULL )
72 {
73 libcerror_error_set(
74 error,
75 LIBCERROR_ERROR_DOMAIN_RUNTIME,
76 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
77 "%s: invalid read/write lock value already set.",
78 function );
79
80 return( -1 );
81 }
82 internal_read_write_lock = memory_allocate_structure(
83 libcthreads_internal_read_write_lock_t );
84
85 if( internal_read_write_lock == NULL )
86 {
87 libcerror_error_set(
88 error,
89 LIBCERROR_ERROR_DOMAIN_MEMORY,
90 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
91 "%s: unable to create read/write lock.",
92 function );
93
94 goto on_error;
95 }
96 if( memory_set(
97 internal_read_write_lock,
98 0,
99 sizeof( libcthreads_internal_read_write_lock_t ) ) == NULL )
100 {
101 libcerror_error_set(
102 error,
103 LIBCERROR_ERROR_DOMAIN_MEMORY,
104 LIBCERROR_MEMORY_ERROR_SET_FAILED,
105 "%s: unable to clear read/write lock.",
106 function );
107
108 memory_free(
109 internal_read_write_lock );
110
111 return( -1 );
112 }
113 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
114 InitializeSRWLock(
115 &( internal_read_write_lock->slim_read_write_lock ) );
116
117 #elif defined( WINAPI ) && ( WINVER >= 0x0400 )
118 InitializeCriticalSection(
119 &( internal_read_write_lock->write_critical_section ) );
120
121 InitializeCriticalSection(
122 &( internal_read_write_lock->read_critical_section ) );
123
124 internal_read_write_lock->no_read_event_handle = CreateEvent(
125 NULL,
126 TRUE,
127 TRUE,
128 NULL );
129
130 if( internal_read_write_lock->no_read_event_handle == NULL )
131 {
132 error_code = GetLastError();
133
134 libcerror_system_set_error(
135 error,
136 error_code,
137 LIBCERROR_ERROR_DOMAIN_RUNTIME,
138 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
139 "%s: unable to initialize no read event handle.",
140 function );
141
142 goto on_error;
143 }
144 #elif defined( WINAPI )
145
146 #error libcthreads_read_write_lock_initialize for Windows earlier than NT4 not implemented
147
148 #elif defined( HAVE_PTHREAD_H )
149 pthread_result = pthread_rwlock_init(
150 &( internal_read_write_lock->read_write_lock ),
151 NULL );
152
153 switch( pthread_result )
154 {
155 case 0:
156 break;
157
158 case EAGAIN:
159 libcerror_error_set(
160 error,
161 LIBCERROR_ERROR_DOMAIN_RUNTIME,
162 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
163 "%s: unable to initialize read/write lock with error: Insufficient resources.",
164 function );
165
166 goto on_error;
167
168 default:
169 libcerror_system_set_error(
170 error,
171 pthread_result,
172 LIBCERROR_ERROR_DOMAIN_RUNTIME,
173 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
174 "%s: unable to initialize read/write lock.",
175 function );
176
177 goto on_error;
178 }
179 #endif
180 *read_write_lock = (libcthreads_read_write_lock_t *) internal_read_write_lock;
181
182 return( 1 );
183
184 on_error:
185 if( internal_read_write_lock != NULL )
186 {
187 #if defined( WINAPI ) && ( WINVER >= 0x0400 ) && ( WINVER < 0x0600 )
188 DeleteCriticalSection(
189 &( internal_read_write_lock->read_critical_section ) );
190
191 DeleteCriticalSection(
192 &( internal_read_write_lock->write_critical_section ) );
193 #endif
194 memory_free(
195 internal_read_write_lock );
196 }
197 return( -1 );
198 }
199
200 /* Frees a read/write lock
201 * Returns 1 if successful or -1 on error
202 */
203 int libcthreads_read_write_lock_free(
204 libcthreads_read_write_lock_t **read_write_lock,
205 libcerror_error_t **error )
206 {
207 libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL;
208 static char *function = "libcthreads_read_write_lock_free";
209 int result = 1;
210
211 #if defined( WINAPI ) && ( WINVER >= 0x0400 ) && ( WINVER < 0x0600 )
212 DWORD error_code = 0;
213
214 #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
215 int pthread_result = 0;
216 #endif
217
218 if( read_write_lock == NULL )
219 {
220 libcerror_error_set(
221 error,
222 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
223 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
224 "%s: invalid read/write lock.",
225 function );
226
227 return( -1 );
228 }
229 if( *read_write_lock != NULL )
230 {
231 internal_read_write_lock = (libcthreads_internal_read_write_lock_t *) *read_write_lock;
232 *read_write_lock = NULL;
233
234 #if defined( WINAPI ) && ( WINVER >= 0x0400 ) && ( WINVER < 0x0600 )
235 if( CloseHandle(
236 internal_read_write_lock->no_read_event_handle ) == 0 )
237 {
238 error_code = GetLastError();
239
240 libcerror_system_set_error(
241 error,
242 error_code,
243 LIBCERROR_ERROR_DOMAIN_RUNTIME,
244 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
245 "%s: unable to free no read event handle.",
246 function );
247
248 result = -1;
249 }
250 DeleteCriticalSection(
251 &( internal_read_write_lock->read_critical_section ) );
252
253 DeleteCriticalSection(
254 &( internal_read_write_lock->write_critical_section ) );
255
256 #elif defined( WINAPI ) && ( WINVER < 0x0400 )
257
258 #error libcthreads_read_write_lock_free for Windows earlier than NT4 not implemented
259
260 #elif defined( HAVE_PTHREAD_H )
261 pthread_result = pthread_rwlock_destroy(
262 &( internal_read_write_lock->read_write_lock ) );
263
264 switch( pthread_result )
265 {
266 case 0:
267 break;
268
269 case EAGAIN:
270 libcerror_error_set(
271 error,
272 LIBCERROR_ERROR_DOMAIN_RUNTIME,
273 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
274 "%s: unable to destroy read/write lock with error: Insufficient resources.",
275 function );
276
277 result = -1;
278 break;
279
280 case EBUSY:
281 libcerror_error_set(
282 error,
283 LIBCERROR_ERROR_DOMAIN_RUNTIME,
284 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
285 "%s: unable to destroy read/write lock with error: Resource busy.",
286 function );
287
288 result = -1;
289 break;
290
291 default:
292 libcerror_system_set_error(
293 error,
294 pthread_result,
295 LIBCERROR_ERROR_DOMAIN_RUNTIME,
296 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
297 "%s: unable to destroy read/write lock.",
298 function );
299
300 result = -1;
301 break;
302 }
303 #endif
304 memory_free(
305 internal_read_write_lock );
306 }
307 return( result );
308 }
309
310 /* Grabs a read/write lock for reading
311 * Returns 1 if successful or -1 on error
312 */
313 int libcthreads_read_write_lock_grab_for_read(
314 libcthreads_read_write_lock_t *read_write_lock,
315 libcerror_error_t **error )
316 {
317 libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL;
318 static char *function = "libcthreads_read_write_lock_grab_for_read";
319
320 #if defined( WINAPI ) && ( WINVER >= 0x0400 ) && ( WINVER < 0x0600 )
321 DWORD error_code = 0;
322 BOOL result = 0;
323
324 #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
325 int pthread_result = 0;
326 #endif
327
328 if( read_write_lock == NULL )
329 {
330 libcerror_error_set(
331 error,
332 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
333 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
334 "%s: invalid read/write lock.",
335 function );
336
337 return( -1 );
338 }
339 internal_read_write_lock = (libcthreads_internal_read_write_lock_t *) read_write_lock;
340
341 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
342 AcquireSRWLockShared(
343 &( internal_read_write_lock->slim_read_write_lock ) );
344
345 #elif defined( WINAPI ) && ( WINVER >= 0x0400 )
346 EnterCriticalSection(
347 &( internal_read_write_lock->write_critical_section ) );
348
349 EnterCriticalSection(
350 &( internal_read_write_lock->read_critical_section ) );
351
352 internal_read_write_lock->number_of_readers++;
353
354 if( internal_read_write_lock->number_of_readers == 1 )
355 {
356 result = ResetEvent(
357 internal_read_write_lock->no_read_event_handle );
358
359 if( result == 0 )
360 {
361 error_code = GetLastError();
362
363 internal_read_write_lock->number_of_readers--;
364 }
365 }
366 else
367 {
368 result = 1;
369 }
370 LeaveCriticalSection(
371 &( internal_read_write_lock->read_critical_section ) );
372
373 LeaveCriticalSection(
374 &( internal_read_write_lock->write_critical_section ) );
375
376 if( result == 0 )
377 {
378 libcerror_system_set_error(
379 error,
380 error_code,
381 LIBCERROR_ERROR_DOMAIN_RUNTIME,
382 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
383 "%s: reset of no read event failed.",
384 function );
385
386 return( -1 );
387 }
388 #elif defined( WINAPI )
389
390 #error libcthreads_read_write_lock_grab_for_read for Windows earlier than NT4 not implemented
391
392 #elif defined( HAVE_PTHREAD_H )
393 pthread_result = pthread_rwlock_rdlock(
394 &( internal_read_write_lock->read_write_lock ) );
395
396 switch( pthread_result )
397 {
398 case 0:
399 break;
400
401 case EAGAIN:
402 libcerror_error_set(
403 error,
404 LIBCERROR_ERROR_DOMAIN_RUNTIME,
405 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
406 "%s: unable to lock read/write lock for read with error: Maximum number of locks exceeded.",
407 function );
408
409 return( -1 );
410
411 case EDEADLK:
412 libcerror_error_set(
413 error,
414 LIBCERROR_ERROR_DOMAIN_RUNTIME,
415 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
416 "%s: unable to lock read/write lock for read with error: Deadlock condition detected.",
417 function );
418
419 return( -1 );
420
421 default:
422 libcerror_system_set_error(
423 error,
424 pthread_result,
425 LIBCERROR_ERROR_DOMAIN_RUNTIME,
426 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
427 "%s: unable to lock read/write lock for read.",
428 function );
429
430 return( -1 );
431 }
432 #endif
433 return( 1 );
434 }
435
436 /* Grabs a read/write lock for writing
437 * Returns 1 if successful or -1 on error
438 */
439 int libcthreads_read_write_lock_grab_for_write(
440 libcthreads_read_write_lock_t *read_write_lock,
441 libcerror_error_t **error )
442 {
443 libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL;
444 static char *function = "libcthreads_read_write_lock_grab_for_write";
445
446 #if defined( WINAPI ) && ( WINVER >= 0x0400 ) && ( WINVER < 0x0600 )
447 DWORD error_code = 0;
448 DWORD wait_status = 0;
449
450 #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
451 int pthread_result = 0;
452 #endif
453
454 if( read_write_lock == NULL )
455 {
456 libcerror_error_set(
457 error,
458 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
459 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
460 "%s: invalid read/write lock.",
461 function );
462
463 return( -1 );
464 }
465 internal_read_write_lock = (libcthreads_internal_read_write_lock_t *) read_write_lock;
466
467 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
468 AcquireSRWLockExclusive(
469 &( internal_read_write_lock->slim_read_write_lock ) );
470
471 #elif defined( WINAPI ) && ( WINVER >= 0x0400 )
472 EnterCriticalSection(
473 &( internal_read_write_lock->write_critical_section ) );
474
475 wait_status = WaitForSingleObject(
476 internal_read_write_lock->no_read_event_handle,
477 INFINITE );
478
479 if( wait_status == WAIT_FAILED )
480 {
481 error_code = GetLastError();
482
483 LeaveCriticalSection(
484 &( internal_read_write_lock->write_critical_section ) );
485
486 libcerror_system_set_error(
487 error,
488 error_code,
489 LIBCERROR_ERROR_DOMAIN_RUNTIME,
490 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
491 "%s: wait for no read event handle failed.",
492 function );
493
494 return( -1 );
495 }
496 #elif defined( WINAPI )
497
498 #error libcthreads_read_write_lock_grab_for_write for Windows earlier than NT4 not implemented
499
500 #elif defined( HAVE_PTHREAD_H )
501 pthread_result = pthread_rwlock_wrlock(
502 &( internal_read_write_lock->read_write_lock ) );
503
504 switch( pthread_result )
505 {
506 case 0:
507 break;
508
509 case EDEADLK:
510 libcerror_error_set(
511 error,
512 LIBCERROR_ERROR_DOMAIN_RUNTIME,
513 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
514 "%s: unable to lock read/write lock for write with error: Deadlock condition detected.",
515 function );
516
517 return( -1 );
518
519 default:
520 libcerror_system_set_error(
521 error,
522 pthread_result,
523 LIBCERROR_ERROR_DOMAIN_RUNTIME,
524 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
525 "%s: unable to lock read/write lock for write.",
526 function );
527
528 return( -1 );
529 }
530 #endif
531 return( 1 );
532 }
533
534 /* Releases a read/write lock for reading
535 * Returns 1 if successful or -1 on error
536 */
537 int libcthreads_read_write_lock_release_for_read(
538 libcthreads_read_write_lock_t *read_write_lock,
539 libcerror_error_t **error )
540 {
541 libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL;
542 static char *function = "libcthreads_read_write_lock_release_for_read";
543
544 #if defined( WINAPI ) && ( WINVER >= 0x0400 ) && ( WINVER < 0x0600 )
545 DWORD error_code = 0;
546 BOOL result = 0;
547
548 #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
549 int pthread_result = 0;
550 #endif
551
552 if( read_write_lock == NULL )
553 {
554 libcerror_error_set(
555 error,
556 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
557 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
558 "%s: invalid read/write lock.",
559 function );
560
561 return( -1 );
562 }
563 internal_read_write_lock = (libcthreads_internal_read_write_lock_t *) read_write_lock;
564
565 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
566 ReleaseSRWLockShared(
567 &( internal_read_write_lock->slim_read_write_lock ) );
568
569 #elif defined( WINAPI ) && ( WINVER >= 0x0400 )
570 EnterCriticalSection(
571 &( internal_read_write_lock->read_critical_section ) );
572
573 internal_read_write_lock->number_of_readers--;
574
575 if( internal_read_write_lock->number_of_readers == 0 )
576 {
577 result = SetEvent(
578 internal_read_write_lock->no_read_event_handle );
579
580 if( result == 0 )
581 {
582 error_code = GetLastError();
583
584 internal_read_write_lock->number_of_readers++;
585 }
586 }
587 else
588 {
589 result = 1;
590 }
591 LeaveCriticalSection(
592 &( internal_read_write_lock->read_critical_section ) );
593
594 if( result == 0 )
595 {
596 libcerror_system_set_error(
597 error,
598 error_code,
599 LIBCERROR_ERROR_DOMAIN_RUNTIME,
600 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
601 "%s: set of no read event failed.",
602 function );
603
604 return( -1 );
605 }
606 #elif defined( WINAPI )
607
608 #error libcthreads_read_write_lock_release_for_read for Windows earlier than NT4 not implemented
609
610 #elif defined( HAVE_PTHREAD_H )
611 pthread_result = pthread_rwlock_unlock(
612 &( internal_read_write_lock->read_write_lock ) );
613
614 if( pthread_result != 0 )
615 {
616 libcerror_system_set_error(
617 error,
618 pthread_result,
619 LIBCERROR_ERROR_DOMAIN_RUNTIME,
620 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
621 "%s: unable to unlock read/write lock.",
622 function );
623
624 return( -1 );
625 }
626 #endif
627 return( 1 );
628 }
629
630 /* Releases a read/write lock for writing
631 * Returns 1 if successful or -1 on error
632 */
633 int libcthreads_read_write_lock_release_for_write(
634 libcthreads_read_write_lock_t *read_write_lock,
635 libcerror_error_t **error )
636 {
637 libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL;
638 static char *function = "libcthreads_read_write_lock_release_for_write";
639
640 #if defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
641 int pthread_result = 0;
642 #endif
643
644 if( read_write_lock == NULL )
645 {
646 libcerror_error_set(
647 error,
648 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
649 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
650 "%s: invalid read/write lock.",
651 function );
652
653 return( -1 );
654 }
655 internal_read_write_lock = (libcthreads_internal_read_write_lock_t *) read_write_lock;
656
657 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
658 ReleaseSRWLockExclusive(
659 &( internal_read_write_lock->slim_read_write_lock ) );
660
661 #elif defined( WINAPI ) && ( WINVER >= 0x0400 )
662 LeaveCriticalSection(
663 &( internal_read_write_lock->write_critical_section ) );
664
665 #elif defined( WINAPI )
666
667 #error libcthreads_read_write_lock_release_for_write for Windows earlier than NT4 not implemented
668
669 #elif defined( HAVE_PTHREAD_H )
670 pthread_result = pthread_rwlock_unlock(
671 &( internal_read_write_lock->read_write_lock ) );
672
673 if( pthread_result != 0 )
674 {
675 libcerror_system_set_error(
676 error,
677 pthread_result,
678 LIBCERROR_ERROR_DOMAIN_RUNTIME,
679 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
680 "%s: unable to unlock read/write lock.",
681 function );
682
683 return( -1 );
684 }
685 #endif
686 return( 1 );
687 }
688
689 #endif /* !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT ) */
690