PreviousUpNext

15.4.1675  src/lib/x-kit/xclient/src/wire/socket-closer-imp.pkg

## socket-closer-imp.pkg
#
# Track sockets open to X-servers
# and close them all at application exit.

# Compiled by:
#     src/lib/x-kit/xclient/xclient-internals.sublib


stipulate
    include package   threadkit;                                # threadkit                     is from   src/lib/src/lib/thread-kit/src/core-thread-kit/threadkit.pkg
    #
#    package xok = xsocket_old;                                 # xsocket_old                   is from   src/lib/x-kit/xclient/src/wire/xsocket-old.pkg
    package sok =  socket__premicrothread;                      # socket__premicrothread        is from   src/lib/std/socket--premicrothread.pkg
herein

#       close:       Threadkit_Socket( A_af, A_sock_type ) -> Void;

    package   socket_closer_imp
    : (weak)  Socket_Closer_Imp                                 # Socket_Closer_Imp             is from   src/lib/x-kit/xclient/src/wire/socket-closer-imp.api
    {
        stipulate
#           Plea_Mail
#             = NOTE_XSOCKET    xok::Xsocket
#             | FORGET_XSOCKET  xok::Xsocket
#             | SHUTDOWN
#              ;

            Plea_Mail
              = NOTE_SOCKET    Int                                              # Actually sok::Socket (X, sok::Stream(sok::Active)) but that's equivalent.
              | FORGET_SOCKET  Int
              | SHUTDOWN
              ;

            my plea_slot:  Mailslot( Plea_Mail ) =  make_mailslot ();
            my reply_slot: Mailslot( Void      ) =  make_mailslot ();

            fun start_imp ()
                =
                {
                    make_thread "socket_closer_imp"  {. loop []; };

                    ();
                }
                where
                    fun loop sockets
                        =
                        case (take_from_mailslot  plea_slot)
                            #
                            NOTE_SOCKET arg
                                =>
                                loop (arg ! sockets);

                            FORGET_SOCKET socket
                                =>
                                loop (remove sockets)
                                where
                                    fun remove [] => [];
                                        #
                                        remove (c ! r)
                                            =>
#                                           xok::same_xsocket (c, xsocket)
                                            (c == socket)
                                                ##
                                                ??   r
                                                ::   c ! (remove r);
                                    end;
                                end;

                            SHUTDOWN
                                =>
                                {
                                                        {   thread = get_current_microthread (); 
                                                            logger::log_if xlogger::lib_logging 0 {. cat [get_thread's_id_as_string thread, " ***** shutdown *****"]; };
                                                        };

                                    apply  sok::close  sockets;

                                    put_in_mailslot (reply_slot, ());
                                };
                         esac;
                end;

            fun shutdown ()
                =
                {
                    put_in_mailslot (plea_slot, SHUTDOWN);
                    take_from_mailslot  reply_slot;
                };

                                                                                                my _ = 
            note_mailslot("x-kit-shutdown: plea_slot", plea_slot);
                                                                                                my _ = 
            note_mailslot("x-kit-shutdown: reply_slot", reply_slot);

                                                                                                my _ = 
            note_imp { name => "x-kit-shutdown", at_startup => start_imp, at_shutdown => shutdown };

        herein

            fun   note_socket arg    =  put_in_mailslot (plea_slot,   NOTE_SOCKET arg   );
            fun forget_socket socket =  put_in_mailslot (plea_slot, FORGET_SOCKET socket);


        end;

    };                                          # package socket_closer_imp 
end;                                            # stipulate

## COPYRIGHT (c) 1990, 1991 by John H. Reppy.  See SMLNJ-COPYRIGHT file for details.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext