📄 ospfh_thread.c
字号:
/*ospfh_thread.c*/
#include "ospfh.h"
#include "ospfh_patch.h"
/* Struct timeval's tv_usec one second value. */
#define TIMER_SECOND_MICRO 1000000L
struct timeval
timeval_adjust (struct timeval a)
{
while (a.tv_usec >= TIMER_SECOND_MICRO)
{
a.tv_usec -= TIMER_SECOND_MICRO;
a.tv_sec++;
}
while (a.tv_usec < 0)
{
a.tv_usec += TIMER_SECOND_MICRO;
a.tv_sec--;
}
if (a.tv_sec < 0)
{
a.tv_sec = 0;
a.tv_usec = 10;
}
if (a.tv_sec > TIMER_SECOND_MICRO)
a.tv_sec = TIMER_SECOND_MICRO;
return a;
}
static struct timeval
timeval_subtract (struct timeval a, struct timeval b)
{
struct timeval ret;
ret.tv_usec = a.tv_usec - b.tv_usec;
ret.tv_sec = a.tv_sec - b.tv_sec;
return timeval_adjust (ret);
}
static int
timeval_cmp (struct timeval a, struct timeval b)
{
return (a.tv_sec == b.tv_sec
? a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
}
static unsigned long
timeval_elapsed (struct timeval a, struct timeval b)
{
return (((a.tv_sec - b.tv_sec) * TIMER_SECOND_MICRO)
+ (a.tv_usec - b.tv_usec));
}
/* List allocation and head/tail print out. */
static void
thread_list_debug (struct thread_list *list)
{
printf ("count [%d] head [%p] tail [%p]\n",
list->count, list->head, list->tail);
}
/* Debug print for thread_master. */
void
thread_master_debug (struct thread_master *m)
{
printf ("-----------\n");
printf ("readlist : ");
thread_list_debug (&m->read);
printf ("writelist : ");
thread_list_debug (&m->write);
printf ("timerlist : ");
thread_list_debug (&m->timer);
printf ("eventlist : ");
thread_list_debug (&m->event);
printf ("unuselist : ");
thread_list_debug (&m->unuse);
printf ("total alloc: [%ld]\n", m->alloc);
printf ("-----------\n");
}
/* Allocate new thread master. */
struct thread_master *
thread_master_create ()
{
struct thread_master * master;
master=(struct thread_master *) XCALLOC (MTYPE_THREAD_MASTER,
sizeof (struct thread_master));
master->alloc=0;
master->readfd.fd_count=0;
master->writefd.fd_count=0;
master->exceptfd.fd_count=0;
master->read.head=NULL;
master->read.tail=NULL;
master->read.count=0;
master->write.head=NULL;
master->write.tail=NULL;
master->write.count=0;
master->timer.head=NULL;
master->timer.tail=NULL;
master->timer.count=0;
master->event.head=NULL;
master->event.tail=NULL;
master->event.count=0;
master->ready.head=NULL;
master->ready.tail=NULL;
master->ready.count=0;
master->unuse.head=NULL;
master->unuse.tail=NULL;
master->unuse.count=0;
return master;
}
/* Add a new thread to the list. */
static void
thread_list_add (struct thread_list *list, struct thread *thread)
{
thread->next = NULL;
thread->prev = list->tail;
if (list->tail)
list->tail->next = thread;
else
list->head = thread;
list->tail = thread;
list->count++;
}
/* Add a new thread just before the point. */
static void
thread_list_add_before (struct thread_list *list,
struct thread *point,
struct thread *thread)
{
thread->next = point;
thread->prev = point->prev;
if (point->prev)
point->prev->next = thread;
else
list->head = thread;
point->prev = thread;
list->count++;
}
/* Delete a thread from the list. */
static struct thread *
thread_list_delete (struct thread_list *list, struct thread *thread)
{
if (thread->next)
thread->next->prev = thread->prev;
else
list->tail = thread->prev;
if (thread->prev)
thread->prev->next = thread->next;
else
list->head = thread->next;
thread->next = thread->prev = NULL;
list->count--;
return thread;
}
/* Move thread to unuse list. */
static void
thread_add_unuse (struct thread_master *m, struct thread *thread)
{
assert (m != NULL);
assert (thread->next == NULL);
assert (thread->prev == NULL);
assert (thread->type == THREAD_UNUSED);
thread_list_add (&m->unuse, thread);
}
/* Free all unused thread. */
static void
thread_list_free (struct thread_master *m, struct thread_list *list)
{
struct thread *t;
struct thread *next;
for (t = list->head; t; t = next)
{
next = t->next;
XFREE (MTYPE_THREAD, t);
list->count--;
m->alloc--;
}
}
/* Stop thread scheduler. */
void
thread_master_free (struct thread_master *m)
{
thread_list_free (m, &m->read);
thread_list_free (m, &m->write);
thread_list_free (m, &m->timer);
thread_list_free (m, &m->event);
thread_list_free (m, &m->ready);
thread_list_free (m, &m->unuse);
XFREE (MTYPE_THREAD_MASTER, m);
}
/* Delete top of the list and return it. */
static struct thread *
thread_trim_head (struct thread_list *list)
{
if (list->head)
return thread_list_delete (list, list->head);
return NULL;
}
/* Thread list is empty or not. */
int
thread_empty (struct thread_list *list)
{
return list->head ? 0 : 1;
}
/* Return remain time in second. */
unsigned long
thread_timer_remain_second (struct thread *thread)
{
struct timeval timer_now;
gettimeofday (&timer_now, NULL);
if (thread->u.sands.tv_sec - timer_now.tv_sec > 0)
return thread->u.sands.tv_sec - timer_now.tv_sec;
else
return 0;
}
/* Get new thread. */
static struct thread *
thread_get (struct thread_master *m, u_char type,
int (*func) (struct thread *), void *arg)
{
struct thread *thread;
if (m->unuse.head)
thread = thread_trim_head (&m->unuse);
else
{
thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread));
m->alloc++;
}
thread->type = type;
thread->master = m;
thread->func = func;
thread->arg = arg;
return thread;
}
/* Add new read thread. */
struct thread *
thread_add_read (struct thread_master *m,
int (*func) (struct thread *), void *arg, int fd)
{
struct thread *thread;
assert (m != NULL);
if (FD_ISSET (fd, &m->readfd))
{
zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd);
return NULL;
}
thread = thread_get (m, THREAD_READ, func, arg);
FD_SET (fd, &m->readfd);
thread->u.fd = fd;
thread_list_add (&m->read, thread);
return thread;
}
/* Add new write thread. */
struct thread *
thread_add_write (struct thread_master *m,
int (*func) (struct thread *), void *arg, int fd)
{
struct thread *thread;
assert (m != NULL);
if (FD_ISSET (fd, &m->writefd))
{
zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd);
return NULL;
}
thread = thread_get (m, THREAD_WRITE, func, arg);
FD_SET (fd, &m->writefd);
thread->u.fd = fd;
thread_list_add (&m->write, thread);
return thread;
}
/* Add timer event thread. */
struct thread *
thread_add_timer (struct thread_master *m,
int (*func) (struct thread *), void *arg, long timer)
{
struct timeval timer_now;
struct thread *thread;
#ifndef TIMER_NO_SORT
struct thread *tt;
#endif /* TIMER_NO_SORT */
assert (m != NULL);
thread = thread_get (m, THREAD_TIMER, func, arg);
/* Do we need jitter here? */
gettimeofday (&timer_now, NULL);
timer_now.tv_sec += timer;
thread->u.sands = timer_now;
/* Sort by timeval. */
#ifdef TIMER_NO_SORT
thread_list_add (&m->timer, thread);
#else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -