;******************************* MODULE HEADER *******************************;
;*                                                                           *;
;* FILE: MediatorFirst.asm                                                   *;
;*                                                                           *;
;* MACHINE: NGenSGen        LANGUAGE: ASSEMBLER  OS: CTOS                   *;
;*                                                                           *;
;* DESCRIPTION:                                                              *;
;*                                                                           *;
;* Segment ordering for DLLs that use the mediator (call nonlarge model     *;
;* object module routines.)                                                  *;
;*                                                                           *;
;* TITLE:  CTOS SYSTEM SOFTWARE                                              *;
;*                                                                           *;
;*            PROPRIETARY PROGRAM MATERIAL                                   *;
;*                                                                           *;
;* THIS MATERIAL IS PROPRIETARY TO UNISYS CORPORATION AND IS NOT TO BE       *;
;* REPRODUCED, USED OR DISCLOSED EXCEPT IN ACCORDANCE WITH PROGRAM LICENSE   *;
;* OR UPON WRITTEN AUTHORIZATION OF THE PATENT DIVISION OF UNISYS            *;
;* CORPORATION, DETROIT, MICHIGAN 48232, USA.                                *;
;*                                                                           *;
;* COPYRIGHT (C) 1980, 1993 CONVERGENT INCORPORATED. ALL RIGHTS RESERVED     *;
;* COPYRIGHT (C) 1980, 1993 UNISYS CORPORATION. ALL RIGHTS RESERVED          *;
;* ************************************************************************* *;
;*                                                                           *;
;* UNISYS BELIEVES THAT THE SOFTWARE FURNISHED HEREWITH IS ACCURATE AND      *;
;* RELIABLE, AND MUCH CARE HAS BEEN TAKEN IN ITS PREPARATION.  HOWEVER,      *;
;* NO RESPONSIBILITY, FINANCIAL OR OTHERWISE, CAN BE ACCEPTED FOR ANY        *;
;* CONSEQUENCES ARISING OUT OF THE USE OF THIS MATERIAL, INCLUDING LOSS OF   *;
;* PROFIT, INDIRECT, SPECIAL, OR CONSEQUENTIAL DAMAGES, THERE ARE NO         *;
;* WARRANTIES WHICH EXTEND BEYOND THE PROGRAM SPECIFICATION.                 *;
;*                                                                           *;
;* THE CUSTOMER SHOULD EXERCISE CARE TO ASSURE THAT USE OF THE SOFTWARE      *;
;* WILL BE IN FULL COMPLIANCE WITH LAWS, RULES AND REGULATIONS OF THE        *;
;* JURISDICTIONS WITH RESPECT TO WHICH IT IS USED.                           *;
;*                                                                           *;
;*                                                                           *;
;*************************** END OF MODULE HEADER ****************************;

$MOD386

MediatorTbl		SEGMENT		WORD PUBLIC 'MediatorTbl'
MediatorTbl		ENDS

; DGroup segments, with data being first.

DGroup			GROUP		DATA, MEMORY, CONST, STACK, ___STACK

Data			SEGMENT		WORD PUBLIC 'Data'
Data			ENDS

Memory			SEGMENT		WORD MEMORY 'Memory'
Memory			ENDS

Const			SEGMENT		WORD PUBLIC 'Const'
Const			ENDS

Stack			SEGMENT		WORD STACK 'Stack'
PUBLIC	stackBottom
	stackBottom LABEL WORD
Stack			ENDS

___STACK		SEGMENT		WORD PUBLIC 'Stack'
PUBLIC			_end_Stack
	_end_Stack LABEL BYTE
___STACK		ENDS

; Init proc used to open semaphore

PUBLIC InitMediator
PUBLIC ExitMediator

EXTRN CheckErc		: FAR
EXTRN SemClose		: FAR
EXTRN semMediator	: DWORD
EXTRN SemOpen		: FAR
EXTRN SetExitList	: FAR


MediatorCode	SEGMENT		WORD PUBLIC 'Code'

ASSUME CS:MediatorCode, DS:DGroup

InitMediator PROC FAR

	PUSH DS
	MOV  AX, DGroup
	MOV  DS, AX

; check if previously invoked
	MOV  EAX, DS:WORD PTR [semMediator]
	CMP  EAX, 0
	JNZ  Finished

; open semaphore
	MOV  EAX, 0				; no semaphore name
	PUSH EAX
	PUSH AX
	PUSH 4					; normal, terminatable, userexclusive semaphore
	LEA  AX, DS:WORD PTR [semMediator]
	PUSH DS
	PUSH AX
	CALL SemOpen
	PUSH AX
	CALL CheckErc

; set exit proc
; Set exit proc priority to 0F0; this DLL's exit proc
; should probably run before CtosLib.dll and System.dll (PM users)
	PUSH 0F001h							; add pProc
	LEA  AX, CS:WORD PTR [ExitMediator]
	PUSH CS
	PUSH AX
	CALL SetExitList
	PUSH AX
	CALL CheckErc

Finished:
	POP  DS
	MOV  AX, 0
	RET

InitMediator ENDP

ExitMediator PROC FAR

	PUSH DS
	MOV  AX, DGroup
	MOV  DS, AX

; close our semaphore
	MOV  EAX, DS:DWORD PTR [semMediator]
	PUSH EAX
	CALL SemClose

	POP  DS
	RET

ExitMediator ENDP

MediatorCode ENDS

END
