⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ospfh_thread.h

📁 实现禁位自动排列在禁位中具体实现了此方法所以这是一个创新很有应用价值在VC平台上调试通过
💻 H
字号:
#include "winsock.h"
#include "string.h"
#include "stdlib.h"
#include "stdio.h"



#ifndef THREAD_H
#define THREAD_H

#ifdef HAVE_RUSAGE
#define RUSAGE_T        struct rusage
#define GETRUSAGE(X)    getrusage (RUSAGE_SELF, X);
#else
#define RUSAGE_T        struct timeval
#define GETRUSAGE(X)    gettimeofday (X, NULL);
#endif /* HAVE_RUSAGE */

/* Linked list of thread. */
struct thread_list
{
  struct thread *head;
  struct thread *tail;
  int count;
};

/* Master of the theads. */
struct thread_master
{
  struct thread_list read;
  struct thread_list write;
  struct thread_list timer;
  struct thread_list event;
  struct thread_list ready;
  struct thread_list unuse;
  fd_set readfd;
  fd_set writefd;
  fd_set exceptfd;
  unsigned long alloc;
};

/* Thread itself. */
struct thread
{
  unsigned char type;		/* thread type */
  struct thread *next;		/* next pointer of the thread */
  struct thread *prev;		/* previous pointer of the thread */
  struct thread_master *master;	/* pointer to the struct thread_master. */
  int (*func) (struct thread *); /* event function */
  void *arg;			/* event argument */
  union {
    int val;			/* second argument of the event. */
    int fd;			/* file descriptor in case of read/write. */
    struct timeval sands;	/* rest of time sands value. */
  } u;
  RUSAGE_T ru;			/* Indepth usage info.  */
};

/* Thread types. */
#define THREAD_READ           0
#define THREAD_WRITE          1
#define THREAD_TIMER          2
#define THREAD_EVENT          3
#define THREAD_READY          4
#define THREAD_UNUSED         5

/* Thread yield time.  */
#define THREAD_YIELD_TIME_SLOT     100 * 1000L /* 100ms */

/* Macros. */
#define THREAD_ARG(X) ((X)->arg)
#define THREAD_FD(X)  ((X)->u.fd)
#define THREAD_VAL(X) ((X)->u.val)

#define THREAD_READ_ON(master,thread,func,arg,sock) \
  do { \
    if (! thread) \
      thread = thread_add_read (master, func, arg, sock); \
  } while (0)

#define THREAD_WRITE_ON(master,thread,func,arg,sock) \
  do { \
    if (! thread) \
      thread = thread_add_write (master, func, arg, sock); \
  } while (0)

#define THREAD_TIMER_ON(master,thread,func,arg,time) \
  do { \
    if (! thread) \
      thread = thread_add_timer (master, func, arg, time); \
  } while (0)

#define THREAD_OFF(thread) \
  do { \
    if (thread) \
      { \
        thread_cancel (thread); \
        thread = NULL; \
      } \
  } while (0)

#define THREAD_READ_OFF(thread)  THREAD_OFF(thread)
#define THREAD_WRITE_OFF(thread)  THREAD_OFF(thread)
#define THREAD_TIMER_OFF(thread)  THREAD_OFF(thread)


struct timeval timeval_adjust (struct timeval );
static struct timeval timeval_subtract (struct timeval , struct timeval );
static int timeval_cmp (struct timeval , struct timeval );
static unsigned long timeval_elapsed (struct timeval , struct timeval );
static void thread_list_debug (struct thread_list *);
void thread_master_debug (struct thread_master *);
struct thread_master * thread_master_create ();
static void thread_list_add (struct thread_list *, struct thread *);
static void thread_list_add_before (struct thread_list *, struct thread *, struct thread *);
static struct thread * thread_list_delete (struct thread_list *, struct thread *);
static void thread_add_unuse (struct thread_master *, struct thread *);
static void thread_list_free (struct thread_master *, struct thread_list *);
void thread_master_free (struct thread_master *);
static struct thread * thread_trim_head (struct thread_list *);
int thread_empty (struct thread_list *);
unsigned long thread_timer_remain_second (struct thread *);
static struct thread * thread_get (struct thread_master *, u_char ,int (*) (struct thread *), void *);
struct thread * thread_add_read (struct thread_master *, int (*) (struct thread *), void *, int );
struct thread * thread_add_write (struct thread_master *,int (*) (struct thread *), void *, int );
struct thread * thread_add_timer (struct thread_master *,int (*) (struct thread *), void *, long );
struct thread * thread_add_event (struct thread_master *,int (*) (struct thread *), void *, int );
void thread_cancel (struct thread *);
void thread_cancel_event (struct thread_master *, void *);

#ifdef TIMER_NO_SORT
struct timeval * thread_timer_wait (struct thread_master *, struct timeval *);
#else /* ! TIMER_NO_SORT */
struct timeval * thread_timer_wait (struct thread_master *, struct timeval *);
#endif /* TIMER_NO_SORT */
struct thread * thread_run (struct thread_master *, struct thread *,
	    struct thread *);
int thread_process_fd (struct thread_master *, struct thread_list *,
		   fd_set *, fd_set *);
struct thread * thread_fetch (struct thread_master *, struct thread *);
static unsigned long thread_consumed_time (RUSAGE_T *, RUSAGE_T *);
int thread_should_yield (struct thread *);
void thread_call (struct thread *);
struct thread * thread_execute (struct thread_master *,
                int (*)(struct thread *), 
                void *,int );


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -