I am using pthreads-win32 for threads on a Win32 Application. I am calculating the timespec using the first function posted here.
The call to sem_timedwait()
appears to be waiting for the specified ts
time, however every time it completes I get the following message:
Error waiting on semaphore: No error
I have checked the file of sem_timedwait.c
here, and they specify the same errno values and return values. Consequently, I do not know why it is exiting with this errno, and would like to know why.
#ifdef _WIN32
#define HAVE_STRUCT_TIMESPEC
#endif // _WIN32
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>
#include <semaphore.h>
#include "include.h"
// Platform includes
#ifdef _WIN32
#include <sched.h>
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#endif // _WIN32
// ...
sem_init(&job_semaphore, 0, 0);
// ...
// Set wait time relative to current time (semaphore wait time 10s)
#ifdef _WIN32
clockGetTime(0, &ts);
#else
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
perror("Error getting current time");
}
#endif // _WIN32
ts.tv_sec += 10;
while ((s = sem_timedwait(&job_semaphore, &ts)) == -1 && errno == EINTR) {
// Restart if interrupted by handler
continue;
}
// Timeout or error occurred
if (s == -1) {
if (errno != ETIMEDOUT) {
perror("Error waiting on semaphore");
}
}
// Job to process
else {
}
Code for clockGetTime
#define HAVE_STRUCT_TIMESPEC
#include <windows.h>
#include <pthread.h>
int clockGetTime(int clock_filler, struct timespec *spec) { //C-file part
__int64 wintime; GetSystemTimeAsFileTime((FILETIME*)&wintime);
wintime -= 116444736000000000i64; //1jan1601 to 1jan1970
spec->tv_sec =(time_t) (wintime / 10000000i64); //seconds
spec->tv_nsec =(long) (wintime % 10000000i64 * 100); //nano-seconds
return 0;
}