summaryrefslogblamecommitdiffstats
path: root/private/crt32/iostream/mtlock.c
blob: 78201917de1cd24bf685888834f611d2c59815ca (plain) (tree)
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














































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                 
/***
*mtlock.c - Multi-thread locking routines
*
*	Copyright (c) 1987-1992, Microsoft Corporation. All rights reserved.
*
*Purpose:
*	Contains definitions for general-purpose multithread locking functions.
*	_mtlockinit()
*	_mtlock()
*	_mtunlock()
*
*Revision History:
*	03-10-92   KRS	Created from mlock.c.
*
*******************************************************************************/

#include <cruntime.h>
#include <oscalls.h>
#include <internal.h>
#include <os2dll.h>
#include <rterr.h>
#include <stddef.h>
#include <limits.h>

#ifdef MTHREAD

void _CALLTYPE2 _mtlockinit( PRTL_CRITICAL_SECTION pLk)
{

#ifdef	_CRUISER_
	if ( DOSCREATEMUTEXSEM(NULL, pLk, 0, 0) != 0)
		_FATAL;
#else	/* ndef _CRUISER_ */
#ifdef	_WIN32_
	/*
	 * Initialize the critical section.
	 */
	InitializeCriticalSection( pLk );
#else	/* ndef _WIN32_ */
#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!
#endif	/* _WIN32_ */
#endif	/* _CRUISER_ */

}

void _CALLTYPE2 _mtlock ( PRTL_CRITICAL_SECTION pLk)
{
#ifdef	_CRUISER_

	if (DOSREQUESTMUTEXSEM(pLk,-1) != 0)
		_FATAL;

#else	/* ndef _CRUISER_ */

#ifdef	_WIN32_

	/*
	 * Enter the critical section.
	 */
	EnterCriticalSection( pLk );

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */

#endif	/* _CRUISER_ */

}

void _CALLTYPE2 _mtunlock ( PRTL_CRITICAL_SECTION pLk)
{
#ifdef	_CRUISER_
	/*
	 * Release the lock
	 */
	if (DOSRELEASEMUTEXSEM(pLk) != 0)
		_FATAL;

#else	/* ndef _CRUISER_ */

#ifdef	_WIN32_

	/*
	 * leave the critical section.
	 */
	LeaveCriticalSection( pLk );

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */

#endif	/* _CRUISER_ */

}

#endif  /* MTHREAD */











/* history: mlock.c */

#if 0
#ifdef DEBUG
#include <assert.h>
#endif

/*
 * Local routines
 */

static void _CALLTYPE1 _lock_create (unsigned);

#ifdef DEBUG
static struct _debug_lock * _CALLTYPE1 _lock_validate(int);
#endif


/*
 * Global Data
 */

#ifdef _CRUISER_
/*
 * Lock Table
 * This table contains the semaphore handle for each lock (lock number
 * is used as an index into the table).
 */

unsigned _locktable[_TOTAL_LOCKS];		/* array of locks */

#else	/* ndef _CRUISER_ */

#ifdef	_WIN32_
/*
 * Lock Table
 * This table contains the critical section management structure of each
 * lock.
 */

RTL_CRITICAL_SECTION _locktable[_TOTAL_LOCKS];	/* array of locks */

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */

#endif	/* _CRUISER_ */

/*
 * Lock Bit Map
 * This table contains one bit for each lock (i.e., each entry in the
 * _locktable[] array).
 *
 *	 If bit = 0, lock has not been created/opened
 *	 If bit = 1, lock has been created/opened
 */

char _lockmap[(_TOTAL_LOCKS/CHAR_BIT)+1];	/* lock bit map */


#ifdef _LOCKCOUNT
/*
 * Total number of locks held
 */

unsigned _lockcnt = 0;
#endif


#ifdef DEBUG
/*
 * Lock Debug Data Table Segment
 * Contains debugging data for each lock.
 */

struct _debug_lock _debug_locktable[_TOTAL_LOCKS];

#endif

#define _FATAL	_amsg_exit(_RT_LOCK)

/***
* Bit map macros
*
*Purpose:
*	_CLEARBIT() - Clear the specified bit
*	_SETBIT()   - Set the specified bit
*	_TESTBIT()  - Test the specified bit
*
*Entry:
*	char a[] = character array
*	unsigned b = bit number (0-based, range from 0 to whatever)
*	unsigned x = bit number (0-based, range from 0 to 31)
*
*Exit:
*	_CLEARBIT() = void
*	_SETBIT()   = void
*	_TESTBIT()  = 0 or 1
*
*Exceptions:
*
*******************************************************************************/

