diff --git a/source/myport/posix/mycore/utils/mcsync.c b/source/myport/posix/mycore/utils/mcsync.c index 6544a55..2e66790 100644 --- a/source/myport/posix/mycore/utils/mcsync.c +++ b/source/myport/posix/mycore/utils/mcsync.c @@ -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; } diff --git a/source/myport/windows/mycore/thread.c b/source/myport/windows/mycore/thread.c deleted file mode 100644 index ae0d6d0..0000000 --- a/source/myport/windows/mycore/thread.c +++ /dev/null @@ -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 diff --git a/source/myport/windows/mycore/utils/mcsync.c b/source/myport/windows/mycore/utils/mcsync.c deleted file mode 100644 index 607211d..0000000 --- a/source/myport/windows/mycore/utils/mcsync.c +++ /dev/null @@ -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 - -#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; -} - - diff --git a/source/myport/windows_nt/Makefile.mk b/source/myport/windows_nt/Makefile.mk new file mode 100644 index 0000000..ce272a0 --- /dev/null +++ b/source/myport/windows_nt/Makefile.mk @@ -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: diff --git a/source/myport/windows/mycore/io.c b/source/myport/windows_nt/mycore/io.c similarity index 100% rename from source/myport/windows/mycore/io.c rename to source/myport/windows_nt/mycore/io.c diff --git a/source/myport/windows/mycore/memory.c b/source/myport/windows_nt/mycore/memory.c similarity index 100% rename from source/myport/windows/mycore/memory.c rename to source/myport/windows_nt/mycore/memory.c diff --git a/source/myport/windows/mycore/perf.c b/source/myport/windows_nt/mycore/perf.c similarity index 100% rename from source/myport/windows/mycore/perf.c rename to source/myport/windows_nt/mycore/perf.c diff --git a/source/myport/windows_nt/mycore/thread.c b/source/myport/windows_nt/mycore/thread.c new file mode 100644 index 0000000..2303e6e --- /dev/null +++ b/source/myport/windows_nt/mycore/thread.c @@ -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 + +/*********************************************************************************** + * + * 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 diff --git a/source/myport/windows_nt/mycore/utils/mcsync.c b/source/myport/windows_nt/mycore/utils/mcsync.c new file mode 100644 index 0000000..98b0fea --- /dev/null +++ b/source/myport/windows_nt/mycore/utils/mcsync.c @@ -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 + +/* 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