fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdio.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdlib.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <string.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <unistd.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <pthread.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "isns_server.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "isns_protocol.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "isns_log.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "isns_sched.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "isns_scn.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "isns_esi.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * extern variables.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * global variables.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortepthread_mutex_t el_mtx = PTHREAD_MUTEX_INITIALIZER;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * local variables.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic el_key_t **il;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int icurr = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic el_notice_t *curr = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic uint32_t DU;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic uint32_t DT;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic uint32_t LB;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic uint32_t NLIM;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * external variables.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * local functions.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ****************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * il_shift:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Shift the indexed-list to the most current time.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * t - most current time.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ****************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteil_shift(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_notice_t *fn, *n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_key_t *fk, *k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t nt;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = il[icurr];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (k->time < t) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fk = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fn = k->notice;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* remove the dummy key and dummy notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fk->left->right = fk->right;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fk->right->left = fk->left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fn->pred->sucd = fn->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fn->sucd->pred = fn->pred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* find the place where the dummy goes to */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = il[(icurr + DU - 1) % DU];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (k->time < INFINITY - DT) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nt = k->time + DT; /* next key time */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nt = INFINITY - 1; /* the last second */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (k->time < nt) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = k->right;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = k->notice->pred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (n->time >= nt) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c ++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = n->pred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = n->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* update lower bound */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LB = fk->time;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* insert the dummy key */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fk->time = nt;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fk->count = k->count - c + 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fk->left = k->left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fk->right = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left->right = fk;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left = fk;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->count = c;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* insert the dummy notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fn->time = nt;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fn->pred = n->pred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fn->sucd = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->pred->sucd = fn;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->pred = fn;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* shift the current index */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte icurr = (icurr + 1) % DU;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = il[icurr];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * global functions.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ****************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * el_init:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Initialize the element list.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * du - Number of uint in the indexed-list.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * dt - Time interval of the indexed-list.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * nlim - Limit number of each notice.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * return - 0: successful, otherwise failed.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ****************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteel_init(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t du,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t dt,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t nlim
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_key_t *k, *kleft;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_notice_t *n, *npred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t t = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (du < 1 || dt < 1 || nlim < 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DU = du;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DT = dt;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LB = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte NLIM = nlim;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * initialize the event set
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* first dummy notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = (el_notice_t *)malloc(sizeof (el_notice_t));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->time = LB;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->event = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->isdummy = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->pred = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte npred = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* first dummy key */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = (el_key_t *)malloc(sizeof (el_key_t));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (k == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->time = LB;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->count = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->notice = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kleft = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->key = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* index list */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte il = (el_key_t **)malloc((DU + 1) * sizeof (el_key_t *));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (il == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* create notice list, key list & index list */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; i < DU; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte t += DT;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = (el_notice_t *)malloc(sizeof (el_notice_t));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->time = t;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->event = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->isdummy = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->pred = npred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte npred->sucd = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte npred = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = (el_key_t *)malloc(sizeof (el_key_t));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (k == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->time = t;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->count = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->notice = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left = kleft;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kleft->right = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kleft = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->key = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte il[i] = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* last dummy notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = (el_notice_t *)malloc(sizeof (el_notice_t));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->time = INFINITY; /* the end of the world */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->event = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->isdummy = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->pred = npred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->sucd = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte npred->sucd = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* last dummy key */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = (el_key_t *)malloc(sizeof (el_key_t));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (k == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->time = INFINITY; /* the end of the world */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->count = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->notice = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left = kleft;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->right = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kleft->right = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->key = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* last index */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte il[DU] = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ****************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * el_add:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Add an event to the element list with it's execution time.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * It might not actually put the event to the list if the event
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * is the most current one for execution.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ev - The Event.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * t - The time when the event is scheduled at.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * evp - Pointer of event for returning.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * return - Error code.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ****************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteel_add(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *ev,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t t,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void **evp
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ec = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t t1 = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i, j;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_key_t *k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_notice_t *n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_key_t *y;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_notice_t *x;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* lock the event set */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) pthread_mutex_lock(&el_mtx);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* strip it off from the event list which is being handled */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (evf_again(ev) != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* if it is rescheduling an event and the event */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* was waiting for execution after idle finishes */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (evf_rem(ev) == 0 &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte evp != NULL &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (curr == NULL || t <= curr->time)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* no need to reschedule it */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *evp = ev;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto add_done;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte evl_strip(ev);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* if it is marked as a removed event, do not add it */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (evf_rem(ev) != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ev_free(ev);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto add_done;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* get the index in the il */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (t == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte t = ev_intval(ev);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* not initialization time */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (evf_init(ev) || evf_again(ev)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte t1 = get_stopwatch(evf_wakeup(ev));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* make il up to date */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte il_shift(t1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* avoid overflow */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (t1 >= INFINITY - t) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* the last second */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte t1 = INFINITY - t1 - 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte t += t1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte i = (t - LB) / DT;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (i >= DU) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte i = DU;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte i = (i + icurr) % DU;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* find the right key */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = (il[i])->left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (k->time > t) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = k->left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = k->right;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* need to split */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (k->count == NLIM) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* insert a new key */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte y = (el_key_t *)malloc(sizeof (el_key_t));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (y == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ec = ISNS_RSP_INTERNAL_ERROR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto add_done;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->count = NLIM / 2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte x = k->notice;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (j = 1; j <= NLIM / 2; j++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte x = x->pred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte y->time = x->time;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte y->count = NLIM - NLIM / 2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte y->notice = x;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte y->right = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte y->left = k->left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left->right = y;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left = y;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* update the key */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte x->key = y;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* shift */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (y->time > t) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = y;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* make a new notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte x = (el_notice_t *)malloc(sizeof (el_notice_t));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (x == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ec = ISNS_RSP_INTERNAL_ERROR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto add_done;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte x->time = t;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte x->event = ev;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte x->isdummy = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte x->key = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* insert it */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = k->notice;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (n->time > t) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = n->pred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte x->pred = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte x->sucd = n->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->sucd->pred = x;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->sucd = x;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* increase number of notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->count ++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* reset current notice and wake up idle */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (curr == NULL || curr->time > t) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte curr = x;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* clear event flags */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte evf_zero(ev);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte isnslog(LOG_DEBUG, "el_add", "%s [%d] is scheduled at %d.",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ((ev_t *)ev)->type == EV_ESI ? "ESI" : "REG_EXP",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ((ev_t *)ev)->uid,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteadd_done:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* unlock the event set */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) pthread_mutex_unlock(&el_mtx);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* failed, free it */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ec != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ev_free(ev);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte isnslog(LOG_DEBUG, "el_add", "failed, no memory.");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ec);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ****************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * el_remove:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Remove or update an event from the element list. If the event is
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * currently not in the element list, it must be in a queue which
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * contains all of event which are being executing at the moment.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * So it seeks the event from the list prior to the element list.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * id1 - The event ID.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * id2 - The second ID for the event update.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * pending - Do not actually remove, mark it for removal pending.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * return - Error code.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ****************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteel_remove(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t id1,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t id2,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int pending
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_key_t *k, *kl, *kr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_notice_t *n, *n2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) pthread_mutex_lock(&el_mtx);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* search the event from the event list which is being handled */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (evl_remove(id1, id2, pending) != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) pthread_mutex_unlock(&el_mtx);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* search the notice starting from current */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = curr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (n != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* found the match notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!n->isdummy && ev_match(n->event, id1) != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ev_remove(n->event, id2, 1, pending) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* update the key of the match notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = n->key;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (k != NULL && k->count == 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* no more notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left->right = k->right;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->right->left = k->left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(k);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (k != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->notice = n->pred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->notice->key = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->time = k->notice->time;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n2 = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = n2->key;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (k == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n2 = n2->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = n2->key;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* decrease the count by one */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->count --;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* merge the keys */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kl = k->left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kr = k->right;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!kl->notice->isdummy &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (kl->count + k->count) <= NLIM) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* delete the left key */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->count += kl->count;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left = kl->left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left->right = k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kl->notice->key = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(kl);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else if (!k->notice->isdummy &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (kr->count + k->count) <= NLIM) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* delete this key */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kr->count += k->count;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kr->left = k->left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte kr->left->right = kr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->notice->key = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(k);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* delete the match notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->pred->sucd = n->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n->sucd->pred = n->pred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* update current */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n == curr) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n2 = n->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (n2 != NULL && n2->isdummy) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n2 = n2->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte curr = n2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(n);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break; /* exit while loop */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = n->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) pthread_mutex_unlock(&el_mtx);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ****************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * el_first:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Fetch the first event from the element list.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * t - Pointer of time of the event for returning.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * return - The event.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ****************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteel_first(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t *t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *p = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_notice_t *n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte el_key_t *k;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) pthread_mutex_lock(&el_mtx);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (curr != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* remove current from the event set */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte curr->pred->sucd = curr->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte curr->sucd->pred = curr->pred;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* decrease number of notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = curr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (n->key == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = n->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k = n->key;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->count --;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* empty not-dummy key */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (k->count == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->left->right = k->right;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte k->right->left = k->left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(k);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* get next notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = curr->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (n != NULL && n->isdummy) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = n->sucd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* return the time */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *t = curr->time;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* reset current notice */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p = curr->event;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(curr);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte curr = n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* the one that is being handled by esi_proc */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (p) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte evl_append(p);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) pthread_mutex_unlock(&el_mtx);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (p) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte isnslog(LOG_DEBUG, "el_first", "%s [%d] is fetched.",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ((ev_t *)p)->type == EV_ESI ? "ESI" : "REG_EXP",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ((ev_t *)p)->uid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (p);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}