#ifndef __SGE_JOB_SCHEDD_H
#define __SGE_JOB_SCHEDD_H
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 * 
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 * 
 *  Sun Microsystems Inc., March, 2001
 * 
 * 
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 * 
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 * 
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 * 
 *   Copyright: 2001 by Sun Microsystems, Inc.
 * 
 *   All Rights Reserved.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/

#define running_status(x) ((x)!=0 && (x)!=JFINISHED)

/* 
 * get order used for job sorting
 */
lSortOrder *sge_job_sort_order(const lDescr *dp);

int up_resort(lList **ulpp, lListElem *job, lList *job_list, lSortOrder *so);

void sge_inc_jc(lList** jcpp, const char *name, int slots);

void sge_dec_jc(lList** jcpp, const char *name, int slots);

int rebuild_jc(lList **jcpp, lList *job_list);

int resort_jobs(lList *jc, lList *job_list, const char *owner, lSortOrder *so);

int set_user_sort(int foo);

/*
 * drop all running jobs into the running list 
 *
 */
int sge_split_job_running(lList **jobs, lList **running, const char *running_name);

/*
 * move first task in job to running list 
 *
 */

int sge_move_to_running(lList **jobs, lList **running, lListElem *job);

/*
 * drop all finished jobs into the finished list 
 *
 */
int sge_split_job_finished(lList **jobs, lList **finished, const char *finished_name);

/*
 * drop all jobs waiting for -a into the waiting list 
 *
 */
int sge_split_job_wait_at_time(lList **jobs, lList **waiting, const char *waiting_name, u_long32 now);

/*
 * drop all jobs in error state
 *
 */
int sge_split_job_error(lList **jobs, lList **error, const char *error_name);

/*
 * drop all jobs in hold state
 *
 */
int sge_split_job_hold(lList **jobs, lList **hold, const char *hold_name);

/*
 * drop all jobs waiting for a predecessor into the waiting list 
 *
 */
int sge_split_job_wait_predecessor(lList **jobs, lList **waiting, const char *waiting_name);

/*
 * drop all jobs restricted by a ckpt environment
 *
 */
int sge_split_job_ckpt_restricted(lList **jobs, lList **restricted, const char *restricted_name, lList *ckpt_list);

void print_job_list(lList *job_list);

lList *filter_max_running_1step(lList *pending_jobs, lList *running_jobs, lList **jct_list, int max_jobs, int elem);

lList *filter_max_running(lList *pending_jobs, lList *jct_list, int max_jobs, int elem);

void trace_job_sort(lList *job_list);

int get_job_contribution(double *dvalp, const char *name, lListElem *jep, lListElem *dcep);

int nslots_granted(lList *granted, const char *qhostname);

int active_subtasks(lListElem *job, const char *qname);

int active_nslots_granted(lListElem *job, lList *granted, const char *qhostname);

lListElem *explicit_job_request(lListElem *jep, const char *name);

int sge_granted_slots(lList *gdil);

/* EB: */

const char *get_name_of_split_value(int value);

enum {
   SPLIT_FIRST,

   /*
    * Pending jobs/tasks which may be dispatched
    */
   SPLIT_PENDING = SPLIT_FIRST,

   /* 
    * Pending jobs/tasks which won't be dispatched because this would
    * exceed 'maxujobs'
    */
   SPLIT_PENDING_EXCLUDED,

   /*
    * Pending jobs/tasks which won't be dispatched because this whould
    * exceed 'max_aj_instances'
    */
   SPLIT_PENDING_EXCLUDED_INSTANCES,
   
   /*
    * Suspended jobs/tasks
    */
   SPLIT_SUSPENDED,

   /* 
    * Jobs/Tasks waiting for others to finish
    */
   SPLIT_WAITING_DUE_TO_PREDECESSOR,

   /*
    * Jobs/Tasks in user/operator/system hold
    */
   SPLIT_HOLD,

   /*
    * Jobs/Tasks which are in error state
    */
   SPLIT_ERROR,
   
   /*
    * These jobs/tasks are not dispatched because start time is in future
    */
   SPLIT_WAITING_DUE_TO_TIME,

   /*
    * These Jobs/Tasks won't be dispatched because they are already running
    */
   SPLIT_RUNNING,

   /*
    * Already finished jobs/tasks
    */
   SPLIT_FINISHED,

   /*
    * This id is used in loops
    */
   SPLIT_LAST
};

void split_jobs(lList **job_list, lList **answer_list, lList *queue_list,
                u_long32 max_aj_instances, lList **result_lists[]); 

void job_lists_split_with_reference_to_max_running(lList **job_lists[],
                                                   lList **user_list,
                                                   const char *user_name,
                                                   int max_jobs_per_user);

void job_move_first_pending_to_running(lListElem **pending_job,
                                       lList **result_lists[]);

void trash_splitted_jobs(lList **job_list[]);

void job_lists_print(lList **job_list[], const char *context);

void user_list_init_jc(lList **user_list, const lList *running_list);

#endif /* __SGE_JOB_SCHEDD_H */

