Skip to content

Commit

Permalink
Clean up ifdefs that select between Windows and Unix code. Assume tha…
Browse files Browse the repository at this point in the history
…t pthread is present unless we're building for Windows.
  • Loading branch information
dcjm committed Apr 9, 2019
1 parent 8d532ab commit 649c3a6
Show file tree
Hide file tree
Showing 29 changed files with 201 additions and 233 deletions.
4 changes: 2 additions & 2 deletions libpolymain/polystub.c
@@ -1,7 +1,7 @@
/*
Title: polystub.c
Copyright (c) 2006, 2015 David C.J. Matthews
Copyright (c) 2006, 2015, 2019 David C.J. Matthews
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -30,7 +30,7 @@

#include "../polyexports.h"

#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
return PolyWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow, &poly_exports);
Expand Down
14 changes: 7 additions & 7 deletions libpolyml/diagnostics.cpp
Expand Up @@ -63,12 +63,12 @@
#define ASSERT(x)
#endif

#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
#include "winstartup.h"
#include "winguiconsole.h"
#endif

#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
#include <tchar.h>
#endif

Expand All @@ -91,7 +91,7 @@ void Exit(const char *msg, ...)
va_end(vl);
fprintf(polyStdout, "\n");
fflush(polyStdout);
#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
if (useConsole)
{
MessageBox(hMainWindow, _T("Poly/ML has exited"), _T("Poly/ML"), MB_OK);
Expand All @@ -112,7 +112,7 @@ void Crash(const char *msg, ...)
fprintf(polyStdout, "\n");
fflush(polyStdout);

#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
if (useConsole)
{
MessageBox(hMainWindow, _T("Poly/ML has exited"), _T("Poly/ML"), MB_OK);
Expand Down Expand Up @@ -140,7 +140,7 @@ void ExitWithError(const char *msg, int err)

fputs("\n", polyStdout);
fflush(polyStdout);
#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
if (useConsole)
{
MessageBox(hMainWindow, _T("Poly/ML has exited"), _T("Poly/ML"), MB_OK);
Expand All @@ -149,7 +149,7 @@ void ExitWithError(const char *msg, int err)
exit(1);
}

#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
// Default is to log with OutputDebugString
static FILE *logStream = NULL;
#else
Expand Down Expand Up @@ -178,7 +178,7 @@ void Log(const char *msg, ...)
va_list vl;
va_start(vl, msg);
if (logStream) vfprintf(logStream, msg, vl);
#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
char buff[1024];
if (_vsnprintf(buff, sizeof(buff), msg, vl) > 0)
::OutputDebugStringA(buff);
Expand Down
6 changes: 3 additions & 3 deletions libpolyml/errors.cpp
@@ -1,6 +1,6 @@
/*
Title: Error Messages.
Copyright (c) 2012, 2017 David C. J. Matthews
Copyright (c) 2012, 2017, 2019 David C. J. Matthews
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -25,7 +25,7 @@
#error "No configuration file"
#endif

#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
#include <winsock2.h>
#endif

Expand All @@ -48,7 +48,7 @@ static struct {
const char* errorString;
} errortable[] =
{
#if(!defined(_WIN32) || defined(__CYGWIN__))
#if (!defined(_WIN32))
#ifdef EPERM
{ EPERM, "EPERM" },
#endif
Expand Down
8 changes: 4 additions & 4 deletions libpolyml/exporter.cpp
Expand Up @@ -50,7 +50,7 @@
#include <stdlib.h>
#endif

#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
#include <tchar.h>
#else
#define _T(x) x
Expand Down Expand Up @@ -83,7 +83,7 @@
#include "machoexport.h"
#endif

#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
#define NOMEMORY ERROR_NOT_ENOUGH_MEMORY
#define ERRORNUMBER _doserrno
#else
Expand Down Expand Up @@ -703,7 +703,7 @@ Handle exportNative(TaskData *taskData, Handle args)
{
#ifdef HAVE_PECOFF
// Windows including Cygwin
#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
const TCHAR *extension = _T(".obj"); // Windows
#else
const char *extension = ".o"; // Cygwin
Expand Down Expand Up @@ -749,7 +749,7 @@ POLYUNSIGNED PolyExport(PolyObject *threadId, PolyWord fileName, PolyWord root)
try {
#ifdef HAVE_PECOFF
// Windows including Cygwin
#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
const TCHAR *extension = _T(".obj"); // Windows
#else
const char *extension = ".o"; // Cygwin
Expand Down
40 changes: 17 additions & 23 deletions libpolyml/gctaskfarm.cpp
@@ -1,12 +1,11 @@
/*
Title: Task farm for Multi-Threaded Garbage Collector
Copyright (c) 2010 David C. J. Matthews
Copyright (c) 2010, 2019 David C. J. Matthews
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
Expand Down Expand Up @@ -59,18 +58,14 @@ GCTaskFarm::GCTaskFarm(): workLock("GC task farm work")
workQueue = 0;
terminate = false;
threadCount = activeThreadCount = 0;
#if (defined(HAVE_PTHREAD_H) || defined(HAVE_WINDOWS_H))
threadHandles = 0;
#endif
}

GCTaskFarm::~GCTaskFarm()
{
Terminate();
free(workQueue);
#if (defined(HAVE_PTHREAD_H) || defined(HAVE_WINDOWS_H))
free(threadHandles);
#endif
}


Expand All @@ -80,28 +75,26 @@ bool GCTaskFarm::Initialise(unsigned thrdCount, unsigned qSize)
if (!waitForWork.Init(0, thrdCount)) return false;
workQueue = (queue_entry*)calloc(qSize, sizeof(queue_entry));
if (workQueue == 0) return false;
#if ((!defined(_WIN32) || defined(__CYGWIN__)) && defined(HAVE_PTHREAD_H))
#if (!defined(_WIN32))
queueSize = qSize;
threadHandles = (pthread_t*)calloc(thrdCount, sizeof(pthread_t));
if (threadHandles == 0) return false;
#elif defined(HAVE_WINDOWS_H)
#else
queueSize = qSize;
threadHandles = (HANDLE*)calloc(thrdCount, sizeof(HANDLE));
if (threadHandles == 0) return false;
#else
queueSize = 0;
#endif
// Create the worker threads.
for (unsigned i = 0; i < thrdCount; i++) {
// Fork a thread
#if ((!defined(_WIN32) || defined(__CYGWIN__)) && defined(HAVE_PTHREAD_H))
#if (!defined(_WIN32))
// Create a thread that isn't joinable since we don't want to wait
// for it to finish.
pthread_t pthreadId;
bool isError = pthread_create(&pthreadId, NULL, WorkerThreadFunction, this) != 0;
if (isError) break;
threadHandles[threadCount++] = pthreadId;
#elif defined(HAVE_WINDOWS_H)
#else
DWORD dwThrdId; // Have to provide this although we don't use it.
HANDLE threadHandle =
CreateThread(NULL, 0, WorkerThreadFunction, this, 0, &dwThrdId);
Expand All @@ -119,10 +112,10 @@ void GCTaskFarm::Terminate()
// Increment the semaphore by the number of threads to release them all.
for (unsigned i = 0; i < threadCount; i++) waitForWork.Signal();
// Wait for the threads to terminate.
#if ((!defined(_WIN32) || defined(__CYGWIN__)) && defined(HAVE_PTHREAD_H))
#if (!defined(_WIN32))
for (unsigned j = 0; j < threadCount; j++)
pthread_join(threadHandles[j], NULL);
#elif defined(HAVE_WINDOWS_H)
#else
if (threadCount != 0)
WaitForMultipleObjects(threadCount, threadHandles, TRUE, 10000);
#endif
Expand All @@ -134,7 +127,8 @@ bool GCTaskFarm::AddWork(gctask work, void *arg1, void *arg2)
bool wantSignal = false;
{
PLocker l(&workLock);
if (queuedItems == queueSize) return false; // Queue is full
if (queuedItems == queueSize)
return false; // Queue is full
workQueue[queueIn].task = work;
workQueue[queueIn].arg1 = arg1;
workQueue[queueIn].arg2 = arg2;
Expand All @@ -157,7 +151,7 @@ void GCTaskFarm::AddWorkOrRunNow(gctask work, void *arg1, void *arg2)
void GCTaskFarm::ThreadFunction()
{
GCTaskId myTaskId;
#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
DWORD startActive = GetTickCount();
#else
struct timeval startTime;
Expand Down Expand Up @@ -198,7 +192,7 @@ void GCTaskFarm::ThreadFunction()

if (debugOptions & DEBUG_GCTASKS)
{
#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
Log("GCTask: Thread %p blocking after %u milliseconds\n", &myTaskId,
GetTickCount() - startActive);
#else
Expand All @@ -216,7 +210,7 @@ void GCTaskFarm::ThreadFunction()
// We've been woken up
if (debugOptions & DEBUG_GCTASKS)
{
#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
startActive = GetTickCount();
#else
gettimeofday(&startTime, NULL);
Expand All @@ -231,14 +225,14 @@ void GCTaskFarm::ThreadFunction()
workLock.Unlock();
}

#if ((!defined(_WIN32) || defined(__CYGWIN__)) && defined(HAVE_PTHREAD_H))
#if (!defined(_WIN32))
void *GCTaskFarm::WorkerThreadFunction(void *parameter)
{
GCTaskFarm *t = (GCTaskFarm *)parameter;
t->ThreadFunction();
return 0;
}
#elif defined(HAVE_WINDOWS_H)
#else
DWORD WINAPI GCTaskFarm::WorkerThreadFunction(void *parameter)
{
GCTaskFarm *t = (GCTaskFarm *)parameter;
Expand All @@ -250,7 +244,7 @@ DWORD WINAPI GCTaskFarm::WorkerThreadFunction(void *parameter)
// Wait until the queue is empty.
void GCTaskFarm::WaitForCompletion(void)
{
#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
DWORD startWait;
if (debugOptions & DEBUG_GCTASKS)
startWait = GetTickCount();
Expand All @@ -266,7 +260,7 @@ void GCTaskFarm::WaitForCompletion(void)

if (debugOptions & DEBUG_GCTASKS)
{
#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
Log("GCTask: Threads completed after %u milliseconds\n", GetTickCount()-startWait);
#else
struct timeval endWait;
Expand Down
6 changes: 3 additions & 3 deletions libpolyml/gctaskfarm.h
@@ -1,7 +1,7 @@
/*
Title: Task farm for Multi-Threaded Garbage Collector
Copyright (c) 2010-12 David C. J. Matthews
Copyright (c) 2010-12, 2019 David C. J. Matthews
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -77,10 +77,10 @@ class GCTaskFarm {

void ThreadFunction(void);

#if ((!defined(_WIN32) || defined(__CYGWIN__)) && defined(HAVE_PTHREAD_H))
#if (!defined(_WIN32))
static void *WorkerThreadFunction(void *parameter);
pthread_t *threadHandles;
#elif defined(HAVE_WINDOWS_H)
#else
static DWORD WINAPI WorkerThreadFunction(void *parameter);
HANDLE *threadHandles;
#endif
Expand Down
6 changes: 3 additions & 3 deletions libpolyml/globals.h
Expand Up @@ -2,7 +2,7 @@
Title: Globals for the system.
Author: Dave Matthews, Cambridge University Computer Laboratory
Copyright David C. J. Matthews 2017-18
Copyright David C. J. Matthews 2017-19
Copyright (c) 2000-7
Cambridge University Technical Services Limited
Expand Down Expand Up @@ -66,7 +66,7 @@

#define POLY_TAGSHIFT 1

#if (defined(_WIN32) && ! defined(__CYGWIN__))
#if (defined(_WIN32))
# include <windows.h>
#endif

Expand Down Expand Up @@ -106,7 +106,7 @@ typedef uintptr_t POLYUNSIGNED;

// We can use the C99 %zu in most cases except MingW since it uses
// the old msvcrt and that only supports C89.
#if (defined(_WIN32) && ! defined(__CYGWIN__) && (! defined(_MSC_VER) || _MSC_VER < 1800))
#if (defined(_WIN32) && (! defined(_MSC_VER) || _MSC_VER < 1800))
# if (SIZEOF_VOIDP == 8)
# define PRI_SIZET PRIu64
# else
Expand Down

0 comments on commit 649c3a6

Please sign in to comment.