?? RIGHT := 110 ??
?? NEWTITLE := 'NOS/VE:  BASIC ACCESS METHOD : Task termination cleanup', EJECT ??
MODULE bam$task_cleanup;
?? NEWTITLE := 'Global Declarations Referenced by this Module', EJECT ??
?? PUSH (LISTEXT := ON) ??
*copyc amt$fap_declarations
*copyc oss$job_paged_literal
*copyc oss$task_private
?? POP ??
*copyc bap$close
*copyc baf$task_file_entry_p
*copyc fmp$close_file
*copyc osp$enforce_exception_policies
*copyc osp$file_access_condition
*copyc osp$generate_log_message
*copyc pmp$establish_condition_handler
*copyc pmp$disestablish_cond_handler
*copyc pmd$system_log_interface

*copyc bav$last_tft_entry
*copyc bav$task_file_table
*copyc bav$tft_entry_assignment
*copyc osv$initial_exception_context

?? TITLE := 'Global Declarations Declared by this Module', EJECT ??

  VAR
    bav$task_cleanup_initiated: [XDCL, #GATE, oss$task_private] boolean := FALSE;

  VAR
    bav$verify_tft_before_cleanup: [XREF] boolean;

?? OLDTITLE ??
?? NEWTITLE := 'PROCEDURE [XDCL] bap$monitor_task_term_cleanup', EJECT ??

  PROCEDURE [XDCL] bap$monitor_task_term_cleanup;

{   Purpose:
{     The purpose of this procedure is to close any files which
{     remain open at task termination.  Note that all files should
{     have been previously closed by the bap$loaded_ring_cleanup
{     procedure.  Therefore this procedure should find files open
{     only in the event of some abnormal situation.

    VAR
      file_identifier: amt$file_identifier,
      index: bat$tft_limit;

    IF bav$task_file_table = NIL THEN
      RETURN;
    IFEND;

    FOR index := 1 TO bav$last_tft_entry DO
      IF (bav$tft_entry_assignment^ (index, 1) = fmc$entry_assigned) AND
            (bav$task_file_table^ [index].close_allowed) AND ((bav$task_file_table^ [index].
            module_dynamically_loaded) OR (bav$task_file_table^ [index].
            device_class = rmc$magnetic_tape_device))  THEN
        file_identifier.ordinal := index;
        file_identifier.sequence := bav$task_file_table^ [index].sequence_number;
        close_file_at_task_exit (file_identifier);
      IFEND;
    FOREND;

  PROCEND bap$monitor_task_term_cleanup;
?? OLDTITLE ??
?? NEWTITLE := 'PROCEDURE [XDCL] bap$task_termination_cleanup', EJECT ??

  PROCEDURE [XDCL] bap$task_termination_cleanup;

{   Purpose:
{     The purpose of this procedure is to close any files which
{     remain open at task termination.  Note that all files should
{     have been previously closed by the bap$loaded_ring_cleanup
{     procedure.  Therefore this procedure should find files open
{     only in the event of some abnormal situation.

    VAR
      file_identifier: amt$file_identifier,
      index: bat$tft_limit;

    IF bav$task_file_table = NIL THEN
      RETURN;
    IFEND;

    FOR index := 1 TO bav$last_tft_entry DO
      IF bav$tft_entry_assignment^ (index, 1) = fmc$entry_assigned THEN
        bav$task_file_table^ [index].close_allowed := TRUE;
        file_identifier.ordinal := index;
        file_identifier.sequence := bav$task_file_table^ [index].sequence_number;
        close_file_at_task_exit (file_identifier);
      IFEND;
    FOREND;

  PROCEND bap$task_termination_cleanup;
?? OLDTITLE ??
?? NEWTITLE := 'FUNCTION [XDCL, #GATE] baf$verify_tft_before_cleanup', EJECT ??

  FUNCTION [XDCL, #GATE] baf$verify_tft_before_cleanup: boolean;

    baf$verify_tft_before_cleanup := bav$verify_tft_before_cleanup;

  FUNCEND baf$verify_tft_before_cleanup;
?? OLDTITLE ??
?? NEWTITLE := 'PROCEDURE [XDCL, #GATE] bap$set_task_cleanup_initiated', EJECT ??

  PROCEDURE [XDCL, #GATE] bap$set_task_cleanup_initiated;

    bav$task_cleanup_initiated := TRUE;

  PROCEND bap$set_task_cleanup_initiated;
?? NEWTITLE := 'PROCEDURE close_file_at_task_exit', EJECT ??

  PROCEDURE close_file_at_task_exit
    (    file_identifier: amt$file_identifier);

    PROCEDURE condition_handler
      (    condition: pmt$condition;
           condition_descriptor: ^pmt$condition_information;
           save_area: ^ost$stack_frame_save_area;
       VAR status: ost$status);

      EXIT close_file_at_task_exit;

    PROCEND condition_handler;

    VAR
      conditions: [READ, STATIC, oss$job_paged_literal] pmt$condition := [pmc$condition_combination,
            [pmc$system_conditions, mmc$segment_access_condition]],
      context: ^ost$ecp_exception_context,
      establish_descriptor: pmt$established_handler,
      file_id_is_valid: boolean,
      file_instance: ^bat$task_file_entry,
      ignore_status: ost$status,
      status: ost$status;

    context := NIL;

    pmp$establish_condition_handler (conditions, ^condition_handler, ^establish_descriptor, status);

    REPEAT
      bap$close (file_identifier, status);
      IF osp$file_access_condition (status) THEN
        IF context = NIL THEN
          PUSH context;
          context^ := osv$initial_exception_context;
          context^.file.selector := osc$ecp_file_identifier;
          context^.file.file_identifier := file_identifier;
        IFEND;

        context^.condition_status := status;
        osp$enforce_exception_policies (context^);
        status := context^.condition_status;
        IF NOT context^.wait THEN
          {If ignoring file access condition, ensure open count is decremented.}
          file_instance := baf$task_file_entry_p (file_identifier);
          IF file_instance <> NIL THEN
            fmp$close_file (file_instance, ignore_status);
          IFEND;
        IFEND;
      IFEND;
    UNTIL status.normal OR (NOT osp$file_access_condition (status)) OR (NOT context^.wait);
    IF NOT osp$file_access_condition (status) THEN
      osp$generate_log_message ($pmt$ascii_logset [pmc$job_log, pmc$system_log], status, ignore_status);
    IFEND;

  PROCEND close_file_at_task_exit;
?? OLDTITLE ??
MODEND bam$task_cleanup;

