55
66typedef struct RMUtilTimer {
77 RMutilTimerFunc cb ;
8+ RMUtilTimerTerminationFunc onTerm ;
89 void * privdata ;
910 struct timespec interval ;
1011 pthread_t thread ;
@@ -45,16 +46,34 @@ static void *rmutilTimer_Loop(void *ctx) {
4546 // It's up to the user to decide whether automemory is active there
4647 if (rctx ) RedisModule_FreeThreadSafeContext (rctx );
4748 }
49+ if (rc == EINVAL ) {
50+ perror ("Error waiting for condition" );
51+ break ;
52+ }
53+ }
54+
55+ // call the termination callback if needed
56+ if (tm -> onTerm != NULL ) {
57+ tm -> onTerm (tm -> privdata );
4858 }
49- // RedisModule_Log(tm->redisCtx, "notice", "Timer cancelled");
59+
60+ // free resources associated with the timer
61+ pthread_cond_destroy (& tm -> cond );
62+ free (tm );
5063
5164 return NULL ;
5265}
5366
54- RMUtilTimer * RMUtil_NewPeriodicTimer (RMutilTimerFunc cb , void * privdata , struct timespec interval ) {
67+ /* set a new frequency for the timer. This will take effect AFTER the next trigger */
68+ void RMUtilTimer_SetInterval (struct RMUtilTimer * t , struct timespec newInterval ) {
69+ t -> interval = newInterval ;
70+ }
71+
72+ RMUtilTimer * RMUtil_NewPeriodicTimer (RMutilTimerFunc cb , RMUtilTimerTerminationFunc onTerm ,
73+ void * privdata , struct timespec interval ) {
5574 RMUtilTimer * ret = malloc (sizeof (* ret ));
5675 * ret = (RMUtilTimer ){
57- .privdata = privdata , .interval = interval , .cb = cb ,
76+ .privdata = privdata , .interval = interval , .cb = cb , . onTerm = onTerm ,
5877 };
5978 pthread_cond_init (& ret -> cond , NULL );
6079 pthread_mutex_init (& ret -> lock , NULL );
@@ -63,14 +82,6 @@ RMUtilTimer *RMUtil_NewPeriodicTimer(RMutilTimerFunc cb, void *privdata, struct
6382 return ret ;
6483}
6584
66- int RMUtilTimer_Stop (RMUtilTimer * t ) {
67- int rc ;
68- if (0 == (rc = pthread_cond_signal (& t -> cond ))) {
69- rc = pthread_join (t -> thread , NULL );
70- }
71- return rc ;
72- }
73-
74- void RMUtilTimer_Free (RMUtilTimer * t ) {
75- free (t );
85+ int RMUtilTimer_Terminate (struct RMUtilTimer * t ) {
86+ return pthread_cond_signal (& t -> cond );
7687}
0 commit comments