63 if (uninterruptible) { () => Isabelle_Thread.uninterruptible { body } } |
63 if (uninterruptible) { () => Isabelle_Thread.uninterruptible { body } } |
64 else { () => body } |
64 else { () => body } |
65 val thread = |
65 val thread = |
66 create(main, name = name, group = group, pri = pri, |
66 create(main, name = name, group = group, pri = pri, |
67 daemon = daemon, inherit_locals = inherit_locals) |
67 daemon = daemon, inherit_locals = inherit_locals) |
68 thread.start |
68 thread.start() |
69 thread |
69 thread |
70 } |
70 } |
71 |
71 |
72 |
72 |
73 /* thread pool */ |
73 /* thread pool */ |
87 object Interrupt_Handler { |
87 object Interrupt_Handler { |
88 def apply(handle: Isabelle_Thread => Unit, name: String = "handler"): Interrupt_Handler = |
88 def apply(handle: Isabelle_Thread => Unit, name: String = "handler"): Interrupt_Handler = |
89 new Interrupt_Handler(handle, name) |
89 new Interrupt_Handler(handle, name) |
90 |
90 |
91 val interruptible: Interrupt_Handler = |
91 val interruptible: Interrupt_Handler = |
92 Interrupt_Handler(_.raise_interrupt, name = "interruptible") |
92 Interrupt_Handler(_.raise_interrupt(), name = "interruptible") |
93 |
93 |
94 val uninterruptible: Interrupt_Handler = |
94 val uninterruptible: Interrupt_Handler = |
95 Interrupt_Handler(_.postpone_interrupt, name = "uninterruptible") |
95 Interrupt_Handler(_.postpone_interrupt(), name = "uninterruptible") |
96 } |
96 } |
97 |
97 |
98 class Interrupt_Handler private(handle: Isabelle_Thread => Unit, name: String) |
98 class Interrupt_Handler private(handle: Isabelle_Thread => Unit, name: String) |
99 extends Function[Isabelle_Thread, Unit] { |
99 extends Function[Isabelle_Thread, Unit] { |
100 def apply(thread: Isabelle_Thread): Unit = handle(thread) |
100 def apply(thread: Isabelle_Thread): Unit = handle(thread) |
130 thread => |
130 thread => |
131 |
131 |
132 thread.setPriority(pri) |
132 thread.setPriority(pri) |
133 thread.setDaemon(daemon) |
133 thread.setDaemon(daemon) |
134 |
134 |
135 override def run: Unit = main.run() |
135 override def run(): Unit = main.run() |
136 |
136 |
137 def is_self: Boolean = Thread.currentThread == thread |
137 def is_self: Boolean = Thread.currentThread == thread |
138 |
138 |
139 |
139 |
140 /* interrupt state */ |
140 /* interrupt state */ |
141 |
141 |
142 // synchronized, with concurrent changes |
142 // synchronized, with concurrent changes |
143 private var interrupt_postponed: Boolean = false |
143 private var interrupt_postponed: Boolean = false |
144 |
144 |
145 def clear_interrupt: Boolean = synchronized { |
145 def clear_interrupt(): Boolean = synchronized { |
146 val was_interrupted = isInterrupted || interrupt_postponed |
146 val was_interrupted = isInterrupted || interrupt_postponed |
147 Exn.Interrupt.dispose() |
147 Exn.Interrupt.dispose() |
148 interrupt_postponed = false |
148 interrupt_postponed = false |
149 was_interrupted |
149 was_interrupted |
150 } |
150 } |
151 |
151 |
152 def raise_interrupt: Unit = synchronized { |
152 def raise_interrupt(): Unit = synchronized { |
153 interrupt_postponed = false |
153 interrupt_postponed = false |
154 super.interrupt() |
154 super.interrupt() |
155 } |
155 } |
156 |
156 |
157 def postpone_interrupt: Unit = synchronized { |
157 def postpone_interrupt(): Unit = synchronized { |
158 interrupt_postponed = true |
158 interrupt_postponed = true |
159 Exn.Interrupt.dispose() |
159 Exn.Interrupt.dispose() |
160 } |
160 } |
161 |
161 |
162 |
162 |
173 require(is_self, "interrupt handler on other thread") |
173 require(is_self, "interrupt handler on other thread") |
174 |
174 |
175 val old_handler = handler |
175 val old_handler = handler |
176 handler = new_handler |
176 handler = new_handler |
177 try { |
177 try { |
178 if (clear_interrupt) interrupt() |
178 if (clear_interrupt()) interrupt() |
179 body |
179 body |
180 } |
180 } |
181 finally { |
181 finally { |
182 handler = old_handler |
182 handler = old_handler |
183 if (clear_interrupt) interrupt() |
183 if (clear_interrupt()) interrupt() |
184 } |
184 } |
185 } |
185 } |
186 } |
186 } |