693f1e87154947811c9ac21a092b5aefe1f38836
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h   1) /*
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h   2)  * EtherPix library
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h   3)  *
Stefan Schuermans update copyright year

Stefan Schuermans authored 7 years ago

include/intern/thread.h   4)  * Copyright 2010-2017 Stefan Schuermans <stefan schuermans info>
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h   5)  *
include/intern/thread.h   6)  * This program is free software: you can redistribute it and/or modify
include/intern/thread.h   7)  * it under the terms of the GNU General Public License as published by
include/intern/thread.h   8)  * the Free Software Foundation, version 3 of the License.
include/intern/thread.h   9)  *
include/intern/thread.h  10)  *
include/intern/thread.h  11)  * This program is distributed in the hope that it will be useful,
include/intern/thread.h  12)  * but WITHOUT ANY WARRANTY; without even the implied warranty of
include/intern/thread.h  13)  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
include/intern/thread.h  14)  * GNU General Public License for more details.
include/intern/thread.h  15)  *
include/intern/thread.h  16)  * You should have received a copy of the GNU Lesser General Public License
include/intern/thread.h  17)  * along with this program. If not, see <http://www.gnu.org/licenses/>.
include/intern/thread.h  18)  */
include/intern/thread.h  19) 
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  20) #ifndef ETP_THREAD_H
include/intern/thread.h  21) #define ETP_THREAD_H
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  22) 
include/intern/thread.h  23) #ifdef WINDOWS
include/intern/thread.h  24) #  include <windows.h>
include/intern/thread.h  25) #else
include/intern/thread.h  26) #  include <errno.h>
include/intern/thread.h  27) #  include <pthread.h>
include/intern/thread.h  28) #  include <sys/time.h>
include/intern/thread.h  29) #  include <stdlib.h>
include/intern/thread.h  30) #endif
include/intern/thread.h  31) 
include/intern/thread.h  32) /** signature to define or declare thread function */
include/intern/thread.h  33) #ifdef WINDOWS
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  34) #  define ETP_THREAD_FUNC(func) DWORD (__stdcall func)(LPVOID vp_displayer)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  35) #else
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  36) #  define ETP_THREAD_FUNC(func) void * (func)(void *vp_displayer)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  37) #endif
include/intern/thread.h  38) 
Stefan Schuermans update Windows support

Stefan Schuermans authored 6 years ago

include/intern/thread.h  39) /** return values for thread functions */
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  40) #ifdef WINDOWS
Stefan Schuermans update Windows support

Stefan Schuermans authored 6 years ago

include/intern/thread.h  41) #  define ETP_THREAD_RETURN_OK (0)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  42) #else
Stefan Schuermans update Windows support

Stefan Schuermans authored 6 years ago

