mytbf_t *mytbf_init(int cps,int burst) { struct mytbf_st *me; int pos; pthread_once(&once_init,module_load); me = malloc(sizeof(*me)); if(me == NULL) return NULL; me->cps = cps; me->burst = burst; me->token = 0; pthread_mutex_init(&me->mut,NULL); pthread_cond_init(&me->cond,NULL); pthread_mutex_lock(&mut_job); pos = get_free_pos_unlocked(); if(pos < 0) { pthread_mutex_unlock(&mut_job); pthread_mutex_destroy(&me->mut); free(me); return NULL; } me->pos = pos; job[pos] = me; pthread_mutex_unlock(&mut_job); return me; }
int mytbf_new(mytbf_t**ptr, int cps, int burst) { struct mytbf_st *self; int pos; pthread_once(&init_once, module_load); self = malloc(sizeof(*self)); if (self==NULL) { return errno; } self->token = 0; self->cps = cps; self->burst = burst; pthread_mutex_lock(&mut_job); pos = get_free_pos_unlocked(); if (pos<0) { pthread_mutex_unlock(&mut_job); free(self); return EBUSY; } self->pos = pos; job[pos] = self; pthread_mutex_init(&self->mut, NULL); pthread_mutex_unlock(&mut_job); *ptr = self; return 0; }
int rel_addjob(int fd1,int fd2) { struct rel_job_st *me; int pos; if(fd1 < 0 || fd2 < 0) return -EINVAL; me = malloc(sizeof(*me)); if(me == NULL) return -ENOMEM; me->fd1 = fd1; me->fd2 = fd2; me->job_state = STATE_RUNNING; //使用阻塞IO完全可以,select可以具体获取那个fd的什么类型事件 //me->fd1_save = fcntl(me->fd1,F_GETFL); //fcntl(me->fd1,F_SETFL,me->fd1_save|O_NONBLOCK); //me->fd2_save = fcntl(me->fd2,F_GETFL); //fcntl(me->fd2,F_SETFL,me->fd2_save|O_NONBLOCK); me->fsm12.sfd = me->fd1; me->fsm12.dfd = me->fd2; me->fsm12.count = 0; me->fsm12.state = STATE_R; me->fsm21.sfd = me->fd2; me->fsm21.dfd = me->fd1; me->fsm21.count = 0; me->fsm21.state = STATE_R; pthread_mutex_lock(&mut_rel_job); pos = get_free_pos_unlocked(); if(pos < 0) { pthread_mutex_unlock(&mut_rel_job); //fcntl(me->fd1,F_SETFL,me->fd1_save); //fcntl(me->fd2,F_SETFL,me->fd2_save); free(me); return -ENOSPC; } rel_job[pos] = me; pthread_mutex_unlock(&mut_rel_job); return pos; }