github.com/primecitizens/pcz/std@v0.2.1/runtime/builtin_chan.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  //
     4  // Copyright 2014 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  //go:build pcz
     9  
    10  package runtime
    11  
    12  import (
    13  	"unsafe"
    14  
    15  	stdgo "github.com/primecitizens/pcz/std/builtin/go"
    16  	"github.com/primecitizens/pcz/std/core/abi"
    17  	"github.com/primecitizens/pcz/std/core/assert"
    18  )
    19  
    20  // see $GOROOT/src/runtime/chan.go
    21  
    22  //
    23  // channel
    24  //
    25  
    26  func makechan64(t *abi.ChanType, size int64) *stdgo.Chan {
    27  	if int64(int(size)) != size {
    28  		assert.Panic("makechan: size out of range")
    29  	}
    30  
    31  	return makechan(t, int(size))
    32  }
    33  
    34  func makechan(t *abi.ChanType, sz int) *stdgo.Chan { return stdgo.MakeChan(t, sz) }
    35  
    36  // entry point for x := <- ch from compiled code.
    37  //
    38  //go:nosplit
    39  func chanrecv1(c *stdgo.Chan, elem unsafe.Pointer) { stdgo.Recv(c, elem, true) }
    40  
    41  // entry point for x, ok := <- ch from compiled code.
    42  //
    43  //go:nosplit
    44  func chanrecv2(c *stdgo.Chan, elem unsafe.Pointer) (received bool) {
    45  	_, received = stdgo.Recv(c, elem, true)
    46  	return received
    47  }
    48  
    49  // entry point for ch <- x from compiled code.
    50  //
    51  //go:nosplit
    52  func chansend1(c *stdgo.Chan, elem unsafe.Pointer) {
    53  	stdgo.Send(c, elem, true, getcallerpc())
    54  }
    55  
    56  // entry point for close(ch) from compiled code.
    57  //
    58  //go:nosplit
    59  func closechan(c *stdgo.Chan) {
    60  	stdgo.CloseChan(c)
    61  }
    62  
    63  //
    64  // select
    65  //
    66  
    67  // compiler implements
    68  //
    69  //	select {
    70  //	case c <- v:
    71  //		... foo
    72  //	default:
    73  //		... bar
    74  //	}
    75  //
    76  // as
    77  //
    78  //	if selectnbsend(c, v) {
    79  //		... foo
    80  //	} else {
    81  //		... bar
    82  //	}
    83  func selectnbsend(c *stdgo.Chan, elem unsafe.Pointer) bool {
    84  	return stdgo.Send(c, elem, false, getcallerpc())
    85  }
    86  
    87  // compiler implements
    88  //
    89  //	select {
    90  //	case v, ok = <-c:
    91  //		... foo
    92  //	default:
    93  //		... bar
    94  //	}
    95  //
    96  // as
    97  //
    98  //	if selected, ok = selectnbrecv(&v, c); selected {
    99  //		... foo
   100  //	} else {
   101  //		... bar
   102  //	}
   103  func selectnbrecv(ep unsafe.Pointer, c *stdgo.Chan) (bool, bool) {
   104  	return stdgo.Recv(c, ep, false)
   105  }
   106  
   107  func selectsetpc(pc *uintptr) { *pc = getcallerpc() }
   108  
   109  func selectgo(cas0 *stdgo.SelectCase, order0 *uint16, pc0 *uintptr, nsends int, nrecvs int, block bool) (int, bool) {
   110  	return stdgo.Select(cas0, order0, pc0, nsends, nrecvs, block)
   111  }
   112  
   113  func block() { stdgo.Block() }