Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Why no LockFile() in Win32API::File ?

by BrowserUk (Patriarch)
on Apr 11, 2006 at 01:37 UTC ( [id://542418]=note: print w/replies, xml ) Need Help??


in reply to Why no LockFile() in Win32API::File ?

However, if you need the emulation, then it already exists in and is exported by perl58.dll under the name win32_flock. It can be access through Win32::API::Prototype as follows:

Update: Changed use to require + import.

#! perl -slw use strict; use Fcntl qw[ :flock ]; $|=1; if( $^O eq 'MSWin32' ) { { local $^W; require Win32::API::Prototype; Win32::API::Prototype->import( 'ApiLink' ); } ApiLink( 'perl58', 'int win32_flock(int fd, int oper)' ) or die $^ +E; our $flock; *flock = *win32_flock = *win32_flock; } die 'Already running' unless flock( DATA, LOCK_EX ); sleep 10; flock( DATA, LOCK_UN ) or die $!; __DATA__ stuff

However, in my tests just now, when running a second copy of the above script, the call to flock( LOCK_EX ), causes the script to silently terminate, so that may not be so useful after all.

I'm not sure whether

  1. I'm simply using the call incorrectly (which is possible as I've never needed flock());
  2. or if there is a bug in the implementation (at win32.c line 2246 in the 5.8.8 sources);
  3. Or there is a bug in the transition between perl and C via Win32::API(::Prototype);

but maybe this and the pointer to the source will let someone who knows what they are doing work out which and, either point out my stupidity or raise a bug against the responsible component?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Why no LockFile() in Win32API::File ?
by demerphq (Chancellor) on Apr 11, 2006 at 08:30 UTC

    Isnt Win32_flock() used for perls flock() function?

    ---
    $world=~s/war/peace/g

      Could well be...:). As I've never had a use for it, and the OP was complaining of it's absence, I assumed it wasn't available. As I was in win32.c at the time and saw win32_flock() right there, I thought I would pass the info along.

      Now, perhaps you can explain why my attempt to use it doesn't work?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Now, perhaps you can explain why my attempt to use it doesn't work?

        I think its because DATA and *DATA arent filedescriptors (int fd). In order to make that work you have to get the fd for the perl filehandle. The way I understand it is you have three handles, the perl level one, the unix file descriptor one, and the win32 handle under that. Im not sure if (or rather to what extent) the unix level one is supported by the Win32 API and what is provided by perl for portability reasons, but obviously the win32 handle one doesn't exist on UNIX at all.

        Bear in mind i havent inspected the code before replying to this, so its just conjecture based on recollection.

        ---
        $world=~s/war/peace/g

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://542418]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-26 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found