+
+int interrupt_thread(pid_t pid, lispobj function)
+{
+ union sigval sigval;
+ sigval.sival_int=function;
+
+ return sigqueue(pid, SIG_INTERRUPT_THREAD, sigval);
+}
+
+int signal_thread_to_dequeue (pid_t pid)
+{
+ return kill (pid, SIG_DEQUEUE);
+}
+
+
+/* stopping the world is a two-stage process. From this thread we signal
+ * all the others with SIG_STOP_FOR_GC. The handler for this thread does
+ * the usual pseudo-atomic checks (we don't want to stop a thread while
+ * it's in the middle of allocation) then kills _itself_ with SIGSTOP.
+ */
+
+void gc_stop_the_world()
+{
+ /* stop all other threads by sending them SIG_STOP_FOR_GC */
+ struct thread *p,*th=arch_os_get_current_thread();
+ pid_t old_pid;
+ int finished=0;
+ do {
+ get_spinlock(&all_threads_lock,th->pid);
+ for(p=all_threads,old_pid=p->pid; p; p=p->next) {
+ if(p==th) continue;
+ if(p->state!=STATE_RUNNING) continue;
+ p->state=STATE_STOPPING;
+ kill(p->pid,SIG_STOP_FOR_GC);
+ }
+ release_spinlock(&all_threads_lock);
+ sched_yield();
+ /* if everything has stopped, and there is no possibility that
+ * a new thread has been created, we're done. Otherwise go
+ * round again and signal anything that sprang up since last
+ * time */
+ if(old_pid==all_threads->pid) {
+ finished=1;
+ for_each_thread(p)
+ finished = finished &&
+ ((p==th) || (p->state==STATE_STOPPED));
+ }
+ } while(!finished);
+}
+
+void gc_start_the_world()
+{
+ struct thread *p,*th=arch_os_get_current_thread();
+ get_spinlock(&all_threads_lock,th->pid);
+ for(p=all_threads;p;p=p->next) {
+ if(p==th) continue;
+ p->state=STATE_RUNNING;
+ kill(p->pid,SIGCONT);
+ }
+ release_spinlock(&all_threads_lock);
+}