📄 ospfh_thread.c
字号:
for (tt = m->timer.head; tt; tt = tt->next)
if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0)
break;
if (tt)
thread_list_add_before (&m->timer, tt, thread);
else
thread_list_add (&m->timer, thread);
#endif /* TIMER_NO_SORT */
return thread;
}
/* Add simple event thread. */
struct thread *
thread_add_event (struct thread_master *m,
int (*func) (struct thread *), void *arg, int val)
{
struct thread *thread;
assert (m != NULL);
thread = thread_get (m, THREAD_EVENT, func, arg);
thread->u.val = val;
thread_list_add (&m->event, thread);
return thread;
}
/* Cancel thread from scheduler. */
void
thread_cancel (struct thread *thread)
{
switch (thread->type)
{
case THREAD_READ:
assert (FD_ISSET (thread->u.fd, &thread->master->readfd));
FD_CLR (thread->u.fd, &thread->master->readfd);
thread_list_delete (&thread->master->read, thread);
break;
case THREAD_WRITE:
assert (FD_ISSET (thread->u.fd, &thread->master->writefd));
FD_CLR (thread->u.fd, &thread->master->writefd);
thread_list_delete (&thread->master->write, thread);
break;
case THREAD_TIMER:
thread_list_delete (&thread->master->timer, thread);
break;
case THREAD_EVENT:
thread_list_delete (&thread->master->event, thread);
break;
case THREAD_READY:
thread_list_delete (&thread->master->ready, thread);
break;
default:
break;
}
thread->type = THREAD_UNUSED;
thread_add_unuse (thread->master, thread);
}
/* Delete all events which has argument value arg. */
void
thread_cancel_event (struct thread_master *m, void *arg)
{
struct thread *thread;
thread = m->event.head;
while (thread)
{
struct thread *t;
t = thread;
thread = t->next;
if (t->arg == arg)
{
thread_list_delete (&m->event, t);
t->type = THREAD_UNUSED;
thread_add_unuse (m, t);
}
}
}
#ifdef TIMER_NO_SORT
struct timeval *
thread_timer_wait (struct thread_master *m, struct timeval *timer_val)
{
struct timeval timer_now;
struct timeval timer_min;
struct timeval *timer_wait;
gettimeofday (&timer_now, NULL);
timer_wait = NULL;
for (thread = m->timer.head; thread; thread = thread->next)
{
if (! timer_wait)
timer_wait = &thread->u.sands;
else if (timeval_cmp (thread->u.sands, *timer_wait) < 0)
timer_wait = &thread->u.sands;
}
if (m->timer.head)
{
timer_min = *timer_wait;
timer_min = timeval_subtract (timer_min, timer_now);
if (timer_min.tv_sec < 0)
{
timer_min.tv_sec = 0;
timer_min.tv_usec = 10;
}
timer_wait = &timer_min;
}
else
timer_wait = NULL;
if (timer_wait)
{
*timer_val = timer_wait;
return timer_val;
}
return NULL;
}
#else /* ! TIMER_NO_SORT */
struct timeval *
thread_timer_wait (struct thread_master *m, struct timeval *timer_val)
{
struct timeval timer_now;
struct timeval timer_min;
if (m->timer.head)
{
gettimeofday (&timer_now, NULL);
timer_min = m->timer.head->u.sands;
timer_min = timeval_subtract (timer_min, timer_now);
if (timer_min.tv_sec < 0)
{
timer_min.tv_sec = 0;
timer_min.tv_usec = 10;
}
*timer_val = timer_min;
return timer_val;
}
return NULL;
}
#endif /* TIMER_NO_SORT */
struct thread *
thread_run (struct thread_master *m, struct thread *thread,
struct thread *fetch)
{
*fetch = *thread;
thread->type = THREAD_UNUSED;
thread_add_unuse (m, thread);
return fetch;
}
int
thread_process_fd (struct thread_master *m, struct thread_list *list,
fd_set *fdset, fd_set *mfdset)
{
struct thread *thread;
struct thread *next;
int ready = 0;
for (thread = list->head; thread; thread = next)
{
next = thread->next;
if (FD_ISSET (THREAD_FD (thread), fdset))
{
assert (FD_ISSET (THREAD_FD (thread), mfdset));
FD_CLR(THREAD_FD (thread), mfdset);
thread_list_delete (list, thread);
thread_list_add (&m->ready, thread);
thread->type = THREAD_READY;
ready++;
}
}
return ready;
}
/* Fetch next ready thread. */
struct thread *
thread_fetch (struct thread_master *m, struct thread *fetch)
{
int num;
int ready;
struct thread *thread;
fd_set readfd;
fd_set writefd;
fd_set exceptfd;
struct timeval timer_now;
struct timeval timer_val;
struct timeval *timer_wait;
struct timeval timer_nowait;
timer_nowait.tv_sec = 0;
timer_nowait.tv_usec = 0;
while (1)
{
/* Normal event is the highest priority. */
if ((thread = thread_trim_head (&m->event)) != NULL)
return thread_run (m, thread, fetch);
/* Execute timer. */
gettimeofday (&timer_now, NULL);
for (thread = m->timer.head; thread; thread = thread->next)
if (timeval_cmp (timer_now, thread->u.sands) >= 0)
{
thread_list_delete (&m->timer, thread);
return thread_run (m, thread, fetch);
}
/* If there are any ready threads, process top of them. */
if ((thread = thread_trim_head (&m->ready)) != NULL)
return thread_run (m, thread, fetch);
/* Structure copy. */
readfd = m->readfd;
writefd = m->writefd;
exceptfd = m->exceptfd;
/* Calculate select wait timer. */
timer_wait = thread_timer_wait (m, &timer_val);
num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait);
if (num == 0)
continue;
if (num < 0)
{
//if (errno == EINTR)
continue;
zlog_warn ("select() error: %s", strerror (errno));
return NULL;
}
/* Normal priority read thead. */
ready = thread_process_fd (m, &m->read, &readfd, &m->readfd);
/* Write thead. */
ready = thread_process_fd (m, &m->write, &writefd, &m->writefd);
if ((thread = thread_trim_head (&m->ready)) != NULL)
return thread_run (m, thread, fetch);
}
}
static unsigned long
thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start)
{
unsigned long thread_time;
#ifdef HAVE_RUSAGE
/* This is 'user + sys' time. */
thread_time = timeval_elapsed (now->ru_utime, start->ru_utime);
thread_time += timeval_elapsed (now->ru_stime, start->ru_stime);
#else
/* When rusage is not available, simple elapsed time is used. */
thread_time = timeval_elapsed (*now, *start);
#endif /* HAVE_RUSAGE */
return thread_time;
}
/* We should aim to yield after THREAD_YIELD_TIME_SLOT
milliseconds. */
int
thread_should_yield (struct thread *thread)
{
RUSAGE_T ru;
GETRUSAGE (&ru);
if (thread_consumed_time (&ru, &thread->ru) > THREAD_YIELD_TIME_SLOT)
return 1;
else
return 0;
}
/* We check thread consumed time. If the system has getrusage, we'll
use that to get indepth stats on the performance of the thread. If
not - we'll use gettimeofday for some guestimation. */
void
thread_call (struct thread *thread)
{
unsigned long thread_time;
RUSAGE_T ru;
GETRUSAGE (&thread->ru);
(*thread->func) (thread);
GETRUSAGE (&ru);
thread_time = thread_consumed_time (&ru, &thread->ru);
#ifdef THREAD_CONSUMED_TIME_CHECK
if (thread_time > 200000L)
{
/*
* We have a CPU Hog on our hands.
* Whinge about it now, so we're aware this is yet another task
* to fix.
*/
zlog_err ("CPU HOG task %lx ran for %ldms",
/* FIXME: report the name of the function somehow */
(unsigned long) thread->func,
thread_time / 1000L);
}
#endif /* THREAD_CONSUMED_TIME_CHECK */
}
/* Execute thread */
struct thread *
thread_execute (struct thread_master *m,
int (*func)(struct thread *),
void *arg,
int val)
{
struct thread dummy;
memset (&dummy, 0, sizeof (struct thread));
dummy.type = THREAD_EVENT;
dummy.master = NULL;
dummy.func = func;
dummy.arg = arg;
dummy.u.val = val;
thread_call (&dummy);
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -