Skip to content

Commit

Permalink
Mixing in changes to support building on Windows with Clang
Browse files Browse the repository at this point in the history
Signed-off-by: Joe <[email protected]>
  • Loading branch information
JoeNemo committed Feb 23, 2022
1 parent 857e73c commit fa76299
Show file tree
Hide file tree
Showing 16 changed files with 747 additions and 4 deletions.
3 changes: 3 additions & 0 deletions libregexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
#include <stdlib.h>
#include <stdio.h>
#ifdef _MSC_VER
#include <malloc.h> /* JOENemo for alloca */
#endif
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
Expand Down
64 changes: 64 additions & 0 deletions porting/windirent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "windirent.h"

DIR *opendir(const char *directoryName){
int nameLength = strlen(directoryName);
WIN32_FIND_DATA findData;
int directoryNameBufferSize = nameLength + 10;
char *directoryNameBuffer = malloc(directoryNameBufferSize);
HANDLE hFind = INVALID_HANDLE_VALUE;

int len = sprintf(directoryNameBuffer,"%s\\*",directoryName);
hFind = FindFirstFile(directoryNameBuffer, &findData);

free(directoryNameBuffer);
directoryNameBuffer = NULL;

if (hFind == INVALID_HANDLE_VALUE){
errno = GetLastError();
return NULL;
} else {
DIR *dir = (DIR*)malloc(sizeof(DIR));
memset(dir,0,sizeof(DIR));
dir->hFind = hFind;
memcpy(&(dir->findData),&findData,sizeof(WIN32_FIND_DATA));
dir->hasMoreEntries = TRUE;

return dir;
}
}

struct dirent *readdir(DIR *dir){
HANDLE hFind = dir->hFind;
WIN32_FIND_DATA *findData = &(dir->findData);

if (dir->hasMoreEntries){
int filenameLength = strlen(findData->cFileName);
struct dirent *entry = &dir->entry;
entry->d_ino = 0;
memcpy(entry->d_name,findData->cFileName,filenameLength);
if (FindNextFile(hFind, findData)){
dir->hasMoreEntries = TRUE;
} else {
/* find loop is done */
dir->hasMoreEntries = FALSE;
}
return entry;
} else {
return NULL;
}
}

int closedir(DIR *dir){
if (dir){
HANDLE hFind = dir->hFind;
FindClose(hFind);
free(dir);
return 0;
} else {
errno = -1;
return -1;
}
}
36 changes: 36 additions & 0 deletions porting/windirent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef __WINDIRENT__
#define __WINDIRENT__ 1

#include <windows.h>
#include <stdbool.h>
#include <sys/types.h>
/* #include <winsock2.h> */

#define NAME_MAX 255


struct dirent {
ino_t d_ino; /* inode number */
char d_name[NAME_MAX+1]; /* filename */
};



typedef struct DIR_tag {
HANDLE hFind;
WIN32_FIND_DATA findData;
bool hasMoreEntries;
struct dirent entry;
} DIR;

DIR *opendir(const char *);

struct dirent *readdir(DIR *);

void rewinddir(DIR *);

int closedir(DIR *);



#endif
107 changes: 107 additions & 0 deletions porting/winpthread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <stdio.h>
#include "winpthread.h"
#include <windows.h>
#include <math.h>

/*
#define mutexLock(x) WaitForSingleObject(x,INFINITE)
#define mutexUnlock(x) ReleaseMutex(x)
#define mutexCreate(x) (x) = CreateMutexEx(NULL,NULL,0x00000000,MUTEX_ALL_ACCESS);
*/

int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *attr){
int initStatus = InitializeCriticalSectionAndSpinCount(&mutex->criticalSection, 0x00000400);
if (!initStatus){ /* Win2003 and XP only */
return 24;
} else {
return 0;
}
}

int pthread_mutex_lock(pthread_mutex_t *mutex){
/* mutex->windowsMutex = CreateMutexEx(NULL,NULL,0x00000000,MUTEX_ALL_ACCESS); */
/* WaitForSingleObject(mutex->windowsMutex,INFINITE); */
EnterCriticalSection(&mutex->criticalSection);
return 0;
}

int pthread_mutex_unlock(pthread_mutex_t *mutex){
/* ReleaseMutex(mutex->windowsMutex); */
LeaveCriticalSection(&mutex->criticalSection);
return 0;
}

/* reference */

int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *),
void *arg){
printf("write pthread_create(), please\n");
return 0;
}


int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr){
(void)attr;
if (cond == NULL){
return 12;
}
InitializeConditionVariable(&cond->condVar);
return 0;
}

