[erlang-questions] how the lists:seq/3 implemented, confused when reading the source code of lists:seq/3

Taotao.Li taotao.li@REDACTED
Tue Nov 4 11:32:17 CET 2014


----Source Code--------

-spec seq(From, To, Incr) -> Seq when
      From :: integer(),
      To :: integer(),
      Incr :: integer(),
      Seq :: [integer()].

seq(First, Last, Inc) 
    when is_integer(First), is_integer(Last), is_integer(Inc) -> 
    if
        Inc > 0, First - Inc =< Last;
        Inc < 0, First - Inc >= Last ->
            N = (Last - First + Inc) div Inc,
            seq_loop(N, Inc*(N-1)+First, Inc, []);
        Inc =:= 0, First =:= Last ->
            seq_loop(1, First, Inc, [])
    end.

seq_loop(N, X, D, L) when N >= 4 ->
     Y = X-D, Z = Y-D, W = Z-D,
     seq_loop(N-4, W-D, D, [W,Z,Y,X|L]);
seq_loop(N, X, D, L) when N >= 2 ->
     Y = X-D,
     seq_loop(N-2, Y-D, D, [Y,X|L]);
seq_loop(1, X, _, L) ->
     [X|L];
seq_loop(0, _, _, L) ->
     L.

-----------------------

-----Problem-----------
when I entered lists:seq(-10, 10, 2).
what happened?
I think it should execute 'Inc > 0, First-Inc =< Last;' and in this way it should just returning nothing.

Feeling so confused, can anyone help me about this? thanks a lot!


-----------------------


----- 原始邮件 -----
发件人: erlang-questions-request@REDACTED
收件人: erlang-questions@REDACTED
发送时间: 星期一, 2014年 11 月 03日 下午 7:00:00
主题: erlang-questions Digest, Vol 190, Issue 1

Send erlang-questions mailing list submissions to
	erlang-questions@REDACTED

To subscribe or unsubscribe via the World Wide Web, visit
	http://erlang.org/mailman/listinfo/erlang-questions
or, via email, send a message with subject or body 'help' to
	erlang-questions-request@REDACTED

You can reach the person managing the list at
	erlang-questions-owner@REDACTED

When replying, please edit your Subject line so it is more specific
than "Re: Contents of erlang-questions digest..."


Today's Topics:

   1. Re:  DTLS/SRTP for WebRTC (Ingela Andin)
   2. Re:  spawn and controlling_process (Hynek Vychodil)
   3. Re:  spawn and controlling_process (Jachym Holecek)
   4. Re:  spawn and controlling_process (Gadi Srebnik)


----------------------------------------------------------------------

Message: 1
Date: Sun, 2 Nov 2014 20:03:28 +0100
From: Ingela Andin <ingela.andin@REDACTED>
To: pablo platt <pablo.platt@REDACTED>
Cc: erlang questions <erlang-questions@REDACTED>
Subject: Re: [erlang-questions] DTLS/SRTP for WebRTC
Message-ID:
	<CAFj9NSTjtsh9u+LmKm-Pv4YM5OJ8Fz=_-Cx_v0LxL5EJLDDHFA@REDACTED>
Content-Type: text/plain; charset="utf-8"

Hi!

2014-10-30 22:33 GMT+01:00 pablo platt <pablo.platt@REDACTED>:

> Hi,
>
> What is the state of DTLS in OTP?
>

Alas Ericsson has prioritised quite a few other things higher than DTLS
just lately so the implementation has been delayed.
I hope it will get back on track soon but I am afraid you can no count on
it in a short term perspective.

Regards Ingela Erlang/OTP team - Ericsson AB



