-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathhthreads.c
1848 lines (1620 loc) · 65.3 KB
/
hthreads.c
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* HTHREADS.C (C) Copyright "Fish" (David B. Trout), 2013-2021 */
/* Hercules locking and threading */
/* */
/* Released under "The Q Public License Version 1" */
/* (http://www.hercules-390.org/herclic.html) as modifications to */
/* Hercules. */
/*-------------------------------------------------------------------*/
/* Based on original pttrace.c (C) Copyright Greg Smith, 2003-2013 */
/* Collaboratively enhanced and extended by Mark L. Gaubatz */
/* and "Fish" (David B. Trout), May-June 2013 */
/*-------------------------------------------------------------------*/
#include "hstdinc.h"
#define _HTHREAD_C_
#define _HUTIL_DLL_
#include "hercules.h"
/*-------------------------------------------------------------------*/
/* Hercules Internal thread structure */
/*-------------------------------------------------------------------*/
struct HTHREAD /* Hercules internal thread structure*/
{
LIST_ENTRY ht_link; /* links threads together in a chain */
const char* ht_cr_locat; /* Location where thread was created */
TIMEVAL ht_cr_time; /* Time of day when it was created */
TID ht_tid; /* Thread-Id of the thread */
LOCK* ht_ob_lock; /* Lock attempting to obtain or NULL */
TIMEVAL ht_ob_time; /* Time of day when obtain attempted */
const char* ht_ob_where; /* Location where obtain attempted */
const char* ht_name; /* strdup of Thread name */
bool ht_footprint; /* Footprint for deadlock detection */
};
typedef struct HTHREAD HTHREAD; /* Shorter name for the same thing */
/*-------------------------------------------------------------------*/
/* Hercules Internal ILOCK structure */
/*-------------------------------------------------------------------*/
struct ILOCK /* Hercules internal ILOCK structure */
{
LIST_ENTRY il_link; /* links locks together in a chain */
void* il_addr; /* Lock address */
const char* il_name; /* strdup of Lock name */
const char* il_ob_locat; /* Location where lock was obtained */
TIMEVAL il_ob_time; /* Time of day when it was obtained */
TID il_ob_tid; /* Thread-Id of who obtained it */
HLOCK il_locklock; /* Internal ILOCK structure lock */
union {
HLOCK il_lock; /* The actual locking model mutex */
HRWLOCK il_rwlock; /* The actual locking model rwlock */
};
const char* il_cr_locat; /* Location where lock was created */
TIMEVAL il_cr_time; /* Time of day when it was created */
TID il_cr_tid; /* Thread-Id of who created it */
};
typedef struct ILOCK ILOCK; /* Shorter name for the same thing */
/*-------------------------------------------------------------------*/
/* Internal PTT trace helper macros */
/*-------------------------------------------------------------------*/
#define PTTRACE(_msg,_data1,_data2,_loc,_result) \
do { \
if (pttclass & PTT_CL_THR) \
ptt_pthread_trace(PTT_CL_THR, \
_msg,_data1,_data2,_loc,_result,NULL); \
} while(0)
#define PTTRACE2(_msg,_data1,_data2,_loc,_result,_tv) \
do { \
if (pttclass & PTT_CL_THR) \
ptt_pthread_trace(PTT_CL_THR, \
_msg,_data1,_data2,_loc,_result,_tv); \
} while(0)
/*-------------------------------------------------------------------*/
/* Default stack size for create_thread */
/*-------------------------------------------------------------------*/
#define HTHREAD_STACK_SIZE ONE_MEGABYTE
/*-------------------------------------------------------------------*/
/* Issue locking call error message */
/*-------------------------------------------------------------------*/
static void loglock( ILOCK* ilk, const int rc, const char* calltype,
const char* err_loc )
{
const char* err_desc;
switch (rc)
{
case EEXIST: err_desc = "already init'ed"; break;
case EAGAIN: err_desc = "max recursion"; break;
case EPERM: err_desc = "not owned"; break;
case EINVAL: err_desc = "invalid argument"; break;
case EDEADLK: err_desc = "deadlock"; break;
case ENOTRECOVERABLE: err_desc = "not recoverable"; break;
case EOWNERDEAD: err_desc = "owner dead"; break;
case EBUSY: err_desc = "busy"; break;
case ETIMEDOUT: err_desc = "timeout"; break;
default: err_desc = "(unknown)"; break;
}
// "'%s(%s)' failed: rc=%d: %s; tid="TIDPAT", loc=%s"
WRMSG( HHC90013, "E", calltype, ilk->il_name, rc, err_desc,
TID_CAST( hthread_self() ), TRIMLOC( err_loc ));
if (EEXIST == rc)
{
// "lock %s was already initialized at %s"
WRMSG( HHC90028, "I", ilk->il_name, TRIMLOC( ilk->il_cr_locat ));
}
if (ilk->il_ob_tid)
{
// "lock %s was %s by thread "TIDPAT" at %s"
WRMSG( HHC90014, "I", ilk->il_name,
EEXIST == rc ? "still held" : "obtained",
TID_CAST( ilk->il_ob_tid ),
TRIMLOC( ilk->il_ob_locat ));
}
}
/*-------------------------------------------------------------------*/
/* Hthreads internal variables */
/*-------------------------------------------------------------------*/
static LIST_ENTRY locklist; /* Internal locks list anchor */
static HLOCK listlock; /* Lock for accessing locklist */
static int lockcount; /* Number of locks in list */
static LIST_ENTRY threadlist; /* Internal threads list anchor */
static HLOCK threadlock; /* Lock for accessing threadlist */
static int threadcount; /* Number of threads in list */
static bool inited = false; /* true = internally initialized */
/*-------------------------------------------------------------------*/
/* Internal macros to control access to our internal lists */
/*-------------------------------------------------------------------*/
#define LockLocksList() hthread_mutex_lock ( &listlock )
#define UnlockLocksList() hthread_mutex_unlock ( &listlock )
#define LockThreadsList() hthread_mutex_lock ( &threadlock )
#define UnlockThreadsList() hthread_mutex_unlock ( &threadlock )
/*-------------------------------------------------------------------*/
/* Initialize HTHTREADS package */
/*-------------------------------------------------------------------*/
DLL_EXPORT void hthreads_internal_init()
{
if (!inited)
{
MATTR attr;
int rc, minprio, maxprio;
#if defined( OPTION_FTHREADS )
fthreads_internal_init();
#endif
/* Initialize our internal locks */
rc = hthread_mutexattr_init( &attr );
if (rc)
goto fatal;
rc = hthread_mutexattr_settype( &attr, HTHREAD_MUTEX_DEFAULT );
if (rc)
goto fatal;
rc = hthread_mutex_init( &listlock, &attr );
if (rc)
goto fatal;
rc = hthread_mutex_init( &threadlock, &attr );
if (rc)
goto fatal;
rc = hthread_mutexattr_destroy( &attr );
if (rc)
goto fatal;
/* Initialize our list anchors */
InitializeListHead( &locklist );
lockcount = 0;
InitializeListHead( &threadlist );
threadcount = 0;
/* Retrieve the minimum/maximum scheduling priority */
minprio = hthread_get_priority_min( HTHREAD_POLICY );
maxprio = hthread_get_priority_max( HTHREAD_POLICY );
if (minprio >= 0 && maxprio >= 0 && maxprio >= minprio)
{
/* "The Open Group Base Specifications Issue 7"
"Scheduling Policies", "SCHED_RR": "[...] Conforming
implementations shall provide a priority range of at
least 32 priorities for this policy."
*/
if (((maxprio - minprio) + 1) < 32)
goto fatal;
sysblk.minprio = minprio;
sysblk.maxprio = maxprio;
}
/* Add an entry for the current thread to our threads list
since it was created by the operating system and not us */
{
const char* ht_cr_locat = PTT_LOC; // (where created)
HTHREAD* ht = calloc_aligned( sizeof( HTHREAD ), 64 );
InitializeListLink( &ht->ht_link );
gettimeofday( &ht->ht_cr_time, NULL );
ht->ht_tid = hthread_self();
ht->ht_name = strdup( "main" );
ht->ht_cr_locat = ht_cr_locat;
ht->ht_ob_lock = NULL;
ht->ht_footprint = false;
InsertListHead( &threadlist, &ht->ht_link );
threadcount++;
}
/* One-time initialization completed */
inited = true;
return;
fatal:
perror( "Fatal error in hthreads_internal_init function" );
exit(1);
}
}
/*-------------------------------------------------------------------*/
/* Find ILOCK structure. */
/* Note: the caller is responsible for locking the list beforehand! */
/*-------------------------------------------------------------------*/
static ILOCK* hthreads_find_ILOCK_locked( const LOCK* plk, LIST_ENTRY* anchor )
{
ILOCK* ilk; /* Pointer to ILOCK structure */
LIST_ENTRY* ple; /* Ptr to LIST_ENTRY structure */
if (!anchor) anchor = &locklist;
for (ple = anchor->Flink; ple != anchor; ple = ple->Flink)
{
ilk = CONTAINING_RECORD( ple, ILOCK, il_link );
if (ilk->il_addr == plk)
{
RemoveListEntry( &ilk->il_link );
InsertListHead( anchor, &ilk->il_link );
return ilk;
}
}
return NULL;
}
#if 0 // (currently unreferenced)
/*-------------------------------------------------------------------*/
/* Find internal ILOCK structure. */
/*-------------------------------------------------------------------*/
static ILOCK* hthreads_find_ILOCK( const LOCK* plk )
{
ILOCK* ilk;
LockLocksList();
{
ilk = hthreads_find_ILOCK_locked( plk, NULL );
}
UnlockLocksList();
return ilk;
}
#endif
/*-------------------------------------------------------------------*/
/* Initialize a lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_initialize_lock( LOCK* plk, const char* name,
const char* create_loc )
{
int rc;
MATTR attr; /* for internal lock structure lock */
ILOCK* ilk;
LockLocksList();
{
ilk = hthreads_find_ILOCK_locked( plk, NULL );
if (ilk)
{
loglock( ilk, EEXIST, "initialize lock", create_loc );
UnlockLocksList();
return EEXIST;
}
if (!(ilk = calloc_aligned( sizeof( ILOCK ), 64 )))
goto fatal;
gettimeofday( &ilk->il_cr_time, NULL );
ilk->il_addr = plk;
ilk->il_name = strdup( name );
ilk->il_cr_locat = create_loc;
ilk->il_cr_tid = hthread_self();
ilk->il_ob_locat = "null:0";
ilk->il_ob_time.tv_sec = 0;
ilk->il_ob_time.tv_usec = 0;
ilk->il_ob_tid = 0;
/* Initialize the requested lock */
rc = hthread_mutexattr_init( &attr );
if (rc)
goto fatal;
rc = hthread_mutexattr_settype( &attr, HTHREAD_MUTEX_DEFAULT );
if (rc)
goto fatal;
rc = hthread_mutex_init( &ilk->il_locklock, &attr );
if (rc)
goto fatal;
rc = hthread_mutex_init( &ilk->il_lock, &attr );
if (rc)
goto fatal;
rc = hthread_mutexattr_destroy( &attr );
if (rc)
goto fatal;
plk->ilk = ilk; /* (LOCK is now initialized) */
InsertListHead( &locklist, &ilk->il_link );
lockcount++;
PTTRACE( "lock init", plk, 0, create_loc, PTT_MAGIC );
}
UnlockLocksList();
return 0;
fatal:
perror( "Fatal error in hthread_initialize_lock function" );
exit(1);
}
/*-------------------------------------------------------------------*/
/* Initialize a R/W lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_initialize_rwlock( RWLOCK* plk, const char* name,
const char* create_loc )
{
int rc;
MATTR attr1; /* for internal lock structure lock */
RWATTR attr2; /* for primary rwlock */
ILOCK* ilk;
LockLocksList();
{
ilk = hthreads_find_ILOCK_locked( (LOCK*) plk, NULL );
if (ilk)
{
loglock( ilk, EEXIST, "initialize rwlock", create_loc );
UnlockLocksList();
return EEXIST;
}
if (!(ilk = calloc_aligned( sizeof( ILOCK ), 64 )))
goto fatal;
gettimeofday( &ilk->il_cr_time, NULL );
ilk->il_addr = plk;
ilk->il_name = strdup( name );
ilk->il_cr_locat = create_loc;
ilk->il_cr_tid = hthread_self();
ilk->il_ob_locat = "null:0";
ilk->il_ob_time.tv_sec = 0;
ilk->il_ob_time.tv_usec = 0;
ilk->il_ob_tid = 0;
/* Initialize the requested lock */
rc = hthread_mutexattr_init( &attr1 );
if (rc)
goto fatal;
rc = hthread_rwlockattr_init( &attr2 );
if (rc)
goto fatal;
rc = hthread_mutexattr_settype( &attr1, HTHREAD_MUTEX_DEFAULT );
if (rc)
goto fatal;
rc = hthread_rwlockattr_setpshared( &attr2, HTHREAD_RWLOCK_DEFAULT );
if (rc)
goto fatal;
rc = hthread_mutex_init( &ilk->il_locklock, &attr1 );
if (rc)
goto fatal;
rc = hthread_rwlock_init( &ilk->il_rwlock, &attr2 );
if (rc)
goto fatal;
rc = hthread_mutexattr_destroy( &attr1 );
if (rc)
goto fatal;
rc = hthread_rwlockattr_destroy( &attr2 );
if (rc)
goto fatal;
plk->ilk = ilk; /* (RWLOCK is now initialized) */
InsertListHead( &locklist, &ilk->il_link );
lockcount++;
PTTRACE( "rwlock init", plk, &attr2, create_loc, PTT_MAGIC );
}
UnlockLocksList();
return 0;
fatal:
perror( "Fatal error in hthread_initialize_rwlock function" );
exit(1);
}
/*-------------------------------------------------------------------*/
/* Find HTHREAD structure. */
/* Note: the caller is responsible for locking the list beforehand! */
/*-------------------------------------------------------------------*/
static HTHREAD* hthread_find_HTHREAD_locked( TID tid, LIST_ENTRY* anchor )
{
HTHREAD* ht;
LIST_ENTRY* ple;
if (!anchor) anchor = &threadlist;
for (ple = anchor->Flink; ple != anchor; ple = ple->Flink)
{
ht = CONTAINING_RECORD( ple, HTHREAD, ht_link );
if (equal_threads( ht->ht_tid, tid ))
{
RemoveListEntry( &ht->ht_link );
InsertListHead( anchor, &ht->ht_link );
return ht;
}
}
return NULL;
}
/*-------------------------------------------------------------------*/
/* Internal thread function to find an HTHREAD entry in our list */
/*-------------------------------------------------------------------*/
static HTHREAD* hthread_find_HTHREAD( TID tid )
{
HTHREAD* ht;
LockThreadsList();
{
ht = hthread_find_HTHREAD_locked( tid, NULL );
}
UnlockThreadsList();
return ht;
}
/*-------------------------------------------------------------------*/
/* Remember that a thread is waiting to obtain a given lock */
/*-------------------------------------------------------------------*/
static void hthread_obtaining_lock( LOCK* plk, const char* loc )
{
HTHREAD* ht;
if (!(ht = hthread_find_HTHREAD( hthread_self() )))
return;
ht->ht_ob_lock = plk;
free( ht->ht_ob_where );
ht->ht_ob_where = strdup( loc );
gettimeofday( &ht->ht_ob_time, NULL );
}
/*-------------------------------------------------------------------*/
/* Forget that a thread was waiting for a lock */
/*-------------------------------------------------------------------*/
static void hthread_lock_obtained()
{
HTHREAD* ht;
if (!(ht = hthread_find_HTHREAD( hthread_self() )))
return;
ht->ht_ob_lock = NULL;
}
/*-------------------------------------------------------------------*/
/* Obtain a lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_obtain_lock( LOCK* plk, const char* obtain_loc )
{
int rc;
U64 waitdur;
ILOCK* ilk;
TIMEVAL tv;
ilk = (ILOCK*) plk->ilk;
hthread_obtaining_lock( plk, obtain_loc );
PTTRACE( "lock before", plk, NULL, obtain_loc, PTT_MAGIC );
rc = hthread_mutex_trylock( &ilk->il_lock );
if (EBUSY == rc)
{
waitdur = host_tod();
rc = hthread_mutex_lock( &ilk->il_lock );
gettimeofday( &tv, NULL );
waitdur = host_tod() - waitdur;
}
else
{
gettimeofday( &tv, NULL );
waitdur = 0;
}
PTTRACE2( "lock after", plk, (void*) waitdur, obtain_loc, rc, &tv );
hthread_lock_obtained();
if (rc)
loglock( ilk, rc, "obtain_lock", obtain_loc );
if (!rc || EOWNERDEAD == rc)
{
hthread_mutex_lock( &ilk->il_locklock );
{
ilk->il_ob_locat = obtain_loc;
ilk->il_ob_tid = hthread_self();
memcpy( &ilk->il_ob_time, &tv, sizeof( TIMEVAL ));
}
hthread_mutex_unlock( &ilk->il_locklock );
}
return rc;
}
/*-------------------------------------------------------------------*/
/* Release a lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_release_lock( LOCK* plk, const char* release_loc )
{
int rc;
ILOCK* ilk;
ilk = (ILOCK*) plk->ilk;
rc = hthread_mutex_unlock( &ilk->il_lock );
PTTRACE( "unlock", plk, NULL, release_loc, rc );
if (rc)
loglock( ilk, rc, "release_lock", release_loc );
hthread_mutex_lock( &ilk->il_locklock );
{
ilk->il_ob_locat = "null:0";
ilk->il_ob_tid = 0;
}
hthread_mutex_unlock( &ilk->il_locklock );
return rc;
}
/*-------------------------------------------------------------------*/
/* Release a R/W lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_release_rwlock( RWLOCK* plk, const char* release_loc )
{
int rc;
ILOCK* ilk;
ilk = (ILOCK*) plk->ilk;
rc = hthread_rwlock_unlock( &ilk->il_rwlock );
PTTRACE( "rwunlock", plk, NULL, release_loc, rc );
if (rc)
loglock( ilk, rc, "release_rwlock", release_loc );
hthread_mutex_lock( &ilk->il_locklock );
{
ilk->il_ob_locat = "null:0";
ilk->il_ob_tid = 0;
}
hthread_mutex_unlock( &ilk->il_locklock );
return rc;
}
/*-------------------------------------------------------------------*/
/* Destroy a lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_destroy_lock( LOCK* plk, const char* destroy_loc )
{
int rc1, rc2 = 0;
ILOCK* ilk = (ILOCK*) plk->ilk;
if (!ilk)
return EINVAL;
/* Destroy the Internal ILOCK structure lock */
if ((rc1 = hthread_mutex_destroy( &ilk->il_locklock )) == 0)
{
/* Now destroy the actual locking model HLOCK lock */
if ((rc2 = hthread_mutex_destroy( &ilk->il_lock )) != 0)
loglock( ilk, rc2, "destroy_lock", destroy_loc );
}
else
loglock( ilk, rc1, "destroy_lock", destroy_loc );
if (rc1 == 0 && rc2 == 0)
{
LockLocksList();
{
RemoveListEntry( &ilk->il_link );
lockcount--;
}
UnlockLocksList();
free( ilk->il_name );
free_aligned( ilk );
plk->ilk = NULL;
}
return (rc1 ? rc1 : (rc2 ? rc2 : 0));
}
/*-------------------------------------------------------------------*/
/* Destroy a R/W lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_destroy_rwlock( RWLOCK* plk, const char* destroy_loc )
{
int rc1, rc2 = 0;
ILOCK* ilk = (ILOCK*) plk->ilk;
if (!ilk)
return EINVAL;
/* Destroy the Internal ILOCK structure HLOCK */
if ((rc1 = hthread_mutex_destroy( &ilk->il_locklock )) == 0)
{
/* Now destroy the actual locking model HRWLOCK rwlock */
if ((rc2 = hthread_rwlock_destroy( &ilk->il_rwlock )) != 0)
loglock( ilk, rc2, "destroy_rwlock", destroy_loc );
}
else
loglock( ilk, rc1, "destroy_rwlock", destroy_loc );
if (rc1 == 0 && rc2 == 0)
{
LockLocksList();
{
RemoveListEntry( &ilk->il_link );
lockcount--;
}
UnlockLocksList();
free( ilk->il_name );
free_aligned( ilk );
plk->ilk = NULL;
}
return (rc1 ? rc1 : (rc2 ? rc2 : 0));
}
/*-------------------------------------------------------------------*/
/* Try to obtain a lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_try_obtain_lock( LOCK* plk, const char* obtain_loc )
{
int rc;
ILOCK* ilk;
TIMEVAL tv;
ilk = (ILOCK*) plk->ilk;
PTTRACE( "try before", plk, NULL, obtain_loc, PTT_MAGIC );
rc = hthread_mutex_trylock( &ilk->il_lock );
gettimeofday( &tv, NULL );
PTTRACE2( "try after", plk, NULL, obtain_loc, rc, &tv );
if (rc && EBUSY != rc)
loglock( ilk, rc, "try_obtain_lock", obtain_loc );
if (!rc || EOWNERDEAD == rc)
{
hthread_mutex_lock( &ilk->il_locklock );
{
ilk->il_ob_locat = obtain_loc;
ilk->il_ob_tid = hthread_self();
memcpy( &ilk->il_ob_time, &tv, sizeof( TIMEVAL ));
}
hthread_mutex_unlock( &ilk->il_locklock );
}
return rc;
}
/*-------------------------------------------------------------------*/
/* Test if *ANYONE* is holding given lock (pseudo-boolean function) */
/*-------------------------------------------------------------------*/
/* PROGRAMMING NOTE: this function ISN'T a boolean function but can */
/* be treated as such, with the following caveat: it only determines */
/* if the given lock is held by anyone, but not necessarily by YOU! */
/* That is to say, if the function returns 0 (false), then it means */
/* the lock is NOT being held -- by ANYONE. If the function returns */
/* non-zero (true) however, then it only means the lock *is* being */
/* held -- by SOMEONE -- but that someone may NOT be you! It might */
/* be held by someone OTHER than you! */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_test_lock( LOCK* plk )
{
int rc;
ILOCK* ilk;
ilk = (ILOCK*) plk->ilk;
rc = hthread_mutex_trylock( &ilk->il_lock );
if (rc)
return rc;
hthread_mutex_unlock( &ilk->il_lock );
return 0;
}
/*-------------------------------------------------------------------*/
/* Check if *YOU* are holding a given lock (boolean function) */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_have_lock( LOCK* plk )
{
int rc;
ILOCK* ilk;
ilk = (ILOCK*) plk->ilk;
rc = hthread_equal_threads( hthread_self(), ilk->il_ob_tid );
return rc;
}
/*-------------------------------------------------------------------*/
/* Obtain read-only access to R/W lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_obtain_rdlock( RWLOCK* plk, const char* obtain_loc )
{
int rc;
U64 waitdur;
ILOCK* ilk;
ilk = (ILOCK*) plk->ilk;
hthread_obtaining_lock( (LOCK*) plk, obtain_loc );
PTTRACE( "rdlock before", plk, NULL, obtain_loc, PTT_MAGIC );
rc = hthread_rwlock_tryrdlock( &ilk->il_rwlock );
if (EBUSY == rc)
{
waitdur = host_tod();
rc = hthread_rwlock_rdlock( &ilk->il_rwlock );
waitdur = host_tod() - waitdur;
}
else
waitdur = 0;
PTTRACE( "rdlock after", plk, (void*) waitdur, obtain_loc, rc );
hthread_lock_obtained();
if (rc)
loglock( ilk, rc, "obtain_rdloc", obtain_loc );
return rc;
}
/*-------------------------------------------------------------------*/
/* Obtain exclusive write access to a R/W lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_obtain_wrlock( RWLOCK* plk, const char* obtain_loc )
{
int rc;
U64 waitdur;
ILOCK* ilk;
TIMEVAL tv;
ilk = (ILOCK*) plk->ilk;
hthread_obtaining_lock( (LOCK*) plk, obtain_loc );
PTTRACE( "wrlock before", plk, NULL, obtain_loc, PTT_MAGIC );
rc = hthread_rwlock_trywrlock( &ilk->il_rwlock );
if (EBUSY == rc)
{
waitdur = host_tod();
rc = hthread_rwlock_wrlock( &ilk->il_rwlock );
gettimeofday( &tv, NULL );
waitdur = host_tod() - waitdur;
}
else
{
gettimeofday( &tv, NULL );
waitdur = 0;
}
PTTRACE2( "wrlock after", plk, (void*) waitdur, obtain_loc, rc, &tv );
hthread_lock_obtained();
if (rc)
loglock( ilk, rc, "obtain_wrlock", obtain_loc );
if (!rc || EOWNERDEAD == rc)
{
hthread_mutex_lock( &ilk->il_locklock );
{
ilk->il_ob_locat = obtain_loc;
ilk->il_ob_tid = hthread_self();
memcpy( &ilk->il_ob_time, &tv, sizeof( TIMEVAL ));
}
hthread_mutex_unlock( &ilk->il_locklock );
}
return rc;
}
/*-------------------------------------------------------------------*/
/* Try to obtain read-only access to a R/W lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_try_obtain_rdlock( RWLOCK* plk, const char* obtain_loc )
{
int rc;
ILOCK* ilk;
ilk = (ILOCK*) plk->ilk;
PTTRACE( "tryrd before", plk, NULL, obtain_loc, PTT_MAGIC );
rc = hthread_rwlock_tryrdlock( &ilk->il_rwlock );
PTTRACE( "tryrd after", plk, NULL, obtain_loc, rc );
if (rc && EBUSY != rc)
loglock( ilk, rc, "try_obtain_rdlock", obtain_loc );
return rc;
}
/*-------------------------------------------------------------------*/
/* Try to obtain exclusive write access to a R/W lock */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_try_obtain_wrlock( RWLOCK* plk, const char* obtain_loc )
{
int rc;
ILOCK* ilk;
TIMEVAL tv;
ilk = (ILOCK*) plk->ilk;
PTTRACE( "trywr before", plk, NULL, obtain_loc, PTT_MAGIC );
rc = hthread_rwlock_trywrlock( &ilk->il_rwlock );
gettimeofday( &tv, NULL );
PTTRACE2( "trywr after", plk, NULL, obtain_loc, rc, &tv );
if (rc && EBUSY != rc)
loglock( ilk, rc, "try_obtain_wrlock", obtain_loc );
if (!rc)
{
hthread_mutex_lock( &ilk->il_locklock );
{
ilk->il_ob_locat = obtain_loc;
ilk->il_ob_tid = hthread_self();
memcpy( &ilk->il_ob_time, &tv, sizeof( TIMEVAL ));
}
hthread_mutex_unlock( &ilk->il_locklock );
}
return rc;
}
/*-------------------------------------------------------------------*/
/* Test if read-only access to a R/W lock is held */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_test_rdlock( RWLOCK* plk )
{
int rc;
ILOCK* ilk;
ilk = (ILOCK*) plk->ilk;
rc = hthread_rwlock_tryrdlock( &ilk->il_rwlock );
if (rc)
return rc;
hthread_rwlock_unlock( &ilk->il_rwlock );
return 0;
}
/*-------------------------------------------------------------------*/
/* Test if exclusive write access to a R/W lock is held */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_test_wrlock( RWLOCK* plk )
{
int rc;
ILOCK* ilk;
ilk = (ILOCK*) plk->ilk;
rc = hthread_rwlock_trywrlock( &ilk->il_rwlock );
if (rc)
return rc;
hthread_rwlock_unlock( &ilk->il_rwlock );
return 0;
}
/*-------------------------------------------------------------------*/
/* Initialize a condition variable */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_initialize_condition( COND* plc, const char* init_loc )
{
int rc;
PTTRACE( "cond init", NULL, plc, init_loc, PTT_MAGIC );
rc = hthread_cond_init( plc );
return rc;
}
/*-------------------------------------------------------------------*/
/* Destroy a condition variable */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_destroy_condition( COND* plc )
{
int rc;
rc = hthread_cond_destroy( plc );
return rc;
}
/*-------------------------------------------------------------------*/
/* Signal a condition variable (releases only one thread) */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_signal_condition( COND* plc, const char* sig_loc )
{
int rc;
rc = hthread_cond_signal( plc );
PTTRACE( "signal", NULL, plc, sig_loc, rc );
return rc;
}
/*-------------------------------------------------------------------*/
/* Broadcast a condition variable (releases all waiting threads) */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_broadcast_condition( COND* plc, const char* bcast_loc )
{
int rc;
rc = hthread_cond_broadcast( plc );
PTTRACE( "broadcast", NULL, plc, bcast_loc, rc );
return rc;
}
/*-------------------------------------------------------------------*/
/* Wait for condition variable to be signaled */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_wait_condition( COND* plc, LOCK* plk, const char* wait_loc )
{
int rc;
ILOCK* ilk;
ilk = (ILOCK*) plk->ilk;
PTTRACE( "wait before", plk, plc, wait_loc, PTT_MAGIC );
rc = hthread_cond_wait( plc, &ilk->il_lock );
PTTRACE( "wait after", plk, plc, wait_loc, rc );
ilk->il_ob_tid = hthread_self();
if (rc)
loglock( ilk, rc, "wait_condition", wait_loc );
return rc;
}
/*-------------------------------------------------------------------*/
/* Timed wait for a condition variable to be signaled */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_timed_wait_condition( COND* plc, LOCK* plk,
const struct timespec* tm,
const char* wait_loc )
{
int rc;
ILOCK* ilk;
ilk = (ILOCK*) plk->ilk;
PTTRACE( "tw before", plk, plc, wait_loc, PTT_MAGIC );
rc = hthread_cond_timedwait( plc, &ilk->il_lock, tm );
PTTRACE( "tw after", plk, plc, wait_loc, rc );
ilk->il_ob_tid = hthread_self();
if (rc && ETIMEDOUT != rc)
loglock( ilk, rc, "timed_wait_condition", wait_loc );
return rc;
}
/*-------------------------------------------------------------------*/
/* Internal helper function to initialize a thread attribute */
/*-------------------------------------------------------------------*/
static INLINE int hthread_init_thread_attr( ATTR* pat, int state )
{
int rc;
rc = hthread_attr_init( pat );
if (!rc)
rc = hthread_attr_setstacksize( pat, HTHREAD_STACK_SIZE );
if (!rc)
rc = hthread_attr_setdetachstate( pat, state );
return rc;
}
/*-------------------------------------------------------------------*/
/* Initialize a thread attribute to "joinable" */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_initialize_join_attr( ATTR* pat )
{
int rc;
rc = hthread_init_thread_attr( pat, HTHREAD_CREATE_JOINABLE );
return rc;
}
/*-------------------------------------------------------------------*/
/* Initialize a thread attribute to "detached" (non-joinable) */
/*-------------------------------------------------------------------*/
DLL_EXPORT int hthread_initialize_detach_attr( ATTR* pat )
{
int rc;
rc = hthread_init_thread_attr( pat, HTHREAD_CREATE_DETACHED );
return rc;
}
/*-------------------------------------------------------------------*/
/* Internal function to list abandoned locks when thread exits */
/*-------------------------------------------------------------------*/
static void hthread_list_abandoned_locks( TID tid, const char* exit_loc )
{
ILOCK* ilk;
LIST_ENTRY* ple;
char threadname[16];
if (sysblk.shutdown)
return;
get_thread_name( tid, threadname );
LockLocksList();
{
for (ple = locklist.Flink; ple != &locklist; ple = ple->Flink)
{
ilk = CONTAINING_RECORD( ple, ILOCK, il_link );
if (hthread_equal( ilk->il_ob_tid, tid ))
{
char tod[27]; /* "YYYY-MM-DD HH:MM:SS.uuuuuu" */