| 59720 |      1 | /*  Title:      Pure/System/posix_interrupt.scala
 | 
| 51250 |      2 |     Author:     Makarius
 | 
|  |      3 | 
 | 
|  |      4 | Support for POSIX interrupts (bypassed on Windows).
 | 
|  |      5 | */
 | 
|  |      6 | 
 | 
|  |      7 | package isabelle
 | 
|  |      8 | 
 | 
|  |      9 | 
 | 
|  |     10 | import sun.misc.{Signal, SignalHandler}
 | 
|  |     11 | 
 | 
|  |     12 | 
 | 
| 75393 |     13 | object POSIX_Interrupt {
 | 
|  |     14 |   def handler[A](h: => Unit)(e: => A): A = {
 | 
| 51250 |     15 |     val SIGINT = new Signal("INT")
 | 
| 73340 |     16 |     val new_handler = new SignalHandler { def handle(s: Signal): Unit = h }
 | 
| 51250 |     17 |     val old_handler = Signal.handle(SIGINT, new_handler)
 | 
|  |     18 |     try { e } finally { Signal.handle(SIGINT, old_handler) }
 | 
|  |     19 |   }
 | 
|  |     20 | 
 | 
| 75393 |     21 |   def exception[A](e: => A): A = {
 | 
| 51250 |     22 |     val thread = Thread.currentThread
 | 
| 73367 |     23 |     handler { thread.interrupt() } { e }
 | 
| 51250 |     24 |   }
 | 
|  |     25 | }
 | 
|  |     26 | 
 |