Hi, I wonder if there has a small cost, little latency and high efficient 
solution 
of event notification from cgo to go side.

Here is 3 ways as far as I know:

    1. Poll

        go func() {
            for {
                // fast ret from cgo 
                ret := C.sema_try_wait()
                if new events {
                    do_new_event_tasks()
                }else{
                    time.Sleep(time.Millisecond * 250)
                }
            }
        }()

        Disadvantage:
        
            Would be some overhead and constant latency.

    2. Long wait

        go func() {
            for {
                // maybe blocking at cgo side arbitrarily long time
                ret := C.sema_wait()
                do_new_event_tasks()
            }
        }()

        Disadvantage:

            As been discussed in issue 12416 
<https://github.com/golang/go/issues/12416>:

Q: What kind of errors will be returned when a C function takes too long? 
>
What is the story with regards to read(2) and blocking devices? (by taruti)

 

A: In the default mode, nothing will happen if a C function takes too long, 

except that your program might eventually run out of memory because the GC 

can't run (that won't happen with 1.6 but it might happen in future 
> releases). 

In the checking mode, you will get a panic. If you want to call a C 
> function 

that does a read on a device that blocks arbitrarily long, you should read 
> the 

data into a buffer allocated by C. We will make the syscall.Read function 
> do the 

right thing on a long-blocking read, whatever that turns out to be.

So, if one cgo call takes too long is not recommend, we still need a cgo 
> notify to 

go mechanism. (by ianlancetaylor)


            So, if the cgo call takes too long to return is not recommend, 
we still need another cgo 
            notify to go mechanism. 

    3. Raise a signal

        c := make(chan os.Signal, 1)
        signal.Notify(c, SignalUsr1)
        for{
            // cgo just raise SignalUsr1 to itself when 
            // it want to notify go side
            s := <-c
            do_new_event_tasks()
        }

        Disadvantage:

            A little wierd, and when there is about 100000 new events to 
notify per seconds, the overhead would be huge.

Is there some much better methods? 

Thanks a lot.

-- Remus

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to