/*
 * Macros for use when managing a bit in a character array (e.g., _lockmap)
 * a = character array
 * b = bit number (0-based)
 */

#define _CLEARBIT(a,b) \
		( a[b>>3] &= (~(1<<(b&7))) )

#define _SETBIT(a,b) \
		( a[b>>3] |= (1<<(b&7)) )

#define _TESTBIT(a,b) \
		( a[b>>3] & (1<<(b&7)) )

/*
 * Macros for use when managing a bit in an unsigned int
 * x = bit number (0-31)
 */

#define _BIT_INDEX(x)	(1 << (x & 0x1F))


/***
*_mtinitlocks() - Initialize the semaphore lock data base
*
*Purpose:
*	Initialize the mthread semaphore lock data base.
*
*	NOTES:
*	(1) Only to be called ONCE at startup
*	(2) Must be called BEFORE any mthread requests are made
*
*	Schemes for creating the mthread locks:
*
*	Create the locks one at a time on demand the first
*	time the lock is attempted.  This is more complicated but
*	is much faster than creating them all at startup time.
*	These is currently the default scheme.
*
*	Create and open the semaphore that protects the lock data
*	base.
*
*Entry:
*	<none>
*
*Exit:
*	returns on success
*	calls _amsg_exit on failure
*
*Exceptions:
*
*******************************************************************************/

void _CALLTYPE1 _mtinitlocks (
	void
	)
{

	/*
	 * All we need to do is create the lock table lock
	 */

	_lock_create(_LOCKTAB_LOCK);

	/*
	 * Make sure the assumptions we make in this source are correct.
	 * The following is a tricky way to validate sizeof() assumptions
	 * at compile time without generating any runtime code (can't
	 * use sizeof() in an #ifdef).	If the assumption fails, the
	 * compiler will generate a divide by 0 error.
	 *
	 * This here only because it must be inside a subroutine.
	 */

	( (sizeof(char) == 1) ? 1 : (1/0) );
	( (sizeof(int) == 4) ? 1 : (1/0) );

}


/***
*_lock_create() - Create and open a lock
*
*Purpose:
*	Create and open a mthread lock.
*
*	NOTES:
*
*	(1) The caller must have previously determined that the lock
*	needs to be created/opened (and this hasn't already been done).
*
*	(2) The caller must have aquired the _LOCKTAB_LOCK, if needed.
*	(The only time this isn't required is at init time.)
*
*Entry:
*	unsigned locknum = lock to create
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

static void _CALLTYPE1 _lock_create (
	unsigned locknum
	)
{

#ifdef DEBUG
	/*
	 * See if the lock already exists; if so, die.
	 */

	if (_TESTBIT(_lockmap, locknum))
		_FATAL;
#endif

	/*
	 * Convert the lock number into a lock address
	 * and create the semaphore.
	 */
#ifdef	_CRUISER_

	if ( DOSCREATEMUTEXSEM(NULL, &_locktable[locknum], 0, 0) != 0)
		_FATAL;

#else	/* ndef _CRUISER_ */

#ifdef	_WIN32_

	/*
	 * Convert the lock number into a lock address
	 * and initialize the critical section.
	 */
	InitializeCriticalSection( &_locktable[locknum] );

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */

#endif	/* _CRUISER_ */

	/*
	 * Set the appropriate bit in the lock bit map.
	 */

	_SETBIT(_lockmap, locknum);

}


/***
* _lock_stream, etc. - Routines to lock/unlock streams, files, etc.
*
*Purpose:
*	_lock_stream = Lock a stdio stream
*	_unlock_stream = Unlock a stdio stream
*	_lock_file = Lock a lowio file
*	_unlock_file = Unlock a lowio file
*
*Entry:
*	stream/file identifier
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

void _CALLTYPE2 _lock_stream (
	int stream_id
	)
{
	_lock(stream_id+_STREAM_LOCKS);
}

void _CALLTYPE2 _unlock_stream (
	int stream_id
	)
{
	_unlock(stream_id+_STREAM_LOCKS);
}

void _CALLTYPE2 _lock_file (
	int fh
	)
{
	_lock(fh+_FH_LOCKS);
}

void _CALLTYPE2 _unlock_file (
	int fh
	)
{
	_unlock(fh+_FH_LOCKS);
}


/***
* _lock - Acquire a multi-thread lock
*
*Purpose:
*	Note that it is legal for a thread to aquire _EXIT_LOCK1
*	multiple times.
*
*Entry:
*	locknum = number of the lock to aquire
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

void _CALLTYPE2 _lock (
	int locknum
	)
{

#ifdef DEBUG
	struct _debug_lock *deblock;
	unsigned tidbit;
#endif

	/*
	 * Create/open the lock, if necessary
	 */

	if (!_TESTBIT(_lockmap, locknum)) {

		_mlock(_LOCKTAB_LOCK);	/*** WARNING: Recursive lock call ***/

		/* if lock still doesn't exist, create it */

		if (!_TESTBIT(_lockmap, locknum))
			_lock_create(locknum);

		_munlock(_LOCKTAB_LOCK);

	}