include/intern/thread.h  43) #  define ETP_THREAD_RETURN_OK (NULL)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  44) #endif
include/intern/thread.h  45) 
include/intern/thread.h  46) /** type for a thread ID */
include/intern/thread.h  47) #ifdef WINDOWS
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  48) typedef HANDLE etp_thread_id_t;
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  49) #else
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  50) typedef pthread_t etp_thread_id_t;
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  51) #endif
include/intern/thread.h  52) 
include/intern/thread.h  53) /** type for a mutex */
include/intern/thread.h  54) #ifdef WINDOWS
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  55) typedef HANDLE etp_thread_mutex_t;
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  56) #else
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  57) typedef pthread_mutex_t etp_thread_mutex_t;
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  58) #endif
include/intern/thread.h  59) 
include/intern/thread.h  60) /** type for a condition */
include/intern/thread.h  61) #ifdef WINDOWS
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  62) typedef HANDLE etp_thread_cond_t;
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  63) #else
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  64) typedef pthread_cond_t etp_thread_cond_t;
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  65) #endif
include/intern/thread.h  66) 
include/intern/thread.h  67) /**
include/intern/thread.h  68)  * \brief create a thread
include/intern/thread.h  69)  * \param[out] *p_tid thread ID
include/intern/thread.h  70)  * \param[in] func thread function
include/intern/thread.h  71)  * \param[in] p_data user data pointer
include/intern/thread.h  72)  * \return 1 if successful, 0 on error
include/intern/thread.h  73)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  74) static inline int etp_thread_create(etp_thread_id_t *p_tid,
include/intern/thread.h  75)                                     ETP_THREAD_FUNC(*func),
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  76)                                     void * p_data)
include/intern/thread.h  77) {
include/intern/thread.h  78) #ifdef WINDOWS
include/intern/thread.h  79)   HANDLE h = CreateThread(NULL, 0, func, (LPVOID)p_data, 0, NULL);
include/intern/thread.h  80)   if (h != NULL)
include/intern/thread.h  81)     *p_tid = h;
include/intern/thread.h  82)   return h != NULL;
include/intern/thread.h  83) #else
include/intern/thread.h  84)   return pthread_create(p_tid, NULL, func, p_data) == 0;
include/intern/thread.h  85) #endif
include/intern/thread.h  86) }
include/intern/thread.h  87) 
include/intern/thread.h  88) /**
include/intern/thread.h  89)  * \brief join a thread
include/intern/thread.h  90)  * \param[in] tid thread ID
include/intern/thread.h  91)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h  92) static inline void etp_thread_join(etp_thread_id_t tid)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h  93) {
include/intern/thread.h  94) #ifdef WINDOWS
include/intern/thread.h  95)   WaitForSingleObject(tid, INFINITE);
include/intern/thread.h  96)   CloseHandle(tid);
include/intern/thread.h  97) #else
include/intern/thread.h  98)   pthread_join(tid, NULL);
include/intern/thread.h  99) #endif
include/intern/thread.h 100) }
include/intern/thread.h 101) 
include/intern/thread.h 102) /**
include/intern/thread.h 103)  * \brief initialize mutex
include/intern/thread.h 104)  * \param[in] *p_mtx mutex variable
include/intern/thread.h 105)  * \return 1 if successful, 0 on error
include/intern/thread.h 106)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 107) static inline int etp_thread_mutex_init(etp_thread_mutex_t *p_mtx)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 108) {
include/intern/thread.h 109) #ifdef WINDOWS
include/intern/thread.h 110)   HANDLE h = CreateMutex(NULL, FALSE, NULL);
include/intern/thread.h 111)   if (h != NULL)
include/intern/thread.h 112)     *p_mtx = h;
include/intern/thread.h 113)   return h != NULL;
include/intern/thread.h 114) #else
include/intern/thread.h 115)   return pthread_mutex_init(p_mtx, NULL) == 0;
include/intern/thread.h 116) #endif
include/intern/thread.h 117) }
include/intern/thread.h 118) 
include/intern/thread.h 119) /**
include/intern/thread.h 120)  * \brief destroy mutex
include/intern/thread.h 121)  * \param[in] *p_mtx mutex variable
include/intern/thread.h 122)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 123) static inline void etp_thread_mutex_destroy(etp_thread_mutex_t *p_mtx)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 124) {
include/intern/thread.h 125) #ifdef WINDOWS
include/intern/thread.h 126)   CloseHandle(*p_mtx);
include/intern/thread.h 127) #else
include/intern/thread.h 128)   pthread_mutex_destroy(p_mtx);
include/intern/thread.h 129) #endif
include/intern/thread.h 130) }
include/intern/thread.h 131) 
include/intern/thread.h 132) /**
include/intern/thread.h 133)  * \brief lock mutex
include/intern/thread.h 134)  * \param[in] *p_mtx mutex variable
include/intern/thread.h 135)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 136) static inline void etp_thread_mutex_lock(etp_thread_mutex_t *p_mtx)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 137) {
include/intern/thread.h 138) #ifdef WINDOWS
include/intern/thread.h 139)   WaitForSingleObject(*p_mtx, INFINITE);
include/intern/thread.h 140) #else
include/intern/thread.h 141)   pthread_mutex_lock(p_mtx);
include/intern/thread.h 142) #endif
include/intern/thread.h 143) }
include/intern/thread.h 144) 
include/intern/thread.h 145) /**
include/intern/thread.h 146)  * \brief unlock mutex
include/intern/thread.h 147)  * \param[in] *p_mtx mutex variable
include/intern/thread.h 148)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 149) static inline void etp_thread_mutex_unlock(etp_thread_mutex_t *p_mtx)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 150) {
include/intern/thread.h 151) #ifdef WINDOWS
include/intern/thread.h 152)   ReleaseMutex(*p_mtx);
include/intern/thread.h 153) #else
include/intern/thread.h 154)   pthread_mutex_unlock(p_mtx);
include/intern/thread.h 155) #endif
include/intern/thread.h 156) }
include/intern/thread.h 157) 
include/intern/thread.h 158) /**
include/intern/thread.h 159)  * \brief initialize condition
include/intern/thread.h 160)  * \param[in] *p_cond conition variable
include/intern/thread.h 161)  * \return 1 if successful, 0 on error
include/intern/thread.h 162)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 163) static inline int etp_thread_cond_init(etp_thread_cond_t *p_cond)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 164) {
include/intern/thread.h 165) #ifdef WINDOWS
include/intern/thread.h 166)   HANDLE h = CreateEvent(NULL, FALSE, FALSE, NULL);
include/intern/thread.h 167)   if (h != NULL)
include/intern/thread.h 168)     *p_cond = h;
include/intern/thread.h 169)   return h != NULL;
include/intern/thread.h 170) #else
include/intern/thread.h 171)   return pthread_cond_init(p_cond, NULL) == 0;
include/intern/thread.h 172) #endif
include/intern/thread.h 173) }
include/intern/thread.h 174) 
include/intern/thread.h 175) /**
include/intern/thread.h 176)  * \brief destroy condition
include/intern/thread.h 177)  * \param[in] *p_cond condition variable
include/intern/thread.h 178)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 179) static inline void etp_thread_cond_destroy(etp_thread_cond_t *p_cond)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 180) {
include/intern/thread.h 181) #ifdef WINDOWS
include/intern/thread.h 182)   CloseHandle(*p_cond);
include/intern/thread.h 183) #else
include/intern/thread.h 184)   pthread_cond_destroy(p_cond);
include/intern/thread.h 185) #endif
include/intern/thread.h 186) }
include/intern/thread.h 187) 
include/intern/thread.h 188) /**
include/intern/thread.h 189)  * \brief signal condition
include/intern/thread.h 190)  * \param[in] *p_cond condition variable
include/intern/thread.h 191)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 192) static inline void etp_thread_cond_signal(etp_thread_cond_t *p_cond)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 193) {
include/intern/thread.h 194) #ifdef WINDOWS
include/intern/thread.h 195)   SetEvent(*p_cond);
include/intern/thread.h 196) #else
include/intern/thread.h 197)   pthread_cond_signal(p_cond);
include/intern/thread.h 198) #endif
include/intern/thread.h 199) }
include/intern/thread.h 200) 
include/intern/thread.h 201) /**
include/intern/thread.h 202)  * \brief wait for condition
include/intern/thread.h 203)  * \param[in] *p_cond condition variable
include/intern/thread.h 204)  * \param[in] *p_mtx mutex variable to unlock while waiting
include/intern/thread.h 205)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 206) static inline void etp_thread_cond_wait(etp_thread_cond_t *p_cond,
include/intern/thread.h 207)                                         etp_thread_mutex_t *p_mtx)
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 208) {
include/intern/thread.h 209) #ifdef WINDOWS
include/intern/thread.h 210)   /* the unlock/wait-begin and wait-end/lock are atomic for pthread,
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 211)      but this is not needed for libetherpix... */
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 212)   ReleaseMutex(*p_mtx); /* unlock mutex */
include/intern/thread.h 213)   WaitForSingleObject(*p_cond, INFINITE); /* wait for condition */
include/intern/thread.h 214)   WaitForSingleObject(*p_mtx, INFINITE); /* lock mutex again */
include/intern/thread.h 215) #else
include/intern/thread.h 216)   pthread_cond_wait(p_cond, p_mtx);
include/intern/thread.h 217) #endif
include/intern/thread.h 218) }
include/intern/thread.h 219) 
include/intern/thread.h 220) /**
include/intern/thread.h 221)  * \brief wait for condition with timeout
include/intern/thread.h 222)  * \param[in] *p_cond condition variable
include/intern/thread.h 223)  * \param[in] *p_mtx mutex variable to unlock while waiting
include/intern/thread.h 224)  * \param[in] *p_now current time
include/intern/thread.h 225)  * \param[in] millisecs number of milliseconds to wait at most
include/intern/thread.h 226)  * \return 0 for condition singnaled, 1 for timeout
include/intern/thread.h 227)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 228) static inline int etp_thread_cond_timedwait(etp_thread_cond_t *p_cond,
include/intern/thread.h 229)                                             etp_thread_mutex_t *p_mtx,
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 230)                                             struct timeval *p_now,
include/intern/thread.h 231)                                             unsigned int millisecs)
include/intern/thread.h 232) {
include/intern/thread.h 233) #ifdef WINDOWS
include/intern/thread.h 234)   int ret;
include/intern/thread.h 235)   /* the unlock/wait-begin and wait-end/lock are atomic for pthread,
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 236)      but this is not needed for libetherpix... */
Stefan Schuermans v1.0.6

