Skip to content

Commit

Permalink
Lock: Add lock mechanism with array of input sigs
Browse files Browse the repository at this point in the history
  • Loading branch information
Catya3 committed May 9, 2024
1 parent 90ca0f1 commit f9713e7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
61 changes: 61 additions & 0 deletions internal/runtime/funcs/polylock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package funcs

import (
"context"
"errors"

"github.com/nevalang/neva/internal/runtime"
)

type polyLock struct{}

func (l polyLock) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) {
sigIn, ok := io.In["sig"]
if !ok {
return nil, errors.New("inport 'sig' is required")
}

dataIn, err := io.In.Port("data")
if err != nil {
return nil, err
}

dataOut, err := io.Out.Port("data")
if err != nil {
return nil, err
}

return l.HandlePoly(sigIn, dataIn, dataOut), nil
}

func (polyLock) HandlePoly(
sigIn []chan runtime.Msg,
dataIn,
dataOut chan runtime.Msg,
) func(ctx context.Context) {
return func(ctx context.Context) {
var data runtime.Msg

for {
for _, ch := range sigIn {
select {
case <-ctx.Done():
return
case <-ch:
}
}

select {
case <-ctx.Done():
return
case data = <-dataIn:
}

select {
case <-ctx.Done():
return
case dataOut <- data:
}
}
}
}
13 changes: 7 additions & 6 deletions internal/runtime/funcs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
func CreatorRegistry() map[string]runtime.FuncCreator {
return map[string]runtime.FuncCreator{
// core
"new": new{},
"del": del{},
"lock": lock{},
"match": match{},
"unwrap": unwrap{},
"panic": panicker{},
"new": new{},
"del": del{},
"lock": lock{},
"polylock": polyLock{},
"match": match{},
"unwrap": unwrap{},
"panic": panicker{},

// streamers
"array_port_to_stream": arrayPortToStream{},
Expand Down
3 changes: 3 additions & 0 deletions std/builtin/core.neva
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ component {
#extern(lock)
pub Lock<T>(sig any, data T) (data T)

#extern(polylock)
pub PolyLock<T>([sig] any, data T) (data T)

#extern(panic)
pub Panic(msg any) ()

Expand Down

0 comments on commit f9713e7

Please sign in to comment.