Skip to content

Instantly share code, notes, and snippets.

@aw
Last active May 7, 2018 06:10
Show Gist options
  • Select an option

  • Save aw/4ca813f8571fda85b20bacfd50f84f07 to your computer and use it in GitHub Desktop.

Select an option

Save aw/4ca813f8571fda85b20bacfd50f84f07 to your computer and use it in GitHub Desktop.

Revisions

  1. aw renamed this gist May 7, 2018. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions native-atomic.l → native-atomic.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,14 @@
    ### PicoLisp's (native) can be used to create files atomically

    The C equivalent would be:

    ```C
    open("atomic.file", O_RDWR|O_CREAT|O_EXCL, 0644);
    ```
    PicoLisp:
    ```picolisp
    # The flags can be read from sysdefs, but here we define them manually (Linux)
    (setq
    O_RDWR 2 #(in '("@src64/sysdefs") (from "O_RDWR") (read))
  2. aw revised this gist May 6, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion native-atomic.l
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ### PicoLisp's (native) can be used to create files atomically

    ```picolisp
    ```
    # The flags can be read from sysdefs, but here we define them manually (Linux)
    (setq
    O_RDWR 2 #(in '("@src64/sysdefs") (from "O_RDWR") (read))
  3. aw revised this gist May 6, 2018. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions native-atomic.l
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # PicoLisp's (native) can be used to create files atomically
    ### PicoLisp's (native) can be used to create files atomically

    ```picolisp
    # The flags can be read from sysdefs, but here we define them manually (Linux)
    (setq
    O_RDWR 2 #(in '("@src64/sysdefs") (from "O_RDWR") (read))
    @@ -12,5 +13,6 @@
    # -> 3
    (native "@" "open" 'I "atomic.file" (| O_RDWR O_CREAT O_EXCL) (oct "0644"))
    # -> -1

    First call is successful, second call fails (-1), as expected.
    ```

    First call is successful (3), second call fails (-1), as expected.
  4. aw created this gist May 6, 2018.
    16 changes: 16 additions & 0 deletions native-atomic.l
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # PicoLisp's (native) can be used to create files atomically

    # The flags can be read from sysdefs, but here we define them manually (Linux)
    (setq
    O_RDWR 2 #(in '("@src64/sysdefs") (from "O_RDWR") (read))
    O_CREAT 64 #(in '("@src64/sysdefs") (from "O_CREAT") (read))
    O_EXCL 128 #(in '("@src64/sysdefs") (from "O_EXCL") (read))
    )

    # The file mode 0644 is octal and must be converted to int
    (native "@" "open" 'I "atomic.file" (| O_RDWR O_CREAT O_EXCL) (oct "0644"))
    # -> 3
    (native "@" "open" 'I "atomic.file" (| O_RDWR O_CREAT O_EXCL) (oct "0644"))
    # -> -1

    First call is successful, second call fails (-1), as expected.