1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>HDF5: HDF5 Thread Safe library</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="hdf5doxy.css" rel="stylesheet" type="text/css">
<!-- <link href="hdf5doxy.css" rel="stylesheet" type="text/css"/>
-->
<script type="text/javascript" src="hdf5_navtree_hacks.js"></script>
</head>
<body>
<div style="background:#FFDDDD;font-size:120%;text-align:center;margin:0;padding:5px">Please, help us to better serve our user community by answering the following short survey: <a href="https://www.hdfgroup.org/website-survey/">https://www.hdfgroup.org/website-survey/</a></div>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="HDFG-logo.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname"><a href="https://www.hdfgroup.org">HDF5</a>
 <span id="projectnumber">1.15.0.54d19b0</span>
</div>
<div id="projectbrief">API Reference</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.svg"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('_m_t.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">HDF5 Thread Safe library </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en-US">
<head>
<title>Thread Safe Library</title>
</head>
<h1>1. Library header files and conditional compilation</h1>
<p>
The following code is placed at the beginning of H5private.h:
</p>
<blockquote>
<pre>
#ifdef H5_HAVE_THREADSAFE
#include <pthread.h>
#endif
</pre>
</blockquote>
<p>
<code>H5_HAVE_THREADSAFE</code> is defined when the HDF5 library is
compiled with the --enable-threadsafe configuration option. In general,
code for the non-threadsafe version of HDF5 library are placed within
the <code>#else</code> part of the conditional compilation. The exception
to this rule are the changes to the <code>FUNC_ENTER</code> (in
H5private.h), <code>HRETURN</code> and <code>HRETURN_ERROR</code> (in
H5Eprivate.h) macros (see section 3.2).
</p>
<h1>2. Global variables/structures</h1>
<h2>2.1 Global library initialization variable</h2>
<p>
In the threadsafe implementation, the global library initialization
variable <code>H5_libinit_g</code> is changed to a global structure
consisting of the variable with its associated lock (locks are explained
in section 4.1):
</p>
<blockquote>
<pre>
hbool_t H5_libinit_g = FALSE;
</pre>
</blockquote>
<p>
becomes
</p>
<blockquote>
<pre>
H5_api_t H5_g;
</pre>
</blockquote>
<p>
where <code>H5_api_t</code> is
</p>
<blockquote>
<pre>
typedef struct H5_api_struct {
H5_mutex_t init_lock; /* API entrance mutex */
hbool_t H5_libinit_g;
} H5_api_t;
</pre>
</blockquote>
<p>
All former references to <code>H5_libinit_g</code> in the library are now
made using the macro <code>H5_INIT_GLOBAL</code>. If the threadsafe
library is to be used, the macro is set to <code>H5_g.H5_libinit_g</code>
instead.
</p>
<h2>2.2 Global serialization variable</h2>
<p>
A new global boolean variable <code>H5_allow_concurrent_g</code> is used
to determine if multiple threads are allowed to an API call
simultaneously. This is set to <code>FALSE</code>.
</p>
<p>
All APIs that are allowed to do so have their own local variable that
shadows the global variable and is set to <code>TRUE</code>. In phase 1,
no such APIs exist.
</p>
<p>
It is defined in <code>H5.c</code> as follows:
</p>
<blockquote>
<pre>
hbool_t H5_allow_concurrent_g = FALSE;
</pre>
</blockquote>
<h2>2.3 Global thread initialization variable</h2>
<p>
The global variable <code>H5_first_init_g</code> of type
<code>pthread_once_t</code> is used to allow only the first thread in the
application process to call an initialization function using
<code>pthread_once</code>. All subsequent calls to
<code>pthread_once</code> by any thread are disregarded.
</p>
<p>
The call sets up the mutex in the global structure <code>H5_g</code> (see
section 3.1) via an initialization function
<code>H5_first_thread_init</code>. The first thread initialization
function is described in section 4.2.
</p>
<p>
<code>H5_first_init_g</code> is defined in <code>H5.c</code> as follows:
</p>
<blockquote>
<pre>
pthread_once_t H5_first_init_g = PTHREAD_ONCE_INIT;
</pre>
</blockquote>
<h2>2.4 Global key for per-thread error stacks</h2>
<p>
A global pthread-managed key <code>H5_errstk_key_g</code> is used to
allow pthreads to maintain a separate error stack (of type
<code>H5E_t</code>) for each thread. This is defined in <code>H5.c</code>
as:
</p>
<blockquote>
<pre>
pthread_key_t H5_errstk_key_g;
</pre>
</blockquote>
<p>
Error stack management is described in section 4.3.
</p>
<h2>2.5 Global structure and key for thread cancellation prevention</h2>
<p>
We need to preserve the thread cancellation status of each thread
individually by using a key <code>H5_cancel_key_g</code>. The status is
preserved using a structure (of type <code>H5_cancel_t</code>) which
maintains the cancellability state of the thread before it entered the
library and a count (which works very much like the recursive lock
counter) which keeps track of the number of API calls the thread makes
within the library.
</p>
<p>
The structure is defined in <code>H5private.h</code> as:
</p>
<blockquote>
<pre>
/* cancellability structure */
typedef struct H5_cancel_struct {
int previous_state;
unsigned int cancel_count;
} H5_cancel_t;
</pre>
</blockquote>
<p>
Thread cancellation is described in section 4.4.
</p>
<h1>3. Changes to Macro expansions</h1>
<h2>3.1 Changes to FUNC_ENTER</h2>
<p>
The <code>FUNC_ENTER</code> macro is now extended to include macro calls
to initialize first threads, disable cancellability and wraps a lock
operation around the checking of the global initialization flag. It
should be noted that the cancellability should be disabled before
acquiring the lock on the library. Doing so otherwise would allow the
possibility that the thread be cancelled just after it has acquired the
lock on the library and in that scenario, if the cleanup routines are not
properly set, the library would be permanently locked out.
</p>
<p>
The additional macro code and new macro definitions can be found in
Appendix E.1 to E.5. The changes are made in <code>H5private.h</code>.
</p>
<h2>3.2 Changes to HRETURN and HRETURN_ERROR</h2>
<p>
The <code>HRETURN</code> and <code>HRETURN_ERROR</code> macros are the
counterparts to the <code>FUNC_ENTER</code> macro described in section
3.1. <code>FUNC_LEAVE</code> makes a macro call to <code>HRETURN</code>,
so it is also covered here.
</p>
<p>
The basic changes to these two macros involve adding macro calls to call
an unlock operation and re-enable cancellability if necessary. It should
be noted that the cancellability should be re-enabled only after the
thread has released the lock to the library. The consequence of doing
otherwise would be similar to that described in section 3.1.
</p>
<p>
The additional macro code and new macro definitions can be found in
Appendix E.9 to E.9. The changes are made in <code>H5Eprivate.h</code>.
</p>
<h1>4. Implementation of threadsafe functionality</h1>
<h2>4.1 Recursive Locks</h2>
<p>
A recursive mutex lock m allows a thread t1 to successfully lock m more
than once without blocking t1. Another thread t2 will block if t2 tries
to lock m while t1 holds the lock to m. If t1 makes k lock calls on m,
then it also needs to make k unlock calls on m before it releases the
lock.
</p>
<p>
Our implementation of recursive locks is built on top of a pthread mutex
lock (which is not recursive). It makes use of a pthread condition
variable to have unsuccessful threads wait on the mutex. Waiting threads
are awaken by a signal from the final unlock call made by the thread
holding the lock.
</p>
<p>
Recursive locks are defined to be the following type
(<code>H5private.h</code>):
</p>
<blockquote>
<pre>
typedef struct H5_mutex_struct {
pthread_t owner_thread; /* current lock owner */
pthread_mutex_t atomic_lock; /* lock for atomicity of new mechanism */
pthread_cond_t cond_var; /* condition variable */
unsigned int lock_count;
} H5_mutex_t;
</pre>
</blockquote>
<p>
Detailed implementation code can be found in Appendix A. The
implementation changes are made in <code>H5TS.c</code>.
</p>
<h2>4.2 First thread initialization</h2>
<p>
Because the mutex lock associated with a recursive lock cannot be
statically initialized, a mechanism is required to initialize the
recursive lock associated with <code>H5_g</code> so that it can be used
for the first time.
</p>
<p>
The pthreads library allows this through the pthread_once call which as
described in section 3.3 allows only the first thread accessing the
library in an application to initialize <code>H5_g</code>.
</p>
<p>
In addition to initializing <code>H5_g</code>, it also initializes the
key (see section 3.4) for use with per-thread error stacks (see section
4.3).
</p>
<p>
The first thread initialization mechanism is implemented as the function
call <code>H5_first_thread_init()</code> in <code>H5TS.c</code>. This is
described in appendix B.
</p>
<h2>4.3 Per-thread error stack management</h2>
<p>
Pthreads allows individual threads to access dynamic and persistent
per-thread data through the use of keys. Each key is associated with
a table that maps threads to data items. Keys can be initialized by
<code>pthread_key_create()</code> in pthreads (see sections 3.4 and 4.2).
Per-thread data items are accessed using a key through the
<code>pthread_getspecific()</code> and <code>pthread_setspecific()</code>
calls to read and write to the association table respectively.
</p>
<p>
Per-thread error stacks are accessed through the key
<code>H5_errstk_key_g</code> which is initialized by the first thread
initialization call (see section 4.2).
</p>
<p>
In the non-threadsafe version of the library, there is a global stack
variable <code>H5E_stack_g[1]</code> which is no longer defined in the
threadsafe version. At the same time, the macro call to gain access to
the error stack <code>H5E_get_my_stack</code> is changed from:
</p>
<blockquote>
<pre>
#define H5E_get_my_stack() (H5E_stack_g+0)
</pre>
</blockquote>
<p>
to:
</p>
<blockquote>
<pre>
#define H5E_get_my_stack() H5E_get_stack()
</pre>
</blockquote>
<p>
where <code>H5E_get_stack()</code> is a surrogate function that does the
following operations:
</p>
<ol>
<li>if a thread is attempting to get an error stack for the first
time, the error stack is dynamically allocated for the thread and
associated with <code>H5_errstk_key_g</code> using
<code>pthread_setspecific()</code>. The way we detect if it is the
first time is through <code>pthread_getspecific()</code> which
returns <code>NULL</code> if no previous value is associated with
the thread using the key.</li>
<li>if <code>pthread_getspecific()</code> returns a non-null value,
then that is the pointer to the error stack associated with the
thread and the stack can be used as usual.</li>
</ol>
<p>
A final change to the error reporting routines is as follows; the current
implementation reports errors to always be detected at thread 0. In the
threadsafe implementation, this is changed to report the number returned
by a call to <code>pthread_self()</code>.
</p>
<p>
The change in code (reflected in <code>H5Eprint</code> of file
<code>H5E.c</code>) is as follows:
</p>
<blockquote>
<pre>
#ifdef H5_HAVE_THREADSAFE
fprintf (stream, "HDF5-DIAG: Error detected in thread %d."
,pthread_self());
#else
fprintf (stream, "HDF5-DIAG: Error detected in thread 0.");
#endif
</pre>
</blockquote>
<p>
Code for <code>H5E_get_stack()</code> can be found in Appendix C. All the
above changes were made in <code>H5E.c</code>.
</p>
<h2>4.4 Thread Cancellation safety</h2>
<p>
To prevent thread cancellations from killing a thread while it is in the
library, we maintain per-thread information about the cancellability
status of the thread before it entered the library so that we can restore
that same status when the thread leaves the library.
</p>
<p>
By <i>enter</i> and <i>leave</i> the library, we mean the points when a
thread makes an API call from a user application and the time that API
call returns. Other API or callback function calls made from within that
API call are considered <i>within</i> the library.
</p>
<p>
Because other API calls may be made from within the first API call, we
need to maintain a counter to determine which was the first and
correspondingly the last return.
</p>
<p>
When a thread makes an API call, the macro <code>H5_API_SET_CANCEL</code>
calls the worker function <code>H5_cancel_count_inc()</code> which does
the following:
</p>
<ol>
<li>if this is the first time the thread has entered the library,
a new cancellability structure needs to be assigned to it.</li>
<li>if the thread is already within the library when the API call is
made, then cancel_count is simply incremented. Otherwise, we set
the cancellability state to <code>PTHREAD_CANCEL_DISABLE</code>
while storing the previous state into the cancellability structure.
<code>cancel_count</code> is also incremented in this case.</li>
</ol>
<p>
When a thread leaves an API call, the macro
<code>H5_API_UNSET_CANCEL</code> calls the worker function
<code>H5_cancel_count_dec()</code> which does the following:
</p>
<ol>
<li>if <code>cancel_count</code> is greater than 1, indicating that the
thread is not yet about to leave the library, then
<code>cancel_count</code> is simply decremented.</li>
<li>otherwise, we reset the cancellability state back to its original
state before it entered the library and decrement the count (back
to zero).</li>
</ol>
<p>
<code>H5_cancel_count_inc</code> and <code>H5_cancel_count_dec</code> are
described in Appendix D and may be found in <code>H5TS.c</code>.
</p>
<h1>5. Test programs</h1>
<p>
Except where stated, all tests involve 16 simultaneous threads that make
use of HDF5 API calls without any explicit synchronization typically
required in a non-threadsafe environment.
</p>
<h2>5.1 Data set create and write</h2>
<p>
The test program sets up 16 threads to simultaneously create 16
different datasets named from <i>zero</i> to <i>fifteen</i> for a single
file and then writing an integer value into that dataset equal to the
dataset's named value.
</p>
<p>
The main thread would join with all 16 threads and attempt to match the
resulting HDF5 file with expected results - that each dataset contains
the correct value (0 for <i>zero</i>, 1 for <i>one</i> etc ...) and all
datasets were correctly created.
</p>
<p>
The test is implemented in the file <code>ttsafe_dcreate.c</code>.
</p>
<h2>5.2 Test on error stack</h2>
<p>
The error stack test is one in which 16 threads simultaneously try to
create datasets with the same name. The result, when properly serialized,
should be equivalent to 16 attempts to create the dataset with the same
name.
</p>
<p>
The error stack implementation runs correctly if it reports 15 instances
of the dataset name conflict error and finally generates a correct HDF5
containing that single dataset. Each thread should report its own stack
of errors with a thread number associated with it.
</p>
<p>
The test is implemented in the file <code>ttsafe_error.c</code>.
</p>
<h2>5.3 Test on cancellation safety</h2>
<p>
The main idea in thread cancellation safety is as follows; a child thread
is spawned to create and write to a dataset. Following that, it makes a
<code>H5Diterate</code> call on that dataset which activates a callback
function.
</p>
<p>
A deliberate barrier is invoked at the callback function which waits for
both the main and child thread to arrive at that point. After that
happens, the main thread proceeds to make a thread cancel call on the
child thread while the latter sleeps for 3 seconds before proceeding to
write a new value to the dataset.
</p>
<p>
After the iterate call, the child thread logically proceeds to wait
another 3 seconds before writing another newer value to the dataset.
</p>
<p>
The test is correct if the main thread manages to read the second value
at the end of the test. This means that cancellation did not take place
until the end of the iteration call despite of the 3 second wait within
the iteration callback and the extra dataset write operation.
Furthermore, the cancellation should occur before the child can proceed
to write the last value into the dataset.
</p>
<h2>5.4 Test on attribute creation</h2>
<p>
A main thread makes 16 threaded calls to <code>H5Acreate</code> with a
generated name for each attribute. Sixteen attributes should be created
for the single dataset in random (chronological) order and receive values
depending on its generated attribute name (e.g. <i>attrib010</i> would
receive the value 10).
</p>
<p>
After joining with all child threads, the main thread proceeds to read
each attribute by generated name to see if the value tallies. Failure is
detected if the attribute name does not exist (meaning they were never
created) or if the wrong values were read back.
</p>
<h1>A. Recursive Lock implementation code</h1>
<blockquote>
<pre>
void H5_mutex_init(H5_mutex_t *H5_mutex)
{
H5_mutex->owner_thread = NULL;
pthread_mutex_init(&H5_mutex->atomic_lock, NULL);
pthread_cond_init(&H5_mutex->cond_var, NULL);
H5_mutex->lock_count = 0;
}
void H5_mutex_lock(H5_mutex_t *H5_mutex)
{
pthread_mutex_lock(&H5_mutex->atomic_lock);
if (pthread_equal(pthread_self(), H5_mutex->owner_thread)) {
/* already owned by self - increment count */
H5_mutex->lock_count++;
} else {
if (H5_mutex->owner_thread == NULL) {
/* no one else has locked it - set owner and grab lock */
H5_mutex->owner_thread = pthread_self();
H5_mutex->lock_count = 1;
} else {
/* if already locked by someone else */
while (1) {
pthread_cond_wait(&H5_mutex->cond_var, &H5_mutex->atomic_lock);
if (H5_mutex->owner_thread == NULL) {
H5_mutex->owner_thread = pthread_self();
H5_mutex->lock_count = 1;
break;
} /* else do nothing and loop back to wait on condition*/
}
}
}
pthread_mutex_unlock(&H5_mutex->atomic_lock);
}
void H5_mutex_unlock(H5_mutex_t *H5_mutex)
{
pthread_mutex_lock(&H5_mutex->atomic_lock);
H5_mutex->lock_count--;
if (H5_mutex->lock_count == 0) {
H5_mutex->owner_thread = NULL;
pthread_cond_signal(&H5_mutex->cond_var);
}
pthread_mutex_unlock(&H5_mutex->atomic_lock);
}
</pre>
</blockquote>
<h1>B. First thread initialization</h1>
<blockquote>
<pre>
void H5_first_thread_init(void)
{
/* initialize global API mutex lock */
H5_g.H5_libinit_g = FALSE;
H5_g.init_lock.owner_thread = NULL;
pthread_mutex_init(&H5_g.init_lock.atomic_lock, NULL);
pthread_cond_init(&H5_g.init_lock.cond_var, NULL);
H5_g.init_lock.lock_count = 0;
/* initialize key for thread-specific error stacks */
pthread_key_create(&H5_errstk_key_g, NULL);
/* initialize key for thread cancellability mechanism */
pthread_key_create(&H5_cancel_key_g, NULL);
}
</pre>
</blockquote>
<h1>C. Per-thread error stack acquisition</h1>
<blockquote>
<pre>
H5E_t *H5E_get_stack(void)
{
H5E_t *estack;
if (estack = pthread_getspecific(H5_errstk_key_g)) {
return estack;
} else {
/* no associated value with current thread - create one */
estack = (H5E_t *)malloc(sizeof(H5E_t));
pthread_setspecific(H5_errstk_key_g, (void *)estack);
return estack;
}
}
</pre>
</blockquote>
<h1>D. Thread cancellation mechanisms</h1>
<blockquote>
<pre>
void H5_cancel_count_inc(void)
{
H5_cancel_t *cancel_counter;
if (cancel_counter = pthread_getspecific(H5_cancel_key_g)) {
/* do nothing here */
} else {
/*
* first time thread calls library - create new counter and
* associate with key
*/
cancel_counter = (H5_cancel_t *)malloc(sizeof(H5_cancel_t));
cancel_counter->cancel_count = 0;
pthread_setspecific(H5_cancel_key_g, (void *)cancel_counter);
}
if (cancel_counter->cancel_count == 0) {
/* thread entering library */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
&(cancel_counter->previous_state));
}
cancel_counter->cancel_count++;
}
void H5_cancel_count_dec(void)
{
H5_cancel_t *cancel_counter = pthread_getspecific(H5_cancel_key_g);
if (cancel_counter->cancel_count == 1)
pthread_setcancelstate(cancel_counter->previous_state, NULL);
cancel_counter->cancel_count--;
}
</pre>
</blockquote>
<h1>E. Macro expansion codes</h1>
<h2>E.1 <code>FUNC_ENTER</code></h2>
<blockquote>
<pre>
/* Initialize the library */ \
H5_FIRST_THREAD_INIT \
H5_API_UNSET_CANCEL \
H5_API_LOCK_BEGIN \
if (!(H5_INIT_GLOBAL)) { \
H5_INIT_GLOBAL = TRUE; \
if (H5_init_library() < 0) { \
HRETURN_ERROR (H5E_FUNC, H5E_CANTINIT, err, \
"library initialization failed"); \
} \
} \
H5_API_LOCK_END \
:
:
:
</pre>
</blockquote>
<h2>E.2 <code>H5_FIRST_THREAD_INIT</code></h2>
<blockquote>
<pre>
/* Macro for first thread initialization */
#define H5_FIRST_THREAD_INIT \
pthread_once(&H5_first_init_g, H5_first_thread_init);
</pre>
</blockquote>
<h2>E.3 <code>H5_API_UNSET_CANCEL</code></h2>
<blockquote>
<pre>
#define H5_API_UNSET_CANCEL \
if (H5_IS_API(__func__)) { \
H5_cancel_count_inc(); \
}
</pre>
</blockquote>
<h2>E.4 <code>H5_API_LOCK_BEGIN</code></h2>
<blockquote>
<pre>
#define H5_API_LOCK_BEGIN \
if (H5_IS_API(__func__)) { \
H5_mutex_lock(&H5_g.init_lock);
</pre>
</blockquote>
<h2>E.5 <code>H5_API_LOCK_END</code></h2>
<blockquote>
<pre>
#define H5_API_LOCK_END }
</pre>
</blockquote>
<h2>E.6 <code>HRETURN</code> and <code>HRETURN_ERROR</code></h2>
<blockquote>
<pre>
:
:
H5_API_UNLOCK_BEGIN \
H5_API_UNLOCK_END \
H5_API_SET_CANCEL \
return ret_val; \
}
</pre>
</blockquote>
<h2>E.7 <code>H5_API_UNLOCK_BEGIN</code></h2>
<blockquote>
<pre>
#define H5_API_UNLOCK_BEGIN \
if (H5_IS_API(__func__)) { \
H5_mutex_unlock(&H5_g.init_lock);
</pre>
</blockquote>
<h2>E.8 <code>H5_API_UNLOCK_END</code></h2>
<blockquote>
<pre>
#define H5_API_UNLOCK_END }
</pre>
</blockquote>
<h2>E.9 <code>H5_API_SET_CANCEL</code></h2>
<blockquote>
<pre>
#define H5_API_SET_CANCEL \
if (H5_IS_API(__func__)) { \
H5_cancel_count_dec(); \
}
</pre>
</blockquote>
<h2>By Chee Wai Lee</h2>
<h4>By Bill Wendling</h4>
</body>
</html>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Thu Aug 31 2023 00:30:20 for HDF5 by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
</ul>
</div>
</body>
</html>
|