#include "str.h"
#include "msg.h"
#include "random.h"
-#if defined(VMX86_VMX)
-#include "vthreadBase.h"
-#endif
#include "uuid.h"
#include "config.h"
#include "posix.h"
}
*mode = fileData.fileMode;
+
#if defined(_WIN32)
/*
- * On Win32 implementation of FileAttributes does not return execution
+ * On Win32 implementation of FileAttributes does not return execution
* bit.
*/
*mode |= S_IXUSR;
}
#endif
+
return TRUE;
}
}
break;
}
+
if (!next) {
break;
}
path = next + 1;
}
+
return newPath;
}
*/
char *
-File_ExpandAndCheckDir(const char *dirName)
+File_ExpandAndCheckDir(const char *dirName) // IN:
{
- char *edirName;
-
if (dirName != NULL) {
- edirName = Util_ExpandString(dirName);
+ char *edirName = Util_ExpandString(dirName);
if ((edirName != NULL) && FileIsWritableDir(edirName)) {
size_t len = strlen(edirName) - 1;
}
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * FileSimpleRandom --
+ *
+ * Return a random number in the range of 0 and 2^32-1.
+ *
+ * This isn't thread safe but it's more than good enough for the
+ * purposes required of it.
+ *
+ * Results:
+ * Random number is returned.
+ *
+ * Side Effects:
+ * None.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+uint32
+FileSimpleRandom(void)
+{
+ static Atomic_Ptr atomic; /* Implicitly initialized to NULL. --mbellon */
+ char *context;
+
+ context = Atomic_ReadPtr(&atomic);
+
+ if (UNLIKELY(context == NULL)) {
+ void *p;
+ uint32 value;
+
+ /*
+ * Threads will hash up this RNG - this isn't officially thread safe
+ * which is just fine - but ensure that different processes have
+ * different answer streams.
+ */
+
+#if defined(_WIN32)
+ value = GetCurrentProcessId();
+#else
+ value = getpid();
+#endif
+
+ p = Random_QuickSeed(value);
+
+ if (Atomic_ReadIfEqualWritePtr(&atomic, NULL, p)) {
+ free(p);
+ }
+
+ context = Atomic_ReadPtr(&atomic);
+ ASSERT(context);
+ }
+
+ return Random_Quick(context);
+}
+
+
/*
*----------------------------------------------------------------------
*
if (variance == 0) {
msecActualSleepTime = msecMinSleepTime;
} else {
- int sample;
- float fpRand;
- static int32 rng; // implicitly 0
-
- if (rng == 0) {
- int pid;
-
-#if defined(_WIN32)
- pid = GetCurrentProcessId();
-#else
- pid = getpid();
-#endif
-
- rng = (pid << 16);
-
-#if defined(VMX86_VMX)
- rng |= VThread_CurID();
-#endif
- }
-
- sample = FastRand(rng);
- fpRand = ((float) sample) / ((float) MAX_INT32);
- rng = sample;
+ float fpRand = ((float) FileSimpleRandom()) / ((float) ~((uint32) 0));
msecActualSleepTime = msecMinSleepTime + (uint32) (fpRand * variance);
}
-#if defined(_WIN32)
- Sleep(msecActualSleepTime);
-#else
usleep(1000 * msecActualSleepTime);
-#endif
return msecActualSleepTime;
}
#define FileCreateDirectory(a) FileCreateDirectoryRetry((a), 0)
#define FileRemoveDirectory(a) FileRemoveDirectoryRetry((a), 0)
-#define FILE_MAX_WAIT_TIME 2000
+#define FILE_MAX_WAIT_TIME_MS 2000 // maximum wait time in milliseconds
#define FileListDirectoryRobust(a, b) \
- FileListDirectoryRetry((a), FILE_MAX_WAIT_TIME, (b))
+ FileListDirectoryRetry((a), FILE_MAX_WAIT_TIME_MS, (b))
#define FileAttributesRobust(a, b) \
- FileAttributesRetry((a), FILE_MAX_WAIT_TIME, (b))
+ FileAttributesRetry((a), FILE_MAX_WAIT_TIME_MS, (b))
#define FileRenameRobust(a, b) \
- FileRenameRetry((a), (b), FILE_MAX_WAIT_TIME)
+ FileRenameRetry((a), (b), FILE_MAX_WAIT_TIME_MS)
#define FileDeletionRobust(a, b) \
- FileDeletionRetry((a), (b), FILE_MAX_WAIT_TIME)
+ FileDeletionRetry((a), (b), FILE_MAX_WAIT_TIME_MS)
#define FileCreateDirectoryRobust(a) \
- FileCreateDirectoryRetry((a), FILE_MAX_WAIT_TIME)
+ FileCreateDirectoryRetry((a), FILE_MAX_WAIT_TIME_MS)
#define FileRemoveDirectoryRobust(a) \
- FileRemoveDirectoryRetry((a), FILE_MAX_WAIT_TIME)
+ FileRemoveDirectoryRetry((a), FILE_MAX_WAIT_TIME_MS)
#else
EXTERN char *FilePosixGetBlockDevice(char const *path);
EXTERN uint32 FileSleeper(uint32 msecMinSleepTime,
uint32 msecMaxSleepTime);
+EXTERN uint32 FileSimpleRandom(void);
+
EXTERN const char *FileLockGetMachineID(void);
EXTERN char *FileLockGetExecutionID(void);
}
-/*
- *-----------------------------------------------------------------------------
- *
- * SimpleRandomNumber --
- *
- * Return a random number in the range of 0 and 2^16-1.
- *
- * Results:
- * Random number is returned.
- *
- * Side Effects:
- * None.
- *
- *-----------------------------------------------------------------------------
- */
-
-static uint32
-SimpleRandomNumber(const char *machineID, // IN:
- const char *executionID) // IN:
-{
- static Atomic_Ptr atomic; /* Implicitly initialized to NULL. --mbellon */
- char *context;
-
- context = Atomic_ReadPtr(&atomic);
-
- if (context == NULL) {
- void *p;
- uint32 value = 0;
-
- /*
- * Use the machineID and executionID to hopefully start each machine
- * and process/thread at a different place in the answer stream.
- */
-
- while (*machineID) {
- value += *machineID++;
- }
-
- while (*executionID) {
- value += *executionID++;
- }
-
- p = Random_QuickSeed(value);
-
- if (Atomic_ReadIfEqualWritePtr(&atomic, NULL, p)) {
- free(p);
- }
-
- context = Atomic_ReadPtr(&atomic);
- ASSERT(context);
- }
-
- return (Random_Quick(context) >> 8) & 0xFFFF;
-}
-
-
/*
*-----------------------------------------------------------------------------
*
*/
static int
-CreateEntryDirectory(const char *machineID, // IN:
- const char *executionID, // IN:
- ConstUnicode lockDir, // IN:
+CreateEntryDirectory(ConstUnicode lockDir, // IN:
Unicode *entryDirectory, // OUT:
Unicode *entryFilePath, // OUT:
Unicode *memberFilePath, // OUT:
}
/* There is a small chance of collision/failure; grab stings now */
- randomNumber = SimpleRandomNumber(machineID, executionID);
+ randomNumber = (FileSimpleRandom() >> 8) & 0xFFFF;
*memberName = Unicode_Format("M%05u%s", randomNumber, FILELOCK_SUFFIX);
* entry and member path names.
*/
- *err = CreateEntryDirectory(myValues.machineID, myValues.executionID,
- lockDir,
- &entryDirectory, &entryFilePath,
+ *err = CreateEntryDirectory(lockDir, &entryDirectory, &entryFilePath,
&memberFilePath, &myValues.memberName);
switch (*err) {
LOG(1, ("%s on %s (%s, %s).\n", __FUNCTION__, UTF8(pathName),
myValues.machineID, myValues.executionID));
- err = CreateEntryDirectory(myValues.machineID, myValues.executionID,
- lockDir,
- &entryDirectory, &entryFilePath,
+ err = CreateEntryDirectory(lockDir, &entryDirectory, &entryFilePath,
&memberFilePath, &myValues.memberName);
if (err != 0) {