#ifdef DEBUG
	/*
	 * Validate the lock and get pointer to debug lock structure, etc.
	 */

	deblock = _lock_validate(locknum);

#ifdef	_CRUISER_

	tidbit = _BIT_INDEX(*_threadid);

#else	/* ndef _CRUISER_ */

#ifdef	_WIN32_

	/*
	 * Set tidbit to 2**(index of ptd[] entry).
	 *
	 * call non-locking form of _getptd to avoid recursing
	 */
	tidbit = _getptd_lk() - _ptd;	/* index of _ptd[] entry */

	tidbit = _BIT_INDEX(tidbit);

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */

#endif	/* _CRUISER_ */

	/*
	 * Make sure we're not trying to get lock we already have
	 * (except for _EXIT_LOCK1).
	 */

	if (locknum != _EXIT_LOCK1)
		if ((deblock->holder) & tidbit)
			_FATAL;

	/*
	 * Set waiter bit for this thread
	 */

	deblock->waiters |= tidbit;

#endif	/* DEBUG */

	/*
	 * Get the lock
	 */

#ifdef _LOCKCOUNT
	_lockcnt++;
#endif

#ifdef	_CRUISER_

	if (DOSREQUESTMUTEXSEM(_locktable[locknum],-1) != 0)
		_FATAL;

#else	/* ndef _CRUISER_ */

#ifdef	_WIN32_

	/*
	 * Enter the critical section.
	 */
	EnterCriticalSection( &_locktable[locknum] );

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */

#endif	/* _CRUISER_ */

#ifdef DEBUG
	/*
	 * Clear waiter bit
	 */

	deblock->waiters &= (~tidbit);

	/*
	 * Make sure there are no lock holders (unless this is
	 * _EXIT_LOCK1); then set holder bit and bump lock count.
	 */

	assert(THREADINTS==1);

	if (locknum != _EXIT_LOCK1)
		if ( (unsigned) deblock->holder != 0)
		       _FATAL;

	deblock->holder &= tidbit;
	deblock->lockcnt++;

#endif

}


/***
* _unlock - Release multi-thread lock
*
*Purpose:
*	Note that it is legal for a thread to aquire _EXIT_LOCK1
*	multiple times.
*
*Entry:
*	locknum = number of the lock to release
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

void _CALLTYPE2 _unlock (
	int locknum
	)
{
#ifdef DEBUG
	struct _debug_lock *deblock;
	unsigned tidbit;
#endif

#ifdef DEBUG
	/*
	 * Validate the lock and get pointer to debug lock structure, etc.
	 */

	deblock = _lock_validate(locknum);

#ifdef	_CRUISER_

	tidbit = _BIT_INDEX(*_threadid);

#else	/* ndef _CRUISER_ */

#ifdef	_WIN32_

	/*
	 * Set tidbit to 2**(index of ptd[] entry).
	 */
	tidbit = _getptd_lk() - _ptd;	/* index of _ptd[] entry */

	tidbit = _BIT_INDEX(tidbit);

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */

#endif	/* _CRUISER_ */

	/*
	 * Make sure we hold this lock then clear holder bit.
	 * [Note: Since it is legal to aquire _EXIT_LOCK1 several times,
	 * it's possible the holder bit is already clear.]
	 */

	if (locknum != _EXIT_LOCK1)
		if (!((deblock->holder) & tidbit))
			_FATAL;

	deblock->holder &= (~tidbit);

	/*
	 * See if anyone else is waiting for this lock.
	 */

	assert(THREADINTS==1);

	if ((unsigned) deblock->waiters != 0)
		deblock->collcnt++;

#endif

#ifdef	_CRUISER_

	/*
	 * Release the lock
	 */
	if (DOSRELEASEMUTEXSEM(_locktable[locknum]) != 0)
		_FATAL;

#else	/* ndef _CRUISER_ */

#ifdef	_WIN32_

	/*
	 * leave the critical section.
	 */
	LeaveCriticalSection( &_locktable[locknum] );

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */

#endif	/* _CRUISER_ */

#ifdef _LOCKCOUNT
	_lockcnt--;
#endif

}


/*
 * Debugging code
 */

