Port for Windows

This commit is contained in:
lexborisov 2017-03-10 15:54:50 +03:00
parent 4c7b823274
commit fb6a3b4b90
9 changed files with 267 additions and 260 deletions

View File

@ -98,20 +98,15 @@ void * mcsync_mutex_create(void)
{
void *mutex = mycore_calloc(1, sizeof(pthread_mutex_t));
if(mutex == NULL)
return mutex;
if(pthread_mutex_init(mutex, NULL))
return mycore_free(mutex);
return NULL;
return mutex;
}
mcsync_status_t mcsync_mutex_init(void* mutex)
{
if(pthread_mutex_init(mutex, NULL)) {
mycore_free(mutex);
if(pthread_mutex_init(mutex, NULL))
return MCSYNC_STATUS_NOT_OK;
}
return MCSYNC_STATUS_OK;
}

View File

@ -1,109 +0,0 @@
/*
Copyright (C) 2015-2017 Alexander Borisov
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.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Author: lex.borisov@gmail.com (Alexander Borisov)
*/
#include "mycore/mythread.h"
#ifndef MyCORE_BUILD_WITHOUT_THREADS
/***********************************************************************************
*
* For Windows
*
***********************************************************************************/
mystatus_t mycore_thread_create(mythread_t *mythread, mythread_list_t *thr, void *work_func)
{
thr->pth = CreateThread(NULL, // default security attributes
0, // use default stack size
work_func, // thread function name
&thr->data, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
return MyCORE_STATUS_OK;
}
mystatus_t mycore_thread_join(mythread_t *mythread, mythread_list_t *thr)
{
WaitForSingleObject(thr->pth, INFINITE);
return MyCORE_STATUS_OK;
}
mystatus_t mycore_thread_cancel(mythread_t *mythread, mythread_list_t *thr)
{
TerminateThread(thr->pth, 0);
return MyCORE_STATUS_OK;
}
mystatus_t mycore_thread_attr_init(mythread_t *mythread)
{
return MyCORE_STATUS_OK;
}
mystatus_t mycore_thread_attr_clean(mythread_t *mythread)
{
return MyCORE_STATUS_OK;
}
mystatus_t mycore_thread_attr_destroy(mythread_t *mythread)
{
return MyCORE_STATUS_OK;
}
mystatus_t mycore_hread_mutex_create(mythread_t *mythread, mythread_context_t *ctx, size_t prefix_id)
{
ctx->mutex = CreateSemaphore(NULL, 0, 1, NULL);
return MyCORE_STATUS_OK;
}
mystatus_t mycore_hread_mutex_post(mythread_t *mythread, mythread_context_t *ctx)
{
ReleaseSemaphore(ctx->mutex, 1, NULL);
return MyCORE_STATUS_OK;
}
mystatus_t mycore_hread_mutex_wait(mythread_t *mythread, mythread_context_t *ctx)
{
WaitForSingleObject(ctx->mutex, INFINITE);
return MyCORE_STATUS_OK;
}
mystatus_t mycore_hread_mutex_try_wait(mythread_t *mythread, mythread_context_t *ctx)
{
return MyCORE_STATUS_OK;
}
mystatus_t mycore_hread_mutex_close(mythread_t *mythread, mythread_context_t *ctx)
{
CloseHandle(ctx->mutex);
return MyCORE_STATUS_OK;
}
void mycore_thread_nanosleep(const struct timespec *tomeout)
{
Sleep(0);
}
#endif

View File

@ -1,144 +0,0 @@
/*
Copyright (C) 2015-2017 Alexander Borisov
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.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Author: lex.borisov@gmail.com (Alexander Borisov)
*/
#include "mycore/utils/mcsync.h"
#include <pthread.h>
#if ((defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) || defined(__ATOMIC_SEQ_CST))
#define MyCORE_MCSYNC_SPINLOCK_PRESENT
#endif
#ifdef MyCORE_MCSYNC_SPINLOCK_PRESENT
static mcsync_status_t mcsync_static_atomic_lock(void* spinlock)
{
int compare = 0;
while (!__atomic_compare_exchange_n(spinlock, &compare, 1, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {}
return MCSYNC_STATUS_OK;
}
static mcsync_status_t mcsync_static_atomic_unlock(void* spinlock)
{
__atomic_store_n(spinlock, 0, __ATOMIC_SEQ_CST);
return MCSYNC_STATUS_OK;
}
#endif /* MyCORE_MCSYNC_SPINLOCK_PRESENT */
/* spinlock */
void * mcsync_spin_init(void)
{
return mycore_calloc(1, sizeof(int));
}
mcsync_status_t mcsync_spin_init(void* spinlock)
{
#ifndef MyCORE_MCSYNC_SPINLOCK_PRESENT
return mcsync_mutex_init(spinlock);
#else
return MCSYNC_STATUS_OK;
#endif
}
void mcsync_spin_clean(void* spinlock)
{
#ifdef MyCORE_MCSYNC_SPINLOCK_PRESENT
*((int*)spinlock) = 0;
#endif
}
void mcsync_spin_destroy(void* spinlock)
{
#ifdef MyCORE_MCSYNC_SPINLOCK_PRESENT
mcsync_spin_clean(spinlock);
#else
mcsync_mutex_destroy(spinlock);
#endif
}
mcsync_status_t mcsync_spin_lock((void* spinlock)
{
#ifdef MyCORE_MCSYNC_SPINLOCK_PRESENT
return mcsync_static_atomic_lock(spinlock);
#else
return mcsync_mutex_lock(spinlock);
#endif
}
mcsync_status_t mcsync_spin_unlock((void* spinlock)
{
#ifdef MyCORE_MCSYNC_SPINLOCK_PRESENT
return mcsync_static_atomic_unlock(spinlock);
#else
return mcsync_mutex_unlock(spinlock);
#endif
}
/* mutex */
void * mcsync_mutex_create(void)
{
void *mutex = mycore_calloc(1, sizeof(pthread_mutex_t));
if(mutex == NULL)
return mutex;
if(pthread_mutex_init(mutex, NULL))
return mycore_free(mutex);
return mutex;
}
mcsync_status_t mcsync_mutex_init(void)
{
if(pthread_mutex_init(mutex, NULL)) {
mycore_free(mutex);
return MCSYNC_STATUS_NOT_OK;
}
return MCSYNC_STATUS_OK;
}
void mcsync_mutex_clean(void* mutex)
{
/* clean function */
}
void mcsync_mutex_destroy(void* mutex)
{
pthread_mutex_destroy(mutex);
mycore_free(mutex);
}
mcsync_status_t mcsync_mutex_lock(void* mutex)
{
if(pthread_mutex_lock(mutex) == 0)
return MCSYNC_STATUS_OK;
return MCSYNC_STATUS_NOT_OK;
}
mcsync_status_t mcsync_mutex_unlock(void* mutex)
{
if(pthread_mutex_unlock(mutex) == 0)
return MCSYNC_STATUS_OK;
return MCSYNC_STATUS_NOT_OK;
}

View File

@ -0,0 +1,11 @@
myport_selected_port := myport/$(strip $(MODEST_PORT_NAME))
myport_dirs := mycore/. mycore/utils
myport_objs := $(call MODEST_UTILS_OBJS,$(myport_selected_port),$(myport_dirs))
myport_all: $(myport_objs)
myport_clean:
rm -f $(myport_objs)
myport_clone:

View File

@ -0,0 +1,135 @@
/*
Copyright (C) 2015-2017 Alexander Borisov
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.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Author: lex.borisov@gmail.com (Alexander Borisov)
*/
#include "mycore/mythread.h"
#ifndef MyCORE_BUILD_WITHOUT_THREADS
#include <windows.h>
/***********************************************************************************
*
* For Windows
*
***********************************************************************************/
void * mythread_thread_create(mythread_t *mythread, void* process_func, void* ctx)
{
return CreateThread(NULL, // default security attributes
0, // use default stack size
work_func, // thread function name
&thr->data, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
return thread;
}
mystatus_t mythread_thread_join(mythread_t *mythread, void* thread)
{
if(WaitForSingleObject(thr->pth, INFINITE) == WAIT_OBJECT_0)
return MyCORE_STATUS_OK;
return MyCORE_STATUS_ERROR;
}
mystatus_t mythread_thread_cancel(mythread_t *mythread, void* thread)
{
if(TerminateThread(thr->pth, 0))
return MyCORE_STATUS_OK;
return MyCORE_STATUS_ERROR;
}
mystatus_t mythread_thread_destroy(mythread_t *mythread, void* thread)
{
if(TerminateThread(thr->pth, 0))
return MyCORE_STATUS_OK;
return MyCORE_STATUS_ERROR;
}
void * mythread_thread_attr_init(mythread_t *mythread)
{
return MyCORE_STATUS_OK;
}
void mythread_thread_attr_clean(mythread_t *mythread, void* attr)
{
return MyCORE_STATUS_OK;
}
void mythread_thread_attr_destroy(mythread_t *mythread, void* attr)
{
return MyCORE_STATUS_OK;
}
void * mythread_mutex_create(mythread_t *mythread)
{
void *mutex = mcsync_mutex_create();
if(mutex == NULL)
return NULL;
if(mcsync_mutex_init(mutex)) {
mycore_free(mutex);
return NULL;
}
return mutex;
}
mystatus_t mythread_mutex_post(mythread_t *mythread, void* mutex)
{
return mcsync_mutex_unlock(mutex);
}
mystatus_t mythread_mutex_wait(mythread_t *mythread, void* mutex)
{
return mcsync_mutex_lock(mutex);
}
mystatus_t mythread_mutex_try_wait(mythread_t *mythread, void* mutex)
{
return mcsync_mutex_try_lock(mutex);
}
void mythread_mutex_close(mythread_t *mythread, void* mutex)
{
mcsync_mutex_destroy(mutex);
}
void * mythread_nanosleep_create(mythread_t* mythread)
{
return NULL;
}
void mythread_nanosleep_clean(void* timespec)
{
}
void mythread_nanosleep_destroy(void* timespec)
{
}
mystatus_t mythread_nanosleep_sleep(void* timespec)
{
Sleep(0);
return MyCORE_STATUS_ERROR;
}
#endif

View File

@ -0,0 +1,119 @@
/*
Copyright (C) 2015-2017 Alexander Borisov
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.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Author: lex.borisov@gmail.com (Alexander Borisov)
*/
#include "mycore/utils/mcsync.h"
#ifndef MyCORE_BUILD_WITHOUT_THREADS
#include <windows.h>
/* spinlock */
void * mcsync_spin_create(void)
{
void *spinlock = NULL;
CRITICAL_SECTION *spinlock = mycore_calloc(1, sizeof(CRITICAL_SECTION);
if(spinlock == NULL)
return NULL;
if(InitializeCriticalSectionAndSpinCount(spinlock, 0x00000400))
return spinlock;
return mycore_free(spinlock);
}
mcsync_status_t mcsync_spin_init(void* spinlock)
{
if(spinlock)
return MCSYNC_STATUS_OK;
return MCSYNC_STATUS_NOT_OK;
}
void mcsync_spin_clean(void* spinlock)
{
}
void mcsync_spin_destroy(void* spinlock)
{
DeleteCriticalSection(spinlock);
mycore_free(spinlock)
}
mcsync_status_t mcsync_spin_lock(void* spinlock)
{
EnterCriticalSection(spinlock)
return MCSYNC_STATUS_OK;
}
mcsync_status_t mcsync_spin_unlock(void* spinlock)
{
LeaveCriticalSection(spinlock)
return MCSYNC_STATUS_OK;
}
/* mutex */
void * mcsync_mutex_create(void)
{
return CreateSemaphore(NULL, 0, 1, NULL);
}
mcsync_status_t mcsync_mutex_init(void* mutex)
{
if(mutex == NULL)
return MCSYNC_STATUS_NOT_OK;
return MCSYNC_STATUS_OK;
}
void mcsync_mutex_clean(void* mutex)
{
/* clean function */
}
void mcsync_mutex_destroy(void* mutex)
{
CloseHandle(mutex);
}
mcsync_status_t mcsync_mutex_lock(void* mutex)
{
if(WaitForSingleObject(ctx->mutex, INFINITE) == WAIT_OBJECT_0)
return MCSYNC_STATUS_OK;
return MCSYNC_STATUS_NOT_OK;
}
mcsync_status_t mcsync_mutex_try_lock(void* mutex)
{
if(WaitForSingleObject(ctx->mutex, 0) != WAIT_FAILED)
return MCSYNC_STATUS_OK;
return MCSYNC_STATUS_NOT_OK;
}
mcsync_status_t mcsync_mutex_unlock(void* mutex)
{
if(ReleaseSemaphore(mutex, 1, NULL))
return MCSYNC_STATUS_OK;
return MCSYNC_STATUS_NOT_OK;
}
#endif