>
> I'm interested in using DTLS/SRTP in Erlang for WebRTC.
>
> I've found two implementations:
>
> Clean code but I couldn't make it work because it's missing SRTP support.
> https://github.com/RoadRunnr/otp/tree/new_crypto_dtls
>
> This works quite well. I'm able to connect clients to the server:
> https://groups.google.com/d/msg/discuss-webrtc/MP-1sCrOljA/qAs4VK-18y4J
>
> The problem is that some clients can't connect and I'm getting HANDSHAKE
> packets while in the CIPHER state in the following order:
> HANDSHAKE, SERVER_HELLO
> HANDSHAKE, CERTIFICATE
> HANDSHAKE, SERVER_KEY_EXCHANGE
> HANDSHAKE, CERTIFICATE_REQUEST
> HANDSHAKE, SERVER_HELLO_DONE
>
> I'm assuming that is related to wrong state change or retransmission.
> The code only implement retransmission for two packet types and have the
> following comment:
> %% problem remainning: different strategy should be adopted while
> state==hello or cipher.
>
> Does anyone use DTLS/SRTP in Erlang and help solve this issue or recommend
> another solution?
>
> Thanks
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141102/0530050f/attachment-0001.html>

------------------------------

Message: 2
Date: Mon, 3 Nov 2014 10:06:28 +0100
From: Hynek Vychodil <vychodil.hynek@REDACTED>
To: Gadi Srebnik <gadi@REDACTED>
Cc: erlang-questions <erlang-questions@REDACTED>
Subject: Re: [erlang-questions] spawn and controlling_process
Message-ID:
	<CAL_wnpfgA93QErFDh6UFQPVH+B837CiqEr7tBE0r3tuzvi9YFA@REDACTED>
Content-Type: text/plain; charset="utf-8"

No, there is not any difference. Both versions of code is compiled exactly
same:

fun1(Socket,Mod, Fun, Args) ->
    Pid = erlang:spawn(Mod, Fun, Args),
    gen_tcp:controlling_process(Socket,Pid).

fun2(Socket,Mod, Fun, Args) ->
    gen_tcp:controlling_process(Socket,erlang:spawn(Mod, Fun, Args)).

Then you can compile it with 'S' and get "assembler":

{function, fun1, 4, 2}.
  {label,1}.
    {line,[{location,"test2.erl",5}]}.
    {func_info,{atom,test2},{atom,fun1},4}.
  {label,2}.
    {allocate,1,4}.
    {move,{x,0},{y,0}}.
    {move,{x,1},{x,0}}.
    {move,{x,2},{x,1}}.
    {move,{x,3},{x,2}}.
    {line,[{location,"test2.erl",6}]}.
    {call_ext,3,{extfunc,erlang,spawn,3}}.
    {move,{x,0},{x,1}}.
    {move,{y,0},{x,0}}.
    {line,[{location,"test2.erl",7}]}.
    {call_ext_last,2,{extfunc,gen_tcp,controlling_process,2},1}.


{function, fun2, 4, 4}.
  {label,3}.
    {line,[{location,"test2.erl",9}]}.
    {func_info,{atom,test2},{atom,fun2},4}.
  {label,4}.
    {allocate,1,4}.
    {move,{x,0},{y,0}}.
    {move,{x,1},{x,0}}.
    {move,{x,2},{x,1}}.
    {move,{x,3},{x,2}}.
    {line,[{location,"test2.erl",10}]}.
    {call_ext,3,{extfunc,erlang,spawn,3}}.
    {move,{x,0},{x,1}}.
    {move,{y,0},{x,0}}.
    {line,[{location,"test2.erl",10}]}.
    {call_ext_last,2,{extfunc,gen_tcp,controlling_process,2},1}.

As you can see, both functions are exactly same. Why should be there any
difference.

Hynek Vychodil

On Sat, Nov 1, 2014 at 11:34 AM, Gadi Srebnik <gadi@REDACTED> wrote:

> Hi all, my first question here.
>
> How safe it is to use controlling_process after spawn? I am using
> additional gen_tcp:controlling_process inside the Mod:Fun itself after few
> additional commands. Is it possible that process will not be owner by then?
>
>
> Pid = erlang:spawn(Mod, Fun, Args),
>
>  gen_tcp:controlling_process(Socket,Pid),
>
> Is one-liner safer?
>
> gen_tcp:controlling_process(Socket,erlang:spawn(Mod, Fun, Args)),
>
>
> Thanks,
> Gadi
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141103/260ac0e5/attachment-0001.html>

