X-Git-Url: http://git.annexia.org/?p=whenjobs.git;a=blobdiff_plain;f=lib%2Fflock.c;fp=lib%2Fflock.c;h=735e8f8b1b1dfb72b60b98ea7b1263d9faa5dbe9;hp=0000000000000000000000000000000000000000;hb=61cad7bbaf63389b520b695eefdd735bc11a8aa6;hpb=21298f7a45ee536800be5e771438b01089a5cb2c diff --git a/lib/flock.c b/lib/flock.c new file mode 100644 index 0000000..735e8f8 --- /dev/null +++ b/lib/flock.c @@ -0,0 +1,50 @@ +/* whenjobs + * (C) Copyright 2012 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +/* flock_exclusive_nonblocking : file_descr -> unit + * + * The reason we need this function is that OCaml only gives us access + * to the 'fcntl'-style of locking. These locks are not preserved + * over fork(2) (WTF POSIX?) making them pretty much useless. + * Therefore use BSD-style flock instead. + */ +value +whenjobs_flock_exclusive_nonblocking (value fdv) +{ + CAMLparam1 (fdv); + + /* file_descr is opaque, but on un*x-like platforms it's an integer */ + int fd = Int_val (fdv); + + if (flock (fd, LOCK_EX|LOCK_NB) == -1) + caml_failwith (strerror (errno)); + + CAMLreturn (Val_unit); +}