;       SCCSID = @(#)sswfree.asm	6.3 91/09/09
; ****************************************************************************
; *                                                                          *
; *                       IBM/Microsoft Confidential                         *
; *                                                                          *
; *                 Copyright (c) IBM Corporation  1987, 1990                *
; *                 Copyright (c) Microsoft Corp.  1987, 1990                *
; *                           All Rights Reserved                            *
; *                                                                          *
; ****************************************************************************

                                page 60,132

;*********************** START OF SPECIFICATIONS ***********************/
;                                                                      */
;  SOURCE FILE NAME: SSWFREE                                           */
;                                                                      */
;  SUBROUTINE NAME:  DOSSGFREE                                         */
;                                                                      */
;  DESCRIPTIVE NAME:  Screen Group memory deallocation                 */
;                                                                      */
;  FUNCTION: Frees all memory allocated through Screen Switcher for    */
;            a specified screen group. This call is issued upon        */
;            termination of a screen group.                            */
;                                                                      */
;  NOTES:  Executes on Level 3                                         */
;                                                                      */
;  ENTRY POINT:  DosSGFree                                             */
;    LINKAGE:                                                          */
;                                                                      */
;  INPUT:  screen group number                                         */
;                                                                      */
;  EXIT-NORMAL:  AX = 0                                                */
;                                                                      */
;  EXIT-ERROR:   AX = error_scs_not_session_mgr   (call issued by      */
;                      other than Session Manager)                     */
;                                                                      */
;  EFFECTS:  Contents of AX destroyed by return code                   */
;                                                                      */
;  INTERNAL REFERENCES:                                                */
;    ROUTINES:  SSCBDeallocate                                         */
;                                                                      */
;  EXTERNAL REFERENCES:                                                */
;    ROUTINES:  VIOFree, DosDevIOCTL, DosGetInfoSeg, KBDFree           */
;                                                                      */
;  Change History:                                                     */
;                                                                      */
;    @TL6 03/01/90 DCR 704 - Merge SMG/SSW instance data into          */
;                            OS2CHAR                                   */
;    @GDW 08/26/91 PTR B725436 Fix to allow semaphore loop on          */
;                              INTERUPT                                */
;    CJ42 09/09/91 PTR B725639 DosSGFree expects SG# without type.     */
;                                                                      */
;************************ END OF SPECIFICATIONS ************************/
EXTRN   ssmgr_pid:word, savelocksem:byte, savelockflag:byte
EXTRN   timedouterror:word, kbd_parm_table:byte, savelockowner:word
EXTRN   DOSFSRAMSEMREQUEST:FAR
EXTRN   DOSFSRAMSEMCLEAR:FAR
.xlist
INCLUDE INFOSEG.INC
INCLUDE SSWEXTR3.INC
INCLUDE SSWINC.INC
.list

.386p

CODE    SEGMENT WORD PUBLIC 'CODE' USE16                                ;@TL6

 ASSUME  CS:CODE, DS:NOTHING,SS:NOTHING,ES:NOTHING                      ;@TL6

 PUBLIC  DOSSGFREE

DOSSGFREE              PROC    FAR

 push    bp
 mov     bp,sp

 push    es
 push    di
 push    bx
;push    cx                                                             ;@TL6
 push    ds
 push    si

;push    ax                                         ; Need to store 2 words;@TL6
;mov     bx,sp                                      ; which will be used to;@TL6
;push    ax                                         ; store the globalsg;@TL6
;mov     cx,sp                                      ; and the localsg   ;@TL6
;push    ss                                                             ;@TL6
;push    bx                                                             ;@TL6
;push    ss                                                             ;@TL6
;push    cx                                                             ;@TL6

;call    DOSGETINFOSEG                              ; Get screen group# ;@TL6
;.IF <zero ax> near                                                     ;@TL6

; pop     es                                        ; Get localsg into es ;@TL6
; pop     bx                                                            ;@TL6
  mov     bx,stdatasg                               ; We can access our ;@TL6
  mov     ds,bx                                     ; static data area  ;@TL6
  mov     es,localsg                                ; Setup local seg   ;@TL6
  mov     bx,es:lis_curprocid                       ; Get current pid#
  .IF <ssmgr_pid ne ax> AND                         ; If this is not SMG;@TL6
  .IF <bx ne ssmgr_pid>                             ; Return an error
   mov     ax,error_scs_not_session_mgr
  .ELSE near
   .repeat                                                              ;@GDW
     push    ds                                     ; System-wide sem
     push    offset savelocksem                     ; which blocks all
     push    dword ptr timeout                      ; calls             ;CJ10
;    push    timeout                                                    ;CJ10
     call    DOSFSRAMSEMREQUEST
   .until  <ax ne ERROR_INTERRUPT>                                      ;@GDW

   .IF <zero ax> near OR                            ; If no error or    ;@TL6
   .IF <ax e error_sem_owner_died> near             ; if owner died
    mov    savelockflag,4                           ;  <<<<DEBUG>>>>
;---------------------------------------------------------------------------
    mov    bx,ss:[bp].f_sgid                        ; SG# into bx
    xor    bh,bh                                    ; zero type byte    ;CJ42
    call   SSFINDSG                                 ; See if we have sgcb
                                                    ; If we do then
                                                    ; DI now points to it
     .IF <zero ax> OR                                                   ;@TL6
     .IF <ax e error_sem_owner_died>
      push    bx                                    ; Sg#
      call    VIOFREE
       .IF <zero ax> OR                                                 ;@TL6
       .IF <ax e error_sem_owner_died>

;***********************************************************************
;         Deallocate memory. Screen Switcher clears out its record     *
;         of the screen group, its associated pids, the thread save    *
;         and restore data, all flags, etc.                            *
;***********************************************************************
        call SSCBDEALLOCATE

       .ENDIF
     .ENDIF
   .ENDIF
   .IF <savelockflag e 4>
    mov     savelockflag,0                         ; ES9/2/87
    push    ds                                       ; Clear the system
    push    offset savelocksem                     ; wide semaphore
    call    DOSFSRAMSEMCLEAR
   .ENDIF
  .ENDIF
;.ENDIF                                                                 ;@TL6

 pop     si
 pop     ds
;pop     cx                                                             ;@TL6
 pop     bx
 pop     di
 pop     es
 pop     bp

Ret     2

DOSSGFREE              ENDP

CODE    ENDS                                                            ;@TL6
 END

