]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: move m's OS-specific semaphore fields into mOS
authorMatthew Dempsky <mdempsky@google.com>
Thu, 22 Oct 2015 01:36:05 +0000 (18:36 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 13 Nov 2015 02:58:12 +0000 (02:58 +0000)
Allows removing fields that aren't relevant to a particular OS or
changing their types to match the underlying OS system calls they'll
be used for.

Change-Id: I5cea89ee77b4e7b985bff41337e561887c3272ff
Reviewed-on: https://go-review.googlesource.com/16176
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>

16 files changed:
src/runtime/lock_sema.go
src/runtime/os1_darwin.go
src/runtime/os1_nacl.go
src/runtime/os1_netbsd.go
src/runtime/os1_openbsd.go
src/runtime/os1_plan9.go
src/runtime/os1_windows.go
src/runtime/os3_solaris.go
src/runtime/os_darwin.go
src/runtime/os_nacl.go
src/runtime/os_netbsd.go
src/runtime/os_openbsd.go
src/runtime/os_plan9.go
src/runtime/os_solaris.go
src/runtime/os_windows.go
src/runtime/runtime2.go

index ebf786f0afa177764973ca7b26d567d79ef60b48..d39b010cf0e3d36bc1385aff09b25319d376fa3b 100644 (file)
@@ -13,18 +13,16 @@ import (
 
 // This implementation depends on OS-specific implementations of
 //
-//     uintptr runtime·semacreate(void)
-//             Create a semaphore, which will be assigned to m->waitsema.
-//             The zero value is treated as absence of any semaphore,
-//             so be sure to return a non-zero value.
+//     func semacreate(mp *m)
+//             Create a semaphore for mp, if it does not already have one.
 //
-//     int32 runtime·semasleep(int64 ns)
-//             If ns < 0, acquire m->waitsema and return 0.
-//             If ns >= 0, try to acquire m->waitsema for at most ns nanoseconds.
+//     func semasleep(ns int64) int32
+//             If ns < 0, acquire m's semaphore and return 0.
+//             If ns >= 0, try to acquire m's semaphore for at most ns nanoseconds.
 //             Return 0 if the semaphore was acquired, -1 if interrupted or timed out.
 //
-//     int32 runtime·semawakeup(M *mp)
-//             Wake up mp, which is or will soon be sleeping on mp->waitsema.
+//     func semawakeup(mp *m)
+//             Wake up mp, which is or will soon be sleeping on its semaphore.
 //
 const (
        locked uintptr = 1
@@ -45,9 +43,7 @@ func lock(l *mutex) {
        if atomic.Casuintptr(&l.key, 0, locked) {
                return
        }
-       if gp.m.waitsema == 0 {
-               gp.m.waitsema = semacreate()
-       }
+       semacreate(gp.m)
 
        // On uniprocessor's, no point spinning.
        // On multiprocessors, spin for ACTIVE_SPIN attempts.
@@ -157,9 +153,7 @@ func notesleep(n *note) {
        if gp != gp.m.g0 {
                throw("notesleep not on g0")
        }
-       if gp.m.waitsema == 0 {
-               gp.m.waitsema = semacreate()
-       }
+       semacreate(gp.m)
        if !atomic.Casuintptr(&n.key, 0, uintptr(unsafe.Pointer(gp.m))) {
                // Must be locked (got wakeup).
                if n.key != locked {
@@ -248,9 +242,7 @@ func notetsleep(n *note, ns int64) bool {
        if gp != gp.m.g0 && gp.m.preemptoff != "" {
                throw("notetsleep not on g0")
        }
-       if gp.m.waitsema == 0 {
-               gp.m.waitsema = semacreate()
-       }
+       semacreate(gp.m)
        return notetsleep_internal(n, ns, nil, 0)
 }
 
@@ -261,9 +253,7 @@ func notetsleepg(n *note, ns int64) bool {
        if gp == gp.m.g0 {
                throw("notetsleepg on g0")
        }
-       if gp.m.waitsema == 0 {
-               gp.m.waitsema = semacreate()
-       }
+       semacreate(gp.m)
        entersyscallblock(0)
        ok := notetsleep_internal(n, ns, nil, 0)
        exitsyscall(0)
index be710599df4fd09f1a0208621d9d8c8c186f9a7b..ba38a78ed1974dff4e022da9eed66e8fd6f90628 100644 (file)
@@ -17,16 +17,17 @@ func unimplemented(name string) {
 
 //go:nosplit
 func semawakeup(mp *m) {
-       mach_semrelease(uint32(mp.waitsema))
+       mach_semrelease(mp.waitsema)
 }
 
 //go:nosplit
-func semacreate() uintptr {
-       var x uintptr
+func semacreate(mp *m) {
+       if mp.waitsema != 0 {
+               return
+       }
        systemstack(func() {
-               x = uintptr(mach_semcreate())
+               mp.waitsema = mach_semcreate()
        })
-       return x
 }
 
 // BSD interface for threading.
@@ -370,7 +371,7 @@ func semasleep1(ns int64) int32 {
        if ns >= 0 {
                var nsecs int32
                secs := timediv(ns, 1000000000, &nsecs)
-               r := mach_semaphore_timedwait(uint32(_g_.m.waitsema), uint32(secs), uint32(nsecs))
+               r := mach_semaphore_timedwait(_g_.m.waitsema, uint32(secs), uint32(nsecs))
                if r == _KERN_ABORTED || r == _KERN_OPERATION_TIMED_OUT {
                        return -1
                }
@@ -381,7 +382,7 @@ func semasleep1(ns int64) int32 {
        }
 
        for {
-               r := mach_semaphore_wait(uint32(_g_.m.waitsema))
+               r := mach_semaphore_wait(_g_.m.waitsema)
                if r == 0 {
                        break
                }
index 143752ada808b10fab40ddd91bd748d81d749057..ad4329cecd12ddd39fed8627c329948a0c1f55f9 100644 (file)
@@ -83,8 +83,10 @@ func newosproc(mp *m, stk unsafe.Pointer) {
 }
 
 //go:nosplit
-func semacreate() uintptr {
-       var cond uintptr
+func semacreate(mp *m) {
+       if mp.waitsema != 0 {
+               return
+       }
        systemstack(func() {
                mu := nacl_mutex_create(0)
                if mu < 0 {
@@ -93,14 +95,12 @@ func semacreate() uintptr {
                }
                c := nacl_cond_create(0)
                if c < 0 {
-                       print("nacl_cond_create: error ", -cond, "\n")
+                       print("nacl_cond_create: error ", -c, "\n")
                        throw("semacreate")
                }
-               cond = uintptr(c)
-               _g_ := getg()
-               _g_.m.waitsemalock = uint32(mu)
+               mp.waitsema = c
+               mp.waitsemalock = mu
        })
-       return cond
 }
 
 //go:nosplit
@@ -109,13 +109,13 @@ func semasleep(ns int64) int32 {
 
        systemstack(func() {
                _g_ := getg()
-               if nacl_mutex_lock(int32(_g_.m.waitsemalock)) < 0 {
+               if nacl_mutex_lock(_g_.m.waitsemalock) < 0 {
                        throw("semasleep")
                }
 
                for _g_.m.waitsemacount == 0 {
                        if ns < 0 {
-                               if nacl_cond_wait(int32(_g_.m.waitsema), int32(_g_.m.waitsemalock)) < 0 {
+                               if nacl_cond_wait(_g_.m.waitsema, _g_.m.waitsemalock) < 0 {
                                        throw("semasleep")
                                }
                        } else {
@@ -123,9 +123,9 @@ func semasleep(ns int64) int32 {
                                end := ns + nanotime()
                                ts.tv_sec = end / 1e9
                                ts.tv_nsec = int32(end % 1e9)
-                               r := nacl_cond_timed_wait_abs(int32(_g_.m.waitsema), int32(_g_.m.waitsemalock), &ts)
+                               r := nacl_cond_timed_wait_abs(_g_.m.waitsema, _g_.m.waitsemalock, &ts)
                                if r == -_ETIMEDOUT {
-                                       nacl_mutex_unlock(int32(_g_.m.waitsemalock))
+                                       nacl_mutex_unlock(_g_.m.waitsemalock)
                                        ret = -1
                                        return
                                }
@@ -136,7 +136,7 @@ func semasleep(ns int64) int32 {
                }
 
                _g_.m.waitsemacount = 0
-               nacl_mutex_unlock(int32(_g_.m.waitsemalock))
+               nacl_mutex_unlock(_g_.m.waitsemalock)
                ret = 0
        })
        return ret
@@ -145,15 +145,15 @@ func semasleep(ns int64) int32 {
 //go:nosplit
 func semawakeup(mp *m) {
        systemstack(func() {
-               if nacl_mutex_lock(int32(mp.waitsemalock)) < 0 {
+               if nacl_mutex_lock(mp.waitsemalock) < 0 {
                        throw("semawakeup")
                }
                if mp.waitsemacount != 0 {
                        throw("semawakeup")
                }
                mp.waitsemacount = 1
-               nacl_cond_signal(int32(mp.waitsema))
-               nacl_mutex_unlock(int32(mp.waitsemalock))
+               nacl_cond_signal(mp.waitsema)
+               nacl_mutex_unlock(mp.waitsemalock)
        })
 }
 
index b127c64ff4d9e5fed2ef00e3f8e2b9b11b9c13b1..3e77d248f79bfa43dedf5620b9a7341ee1862e3d 100644 (file)
@@ -40,8 +40,7 @@ func getncpu() int32 {
 }
 
 //go:nosplit
-func semacreate() uintptr {
-       return 1
+func semacreate(mp *m) {
 }
 
 //go:nosplit
index beda59789c035499f258ac507ddad9d1bcd2e679..11034a64f664f07dba1494274589ff0daa80c19d 100644 (file)
@@ -47,8 +47,7 @@ func getncpu() int32 {
 }
 
 //go:nosplit
-func semacreate() uintptr {
-       return 1
+func semacreate(mp *m) {
 }
 
 //go:nosplit
index 07ad498fbca2bdaed1dc35eb827099d23c13a014..bc7ce65dafd18c02e59971c8a43360e69e4e3479 100644 (file)
@@ -205,8 +205,7 @@ func newosproc(mp *m, stk unsafe.Pointer) {
 }
 
 //go:nosplit
-func semacreate() uintptr {
-       return 1
+func semacreate(mp *m) {
 }
 
 //go:nosplit
index bd514724f18f139e236b0e75574995de5436b8d5..813454357824e8537c221132cca8d952777bb2c9 100644 (file)
@@ -365,8 +365,11 @@ func semawakeup(mp *m) {
 }
 
 //go:nosplit
-func semacreate() uintptr {
-       return stdcall4(_CreateEventA, 0, 0, 0, 0)
+func semacreate(mp *m) {
+       if mp.waitsema != 0 {
+               return
+       }
+       mp.waitsema = stdcall4(_CreateEventA, 0, 0, 0, 0)
 }
 
 // May run with m.p==nil, so write barriers are not allowed.
index 792188fea639515b08b343e629d81ffaecbebdd7..3ac121a7b828debe83d779c63a8d3c6b2649e061 100644 (file)
@@ -314,7 +314,11 @@ func unblocksig(sig int32) {
 }
 
 //go:nosplit
-func semacreate() uintptr {
+func semacreate(mp *m) {
+       if mp.waitsema != 0 {
+               return
+       }
+
        var sem *semt
        _g_ := getg()
 
@@ -331,7 +335,7 @@ func semacreate() uintptr {
        if sem_init(sem, 0, 0) != 0 {
                throw("sem_init")
        }
-       return uintptr(unsafe.Pointer(sem))
+       mp.waitsema = uintptr(unsafe.Pointer(sem))
 }
 
 //go:nosplit
index 0fedb707e9fb402c492f60c413944bd511f0f17d..7a70639b022f0627d9a494d14e8536a83a8711f3 100644 (file)
@@ -6,7 +6,9 @@ package runtime
 
 import "unsafe"
 
-type mOS struct{}
+type mOS struct {
+       waitsema uint32 // semaphore for parking on locks
+}
 
 func bsdthread_create(stk, arg unsafe.Pointer, fn uintptr) int32
 func bsdthread_register() int32
index 58330d2810c9a0e81807caf3390dd020cef48060..69eaf4c14e78322ec64176564619f37822ba902b 100644 (file)
@@ -6,7 +6,11 @@ package runtime
 
 import "unsafe"
 
-type mOS struct{}
+type mOS struct {
+       waitsema      int32 // semaphore for parking on locks
+       waitsemacount int32
+       waitsemalock  int32
+}
 
 func nacl_exception_stack(p uintptr, size int32) int32
 func nacl_exception_handler(fn uintptr, arg unsafe.Pointer) int32
index 659ec2d65a517b54e1fee07048578006570cc030..988374120d12c3d4f2e6827aefc122dd93ecc1d4 100644 (file)
@@ -6,7 +6,9 @@ package runtime
 
 import "unsafe"
 
-type mOS struct{}
+type mOS struct {
+       waitsemacount uint32
+}
 
 //go:noescape
 func setitimer(mode int32, new, old *itimerval)
index 74a838fa416f23bff2a9751a75aa437c1475a42b..12f4cd1a246ed9dcda8af5ad3b437b85eb743e79 100644 (file)
@@ -4,7 +4,9 @@
 
 package runtime
 
-type mOS struct{}
+type mOS struct {
+       waitsemacount uint32
+}
 
 //go:noescape
 func setitimer(mode int32, new, old *itimerval)
index 3b3e940cbc93d8b72e46519adc32deac7a7d8608..6e6a55e636f097e22506d1a35ff7d363ab518fa2 100644 (file)
@@ -7,8 +7,9 @@ package runtime
 import "unsafe"
 
 type mOS struct {
-       notesig *int8
-       errstr  *byte
+       waitsemacount uint32
+       notesig       *int8
+       errstr        *byte
 }
 
 func closefd(fd int32) int32
index 129653ef19f0561213e36e0f349f18c4de0e5aa3..9dbe38a32a6b23f15f26722c7e98bf468fb93579 100644 (file)
@@ -16,7 +16,8 @@ type mscratch struct {
 }
 
 type mOS struct {
-       perrno *int32 // pointer to tls errno
+       waitsema uintptr // semaphore for parking on locks
+       perrno   *int32  // pointer to tls errno
        // these are here because they are too large to be on the stack
        // of low-level NOSPLIT functions.
        //LibCall       libcall;
index 22f7daad5182b870287c899fb3033969c87fdd74..5dab1dec161c7bebc82daa5da9fd416c91b6dc0f 100644 (file)
@@ -6,7 +6,9 @@ package runtime
 
 import "unsafe"
 
-type mOS struct{}
+type mOS struct {
+       waitsema uintptr // semaphore for parking on locks
+}
 
 type stdFunction *byte
 
index 9ec0d1545edde90152924ace7631f491c28daf44..1dbd3d20944ff18a909f8482e58b3a898a32ac88 100644 (file)
@@ -308,9 +308,6 @@ type m struct {
        fflag         uint32      // floating point compare flags
        locked        uint32      // tracking for lockosthread
        nextwaitm     uintptr     // next m waiting for lock
-       waitsema      uintptr     // semaphore for parking on locks
-       waitsemacount uint32
-       waitsemalock  uint32
        gcstats       gcstats
        needextram    bool
        traceback     uint8