28 #if defined(_USE_WIN_API) 31 #elif defined(_USE_LINUX_API) 43 #if defined(_USE_LINUX_API) 56 gettimeofday(&cur, NULL);
59 spec->tv_sec = cur.tv_sec + (timeout / 1000);
62 tmp_usec = cur.tv_usec + ((timeout % 1000) * 1000);
65 spec->tv_sec += (tmp_usec / 1000000);
68 spec->tv_nsec = (tmp_usec % 1000000) * 1000;
87 #if defined(_USE_WIN_API) 88 *pmutex = CreateMutex(NULL, FALSE, NULL);
92 #elif defined(_USE_LINUX_API) 93 pthread_mutex_init(pmutex, NULL);
115 #if defined(_USE_WIN_API) 116 if(*pmutex != NULL) {
118 ret = CloseHandle(*pmutex);
125 #elif defined(_USE_LINUX_API) 127 ret = pthread_mutex_destroy(pmutex);
149 if (pmutex != NULL) {
150 #if defined(_USE_WIN_API) 151 if(*pmutex != NULL) {
152 DWORD dwRet = WaitForSingleObject(*pmutex, timeout);
159 #elif defined(_USE_LINUX_API) 162 ret = pthread_mutex_lock(pmutex);
164 struct timespec spec;
165 create_timeout(&spec, timeout);
166 ret = pthread_mutex_timedlock(pmutex, &spec);
171 else if(ret == ETIMEDOUT) {
192 if (pmutex != NULL) {
193 #if defined(_USE_WIN_API) 194 if(*pmutex != NULL) {
195 ReleaseMutex(*pmutex);
198 #elif defined(_USE_LINUX_API) 200 ret = pthread_mutex_unlock(pmutex);
227 #if defined(_USE_WIN_API) 228 *pevt = CreateEvent(NULL, reset_mode, init_signal, NULL);
232 #elif defined(_USE_LINUX_API) 233 pthread_mutex_init(&pevt->mutex, NULL);
234 pthread_cond_init(&pevt->cond, NULL);
235 pevt->mode = (reset_mode != 0);
236 pevt->signal = (init_signal != 0);
258 #if defined(_USE_WIN_API) 261 ret = CloseHandle(*pevt);
268 #elif defined(_USE_LINUX_API) 270 ret = pthread_mutex_destroy(&pevt->mutex);
275 ret = pthread_cond_destroy(&pevt->cond);
300 #if defined(_USE_WIN_API) 305 #elif defined(_USE_LINUX_API) 307 ret = pthread_mutex_lock(&pevt->mutex);
309 pthread_cond_broadcast(&pevt->cond);
311 pthread_mutex_unlock(&pevt->mutex);
333 #if defined(_USE_WIN_API) 338 #elif defined(_USE_LINUX_API) 340 ret = pthread_mutex_lock(&pevt->mutex);
343 pthread_mutex_unlock(&pevt->mutex);
366 #if defined(_USE_WIN_API) 368 DWORD dwRet = WaitForSingleObject(*pevt, timeout);
375 #elif defined(_USE_LINUX_API) 378 ret = pthread_mutex_lock(&pevt->mutex);
380 if(pevt->signal == 0) {
381 pthread_cond_wait(&pevt->cond, &pevt->mutex);
383 if(pevt->mode == 0) {
386 pthread_mutex_unlock(&pevt->mutex);
389 ret = pthread_mutex_lock(&pevt->mutex);
391 if(pevt->signal == 0) {
392 struct timespec spec;
393 create_timeout(&spec, timeout);
394 ret = pthread_cond_timedwait(&pevt->cond, &pevt->mutex, &spec);
396 if((ret == 0) && (pevt->mode == 0)) {
399 pthread_mutex_unlock(&pevt->mutex);
405 else if(ret == ETIMEDOUT) {
416 #if defined(_USE_LINUX_API) 426 volatile int *flag_all;
432 struct multi_arg *parg = (
struct multi_arg *)arg;
442 while(*parg->flag_all) {
443 parg->hr =
wait_event(parg->pevt[parg->index], 1);
460 if(parg->wait_all == 0) {
483 #if defined(_USE_WIN_API) 487 HANDLE *evts = (HANDLE *)malloc(count *
sizeof(HANDLE));
499 for(i = 0; i < count; i++) {
503 dwRet = WaitForMultipleObjects(count, evts, wait_all, timeout);
515 #elif defined(_USE_LINUX_API) 516 volatile int flag_all = 1;
518 base_stamp, diff_stamp, min_stamp = (
uint32_t)-1;
520 struct multi_arg *pret, *parg = (
struct multi_arg *)malloc(count *
sizeof(
struct multi_arg));
522 if((pthread == NULL) || (parg == NULL)) {
536 for(i = 0; i < count; i++) {
538 parg[i].count = count;
540 parg[i].timeout = timeout;
541 parg[i].wait_all = wait_all;
542 parg[i].flag_all = &flag_all;
548 for(i = 0; i < count; i++) {
549 pthread_join(pthread[i], (
void **)&pret);
553 if(diff_stamp < min_stamp) {
555 min_stamp = diff_stamp;
569 if(pthread != NULL) {
591 #if defined(_USE_WIN_API) 592 return GetTickCount();
593 #elif defined(_USE_LINUX_API) 595 gettimeofday(&tv, NULL);
static int32_t _release_mutex(MUTEX *pmutex)
static int32_t _lock_mutex(MUTEX *pmutex, uint32_t timeout)
HRESULT wait_event_multi(EVENT **pevt, uint32_t count, uint32_t timeout, int wait_all)
Waits multiple events.
static int32_t _wait_event_multi(EVENT **pevt, uint32_t count, uint32_t timeout, int wait_all)
static int32_t _reset_event(EVENT *pevt)
static int32_t _initialize_mutex(MUTEX *pmutex)
HRESULT destroy_event(EVENT *pevt)
Destroys a event object.
Thread and mutex API file.
#define calc_time_diff(start, end)
A macro that calculates the time difference between start and end.
#define E_ACCESSDENIED
Failed because the resource is not ready.
#define E_INVALIDARG
Failed because some arguments are invalid.
HRESULT set_event(EVENT *pevt)
Sets a event.
static uint32_t _gettimeofday_msec()
uint32_t gettimeofday_msec()
Gets the current time value [ms].
#define E_UNEXPECTED
Failed because unexpected error happens.
HRESULT release_mutex(MUTEX *pmutex)
Releases mutex handle.
#define E_OUTOFMEMORY
Failed because there is no enough memory space.
#define SUCCEEDED(hr)
A macro that returns TRUE/FALSE. If hr is zero or more, then returns TRUE.
static int32_t _create_event(EVENT *pevt, int reset_mode, int init_signal)
#define INFINITE
A definition for infinite wait.
HRESULT initialize_mutex(MUTEX *pmutex)
Initializes mutex handle.
static int32_t _destroy_event(EVENT *pevt)
HRESULT reset_event(EVENT *pevt)
Resets a event.
static int32_t _wait_event(EVENT *pevt, uint32_t timeout)
HRESULT wait_event(EVENT *pevt, uint32_t timeout)
Waits a event.
HRESULT unlock_mutex(MUTEX *pmutex)
Unlocks mutex handle.
HRESULT create_event(EVENT *pevt, int reset_mode, int init_signal)
Creates a event object.
static int32_t _unlock_mutex(MUTEX *pmutex)
#define E_TIMEOUT
Failed because the communication timed out.
static int32_t _set_event(EVENT *pevt)
HRESULT lock_mutex(MUTEX *pmutex, uint32_t timeout)
Locks mutex handle.