home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / windows / x / 21467 < prev    next >
Encoding:
Text File  |  1993-01-22  |  6.3 KB  |  238 lines

  1. Path: sparky!uunet!spool.mu.edu!uwm.edu!linac!pacific.mps.ohio-state.edu!cis.ohio-state.edu!oboe!ware
  2. From: ware@oboe.cis.ohio-state.edu (Peter Ware)
  3. Newsgroups: comp.windows.x
  4. Subject: Re: Why X libraries are not reentrant?
  5. Date: 22 Jan 1993 08:55:43 -0500
  6. Organization: Ohio State Computer Science
  7. Lines: 221
  8. Sender: ware@cis.ohio-state.edu
  9. Message-ID: <WARE.93Jan22085540@oboe.cis.ohio-state.edu>
  10. References: <1993Jan14.232031.9316@selway.umt.edu>
  11.     <1993Jan19.025831.19583@thunder.mcrcim.mcgill.edu>
  12.     <1993Jan20.090241.1810@cenatls.cena.dgac.fr>
  13.     <1993Jan21.171844.23941@jpl-devvax.jpl.nasa.gov>
  14. NNTP-Posting-Host: oboe.cis.ohio-state.edu
  15. In-reply-to: kaleb@swat's message of Thu, 21 Jan 1993 17:18:44 GMT
  16.  
  17. The following code, which I'll someday include in the Xt-FAQ,
  18. implements signal handling using pipes.
  19.  
  20. --pete
  21.  
  22. #! /bin/sh
  23. # This is a shell archive, meaning:
  24. # 1. Remove everything above the #! /bin/sh line.
  25. # 2. Save the resulting text in a file.
  26. # 3. Execute the file with /bin/sh (not csh) to create:
  27. #    signals.c
  28. #    sighandler.h
  29. # This archive created: Fri Jan 22 08:54:10 1993
  30. export PATH; PATH=/bin:/usr/bin:$PATH
  31. if test -f 'signals.c'
  32. then
  33.     echo shar: "will not over-write existing file 'signals.c'"
  34. else
  35. cat << \SHAR_EOF > 'signals.c'
  36. /*
  37.  * signals -
  38.  *    An Xt Intrinsics signal handler developed based on discussions
  39.  *    in comp.windows.x and written by someone who wishes to be
  40.  *    anonymous.
  41.  *
  42.  *    A pipe is created and the read side is passed off to
  43.  *    XtAppAddInput().  Everytime a signal occurs a byte, indicating
  44.  *    which signal, is written by the signal handler on the write
  45.  *    side of the pipe.  This causes the Intrinsics to call the
  46.  *    input handler which then invokes the correct callback.
  47.  *
  48.  *    The potential for deadlock exists if the pipe is ever filled!
  49.  *
  50.  * $Id: signals.c,v 1.1 92/12/10 08:51:01 ware Exp $
  51.  * $Log:    signals.c,v $
  52.  * Revision 1.1  92/12/10  08:51:01  ware
  53.  * Initial revision
  54.  * 
  55.  */
  56.  
  57. #include <X11/Intrinsic.h>
  58. #include <signal.h>        /* signal stuff */
  59. #include <sys/types.h>        /* for pipe */
  60. #include <string.h>        /* for memset */
  61. #include "sighandler.h"
  62.  
  63. static _XoSignalData sig_info[NSIG + 1]; /* NSIG is max signal value */
  64.  
  65. static int pipefd [2];            /* the input & output pipes */
  66.  
  67. static void _xsig_handler (
  68. #if NeedFunctionPrototypes
  69.     int    sig,
  70.     int    code
  71. #endif
  72. );
  73.  
  74. static void
  75. _xsig_pipe_handler (
  76. #if NeedFunctionPrototypes
  77.     XtPointer    client_data,
  78.     int        *source,
  79.     XtInputId    *id
  80. #endif
  81. );
  82.  
  83. /*
  84.  * XoAppAddSignal -
  85.  *    Install a handler for a particular signal.  There can be only
  86.  *    a single handler per signal (it might be nice to use a callback).
  87.  */
  88.  
  89. void
  90. XoAppAddSignal (context, sig, handler, client_data)
  91.     XtAppContext    context;    /* application context */
  92.     int        sig;        /* which signal */
  93.     XoSignalCallbackProc handler;    /* the handler */
  94.     XtPointer    client_data;    /* private data */
  95. {
  96.     static int firsttime = True;
  97.  
  98.     /*
  99.      * We need to create the pipe and tell the intrinsics about
  100.      * the new file descriptor
  101.      */
  102.     if (firsttime)
  103.     {
  104.         firsttime = False;
  105.         pipe (pipefd);
  106.         XtAppAddInput (context, pipefd [0],
  107.                    (XtPointer) XtInputReadMask,
  108.                    _xsig_pipe_handler, (XtPointer) NULL);
  109.     }
  110.     signal (sig, _xsig_handler);
  111.     sig_info[sig].handler = handler;
  112.     sig_info[sig].client_data = client_data;
  113. }                /* XoAppAddSignal */
  114.  
  115. /*
  116.  * XoAppRemoveSignal -
  117.  *    Uninstalls a handler for a particular signal.  The values
  118.  *    of handler and client_data most match in order to remove the
  119.  *    particular signal handler.  If there are no more remaining
  120.  *    signal handlers for that signal then SIG_DFL is installed.
  121.  *
  122.  *    Of course, the current implementation only allows one handler
  123.  *    per signal but in the future when multiple ones are added this
  124.  *    will continue to work.  The application context is not used
  125.  *    and is left merely for consistency.
  126.  */
  127.  
  128. void
  129. XoAppRemoveSignal (context, sig, handler, client_data)
  130.     XtAppContext    context;    /* application context */
  131.     int        sig;        /* which signal */
  132.     XoSignalCallbackProc handler;    /* the handler */
  133.     XtPointer    client_data;    /* private data */
  134. {
  135.     signal (sig, SIG_DFL);    /* restore old signal handler */
  136.     sig_info [sig].handler = (XtInputCallbackProc) NULL;
  137.     sig_info [sig].client_data = NULL;
  138. }                /* XoAppRemoveSignal */
  139.  
  140. /*
  141.  * _xsig_handler -
  142.  *    the actual signal handler (custom), writes a byte to a pipe
  143.  */
  144. static void
  145. _xsig_handler (sig, code)
  146.     int    sig;
  147.     int    code;
  148. {
  149.     char    sig_value;
  150.     
  151.     sig_value = sig;
  152.     write (pipefd [1], &sig_value, 1);
  153. }                /* _xsig_handler */
  154.  
  155. /*
  156.  * _xsig_pipe_handler -
  157.  *    reads input from the pipe and executes the corresponding callback.
  158.  */
  159.  
  160. static void
  161. _xsig_pipe_handler (client_data, source, id)
  162.     XtPointer    client_data;
  163.     int        *source;
  164.     XtInputId    *id;
  165. {
  166.     unsigned char    sig_value;
  167.     int        sig;
  168.     
  169.     read (pipefd [0], &sig_value, 1);
  170.     sig = sig_value;
  171.     if (sig > 0 && sig < NSIG && sig_info[sig].handler)
  172.         (*sig_info [sig].handler) (sig, sig_info[sig].client_data);
  173. }                /* _xsig_pipe_handler */
  174. SHAR_EOF
  175. fi
  176. if test -f 'sighandler.h'
  177. then
  178.     echo shar: "will not over-write existing file 'sighandler.h'"
  179. else
  180. cat << \SHAR_EOF > 'sighandler.h'
  181. /*
  182.  * sighandler.h -
  183.  *    Defines the interface to signalling handling in an Xt save way
  184.  * $Id: sighandler.h,v 1.1 92/12/10 08:50:54 ware Exp $
  185.  * $Log:    sighandler.h,v $
  186.  * Revision 1.1  92/12/10  08:50:54  ware
  187.  * Initial revision
  188.  * 
  189.  */
  190.  
  191. typedef void (*XoSignalCallbackProc) (
  192. #if NeedFunctionPrototypes
  193.     int        signo,        /* the signal number */
  194.     XtPointer     client_data    /* closure */
  195. #endif
  196. );
  197.  
  198. /*
  199.  * Private structure used to store the information about the currently
  200.  * installed signal handlers
  201.  */
  202.  
  203. typedef struct _xo_signal_data_
  204. {
  205.     XoSignalCallbackProc    handler; /* function to execute */
  206.     XtPointer        client_data; /* data to pass */
  207. } _XoSignalData;
  208.  
  209. extern void XoAppAddSignal (
  210. #if NeedFunctionPrototypes
  211.     XtAppContext    context,    /* application context */
  212.     int        sig,        /* which signal */
  213.     XoSignalCallbackProc handler,    /* the handler */
  214.     XtPointer    client_data    /* private data */
  215. #endif
  216. );
  217.  
  218. extern void XoAppRemoveSignal (
  219. #if NeedFunctionPrototypes
  220.     XtAppContext    context,    /* application context */
  221.     int        sig,        /* which signal */
  222.     XoSignalCallbackProc handler,    /* the handler */
  223.     XtPointer    client_data    /* private data */
  224. #endif
  225. );
  226.  
  227.  
  228. SHAR_EOF
  229. fi
  230. exit 0
  231. #    End of shell archive
  232. --
  233. Pete Ware                    ware@cis.ohio-state.edu
  234. CIS Dept, Ohio State University            w/ (614) 292-7318
  235. 228 Bolz Hall, 2036 Neil Ave.            h/ (614) 538-0965
  236. Columbus, OH 43210
  237. Welcome William Patrick Ware to the world!  Born 10/21/92, 8lbs 6.2oz (3.83kg)
  238.