threadpool_job.c 3.6 KB
Newer Older
L
libb 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/*****************************************************************************
 * threadpool.c: thread pooling
 *****************************************************************************
 * Copyright (C) 2010-2017 x264 project
 *
 * Authors: Steven Walters <kemuri9@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at licensing@x264.com.
 *****************************************************************************/

/*
 *  threadpool_job.c
 *  FFL
 *  基于x264 中的线程池修改的,这是线程池中的工作结构,和链表中读取,添加任务的接口
 *  Created by zhufeifei on 2017/12/15.
 *  Copyright (C) 2017-2018 zhufeifei All rights reserved.
 *
*/

#include "threadpool_job.h"

int FFL_job_list_init( FFL_sync_job_list *slist, int max_size )
{
    if( max_size < 0 )
        return -1;
    slist->i_max_size = max_size;
    slist->i_size = 0;
    slist->list= FFL_mallocz ((max_size+1) * sizeof(FFL_threadpool_job*) );
	if (slist->list == 0) {
		return -1;
	}
    slist->mutex=FFL_CreateMutex();
    slist->cv_fill=FFL_CreateCond();
    slist->cv_empty=FFL_CreateCond();

    if( slist->mutex==0 ||
            slist->cv_fill==0 ||
            slist->cv_empty==0  )
    {
        FFL_DestroyMutex(slist->mutex);
        FFL_DestroyCond(slist->cv_fill);
        FFL_DestroyCond(slist->cv_empty);
        return -1;
    }
    return 0;
}

void FFL_job_list_delete( FFL_sync_job_list *slist )
{
    int i = 0;

    FFL_DestroyMutex( slist->mutex );
    FFL_DestroyCond( slist->cv_fill );
    FFL_DestroyCond( slist->cv_empty );

    if( !slist->list )
        return;
    while( slist->list[i] )
        FFL_free( slist->list[i++] );    
}

void FFL_job_list_push( FFL_sync_job_list *slist, FFL_threadpool_job *job )
{
    FFL_LockMutex( slist->mutex );
	FFL_job_list_push_unlock(slist, job);
	FFL_UnlockMutex(slist->mutex);
    
}

FFL_threadpool_job *FFL_job_list_pop( FFL_sync_job_list *slist )
{
    FFL_threadpool_job *job=0;
    FFL_LockMutex( slist->mutex );
	job=FFL_job_list_pop_unlock(slist);
    FFL_UnlockMutex( slist->mutex );
    return job;
}


void FFL_job_list_push_unlock(FFL_sync_job_list *slist, FFL_threadpool_job *job)
{
	while (slist->i_size == slist->i_max_size)
		FFL_CondWait(slist->cv_empty, slist->mutex);
	slist->list[slist->i_size++] = job;	
	FFL_CondBroadcast(slist->cv_fill);
}

FFL_threadpool_job *FFL_job_list_pop_unlock(FFL_sync_job_list *slist)
{
	FFL_threadpool_job *job;	
	while (!slist->i_size)
		FFL_CondWait(slist->cv_fill, slist->mutex);
	job = slist->list[--slist->i_size];
	slist->list[slist->i_size] = NULL;
	FFL_CondBroadcast(slist->cv_empty);	
	return job;
}

FFL_threadpool_job *FFL_job_list_pop_front_unlock( FFL_sync_job_list *slist )
{
    FFL_threadpool_job *job=slist->list[0];
    int i;
    for( i = 0; slist->list[i]; i++ )
        slist->list[i] = slist->list[i+1];
    assert(job);
    return job;
}