Advertisement
xverizex

new alarm

Nov 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. /*
  2.  * compile gcc main.c -lrt -o test
  3. */
  4. #include <stdio.h>
  5. #include <sys/time.h>
  6. #include <signal.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <pthread.h>
  10.  
  11. pthread_t t;
  12.  
  13. struct object {
  14.     timer_t *timerid;
  15.     struct sigevent *sev;
  16.     struct itimerspec *its;
  17.     int count;
  18.     void **func;
  19. };
  20.  
  21. struct thread_func {
  22.     void (*func) ( void *data );
  23.     void *data;
  24.     int thread;
  25. };
  26.  
  27. void func_hello ( void *data ) {
  28.     sleep ( 10000 );
  29.     printf ( "func_hello\n" );
  30. }
  31.  
  32. void func_neho ( void *data ) {
  33.     sleep ( 10000 );
  34.     printf ( "func_neho\n" );
  35. }
  36.  
  37. void func_fuck ( void *data ) {
  38.     sleep ( 10000 );
  39.     printf ( "func_fuck %s\n", (char *) data );
  40. }
  41.  
  42. void hh ( union sigval val ) {
  43.     struct thread_func *thf = ( struct thread_func * ) val.sival_ptr;
  44.     if ( thf->thread == 1 ) return;
  45.  
  46.     thf->thread = 1;
  47.     thf->func ( thf->data );
  48.     thf->thread = 0;
  49. }
  50.  
  51. void *handle ( void *data ) {
  52.     struct object *obj = ( struct object * ) data;
  53.  
  54.     char ch = getchar ( );
  55. }
  56.  
  57. struct object *obj_init ( ) {
  58.     struct object *obj = calloc ( 1, sizeof ( struct object ) );
  59.     obj->timerid = calloc ( 0, sizeof ( timer_t ) );
  60.     obj->sev = calloc ( 0, sizeof ( struct sigevent ) );
  61.     obj->its = calloc ( 0, sizeof ( struct itimerspec ) );
  62.     obj->func = calloc ( 0, sizeof ( void * ) );
  63.     obj->count = 0;
  64.     return obj;
  65. }
  66.  
  67. void obj_set_timer ( struct object *obj, int num, void (*func ) ( void *data ), void *data ) {
  68.     int index = obj->count;
  69.     obj->count++;
  70.     obj->timerid = realloc ( obj->timerid, sizeof ( timer_t ) * obj->count );
  71.     obj->sev = realloc ( obj->sev, sizeof ( struct sigevent ) * obj->count );
  72.     obj->its = realloc ( obj->its, sizeof ( struct itimerspec ) * obj->count );
  73.     obj->func = realloc ( obj->func, sizeof ( void * ) * obj->count );
  74.  
  75.     obj->sev[index].sigev_notify = SIGEV_THREAD;
  76.     obj->sev[index].sigev_notify_function = hh;
  77.     obj->sev[index].sigev_signo = SIGALRM;
  78.     struct thread_func *thf = calloc ( 1, sizeof ( struct thread_func ) );
  79.     thf->func = func;
  80.     thf->data = data;
  81.     obj->sev[index].sigev_value.sival_ptr = thf;
  82.  
  83.     if ( timer_create ( CLOCK_REALTIME, &obj->sev[index], &obj->timerid[index] ) == -1 ) fprintf ( stderr, "error timer_create.\n" );
  84.  
  85.     obj->its[index].it_value.tv_sec = num;
  86.     obj->its[index].it_value.tv_nsec = 0;
  87.     obj->its[index].it_interval.tv_sec = num;
  88.     obj->its[index].it_interval.tv_nsec = 0;
  89.     if ( timer_settime ( obj->timerid[index], 0, &obj->its[index], NULL ) == -1 ) fprintf ( stderr, "error timer settimer.\n" );
  90. }
  91.  
  92. int main ( ) {
  93.  
  94.     struct object *obj = obj_init ( );
  95.    
  96.     obj_set_timer ( obj, 1, func_hello, NULL );
  97.     obj_set_timer ( obj, 1, func_neho, NULL );
  98.     obj_set_timer ( obj, 1, func_fuck, "test" );
  99.  
  100.     char ch = getchar ( );
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement