[#7055] More on VC++ 2005 — Austin Ziegler <halostatue@...>

Okay. I've got Ruby compiling. I'm attempting to get everything in

17 messages 2006/01/05
[#7058] Re: More on VC++ 2005 — nobuyoshi nakada <nobuyoshi.nakada@...> 2006/01/06

Hi,

[#7084] mathn: ugly warnings — hadmut@... (Hadmut Danisch)

Hi,

22 messages 2006/01/10
[#7097] Re: mathn: ugly warnings — Daniel Berger <Daniel.Berger@...> 2006/01/10

Hadmut Danisch wrote:

[#7098] Design contracts and refactoring (was Re: mathn: ugly warnings) — mathew <meta@...> 2006/01/10

Daniel Berger wrote:

[#7118] Re: Design contracts and refactoring (was Re: mathn: ugly warnings) — mathew <meta@...> 2006/01/12

*Dean Wampler *<deanwampler gmail.com> writes:

[#7226] Fwd: Re: Question about massive API changes — "Sean E. Russell" <ser@...>

Hello,

23 messages 2006/01/28
[#7228] Re: Question about massive API changes — Caleb Tennis <caleb@...> 2006/01/28

>

Calling flock with block?

From: Bertram Scharpf <lists@...>
Date: 2006-01-11 23:10:32 UTC
List: ruby-core #7109
Hi,

I don't feel very well when I have to write code like this:

  File.open "somefile", "a" do |f|
    f.flock File::LOCK_EX
    f.print ...
    f.flock File::LOCK_UN
  end

Rather, I would prefer

  File.open "somefile", "a" do |f|
    f.flock File::LOCK_EX do
      f.print ...
    end
  end

My primary problem is how to handle exceptions that occur
while writing to the file.

A solution could be implemented easily by extending the
`rb_file_flock' function in `file.c'. I'm posting the code
below. Maybe you have any idea how to improve it.

What would you think, is it worth a consideration?

Bertram


--->---
static VALUE
file_flock(VALUE obj, VALUE operation)
{
#ifndef __CHECKER__
    OpenFile *fptr;
    int op;

    rb_secure(2);
    op = NUM2INT(operation);
    GetOpenFile(obj, fptr);

    if (fptr->mode & FMODE_WRITABLE) {
        rb_io_flush(obj);
    }
  retry:
    if (flock(fptr->fd, op) < 0) {
        switch (errno) {
          case EAGAIN:
          case EACCES:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
          case EWOULDBLOCK:
#endif
              return Qfalse;
          case EINTR:
#if defined(ERESTART)
          case ERESTART:
#endif
            goto retry;
        }
        rb_sys_fail(fptr->path);
    }
#endif
    return INT2FIX(0);
}

static VALUE
file_flock_un(VALUE obj)
{
#ifndef __CHECKER__
    OpenFile *fptr;

    rb_secure(2);
    GetOpenFile(obj, fptr);

    if (flock(fptr->fd, LOCK_UN) < 0) {
        rb_sys_fail(fptr->path);
    }
#endif
    return INT2FIX(0);
}

static VALUE
rb_file_flock(VALUE obj, VALUE operation)
{
    if (rb_block_given_p()) {
        file_flock(obj, operation);
        rb_ensure( rb_yield, obj, file_flock_un, obj);
    } else
        file_flock(obj, operation);
    return INT2FIX(0);
}
---<---


-- 
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

In This Thread

Prev Next