| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Periodic tasks centralization. Used for TX part of network devices. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <stdarg.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <errno.h> |
| 14 |
#include <signal.h> |
| 15 |
#include <fcntl.h> |
| 16 |
#include <ctype.h> |
| 17 |
#include <time.h> |
| 18 |
#include <sys/time.h> |
| 19 |
#include <sys/types.h> |
| 20 |
#include <pthread.h> |
| 21 |
#include <assert.h> |
| 22 |
|
| 23 |
#include "ptask.h" |
| 24 |
|
| 25 |
static pthread_t ptask_thread; |
| 26 |
static pthread_mutex_t ptask_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 27 |
static ptask_t *ptask_list = NULL; |
| 28 |
static ptask_id_t ptask_current_id = 0; |
| 29 |
|
| 30 |
u_int ptask_sleep_time = 10; |
| 31 |
|
| 32 |
#define PTASK_LOCK() pthread_mutex_lock(&ptask_mutex) |
| 33 |
#define PTASK_UNLOCK() pthread_mutex_unlock(&ptask_mutex) |
| 34 |
|
| 35 |
/* Periodic task thread */ |
| 36 |
static void *ptask_run(void *arg) |
| 37 |
{ |
| 38 |
pthread_mutex_t umutex = PTHREAD_MUTEX_INITIALIZER; |
| 39 |
pthread_cond_t ucond = PTHREAD_COND_INITIALIZER; |
| 40 |
|
| 41 |
ptask_t *task; |
| 42 |
|
| 43 |
for(;;) { |
| 44 |
PTASK_LOCK(); |
| 45 |
for(task=ptask_list;task;task=task->next) |
| 46 |
task->cbk(task->object,task->arg); |
| 47 |
PTASK_UNLOCK(); |
| 48 |
|
| 49 |
/* For testing! */ |
| 50 |
{ |
| 51 |
struct timespec t_spc; |
| 52 |
m_tmcnt_t expire; |
| 53 |
|
| 54 |
expire = m_gettime_usec() + (ptask_sleep_time * 1000); |
| 55 |
|
| 56 |
pthread_mutex_lock(&umutex); |
| 57 |
t_spc.tv_sec = expire / 1000000; |
| 58 |
t_spc.tv_nsec = (expire % 1000000) * 1000; |
| 59 |
pthread_cond_timedwait(&ucond,&umutex,&t_spc); |
| 60 |
pthread_mutex_unlock(&umutex); |
| 61 |
} |
| 62 |
|
| 63 |
/* Old method... */ |
| 64 |
//usleep(ptask_sleep_time*1000); |
| 65 |
} |
| 66 |
|
| 67 |
return NULL; |
| 68 |
} |
| 69 |
|
| 70 |
/* Add a new task */ |
| 71 |
ptask_id_t ptask_add(ptask_callback cbk,void *object,void *arg) |
| 72 |
{ |
| 73 |
ptask_t *task; |
| 74 |
ptask_id_t id; |
| 75 |
|
| 76 |
if (!(task = malloc(sizeof(*task)))) { |
| 77 |
fprintf(stderr,"ptask_add: unable to add new task.\n"); |
| 78 |
return(-1); |
| 79 |
} |
| 80 |
|
| 81 |
memset(task,0,sizeof(*task)); |
| 82 |
task->cbk = cbk; |
| 83 |
task->object = object; |
| 84 |
task->arg = arg; |
| 85 |
|
| 86 |
PTASK_LOCK(); |
| 87 |
id = ++ptask_current_id; |
| 88 |
assert(id != 0); |
| 89 |
task->id = id; |
| 90 |
task->next = ptask_list; |
| 91 |
ptask_list = task; |
| 92 |
PTASK_UNLOCK(); |
| 93 |
return(id); |
| 94 |
} |
| 95 |
|
| 96 |
/* Remove a task */ |
| 97 |
int ptask_remove(ptask_id_t id) |
| 98 |
{ |
| 99 |
ptask_t **task,*p; |
| 100 |
int res = -1; |
| 101 |
|
| 102 |
PTASK_LOCK(); |
| 103 |
|
| 104 |
for(task=&ptask_list;*task;task=&(*task)->next) |
| 105 |
if ((*task)->id == id) { |
| 106 |
p = *task; |
| 107 |
*task = (*task)->next; |
| 108 |
free(p); |
| 109 |
res = 0; |
| 110 |
break; |
| 111 |
} |
| 112 |
|
| 113 |
PTASK_UNLOCK(); |
| 114 |
return(res); |
| 115 |
} |
| 116 |
|
| 117 |
/* Initialize ptask module */ |
| 118 |
int ptask_init(u_int sleep_time) |
| 119 |
{ |
| 120 |
if (sleep_time) |
| 121 |
ptask_sleep_time = sleep_time; |
| 122 |
|
| 123 |
if (pthread_create(&ptask_thread,NULL,ptask_run,NULL) != 0) { |
| 124 |
fprintf(stderr,"ptask_init: unable to create thread.\n"); |
| 125 |
return(-1); |
| 126 |
} |
| 127 |
|
| 128 |
return(0); |
| 129 |
} |