From: Matt Birkholz Date: Wed, 17 Jun 2015 03:03:56 +0000 (-0700) Subject: Define make-thread-mutex early. X-Git-Tag: mit-scheme-pucked-9.2.12~376^2~48 X-Git-Url: https://birchwood-abbey.net/git?a=commitdiff_plain;h=65d2d32dc13779bdebc5bcba37224cde73468ee6;p=mit-scheme.git Define make-thread-mutex early. Global data structures like event-distributors and gc-daemon queues need to serialize operations and could use thread mutexes except that they are naturally created early in the cold load, before the thread system is loaded. So the mutex data structure is defined (withOUT define-structure syntax) in a new file: runtime/thread-low.scm. The rest of thread.scm must be loaded after record.scm. This breaks the circularity where thread system initialization requires population and 1d-table operations which are serialized by the thread system. --- diff --git a/src/runtime/make.scm b/src/runtime/make.scm index 32191c2c3..d7228fcb7 100644 --- a/src/runtime/make.scm +++ b/src/runtime/make.scm @@ -366,6 +366,7 @@ USA. ("fixart" . (RUNTIME FIXNUM-ARITHMETIC)) ("random" . (RUNTIME RANDOM-NUMBER)) ("gentag" . (RUNTIME GENERIC-PROCEDURE)) + ("thread-low" . (RUNTIME THREAD)) ("poplat" . (RUNTIME POPULATION)) ("record" . (RUNTIME RECORD)))) (files2 diff --git a/src/runtime/runtime.pkg b/src/runtime/runtime.pkg index e6e3d9b68..c087eb5aa 100644 --- a/src/runtime/runtime.pkg +++ b/src/runtime/runtime.pkg @@ -5024,7 +5024,7 @@ USA. (initialization (initialize-package!))) (define-package (runtime thread) - (files "thread") + (files "thread-low" "thread") (parent (runtime)) (export () assert-thread-mutex-owned diff --git a/src/runtime/thread-low.scm b/src/runtime/thread-low.scm new file mode 100644 index 000000000..e46662baf --- /dev/null +++ b/src/runtime/thread-low.scm @@ -0,0 +1,124 @@ +#| -*-Scheme-*- + +Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, + 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, + 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Massachusetts + Institute of Technology + +This file is part of MIT/GNU Scheme. + +MIT/GNU Scheme is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at +your option) any later version. + +MIT/GNU Scheme is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with MIT/GNU Scheme; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, +USA. + +|# + +;;;; Some thread system structures needed during the early cold load. +;;; package: (runtime thread) + +(declare (usual-integrations)) + +(define-integrable thread-mutex-tag + '|#[(runtime thread)thread-mutex]|) + +(define-integrable (thread-mutex? object) + (and (vector? object) + (fix:= 3 (vector-length object)) + (eq? (vector-ref object 0) thread-mutex-tag))) + +(define-integrable (make-thread-mutex) + (vector thread-mutex-tag (make-ring) #f)) + +(define-integrable (thread-mutex/waiting-threads t) (vector-ref t 1)) + +(define-integrable (thread-mutex/owner t) (vector-ref t 2)) +(define-integrable (set-thread-mutex/owner! t o) (vector-set! t 2 o)) + +;;;; Circular Rings + +#;(define-structure (link (conc-name link/)) + prev + next + item) + +(define-integrable link-tag + '|#[(runtime thread)link]|) + +(define-integrable (link? object) + (and (vector? object) + (fix:= 4 (vector-length object)) + (eq? (vector-ref object 0) link-tag))) + +(define-integrable (make-link prev next item) + (vector link-tag prev next item)) + +(define-integrable (link/prev l) (vector-ref l 1)) +(define-integrable (set-link/prev! l p) (vector-set! l 1 p)) + +(define-integrable (link/next l) (vector-ref l 2)) +(define-integrable (set-link/next! l n) (vector-set! l 2 n)) + +(define-integrable (link/item l) (vector-ref l 3)) +(define-integrable (set-link/item! l i) (vector-set! l 3 i)) + +(define (make-ring) + (let ((link (make-link #f #f #f))) + (set-link/prev! link link) + (set-link/next! link link) + link)) + +(define-integrable (ring/empty? ring) + (eq? (link/next ring) ring)) + +(define (ring/enqueue ring item) + (let ((prev (link/prev ring))) + (let ((link (make-link prev ring item))) + (set-link/next! prev link) + (set-link/prev! ring link)))) + +(define (ring/dequeue ring default) + (let ((link (link/next ring))) + (if (eq? link ring) + default + (begin + (let ((next (link/next link))) + (set-link/next! ring next) + (set-link/prev! next ring)) + (link/item link))))) + +(define (ring/discard-all ring) + (set-link/prev! ring ring) + (set-link/next! ring ring)) + +(define (ring/remove-item ring item) + (let loop ((link (link/next ring))) + (if (not (eq? link ring)) + (if (eq? (link/item link) item) + (let ((prev (link/prev link)) + (next (link/next link))) + (set-link/next! prev next) + (set-link/prev! next prev)) + (loop (link/next link)))))) + +(define (ring/count-max-2 ring) + (let ((link (link/next ring))) + (cond ((eq? link ring) 0) + ((eq? (link/next link) ring) 1) + (else 2)))) + +(define (ring/first-item ring) + (link/item (link/next ring))) + +(define (ring/set-first-item! ring item) + (set-link/item! (link/next ring) item)) \ No newline at end of file diff --git a/src/runtime/thread.scm b/src/runtime/thread.scm index f61946fd0..37916cdf1 100644 --- a/src/runtime/thread.scm +++ b/src/runtime/thread.scm @@ -110,7 +110,25 @@ USA. (initialize-io-blocking) (add-event-receiver! event:after-restore initialize-io-blocking) (detach-thread (make-thread #f)) - (add-event-receiver! event:before-exit stop-thread-timer)) + (add-event-receiver! event:before-exit stop-thread-timer) + (named-structure/set-tag-description! thread-mutex-tag + (make-define-structure-type 'VECTOR + "thread-mutex" + '#(WAITING-THREADS OWNER) + '#(1 2) + (vector 2 (lambda () #f)) + (standard-unparser-method 'THREAD-MUTEX #f) + thread-mutex-tag + 3)) + (named-structure/set-tag-description! link-tag + (make-define-structure-type 'VECTOR + "link" + '#(PREV NEXT ITEM) + '#(1 2 3) + (vector 3 (lambda () #f)) + (standard-unparser-method 'LINK #f) + link-tag + 4))) (define (make-thread continuation) (let ((thread (%make-thread))) @@ -1041,7 +1059,12 @@ USA. ;;;; Mutexes -(define-structure (thread-mutex +;;; A record type cannot be created very early in the cold load, but +;;; creating thread mutexes early is convenient for users of serial +;;; populations, queues, etc. The following define-structure is +;;; hand-expanded as a tagged vector (not record) in thread-low.scm. + +#;(define-structure (thread-mutex (constructor make-thread-mutex ()) (conc-name thread-mutex/)) (waiting-threads (make-ring) read-only #t) @@ -1200,64 +1223,6 @@ USA. (define-integrable (remove-thread-mutex! thread mutex) (set-thread/mutexes! thread (delq! mutex (thread/mutexes thread)))) -;;;; Circular Rings - -(define-structure (link (conc-name link/)) - prev - next - item) - -(define (make-ring) - (let ((link (make-link #f #f #f))) - (set-link/prev! link link) - (set-link/next! link link) - link)) - -(define-integrable (ring/empty? ring) - (eq? (link/next ring) ring)) - -(define (ring/enqueue ring item) - (let ((prev (link/prev ring))) - (let ((link (make-link prev ring item))) - (set-link/next! prev link) - (set-link/prev! ring link)))) - -(define (ring/dequeue ring default) - (let ((link (link/next ring))) - (if (eq? link ring) - default - (begin - (let ((next (link/next link))) - (set-link/next! ring next) - (set-link/prev! next ring)) - (link/item link))))) - -(define (ring/discard-all ring) - (set-link/prev! ring ring) - (set-link/next! ring ring)) - -(define (ring/remove-item ring item) - (let loop ((link (link/next ring))) - (if (not (eq? link ring)) - (if (eq? (link/item link) item) - (let ((prev (link/prev link)) - (next (link/next link))) - (set-link/next! prev next) - (set-link/prev! next prev)) - (loop (link/next link)))))) - -(define (ring/count-max-2 ring) - (let ((link (link/next ring))) - (cond ((eq? link ring) 0) - ((eq? (link/next link) ring) 1) - (else 2)))) - -(define (ring/first-item ring) - (link/item (link/next ring))) - -(define (ring/set-first-item! ring item) - (set-link/item! (link/next ring) item)) - ;;;; Error Conditions (define condition-type:thread-control-error)