Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock: Add lock mechanism with array of input sigs #611

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions internal/runtime/funcs/lockall.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 lockAll struct{}

func (l lockAll) 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.Handle(sigIn, dataIn, dataOut), nil
}

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

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

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

select {
case <-ctx.Done():
return
case dataOut <- data:
}
}
}
}
9 changes: 5 additions & 4 deletions internal/runtime/funcs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
func CreatorRegistry() map[string]runtime.FuncCreator {
return map[string]runtime.FuncCreator{
// core
"new": new{},
"del": del{},
"lock": lock{},
"unwrap": unwrap{},
"new": new{},
"del": del{},
"lock": lock{},
"unwrap": unwrap{},
"lock_all": lockAll{},

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

#extern(lock_all)
pub component LockAll<T>([sig] any, data T) (data T)

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

Expand Down
Loading