if (length == size_t(-1)){
length = strlen(data);
}
- if (length > maxLength)
+ if ((int) length > maxLength)
length = maxLength;
// test if text or binary:
bool isBinary = false;
unsigned char cc;
- for (int ii = 0; ii < length; ii++){
+ for (size_t ii = 0; ii < length; ii++){
if ( (cc = (unsigned char) data[ii]) > 126
|| (cc < ' ' && cc != '\n' && cc != '\r' && cc != '\t')){
isBinary = true;
*
*/
void ReLogger::end(void) {
+ m_mutex.lock();
for (size_t ii = 0; ii < m_appenderListLength; ii++) {
ReAppender* app = m_appenderList[ii];
if (app->accept(m_mode))
app->say(this, NULL);
}
+ m_mutex.unlock();
m_locationOfOpenSayF = 0;
}
* Constructor.
*
* @param maxWaitSec maximal timeout. If reached an error occurres
+ * @param assignments time units for busy wait before waiting. Only used in WIN32<br>
+ * use it for very short critical sections like variable assignment
+ * 0: wait at once<br>
+ * otherwise: wait spinCount time (3 instructions) units before wait
*/
-ReMutex::ReMutex(int location, int maxWaitSec) :
+ReMutex::ReMutex(int location, int maxWaitSec, int assignments) :
#if defined __linux__
m_mutex(),
#elif defined __WIN32__
- m_mutex(0),
+ m_mutex(),
#endif
m_maxWaitSec(maxWaitSec) {
#if defined __linux__
sem_init(&m_mutex, 0, 1);
#elif defined __WIN32__
- m_mutex = CreateMutex(NULL, FALSE, NULL);
+ InitializeCriticalSectionAndSpinCount(&m_mutex, min (assignments, 8) * 0x100);
#endif
}
#if defined __linux__
sem_destroy(&m_mutex);
#elif defined __WIN32__
- CloseMutex(m_mutex);
+ DeleteCriticalSection(&m_mutex);
#endif
}
time.tv_nsec = 0;
rc = sem_timedwait(&m_mutex, &time) == 0;
#elif defined __WIN32__
-
+ int maxCount = sec * 50;
+ int count = 0;
+ while(TryEnterCriticalSection(&m_mutex) != 0 && ++count < maxCount){
+ Sleep(20);
+ }
+ rc = count < maxCount;
#endif
return rc;
}
class ReMutex {
public:
- ReMutex(int location, int maxWaitSec = -1);
+ ReMutex(int location, int maxWaitSec = -1, int spinCount = 0);
virtual ~ReMutex();
public:
bool timedLock(int sec = -1);
#if defined __linux__
sem_wait(&m_mutex);
#elif defined __WIN32__
-#error "mutex in ReMutex missed"
+ EnterCriticalSection(&m_mutex);
#endif
}
inline void unlock() {
#if defined __linux__
sem_post(&m_mutex);
#elif defined __WIN32__
-#error "mutex in ReMutex missed"
+ LeaveCriticalSection(&m_mutex);
#endif
}
private:
#if defined __linux__
sem_t m_mutex;
#elif defined __WIN32__
- HANDLE m_mutex;
+ CRITICAL_SECTION m_mutex;
#endif
int m_maxWaitSec;
};
#if defined __linux__\r
m_threadInfo(),\r
#elif defined __WIN32__\r
- m_threadInfo(UNDEF_HANDLE),\r
+ m_threadInfo(NULL),\r
+ m_osThreadId(0),\r
#endif\r
m_shouldStop(false),\r
m_isStopped(false),\r
#if defined __linux__\r
pthread_kill(m_threadInfo, SIGKILL);\r
#elif defined __WIN32__\r
- KillThread(m_threadInfo);\r
+ TerminateThread(m_threadInfo, 254);\r
#endif\r
}\r
/**\r
#if defined __linux__\r
pthread_exit(NULL);\r
#elif defined __WIN32__\r
- StopThread(m_threadInfo);\r
+ // Nothing to do\r
#endif\r
m_isStopped = true;\r
}\r
}\r
m_mutexThreads.unlock();\r
if (countWaiting > 0)\r
- sleep(1);\r
+ millisecSleep(1000);\r
}\r
// now we kill:\r
countWaiting = 0;\r
}\r
if (countWaiting > 0)\r
// wait 1 msec for end of kill:\r
- usleep(1000);\r
+ millisecSleep(1);\r
// we destroy all threads marked with auto delete:\r
m_mutexThreads.lock();\r
for (int ii = 0; ii < m_maxThreads; ii++) {\r
return NULL;\r
}\r
#elif defined __WIN32__\r
-DWORD WINAPI globalThreadStarterFunction(_In_ LPVOID pParameter) {\r
+DWORD WINAPI globalThreadStarterFunction(_In_ LPVOID pConnection) {\r
ReThread* thread = reinterpret_cast<ReThread*>(pConnection);\r
thread->runAndFinish();\r
return 0;\r
globalThreadStarterFunction, reinterpret_cast<void*>(thread))\r
>= 0;\r
#elif defined __WIN32__\r
- HANDLE threadHandle;\r
- ok = (threadHandle = CreateThread(NULL, 0, globalThreadStarterFunction,\r
- &thread, 0)) != NULL;\r
+ ok = (thread->m_threadInfo = CreateThread(NULL, 0, globalThreadStarterFunction,\r
+ reinterpret_cast<void*>(thread), 0, &thread->m_osThreadId)) != NULL;\r
#endif\r
if (!ok)\r
m_logger->sayF(LOG_ERROR | CAT_PROCESS, LC_START_THREAD_1,\r
rc = true;\r
break;\r
}\r
- usleep(200 * 1000);\r
+ millisecSleep(200);\r
now = time(NULL);\r
} while (now < start + timeoutSec);\r
return rc;\r
inline void setShouldStop(bool value) {\r
m_shouldStop = value;\r
}\r
-private:\r
private:\r
friend class ReThreadPool;\r
void kill();\r
pthread_t m_threadInfo;\r
#elif defined __WIN32__\r
HANDLE m_threadInfo;\r
+ DWORD m_osThreadId;\r
#endif\r
bool m_shouldStop;\r
bool m_isStopped;\r
* The latest sources: https://github.com/republib\r
*/\r
#ifndef REBASE_HPP_\r
+#define min()\r
+#define max()\r
#define REBASE_HPP_\r
#include <stdlib.h>\r
#include <string.h>\r
# define _rmdir(path) rmdir(path)\r
# define OS_SEPARATOR_CHAR '/'\r
# define OS_SEPARATOR "/"\r
+# define millisecSleep(msec) usleep(msec*1000)\r
inline int getLastOSError() {\r
return errno;\r
}\r
# include <direct.h>\r
# include <WinSock2.h>\r
# include <windows.h>\r
+#undef min\r
+#undef max\r
# define _memcmp(t,s,n) memcmp(t,s,n)\r
# define lstat stat\r
+# define millisecSleep(msec) Sleep(msec)\r
# define OS_SEPARATOR_CHAR '\\'\r
# define OS_SEPARATOR "\\"\r
typedef _int64 int64_t;\r
class TCPThread: public ReThread {
public:
TCPThread(const char* task) :
- ReThread(999),
+ ReThread(true),
m_task(task) {
}
public:
client.receive(command, answer);
}
int64_t duration = ReBaseUtils::milliSecSince(start);
- int duration2 = time(NULL) - start2;
+ int duration2 = int(time(NULL) - start2);
if (duration2 == 0)
duration2 = 1;
char msg[256];
int miByte = count * (size / (1024 * 1024));
- snprintf(msg, sizeof msg,
+ _snprintf(msg, sizeof msg,
"%s: %d MiByte in %s/%d sec: %.3f (%.3f) MiByte/sec", direction,
- miByte, ReByteBuffer("").appendMilliSec(duration).str(),
+ miByte, ReByteBuffer("").appendMilliSec((int) duration).str(),
duration2, miByte * 1000.0 / (double) duration,
miByte / (double) duration2);
logger->say(LOG_INFO | CAT_LIB, location, msg);
*
* @param logger logger for the error handling
*/
+#pragma warning( push )\r
+#pragma warning( disable : 4355 )\r
ReTCPClient::ReTCPClient(ReLogger* logger) :
ReTCPConnection(-1, this),
m_logger(logger) {
+#pragma warning( pop )
}
/**
* Destructor.
#if defined __WIN32__
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
- m_logger->sayF(LOG_ERROR | CAT_NETWORK, LC_TCP_CONNECTION_1,
+ loggerOwner->logger()->sayF(LOG_ERROR | CAT_NETWORK, LC_TCP_CONNECTION_1,
i18n("WSAStartup() failed: $1")).arg(errno).arg(getLastOSError()).end();
throw ReException("WSAStartup() failed");
}
-
#endif
}
* @param id an identifier for logging
* @param logger the logger for error handling
*/
+#pragma warning( push )\r
+#pragma warning( disable : 4355 )\r
ReTCPServerConnection::ReTCPServerConnection(int id, ReTCPServer* server) :
ReTCPConnection(id, this),
ReThread(true),
m_server(server) {
+#pragma warning( pop )\r
}
/**
* @param logger the logger for error handling
* @param maxConnections maximal count of threads handling a connection
*/
+#pragma warning( push )\r
+#pragma warning( disable : 4355 )\r
ReTCPServer::ReTCPServer(int port, class ReNetCommandHandler& commandHandler,
ReLogger* logger, int maxConnections) :
ReTCPConnection(0, this),
m_connections(new ReTCPServerConnection*[maxConnections]),
m_handler(commandHandler),
m_logger(logger) {
+#pragma warning( pop )
m_port = port;
memset(m_connections, 0, maxConnections * sizeof *m_connections);
}
* @param port port for listening
* @param logger logger for error handling
*/
+#pragma warning( push )\r
+#pragma warning( disable : 4355 )\r
ReTCPEchoServer::ReTCPEchoServer(int port, ReLogger* logger) :
ReTCPServer(port, *this, logger),
ReNetCommandHandler() {
+#pragma warning( pop )
}
/**
* Destructor.
* directory will be added as argument\r
* @param logger logger for error messages\r
*/\r
+#pragma warning( push )\r
+#pragma warning( disable : 4355 )\r
ReTool::ReTool(const char* usage[], const char* example[], int minArguments,\r
int reservedFirst, int reservedLast, bool addCurrentDirIfNoArguments,\r
ReLogger* logger) :\r
m_filter(),\r
m_start(time(NULL)),\r
m_logger(logger) {\r
+#pragma warning( pop )\r
}\r
\r
/**\r
ReTCPClient client(m_logger);\r
if (client.connect(ip, port)) {\r
time_t start = time(NULL);\r
- int64_t millisec;\r
ReByteBuffer message;\r
message.appendChar('x', bufferSize);\r
time_t lastPrint = start;\r