Using Linux timerfd_create to implement Timer sample sharing _linux Shell

Source: Internet
Author: User

Timer_poll.h

Copy Code code as follows:

/*
* File:timer_poll.h
* Author:administrator
*/

#ifndef Timer_poll_h
#define Timer_poll_h
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/epoll.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <pthread.h>
#include <map>

#define MAXFDS 128
#define EVENTS 100
class timer;
typedef int (*timer_callback) (timer &);//user callback

Class Timer
{
Public

Timer (): timer_internal (0.0), CB (0), timer_id (0), repeat (0), UserData (0) {}
Timer (double internal_value, int (*callback) (timer &ptimer), void *data, int rep): Timer_internal (Internal_value), C B (Callback), UserData (data), repeat (Rep)
{
timer_id = timerfd_create (clock_realtime, 0);
Setnonblock (timer_id);
}

Timer (const timer &ptimer);
Timer & operator= (const timer &ptimer);
int Timer_start ();
int Timer_stop ();
int timer_modify_internal (double timer_internal);

int timer_get_id ()
{
return timer_id;
}

void *timer_get_userdata ()
{
return userdata;
}

Timer_callback Get_user_callback ()
{
return CB;
}

~timer ()
{
Timer_stop ();
}

Private

BOOL Setnonblock (int fd)
{
int flags = FCNTL (FD, F_GETFL, 0);
Flags |= O_nonblock;
if ( -1 = = Fcntl (FD, F_SETFL, flags))
{
return false;
}
return true;
}
int timer_id;
Double timer_internal;
void *userdata;
BOOL Repeat;//will the timer repeat or only once
Timer_callback CB;
} ;
Class Timers_poll
{
Public
Timers_poll (int max_num=128)
{
active = 1;
EPFD = Epoll_create (max_num);
}

int Timers_poll_add_timer (timer &ptimer);
int Timers_poll_del_timer (timer &ptimer);
int run ();

int timers_poll_deactive ()
{
active = 0;
}

~ Timers_poll ()
{

}
Private
int EPFD;
int active;
Std::map<int, timer> Timers_map;
/* Data * *
} ;
#endif/* Timer_poll_h * *

Timer_poll.cpp

Copy Code code as follows:

/*
* File:timer_poll.cpp
* Author:administrator
*/

#include <cstdlib>
#include "timer_poll.h"

using namespace Std;

Timer::timer (const timer& Ptimer)
{
Timer_internal = ptimer.timer_internal;
cb = PTIMER.CB;
timer_id = ptimer.timer_id;
repeat = ptimer.repeat;
UserData = Ptimer.userdata;
}

Timer & timer::operator = (const timer& ptimer)
{
if (this = = &ptimer)
{
return *this;
}

Timer_internal = ptimer.timer_internal;
cb = PTIMER.CB;
timer_id = ptimer.timer_id;
repeat = ptimer.repeat;
UserData = Ptimer.userdata;
return *this;
}

int Timer::timer_start ()
{
struct Itimerspec ptime_internal = {0};
ptime_internal.it_value.tv_sec = (int) timer_internal;
Ptime_internal.it_value.tv_nsec = (timer_internal-(int) timer_internal) *1000000;
if (repeat)
{
Ptime_internal.it_interval.tv_sec = ptime_internal.it_value.tv_sec;
Ptime_internal.it_interval.tv_nsec = ptime_internal.it_value.tv_nsec;
}

Timerfd_settime (timer_id, 0, &ptime_internal, NULL);
return 0;
}

int Timer::timer_stop ()
{
Close (timer_id);
return 0;
}

int timer::timer_modify_internal (double timer_internal)
{
This->timer_internal = timer_internal;
Timer_start ();
}

int Timers_poll::timers_poll_add_timer (timer& ptimer)
{
int timer_id = ptimer.timer_get_id ();
struct epoll_event ev;
EV.DATA.FD = timer_id;
ev.events = Epollin | Epollet;
TIMERS_MAP[TIMER_ID] = Ptimer; Add or modify
Epoll_ctl (EPFD, Epoll_ctl_add, timer_id, &ev);
Ptimer.timer_start ();

return 0;
}

int Timers_poll::timers_poll_del_timer (timer& ptimer)
{
int timer_id = ptimer.timer_get_id ();
struct epoll_event ev;
EV.DATA.FD = timer_id;
ev.events = Epollin | Epollet;
Epoll_ctl (EPFD, Epoll_ctl_del, timer_id, &ev);
Timers_map.erase (timer_id);

return 0;
}

int Timers_poll::run ()
{
Char buf[128] ={0};
for (; active;)
{
struct epoll_event Events[maxfds] ={0};
int Nfds = epoll_wait (EPFD, events, Maxfds,-1);
for (int i = 0; i < Nfds; ++i)
{
Std::map<int, Timer>::iterator itmp = Timers_map.find (EVENTS[I].DATA.FD);
if (itmp!= timers_map.end ())
{
Timer Ptimer = itmp->second;
while (read (EVENTS[I].DATA.FD, buf, 128) > 0);
Itmp->second.get_user_callback () (Itmp->second);
}
}
}
}

Main.cpp

Copy Code code as follows:

/*
* File:main.cpp
* Author:administrator
*/

#include <cstdlib>
#include <iostream>

#include "timer_poll.h"

using namespace Std;

int callback (Timer &ptimer)
{
printf ("Timer id=%d:%s\n", ptimer.timer_get_id (), (char *) ptimer.timer_get_userdata ());
return 0;
}

void *thread_fun (void *data)
{
Timers_poll *my_timers = (Timers_poll *) data;
My_timers->run ();
}

/*
*
*/
int main (int argc, char** argv)
{
Timers_poll my_timers (128);
pthread_t thread_id = 0;
Pthread_create (&thread_id, NULL, Thread_fun, &my_timers);


Timer timer1 (1.05, callback, (void *) "Hello 1", 0);
Timer Timer2 (1.10, Callback, (void *) "Hello 2", 0);

Timer1.timer_start ();
Timer2.timer_start ();

My_timers.timers_poll_add_timer (timer1);
My_timers.timers_poll_add_timer (TIMER2);

Sleep (5);
My_timers.timers_poll_del_timer (TIMER2);
cout<< "del complete" <<endl;
Timer1.timer_modify_internal (5.1);
Timer2.timer_modify_internal (10.1);
cout<< "Modify Complete" <<endl;
Sleep (4);
My_timers.timers_poll_del_timer (TIMER2);

Sleep (5);

My_timers.timers_poll_deactive ();

Pthread_join (Thread_id,null);
return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.