Git Product home page Git Product logo

cl-chan's Introduction

Cl-Chan

Description

cl-chan provides a golang like unbuffered/buffered channel.

With send or recv, you can send or receive value to channel.

Also with select, you can randomly listen to event from a channel.

It based on multi-threads library bordeaux-threads to act like gorotine stuff.

Note,

This project is inspired by chanl and chan. Lots of utils code is copy from chanl, such as queues.lisp, threads.lisp, utils.lisp.

The reason I write cl-chan is that as the only useable CSP lib I find in cl world, chanl test cases may cause race condition, you can refer to this issue. It seems hard to fix it as it abstracts a lot from different channels. So I made a simple worked one.

I refer to project chan. But I found the select implenmentation may have a problem (issue), So I chose the select the implementation of chanl . Also, in order to add functionality of non-block send and receive, I rewrite the logic of send and recv.

Usage

cl-chan provides two types channel: unbuffered-channel and buffered-channel.

;; make a unbuffered channel
CL-CHAN> (defvar *c1* (make-instance 'unbuffered-channel))
*C1*


;; make a buffered channel with size 2.
CL-CHAN> (defvar *c2* (make-instance 'buffered-channel :size 2))
*C2*

;; check if channel is buffered
CL-CHAN> (channel-bufferedp *c1*)
NIL

CL-CHAN> (channel-bufferedp *c2*)
T

You can use send to send value to a channel or recv to receive value from it.

;; return value is channel itself
CL-CHAN> (send *c2* 1)
#<BUFFERED-CHANNEL {100285CD33}>

;; first return value is value received from channel
;; second return value is channel itself
CL-CHAN> (recv *c2*)
1
#<BUFFERED-CHANNEL {100285CD33}>

;; send and receive with blockp nil
CL-CHAN> (send *c2* :blockp nil)
#<BUFFERED-CHANNEL {100285CD33}>

CL-CHAN> (recv *c2* :blockp nil)
1
#<BUFFERED-CHANNEL {100285CD33}>

Also there is a select which could be used to wait one of multi channels events to happen.

If there is no default case, the select statement blocks until at least one of the communications can proceed. (The select will keep looping, which is not good. Need to fix.)

The difference to golang select is select an empty expression. Golang select will block forever however, our's will return nil. (This may change overtime, don't rely on it.)

(select
   ((recv c d)
    (format t "got ~a from c~%" d))
   ((send e val)
    (print "sent val on e~%"))
   ((recv *lots-of-channels* value channel)
    (format t "Got ~A from ~C~%" value channel))
   (otherwise
    (print "would have blocked~%")))

Create a new thread continually reading values and printing them:

(pexec ()
   (loop (format t "~a~%" (recv *c*))))

Create a new thread that runs a function:

(pcall #'my-function)

Installation

Download the repo in where your quicklisp can find (such as ~/quicklisp/local-projects), then use (ql:quickload :cl-chan) to load.

Currently tested on sbcl and ccl, you can test as: (asdf:test-system :cl-chan)

Author

Copyright/License

Do whatever you want.

cl-chan's People

Contributors

imnisen avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.