|
|||||||||||
|
[patch 3/8] portlib for ndb on win32
From: <stewart(at)flamingspork.com>
Date: Wed Sep 19 2007 - 03:46:44 EDT
--- a/ndb/include/kernel/ndb_limits.h 2007-08-29 07:25:53 +0000 +++ b/ndb/include/kernel/ndb_limits.h 2007-09-08 11:07:00 +0000@@ -15,8 +15,8 @@ #ifndef NDB_LIMITS_H -#include #define RNIL 0xffffff00
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32) -#include #define PATH_MAX 256 #define DIR_SEPARATOR "\\" #define MYSQLCLUSTERDIR "c:\\mysql\\mysql-cluster" @@ -32,6 +32,7 @@ #pragma warning(disable: 4503 4786) #else #undef NDB_WIN32 +#undef NDB_WIN #define DIR_SEPARATOR "/" #endif
-#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(_WIN64) -#define NDB_SIZEOF_CHARP SIZEOF_CHARP -#define NDB_SIZEOF_CHAR SIZEOF_CHAR -#define NDB_SIZEOF_SHORT 2 -#define NDB_SIZEOF_INT SIZEOF_INT -#define NDB_SIZEOF_LONG SIZEOF_LONG -#define NDB_SIZEOF_LONG_LONG SIZEOF_LONG_LONG -typedef unsigned __int64 Uint64; -typedef signed __int64 Int64; -#else#define NDB_SIZEOF_LONG @NDB_SIZEOF_LONG@ #define NDB_SIZEOF_LONG_LONG @NDB_SIZEOF_LONG_LONG@ + +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(_WIN64) +typedef unsigned __int64 Uint64; +typedef signed __int64 Int64; +#define ssize_t SSIZE_T +#ifndef my_socket_defined +#define my_socket SOCKET +#define my_socket_defined +#endif +#else typedef unsigned long long Uint64; typedef signed long long Int64; #endif
+typedef int(*F)(void*); +extern int +NdbDaemonService_Make(const char*argv0,const char* lockfile, const char* logfile, unsigned flags, F restofmain); + /* * Test if the daemon is running (file is locked). * lockfile the "pid file" --- a/ndb/include/portlib/NdbMutex.h 2007-08-29 07:25:53 +0000 +++ b/ndb/include/portlib/NdbMutex.h 2007-09-08 13:35:49 +0000@@ -18,11 +18,6 @@ #include <ndb_global.h> -#ifdef NDB_WIN32 -#include extern "C" { #endif @@ -30,10 +25,8 @@ #if defined NDB_OSE || defined NDB_SOFTOSE #include <ose.h> typedef SEMAPHORE NdbMutex; -#elif defined NDB_WIN32 -typedef CRITICAL_SECTION NdbMutex; #else -#include <pthread.h> +#include <my_pthread.h> typedef pthread_mutex_t NdbMutex; #endif
/**
#elif defined NDB_WIN32 -/** - * Include files needed - */ -#include #define InetErrno WSAGetLastError() #define EWOULDBLOCK WSAEWOULDBLOCK #define NDB_SOCKET_TYPE SOCKET
+typedef int(*NdbOutF)(char*); +extern NdbOutF ndbout_svc; + inline NdbOut& NdbOut::operator<<(NdbOut& (* _f)(NdbOut&)) { (* _f)(*this); return * this;
-
if (p_cond == NULL || p_mutex == NULL) return 1;
-
-#ifdef HAVE_CLOCK_GETTIME
- clock_gettime(CLOCK_REALTIME, &abstime);
-#else
- {
- struct timeval tick_time;
- gettimeofday(&tick_time, 0);
- abstime.tv_sec = tick_time.tv_sec;
- abstime.tv_nsec = tick_time.tv_usec * 1000;
- }
-#endif
-
- if(msecs >= 1000){
- secs = msecs / 1000;
- msecs = msecs % 1000;
- }
-
- abstime.tv_sec += secs;
- abstime.tv_nsec += msecs * 1000000;
- if (abstime.tv_nsec >= 1000000000) {
- abstime.tv_sec += 1;
- abstime.tv_nsec -= 1000000000;
- }
-
+ + set_timespec_nsec(abstime,msecs*1000000) ; result = pthread_cond_timedwait(&p_cond->cond, p_mutex, &abstime);
return result;
return 1;
result = pthread_cond_broadcast(&p_cond->cond);
-
return result;
buf= NdbConfig_AllocHomePath(len+strlen(tmp_buf));
+#ifdef NDB_WIN32
+#define F_ULOCK _LK_UNLCK
+#define F_LOCK _LK_LOCK
+#define F_TLOCK _LK_NBLCK
+#define lockf(fd,mode,size) _locking((fd),(mode),(size))
+#endif
+
+int restofmake(void*);
+struct Dstate
+{ int lockfd,logfd;
+ const char*lockfile,*logfile;
+ F restofmain;
+} dstate;
+
+int nothing(void*p){return 0; }
+
int NdbDaemon_Make(const char* lockfile, const char* logfile, unsigned flags)
+{ return NdbDaemonService_Make("",lockfile,logfile,flags,nothing); }
+
+int
+NdbDaemonService_Make(const char*argv0,const char* lockfile, const char* logfile, unsigned flags, F restofmain)
{
- int lockfd = -1, logfd = -1, n; + int n;
char buf[64];
+ dstate.lockfd = -1, dstate.logfd = -1; + dstate.lockfile=lockfile, dstate.logfile=logfile; + dstate.restofmain=restofmain; (void)flags; /* remove warning for unused parameter */ /* Check that we have write access to lock file */
- assert(lockfile != NULL);
- lockfd = open(lockfile, O_CREAT|O_RDWR, 0644);
- if (lockfd == -1) {
+ assert(dstate.lockfile != NULL);
+ fopen(dstate.lockfile,"w");
+ dstate.lockfd = open(dstate.lockfile, O_CREAT|O_RDWR, 0644); + if (dstate.lockfd == -1) {
NdbDaemon_ErrorCode = errno;
+ basestring_snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
+ "%s: open for write failed: %s", dstate.lockfile, strerror(errno));
+
return -1;
} /* Read any old pid from lock file */ buf[0] = 0; - n = read(lockfd, buf, sizeof(buf)); + n = read(dstate.lockfd, buf, sizeof(buf));
if (n < 0) {
+ "%s: read failed: %s", dstate.lockfile, strerror(errno));
return -1;
} NdbDaemon_DaemonPid = atol(buf); - if (lseek(lockfd, 0, SEEK_SET) == -1) { + if (lseek(dstate.lockfd, 0, SEEK_SET) == -1) {
NdbDaemon_ErrorCode = errno;
+ "%s: lseek failed: %s", dstate.lockfile, strerror(errno));
return -1;
} #ifdef F_TLOCK /* Test for lock before becoming daemon */ - if (lockf(lockfd, F_TLOCK, 0) == -1) + if (lockf(dstate.lockfd, F_TLOCK, 0) == -1) {
if (errno == EACCES || errno == EAGAIN) { /* results may vary */
- snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
- "%s: already locked by pid=%ld", lockfile, NdbDaemon_DaemonPid);
+ basestring_snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
+ "%s: already locked by pid=%ld", dstate.lockfile, NdbDaemon_DaemonPid);
return -1;
}
NdbDaemon_ErrorCode = errno;
- snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
- "%s: lock test failed: %s", lockfile, strerror(errno));
+ basestring_snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
+ "%s: lock test failed: %s", dstate.lockfile, strerror(errno));
return -1;
} #endif /* Test open log file before becoming daemon */
- if (logfile != NULL) {
- logfd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0644);
- if (logfd == -1) {
+ if (dstate.logfile != NULL) {
+ dstate.logfd = open(dstate.logfile, O_CREAT|O_WRONLY|O_APPEND, 0644); + if (dstate.logfd == -1) {
NdbDaemon_ErrorCode = errno;
- snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
- "%s: open for write failed: %s", logfile, strerror(errno));
+ basestring_snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
+ "%s: open for write failed: %s", dstate.logfile, strerror(errno));
return -1;
}
} #ifdef F_TLOCK - if (lockf(lockfd, F_ULOCK, 0) == -1) + if (lockf(dstate.lockfd, F_ULOCK, 0) == -1)
{
+ "%s: fail to unlock", dstate.lockfile);
return -1;
} #endif - - /* Fork */ +#ifndef NDB_WIN32
n = fork();
"fork failed: %s", strerror(errno));
return -1;
} @@ -100,66 +123,67 @@
if (n != 0) {
- /* Running in child process */
- NdbDaemon_DaemonPid = getpid();
- /* Lock the lock file (likely to succeed due to test above) */
- if (lockf(lockfd, F_LOCK, 0) == -1) {
- NdbDaemon_ErrorCode = errno;
- snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
- "%s: lock failed: %s", lockfile, strerror(errno));
- return -1;
+#endif
+ {
+ char buf[64];int n;
+ /* Running in child process */
+ NdbDaemon_DaemonPid = NdbHost_GetProcessId();
+ /* Lock the lock file (likely to succeed due to test above) */
+ if (lockf(dstate.lockfd, F_LOCK, 0) == -1) {
+ NdbDaemon_ErrorCode = errno;
+ basestring_snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
+ "%s: lock failed: %s", dstate.lockfile, strerror(errno));
+ return -1;
}
/* Become process group leader */
"setsid failed: %s", strerror(errno));
return -1;
} /* Write pid to lock file */ - if (ftruncate(lockfd, 0) == -1) {
+ if (ftruncate(dstate.lockfd, 0) == -1) {
+#else
+ if (_chsize(dstate.lockfd, 0) == -1) {
+#endif
NdbDaemon_ErrorCode = errno;
- snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
- "%s: ftruncate failed: %s", lockfile, strerror(errno));
+ basestring_snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
+ "%s: ftruncate failed: %s", dstate.lockfile, strerror(errno));
return -1;
} sprintf(buf, "%ld\n", NdbDaemon_DaemonPid); n = strlen(buf); - if (write(lockfd, buf, n) != n) { + if (write(dstate.lockfd, buf, n) != n) {
NdbDaemon_ErrorCode = errno;
+ "%s: write failed: %s", dstate.lockfile, strerror(errno));
return -1;
} /* Do input/output redirections (assume fd 0,1,2 not in use) */ close(0); +#ifndef NDB_WIN32 open("/dev/null", O_RDONLY);
- if (logfile != 0) {
- dup2(logfd, 1);
- dup2(logfd, 2);
- close(logfd);
+#else
+ open("nul:", O_RDONLY);
+#endif
+ if (dstate.logfile != 0) {
+ dup2(dstate.logfd, 1);
+ dup2(dstate.logfd, 2); + close(dstate.logfd);
}
- return 0;
-}
-
-#if 0
-int
-NdbDaemon_Make(const char* lockfile, const char* logfile, unsigned flags)
-{
- /* Fail */
- snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
- "Daemon mode not implemented");
- return -1;
-}
-#endif
+ return dstate.restofmain(0);
+ } +} #ifdef NDB_DAEMON_TEST
fprintf(stderr, "NdbDaemon_Make: %s\n", NdbDaemon_ErrorText);
return 1;
}
int NdbHost_GetHostName(char* buf)
- if (gethostname(buf, MAXHOSTNAMELEN) != 0)
- {
- return -1;
- }
+ if(gethostname(buf, MAXHOSTNAMELEN)) + return -1;
return 0;
+#ifdef NDB_WIN +#include int NdbHost_GetProcessId(void) { return getpid();
#include <NdbThread.h> +#include #include <NdbMutex.h> #include <NdbMem.h>
// test 2 variables and funcs
fail("TEST2", "Failed to unlock mutex");
int returnvalue = arg1;
int
+NdbSleep_MilliSleep(int milliseconds)
+{
+ my_sleep(milliseconds);
return 0; -#if 0 - int result = 0; - struct timespec sleeptime; - sleeptime.tv_sec = milliseconds / 1000; - sleeptime.tv_nsec = (milliseconds - (sleeptime.tv_sec * 1000)) * 1000000; - result = nanosleep(&sleeptime, NULL); - return result; -#endif } int
-NdbSleep_SecSleep(int seconds){
- int result = 0;
- result = sleep(seconds);
- return result;
+NdbSleep_SecSleep(int seconds)
+{ + return NdbSleep_MilliSleep(seconds*1000); }
if(FD_ISSET(sock,&errorfds))
struct NdbThread
pthread_t thread;
my_thread_init();
if (g_ndb_shm_signum)
{
@@ -60,6 +61,7 @@
sigfillset(&mask);
pthread_sigmask(SIG_BLOCK, &mask, 0);
}
+#endif
{
void *ret;
--- a/ndb/src/common/portlib/NdbTick.c 2007-08-29 07:25:53 +0000
+++ b/ndb/src/common/portlib/NdbTick.c 2007-09-09 09:06:04 +0000
@@ -24,7 +24,26 @@
#define MILLISEC_PER_NANOSEC 1000000 -#ifdef HAVE_CLOCK_GETTIME
+#ifdef NDB_WIN32
+NDB_TICKS NdbTick_CurrentMillisecond(void)
+{
+ NDB_TICKS sec;Uint32 usec;
+ NdbTick_CurrentMicrosecond(&sec,&usec);
+ return sec*1000+usec/1000;
+}
+
+int
+NdbTick_CurrentMicrosecond(NDB_TICKS * secs, Uint32 * micros)
+{
+ LARGE_INTEGER liCount, liFreq;
+ QueryPerformanceCounter(&liCount);
+ QueryPerformanceFrequency(&liFreq);
+ *secs = liCount.QuadPart / liFreq.QuadPart;
+ liCount.QuadPart -= *secs * liFreq.QuadPart;
+ *micros = (liCount.QuadPart*1000000) / liFreq.QuadPart;
+ return 0;
+}
+#elif defined(HAVE_CLOCK_GETTIME)
NDB_TICKS NdbTick_CurrentMillisecond(void) { struct timespec tick_time; -- Stewart Smith -- MySQL Code Commits Mailing List For list archives: http://lists.mysql.com/commits To unsubscribe: http://lists.mysql.com/commits?unsub=lists@pantek.comReceived on Wed Sep 19 04:12:08 2007 This archive was generated by hypermail 2.1.8 : Sun Oct 07 2007 - 09:32:57 EDT |
||||||||||
|
|||||||||||