int pthread_cond_destroy(pthread_cond_t *cond){
(void)cond;
return 0; /* n/a in windows */
}

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex){
if (cond == NULL || mutex == NULL){
return 12;
}
return pthread_cond_timedwait(cond, mutex, NULL);
}

int pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t *mutex,
const struct timespec *abstime){
if (cond == NULL || mutex == NULL){
return 12;
}
long ms = 0;
if (abstime){
ms = round(abstime->tv_nsec/1.0e6);
}
if (!SleepConditionVariableCS(&cond->condVar, &mutex->criticalSection, ms)){
return 16;
}
return 0;
}

int pthread_cond_signal(pthread_cond_t *cond){
if (cond == NULL){
return 12;
}
WakeConditionVariable(&cond->condVar);
return 0;
}

int pthread_cond_broadcast(pthread_cond_t *cond){
if (cond == NULL){
return 12;
}
WakeAllConditionVariable(&cond->condVar);
return 0;
}


/*
quickjs-835b20.o : error LNK2019: unresolved external symbol pthread_cond_init referenced in function js_atomics_wait
quickjs-835b20.o : error LNK2019: unresolved external symbol pthread_cond_wait referenced in function js_atomics_wait
quickjs-835b20.o : error LNK2019: unresolved external symbol pthread_cond_timedwait referenced in function js_atomics_wait
quickjs-835b20.o : error LNK2019: unresolved external symbol pthread_cond_destroy referenced in function js_atomics_wait
quickjs-835b20.o : error LNK2019: unresolved external symbol pthread_cond_signal referenced in function js_atomics_notify
*/
99 changes: 99 additions & 0 deletions porting/winpthread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@


/*
This program and the accompanying materials are
made available under the terms of the Eclipse Public License v2.0 which accompanies
this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
*/

/*
What BSD thinks
https://github.com/openbsd/src/blob/master/include/pthread.h
*/

#ifndef __WINPTHREAD__
#define __WINPTHREAD__ 1

#include <windows.h>
#include <time.h>
/* #include <winsock2.h> */

typedef HANDLE Mutex;
typedef HANDLE Thread;

/*
#define mutexLock(x) WaitForSingleObject(x,INFINITE)
#define mutexUnlock(x) ReleaseMutex(x)
#define mutexCreate(x) (x) = CreateMutexEx(NULL,NULL,0x00000000,MUTEX_ALL_ACCESS);
*/

struct pthread;
struct pthread_attr;
struct pthread_cond_attr;
struct pthread_mutex_attr;
struct pthread_once;
struct pthread_rwlock;
struct pthread_rwlockattr;

typedef struct pthread_attr_tag {
int foo;
} pthread_attr_t;

typedef struct pthread_cond {
CONDITION_VARIABLE condVar;
} pthread_cond_t;

typedef struct pthread_tag {
HANDLE windowsThread;
int threadID;
} pthread_t;

typedef struct pthread_mutex {
/* HANDLE windowsMutex; Mutex's can be used inter-process, critical_section within a process */
CRITICAL_SECTION criticalSection;
} pthread_mutex_t;

typedef struct pthread_mutex_attr *pthread_mutexattr_t;
typedef struct pthread_cond_attr *pthread_condattr_t;

#define PTHREAD_MUTEX_INITIALIZER { 0, 0 }
#define PTHREAD_COND_INITIALIZER NULL
#define PTHREAD_RWLOCK_INITIALIZER NULL

int pthread_mutex_destroy(pthread_mutex_t *);
int pthread_mutex_init(pthread_mutex_t *,
const pthread_mutexattr_t *);
int pthread_mutex_lock(pthread_mutex_t *);
int pthread_mutex_timedlock(pthread_mutex_t *,
const struct timespec *);
int pthread_mutex_trylock(pthread_mutex_t *);
int pthread_mutex_unlock(pthread_mutex_t *);

int pthread_mutexattr_init(pthread_mutexattr_t *);
int pthread_mutexattr_destroy(pthread_mutexattr_t *);
int pthread_mutexattr_gettype(pthread_mutexattr_t *, int *);
int pthread_mutexattr_settype(pthread_mutexattr_t *, int);

/* Threads */

int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);

int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destroy(pthread_attr_t *attr);
int pthread_cond_broadcast(pthread_cond_t *);
int pthread_cond_destroy(pthread_cond_t *);
int pthread_cond_init(pthread_cond_t *,
const pthread_condattr_t *);
int pthread_cond_signal(pthread_cond_t *);
int pthread_cond_timedwait(pthread_cond_t *,
pthread_mutex_t *, const struct timespec *);
int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);

#endif
Loading

0 comments on commit fa76299

Please sign in to comment.