BlinkenArea - GitList
Repositories
Blog
Wiki
libetherpix
Code
Commits
Branches
Tags
Search
Tree:
aa3270a
Branches
Tags
master
libetherpix
include
intern
thread.h
removed version information from file headers (manged via git now)
Stefan Schuermans
commited
aa3270a
at 2011-09-11 17:30:15
thread.h
Blame
History
Raw
/* * FlexiPix library * * Copyright 2010-2011 Stefan Schuermans <stefan schuermans info> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3 of the License. * * * This program 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 General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef FLP_THREAD_H #define FLP_THREAD_H #ifdef WINDOWS # include <windows.h> #else # include <errno.h> # include <pthread.h> # include <sys/time.h> # include <stdlib.h> #endif /** signature to define or declare thread function */ #ifdef WINDOWS # define FLP_THREAD_FUNC(func) DWORD (__stdcall func)(LPVOID vp_displayer) #else # define FLP_THREAD_FUNC(func) void * (func)(void *vp_displayer) #endif /** return a void pointer from a thread function */ #ifdef WINDOWS # define FLP_THREAD_RETURN(vp) return (DWORD)(vp); #else # define FLP_THREAD_RETURN(vp) return (vp); #endif /** type for a thread ID */ #ifdef WINDOWS typedef HANDLE flp_thread_id_t; #else typedef pthread_t flp_thread_id_t; #endif /** type for a mutex */ #ifdef WINDOWS typedef HANDLE flp_thread_mutex_t; #else typedef pthread_mutex_t flp_thread_mutex_t; #endif /** type for a condition */ #ifdef WINDOWS typedef HANDLE flp_thread_cond_t; #else typedef pthread_cond_t flp_thread_cond_t; #endif /** * \brief create a thread * \param[out] *p_tid thread ID * \param[in] func thread function * \param[in] p_data user data pointer * \return 1 if successful, 0 on error */ extern inline int flp_thread_create(flp_thread_id_t *p_tid, FLP_THREAD_FUNC(*func), void * p_data) { #ifdef WINDOWS HANDLE h = CreateThread(NULL, 0, func, (LPVOID)p_data, 0, NULL); if (h != NULL) *p_tid = h; return h != NULL; #else return pthread_create(p_tid, NULL, func, p_data) == 0; #endif } /** * \brief join a thread * \param[in] tid thread ID */ extern inline void flp_thread_join(flp_thread_id_t tid) { #ifdef WINDOWS WaitForSingleObject(tid, INFINITE); CloseHandle(tid); #else pthread_join(tid, NULL); #endif } /** * \brief initialize mutex * \param[in] *p_mtx mutex variable * \return 1 if successful, 0 on error */ extern inline int flp_thread_mutex_init(flp_thread_mutex_t *p_mtx) { #ifdef WINDOWS HANDLE h = CreateMutex(NULL, FALSE, NULL); if (h != NULL) *p_mtx = h; return h != NULL; #else return pthread_mutex_init(p_mtx, NULL) == 0; #endif } /** * \brief destroy mutex * \param[in] *p_mtx mutex variable */ extern inline void flp_thread_mutex_destroy(flp_thread_mutex_t *p_mtx) { #ifdef WINDOWS CloseHandle(*p_mtx); #else pthread_mutex_destroy(p_mtx); #endif } /** * \brief lock mutex * \param[in] *p_mtx mutex variable */ extern inline void flp_thread_mutex_lock(flp_thread_mutex_t *p_mtx) { #ifdef WINDOWS WaitForSingleObject(*p_mtx, INFINITE); #else pthread_mutex_lock(p_mtx); #endif } /** * \brief unlock mutex * \param[in] *p_mtx mutex variable */ extern inline void flp_thread_mutex_unlock(flp_thread_mutex_t *p_mtx) { #ifdef WINDOWS ReleaseMutex(*p_mtx); #else pthread_mutex_unlock(p_mtx); #endif } /** * \brief initialize condition * \param[in] *p_cond conition variable * \return 1 if successful, 0 on error */ extern inline int flp_thread_cond_init(flp_thread_cond_t *p_cond) { #ifdef WINDOWS HANDLE h = CreateEvent(NULL, FALSE, FALSE, NULL); if (h != NULL) *p_cond = h; return h != NULL; #else return pthread_cond_init(p_cond, NULL) == 0; #endif } /** * \brief destroy condition * \param[in] *p_cond condition variable */ extern inline void flp_thread_cond_destroy(flp_thread_cond_t *p_cond) { #ifdef WINDOWS CloseHandle(*p_cond); #else pthread_cond_destroy(p_cond); #endif } /** * \brief signal condition * \param[in] *p_cond condition variable */ extern inline void flp_thread_cond_signal(flp_thread_cond_t *p_cond) { #ifdef WINDOWS SetEvent(*p_cond); #else pthread_cond_signal(p_cond); #endif } /** * \brief wait for condition * \param[in] *p_cond condition variable * \param[in] *p_mtx mutex variable to unlock while waiting */ extern inline void flp_thread_cond_wait(flp_thread_cond_t *p_cond, flp_thread_mutex_t *p_mtx) { #ifdef WINDOWS /* the unlock/wait-begin and wait-end/lock are atomic for pthread, but this is not needed for libflexipix... */ ReleaseMutex(*p_mtx); /* unlock mutex */ WaitForSingleObject(*p_cond, INFINITE); /* wait for condition */ WaitForSingleObject(*p_mtx, INFINITE); /* lock mutex again */ #else pthread_cond_wait(p_cond, p_mtx); #endif } /** * \brief wait for condition with timeout * \param[in] *p_cond condition variable * \param[in] *p_mtx mutex variable to unlock while waiting * \param[in] *p_now current time * \param[in] millisecs number of milliseconds to wait at most * \return 0 for condition singnaled, 1 for timeout */ extern inline int flp_thread_cond_timedwait(flp_thread_cond_t *p_cond, flp_thread_mutex_t *p_mtx, struct timeval *p_now, unsigned int millisecs) { #ifdef WINDOWS int ret; /* the unlock/wait-begin and wait-end/lock are atomic for pthread, but this is not needed for libflexipix... */ ReleaseMutex(*p_mtx); /* unlock mutex */ ret = WaitForSingleObject(*p_cond, millisecs); /* wait for condition */ WaitForSingleObject(*p_mtx, INFINITE); /* lock mutex again */ return ret == WAIT_TIMEOUT; (void)p_now; #else struct timespec until; until.tv_sec = p_now->tv_sec + millisecs / 1000; until.tv_nsec = p_now->tv_usec * 1000 + (millisecs % 1000) * 1000000; if (until.tv_nsec > 1000000000) { until.tv_nsec -= 1000000000; until.tv_sec++; } return pthread_cond_timedwait(p_cond, p_mtx, &until) == ETIMEDOUT; #endif } #endif /* #ifndef FLP_THREAD_H */