[distcc] Re: cygwin port (distcc V1.1) using native win32 mutexes

Heiko Elger heiko.elger at gmx.de
Tue Feb 11 05:44:11 GMT 2003


Hello Martin,

thanks for your opinion. I'm not very familiar with the diff/patch
utilities. I think using mutexes fpr syncronisation should not be a problem,
cause the owner of the locked mutex can lock it recursively. One possible
problem could be, the unlock of the mutex. Using windows you normally have
to unlock a mutex a often as you've locked it. Or if your process dies od is
killed, windows do that for you and especially when the mutex is locked
while dieing it unblock the next waiting thread/process.

I'm not sure how this behavior works using real linux pthread calls.

Best regards ...



Heiko Elger



Here ist the output using "diff -u"

--------------- snip - snip -------------------------------------

--- distcc-1.1/src/lock.c 2003-01-27 14:27:30.000000000 +0100

+++ distcc-cygwinport-1.1/src/lock.c 2003-02-10 06:00:37.000000000 +0100

@@ -61,9 +61,16 @@

#include <errno.h>

#include <time.h>


+#if defined(__CYGWIN__)

+/* if not defined there were errors while including sub headerfiles */

+# define WIN32_LEAN_AND_MEAN

+# include <w32api/windows.h>

+#endif

+

#include <sys/stat.h>

#include <sys/file.h>


+

#include "distcc.h"

#include "trace.h"

#include "util.h"

@@ -85,6 +92,29 @@


struct dcc_hostdef *dcc_hostdef_local = &_dcc_local;


+

+#if defined(__CYGWIN__)

+/* Convert error number to string */

+/* not reentrant ! */

+static const char* strLastError(DWORD err)

+{

+ static TCHAR msgBuf[100];

+ if (!FormatMessage(

+ FORMAT_MESSAGE_FROM_SYSTEM |

+ FORMAT_MESSAGE_IGNORE_INSERTS,

+ NULL,

+ err,

+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language

+ (LPTSTR) &msgBuf[0],

+ sizeof(msgBuf)/sizeof(msgBuf[0]),

+ NULL ))

+ {

+ msgBuf[0]='\0';

+ }

+ return (char*)&msgBuf[0];

+}

+#endif

+

static char * dcc_make_lock_filename(const char *host, int iter) {

int need_len;

@@ -111,6 +141,18 @@

**/

static int sys_lock(int fd, int block)

{

+#if defined(__CYGWIN__)

+ /****

+ * we have not to Release() the mutex, cause the system release it, when
the

+ * process terminates or is killed

+ *

+ * the same behavior as using file handles (original code)

+ *****/

+ HANDLE h=(HANDLE)fd;

+ DWORD rc;

+ rc = WaitForSingleObject(h, block ? INFINITE : 0);

+ return rc;

+#else

#if defined(F_SETLK)

struct flock lockparam;


@@ -127,6 +169,7 @@

#else

# error "No supported lock method. Please port this code." #endif

+#endif

}



@@ -139,7 +182,36 @@


tempdir = dcc_get_tempdir();

fname = dcc_make_lock_filename(host->hostname, slot);

-

+#if defined(__CYGWIN__)

+ {

+ HANDLE h;

+ DWORD lastError;

+ h = CreateMutex(NULL, FALSE, fname);

+ if (NULL==h)

+ {

+ rs_log_error("failed to creat %s: %s", fname,
strLastError(GetLastError()));

+ goto bomb;

+ }

+ ret = sys_lock((int)h, block);

+ lastError = GetLastError();

+ switch (ret)

+ {

+ case WAIT_OBJECT_0: /* Locked OK */

+ /* fall through */

+ case WAIT_ABANDONED: /* other process ends, killed or terminated -> I got
lock */

+ rs_trace("locked %s", fname);

+ free(fname);

+ return 0;

+ break;

+ case WAIT_TIMEOUT: /* Timeout */

+ rs_trace("%s already locked", fname);

+ break;

+ case WAIT_FAILED: /* unknown error */

+ default:

+ rs_log_error("lock(%lu) %s failed: %s", lastError, fname,
strLastError(lastError));

+ }

+ }

+#else

/* Create if it doesn't exist. We don't actually do anything with

* the file except lock it.*/

if ((fd = open(fname, O_WRONLY|O_CREAT, 0600)) == -1

@@ -165,13 +237,14 @@

rs_trace("%s already locked", fname);

break;

default:

- rs_log_error("lock %s failed: %s", fname, strerror(errno));

+ rs_log_error("lock(%d) %s failed: %s", errno, fname,

+ strerror(errno));

break;

}

-

+#endif

bomb:

if (fd != -1)

dcc_close(fd);

free(fname);

return -1;

+

}

--------------- snip - snip -------------------------------------

"Martin Pool" <mbp at samba.org> schrieb im Newsbeitrag
news:<20030211001150.GC9181 at samba.org>...

> On 10 Feb 2003, Heiko Elger <heiko.elger at gmx.de> wrote:

> > Hello,

> >

> > cause cygwin doesn't support advisory file locking

> > http://cygwin.com/ml/cygwin/2003-01/msg00674.html I do a port in

> > using native mutexes for syncronisation. I use native win32 mutexes

> > cause I don't no much about in using posix syncronisation objects.

>

> Hello Heiko,

>

> Thanks for investigating this, and for the patch.

>

> Please, when you send patches to software, generate them with the -u

> option to diff. This includes some context in the patch that allows

> it to be merged more reliably and easily, even if the source has

> changed since you wrote the patch.

>

> I thought recursive locks would probably be the problem. It might be

> cleaner to change distcc to avoid ever locking a single file twice.

> That should not require major changes and will make Cygwin work with

> no need for Windows-specific code.

>

> Regards

> --

> Martin

> __

> distcc mailing list http://distcc.samba.org/

> To unsubscribe or change options:

> http://lists.samba.org/cgi-bin/mailman/listinfo/distcc

>





More information about the distcc mailing list