FreeBSD Bugzilla – Attachment 234909 Details for
Bug 264867
kevent EVFILT_TIMER, timer expiration with twice the period
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
example reproducing the problem
test_MT.c (text/plain), 4.04 KB, created by
felice.tufo
on 2022-06-24 09:38:28 UTC
(
hide
)
Description:
example reproducing the problem
Filename:
MIME Type:
Creator:
felice.tufo
Created:
2022-06-24 09:38:28 UTC
Size:
4.04 KB
patch
obsolete
>#include <stdio.h> >#include <stdbool.h> >#include <errno.h> >#include <pthread.h> >#include <string.h> >#include <time.h> >#include <unistd.h> >#include <sys/event.h> >#define NUM_TIMERS 15U >#define WAIT_MS 20U >#define WAIT_TOL_MS 2U > >static pthread_mutex_t printMutex; > > >extern void time_utils_diff(const struct timespec *start, const struct timespec *stop, struct timespec *result) >{ > long tv_nsec_diff; > > tv_nsec_diff = stop->tv_nsec - start->tv_nsec; > if (tv_nsec_diff < 0) > { > result->tv_sec = stop->tv_sec - start->tv_sec - 1; > result->tv_nsec = tv_nsec_diff + 1000000000L; > } > else > { > result->tv_sec = stop->tv_sec - start->tv_sec; > result->tv_nsec = tv_nsec_diff; > } >} > >static void *runJob(void *par) >{ > struct kevent eventsMon[1]; > struct kevent eventsTrig[1]; > bool running = true; > int numEvent; > struct timespec preTime; > struct timespec postTime; > struct timespec resTime; > > unsigned long cicle = 0; > > uint32_t *pInt = (uint32_t*)par; > > int WAIT_NS_TOL = ((WAIT_MS + WAIT_TOL_MS) * 1000000); > int WAITS_PER_MIN = (60000 / WAIT_MS); > > int kq = kqueue(); > if (-1 == kq) > { > pthread_mutex_lock(&printMutex); > printf("kqueue creation failed, thread %d\n\n", *pInt); > fflush(stdout); > pthread_mutex_unlock(&printMutex); > > return NULL; > } > > pthread_mutex_lock(&printMutex); > printf("Thread %d started\n\n", *pInt); > fflush(stdout); > pthread_mutex_unlock(&printMutex); > > > while (running) > { > EV_SET(&eventsMon[0], *pInt, EVFILT_TIMER, (EV_ADD | EV_ENABLE), NOTE_MSECONDS, WAIT_MS, NULL); > > clock_gettime(CLOCK_MONOTONIC, &preTime); > numEvent = kevent(kq, &eventsMon[0], 1, &eventsTrig[0], 1, NULL); > clock_gettime(CLOCK_MONOTONIC, &postTime); > > if ((numEvent < 0) || (eventsTrig[0].flags == EV_ERROR)) > { > /* An error occurred. */ > pthread_mutex_lock(&printMutex); > printf("An error occurred on thread %d (event count %d). The error was %s.\n\n", *pInt, numEvent, strerror(errno)); > fflush(stdout); > pthread_mutex_unlock(&printMutex); > > running = false; > } > else if (numEvent == 0) > { > pthread_mutex_lock(&printMutex); > printf("Numevent 0 on thread %d\n\n", *pInt); > fflush(stdout); > pthread_mutex_unlock(&printMutex); > } > else > { > time_utils_diff(&preTime, &postTime, &resTime); > > if((resTime.tv_sec > 0) || (resTime.tv_nsec > WAIT_NS_TOL)) > { > pthread_mutex_lock(&printMutex); > printf("Thread %d\nstart sec %ld nsec %ld \nstop sec %ld nsec %ld \nwait sec %ld nsec %ld\n\n", > *pInt,preTime.tv_sec, preTime.tv_nsec, postTime.tv_sec, postTime.tv_nsec, resTime.tv_sec, resTime.tv_nsec); > fflush(stdout); > pthread_mutex_unlock(&printMutex); > > running = false; > } > else > { > if((cicle % WAITS_PER_MIN) == 0U) > { > pthread_mutex_lock(&printMutex); > printf("Cycle %ld - Thread %d OK\n", cicle, *pInt); > fflush(stdout); > pthread_mutex_unlock(&printMutex); > } > > cicle++; > } > } > } > > close(kq); > return NULL; >} > >int main() >{ > struct sched_param param; > param.sched_priority = 0; > if (sched_setscheduler(0, SCHED_FIFO, ¶m) < 0) > { > return false; > } > > pthread_t thrList[NUM_TIMERS]; > uint32_t thrIds[NUM_TIMERS] = {0}; > pthread_mutex_init(&printMutex, NULL); > long unsigned int cicle = 0; > > for (uint32_t idx = 0; idx < NUM_TIMERS; idx++) > { > thrIds[idx] = 1230U + idx; > if (pthread_create(&thrList[idx], NULL, runJob, &thrIds[idx]) != 0) > { > pthread_mutex_lock(&printMutex); > printf("Cannot create thread %d\n\n", thrIds[idx]); > fflush(stdout); > pthread_mutex_unlock(&printMutex); > } > } > > while(true) > { > sleep(1); > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 264867
: 234909 |
235005
|
235023
|
235195