------------------------------

Message: 3
Date: Mon, 3 Nov 2014 04:30:26 -0500
From: Jachym Holecek <freza@REDACTED>
To: Gadi Srebnik <gadi@REDACTED>
Cc: erlang-questions@REDACTED
Subject: Re: [erlang-questions] spawn and controlling_process
Message-ID: <20141103093026.GA18056@REDACTED>
Content-Type: text/plain; charset=us-ascii

# Gadi Srebnik 2014-11-01:
> How safe it is to use controlling_process after spawn? I am using
> additional gen_tcp:controlling_process inside the Mod:Fun itself after few
> additional commands. Is it possible that process will not be owner by then?

Yes, there's a race condition there, you need explicit synchronization to
be safe:

  start_worker(Sock) ->
      Pid = erlang:spawn(?MODULE, worker_init, [Sock]),
      gen_tcp:controlling_process(Sock, Pid),
      Pid ! proceed.

  worker_init(Sock) ->
      receive
          proceed ->
              %% Okay now we actually own it.
              worker_loop(Sock)
      after 1000 ->
          exit(things_seem_awfully_slow_today)
      end.
 
I wish there were a variant of controlling_process that would advise the
new owner automatically by sending {tcp_handover, Sock, Info} to it, the
last item being arbitrary term passed by previous owner. No idea how hard
would it be to implement.

BR,
	-- Jachym


------------------------------

Message: 4
Date: Mon, 3 Nov 2014 11:34:31 +0200
From: Gadi Srebnik <gadi@REDACTED>
To: erlang-questions@REDACTED
Subject: Re: [erlang-questions] spawn and controlling_process
Message-ID:
	<CAFjX2eC2y8oE4O7COpiwpwhExFv-Zo0Gm+iF_fHexXRZOkh=Yg@REDACTED>
Content-Type: text/plain; charset="utf-8"

Thank you, Jachym and Hynek.

On Mon, Nov 3, 2014 at 11:30 AM, Jachym Holecek <freza@REDACTED>
wrote:

> # Gadi Srebnik 2014-11-01:
> > How safe it is to use controlling_process after spawn? I am using
> > additional gen_tcp:controlling_process inside the Mod:Fun itself after
> few
> > additional commands. Is it possible that process will not be owner by
> then?
>
> Yes, there's a race condition there, you need explicit synchronization to
> be safe:
>
>   start_worker(Sock) ->
>       Pid = erlang:spawn(?MODULE, worker_init, [Sock]),
>       gen_tcp:controlling_process(Sock, Pid),
>       Pid ! proceed.
>
>   worker_init(Sock) ->
>       receive
>           proceed ->
>               %% Okay now we actually own it.
>               worker_loop(Sock)
>       after 1000 ->
>           exit(things_seem_awfully_slow_today)
>       end.
>
> I wish there were a variant of controlling_process that would advise the
> new owner automatically by sending {tcp_handover, Sock, Info} to it, the
> last item being arbitrary term passed by previous owner. No idea how hard
> would it be to implement.
>
> BR,
>         -- Jachym
>



-- 
Gadi Srebnik
VP Communications
 m: +972 54 5929261
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141103/72d1c16d/attachment-0001.html>

------------------------------

_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions


End of erlang-questions Digest, Vol 190, Issue 1
************************************************

-- 


--------------------------------------------------------------------------- 

Thanks & Best regards 

李涛涛 Taotao · Li | Fixed Income@REDACTED | Software Engineer 

地址:上海市浦东新区陆家嘴西路 99 号万向大厦8 楼, 200120 
Address :Wanxiang Towen 8 F, Lujiazui West Rd. No.99, Pudong New District, Shanghai, 200120 

电话 |Phone : 021-60216502 手机 |Mobile: +86-18202171279 



More information about the erlang-questions mailing list