Stefan Schuermans authored 12 years ago

include/intern/thread.h 237)   ReleaseMutex(*p_mtx); /* unlock mutex */
include/intern/thread.h 238)   ret = WaitForSingleObject(*p_cond, millisecs); /* wait for condition */
include/intern/thread.h 239)   WaitForSingleObject(*p_mtx, INFINITE); /* lock mutex again */
include/intern/thread.h 240)   return ret == WAIT_TIMEOUT;
include/intern/thread.h 241)   (void)p_now;
include/intern/thread.h 242) #else
include/intern/thread.h 243)   struct timespec until;
include/intern/thread.h 244)   until.tv_sec = p_now->tv_sec + millisecs / 1000;
include/intern/thread.h 245)   until.tv_nsec = p_now->tv_usec * 1000 + (millisecs % 1000) * 1000000;
include/intern/thread.h 246)   if (until.tv_nsec > 1000000000) {
include/intern/thread.h 247)     until.tv_nsec -= 1000000000;
include/intern/thread.h 248)     until.tv_sec++;
include/intern/thread.h 249)   }
include/intern/thread.h 250)   return pthread_cond_timedwait(p_cond, p_mtx, &until) == ETIMEDOUT;
include/intern/thread.h 251) #endif
include/intern/thread.h 252) }
include/intern/thread.h 253) 
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

include/intern/thread.h 254) #endif /* #ifndef ETP_THREAD_H */