sl@0: /* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * sl@0: * Permission is hereby granted, free of charge, to any person obtaining a sl@0: * copy of this software and/or associated documentation files (the sl@0: * "Materials"), to deal in the Materials without restriction, including sl@0: * without limitation the rights to use, copy, modify, merge, publish, sl@0: * distribute, sublicense, and/or sell copies of the Materials, and to sl@0: * permit persons to whom the Materials are furnished to do so, subject to sl@0: * the following conditions: sl@0: * sl@0: * The above copyright notice and this permission notice shall be included sl@0: * in all copies or substantial portions of the Materials. sl@0: * sl@0: * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, sl@0: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF sl@0: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. sl@0: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY sl@0: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, sl@0: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE sl@0: * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. sl@0: */ sl@0: sl@0: #ifdef __cplusplus sl@0: extern "C" { sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifndef _XOPEN_SOURCE sl@0: #define LIPLAP sl@0: #define _XOPEN_SOURCE 1 /* nanosleep */ sl@0: #endif sl@0: sl@0: #include sl@0: sl@0: #ifdef LIPLAP sl@0: #undef _XOPEN_SOURCE sl@0: #endif sl@0: sl@0: #include "owfthread.h" sl@0: #include "owfmemory.h" sl@0: #include "owftypes.h" sl@0: sl@0: OWF_API_CALL void sl@0: OWF_Thread_Destroy(OWF_THREAD thread) sl@0: { sl@0: if (thread) sl@0: { sl@0: OWF_Thread_Join(thread, NULL); sl@0: xfree(thread); sl@0: } sl@0: } sl@0: sl@0: OWF_API_CALL OWF_THREAD sl@0: OWF_Thread_Create(void* (*threadfunc)(void*), void* data) sl@0: { sl@0: pthread_t* handle; sl@0: sl@0: handle = (pthread_t*)xalloc(sizeof(pthread_t), 1); sl@0: if (!handle) sl@0: { sl@0: return NULL; sl@0: } sl@0: sl@0: if (pthread_create(handle, NULL, threadfunc, data)) sl@0: { sl@0: xfree(handle); sl@0: return NULL; sl@0: } sl@0: sl@0: return (OWF_THREAD) handle; sl@0: } sl@0: sl@0: OWF_API_CALL OWFint sl@0: OWF_Thread_Join(OWF_THREAD thread, void** retval) sl@0: { sl@0: OWFint ret = EINVAL; sl@0: sl@0: if (!thread) sl@0: { sl@0: return ret; sl@0: } sl@0: ret = (int) pthread_join(*(pthread_t*) thread, retval); sl@0: return ret; sl@0: } sl@0: sl@0: OWF_API_CALL OWFint sl@0: OWF_Thread_Cancel(OWF_THREAD thread) sl@0: { sl@0: (void)thread; sl@0: return EINVAL; sl@0: } sl@0: sl@0: sl@0: OWF_API_CALL void sl@0: OWF_Thread_Exit(void* retval) sl@0: { sl@0: pthread_exit(retval); sl@0: } sl@0: sl@0: OWF_API_CALL void sl@0: OWF_Thread_MicroSleep(OWFuint32 usecs) sl@0: { sl@0: #if _POSIX_C_SOURCE >= 199309L sl@0: struct timespec ts; sl@0: ts.tv_sec = usecs / 1000000; sl@0: ts.tv_nsec = (usecs % 1000000) * 1000; sl@0: nanosleep(&ts, NULL); sl@0: #else sl@0: usleep(usecs); sl@0: #endif sl@0: } sl@0: sl@0: OWF_API_CALL void sl@0: OWF_Thread_Sleep(OWFuint32 secs) sl@0: { sl@0: sleep(secs); sl@0: } sl@0: sl@0: sl@0: #ifdef __cplusplus sl@0: } sl@0: #endif