#ifdef DEBUG

/***
*_lock_validate() - Validate a lock
*
*Purpose:
*	Debug lock validations common to both lock and unlock.
*
*Entry:
*	lock number
*
*Exit:
*	ptr to lock's debug structure
*
*Exceptions:
*
*******************************************************************************/

static struct _debug_lock * _CALLTYPE1 _lock_validate (
	int locknum
	)
{
	/*
	 * Make sure lock is legal
	 */

	if (locknum > _TOTAL_LOCKS)
		_FATAL;

#ifdef	_CRUISER_

	/*
	 * Make sure threadid is in range
	 */

	if (*_threadid > MAXTHREADID)
		_FATAL;

#endif	/* _CRUISER_ */

	/*
	 * Return pointer to this lock's debug structure
	 */

	return(&_debug_locktable[locknum]);

}


/***
*_fh_locknum() - Return the lock number for a file handle
*
*Purpose:
*
*Entry:
*	int fh = file handle
*
*Exit:
*	int locknum = corresponding lock number
*
*Exceptions:
*
*******************************************************************************/

int  _CALLTYPE2 _fh_locknum (
	int fh
	)
{
	return(fh+_FH_LOCKS);
}


/***
*_stream_locknum() - Return the lock number for a stream
*
*Purpose:
*
*Entry:
*	int stream = stream number (i.e., offset of the stream
*			in the _iob table)
*
*Exit:
*	int locknum = corresponding lock number
*
*Exceptions:
*
*******************************************************************************/

int  _CALLTYPE2 _stream_locknum (
	int stream
	)
{
	return(stream+_STREAM_LOCKS);
}


#ifdef	_CRUISER_

/***
*_check_lock() - Make sure a lock is in good shape
*
*Purpose:
*	This routine checks to make sure that a lock is in a 'good'
*	released state.  That is, abort if any of the following
*	are true:
*		(1) lock is held
*		(2) holder bits are set
*		(3) waiter bits are set
*
*	NOTE: This routine does NOT aquire the lock but simply looks
*	at the lock data bases.
*
*Entry:
*	locknum = number of the lock to aquire
*
*Exit:
*	success = 0
*	failure = !0
*
*Exceptions:
*
*******************************************************************************/

int  _CALLTYPE2 _check_lock (
	int locknum
	)
{
	unsigned pid;
	unsigned long tid;
	unsigned cnt;
	struct _debug_lock *deblock;

	/*
	 * See if lock exists.
	 */

	if (_TESTBIT(_lockmap, locknum)) {

		/*
		 * Lock exists.  Ask the OS about the lock and
		 * make sure lock isn't held.
		 */

		if (DOSQUERYMUTEXSEM(_locktable[locknum], &pid, &tid, &cnt) != 0)
			goto error_return;

		if (cnt != 0)
			goto error_return;
	}

	else {

		/*
		 * Lock doesn't exist, handle better be 0.
		 */

		if (_locktable[locknum] != 0)
			goto error_return;
	}


	/*
	 * Make sure there are no lock waiters or holders.
	 */

	assert(THREADINTS==1);

	if ((deblock->waiters != 0) || (deblock->holder != 0))
		goto error_return;

	/*
	 * Good Return
	 */

	return(0);

	/*
	 * Error return
	 */

error_return:
	return(-1);

}

#endif	/* _CRUISER_ */


/***
*_collide_cnt() - Return the collision count for a lock
*
*Purpose:
*
*Entry:
*	int lock = lock number
*
*Exit:
*	int count = collision count
*
*Exceptions:
*
*******************************************************************************/

int  _CALLTYPE2 _collide_cnt (
	int locknum
	)
{
	return(_debug_locktable[locknum].collcnt);
}


/***
*_lock_cnt() - Return the lock count for a lock
*
*Purpose:
*
*Entry:
*	int lock = lock number
*
*Exit:
*	int count = lock count
*
*Exceptions:
*
*******************************************************************************/

int  _CALLTYPE2 _lock_cnt (
	int locknum
	)
{
	return(_debug_locktable[locknum].lockcnt);
}


/***
*_lock_exist() - Check to see if a lock exists
*
*Purpose:
*	Test lock bit map to see if the lock has
*	been created or not.
*
*Entry:
*	int lock = lock number
*
*Exit:
*	int 0 = lock has NOT been created
*	    1 = lock HAS been created
*
*Exceptions:
*
*******************************************************************************/

int  _CALLTYPE2 _lock_exist (
	int locknum
	)
{
	if (_TESTBIT(_lockmap, locknum))
		return(1);
	else
		return(0);
}

#endif
#endif