1 Star 0 Fork 419

子安/workflow

forked from 搜狗开源/workflow 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Executor.cc 3.18 KB
一键复制 编辑 原始数据 按行查看 历史
XieHan 提交于 2020-07-29 13:42 +08:00 . initialization
/*
Copyright (c) 2019 Sogou, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Xie Han (xiehan@sogou-inc.com)
*/
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include "list.h"
#include "thrdpool.h"
#include "Executor.h"
struct ExecTaskEntry
{
struct list_head list;
ExecSession *session;
thrdpool_t *thrdpool;
};
int ExecQueue::init()
{
int ret;
ret = pthread_mutex_init(&this->mutex, NULL);
if (ret == 0)
{
INIT_LIST_HEAD(&this->task_list);
return 0;
}
errno = ret;
return -1;
}
void ExecQueue::deinit()
{
pthread_mutex_destroy(&this->mutex);
}
int Executor::init(size_t nthreads)
{
if (nthreads == 0)
{
errno = EINVAL;
return -1;
}
this->thrdpool = thrdpool_create(nthreads, 0);
if (this->thrdpool)
return 0;
return -1;
}
void Executor::deinit()
{
thrdpool_destroy(Executor::executor_cancel_tasks, this->thrdpool);
}
extern "C" void __thrdpool_schedule(const struct thrdpool_task *, void *,
thrdpool_t *);
void Executor::executor_thread_routine(void *context)
{
ExecQueue *queue = (ExecQueue *)context;
struct ExecTaskEntry *entry;
ExecSession *session;
pthread_mutex_lock(&queue->mutex);
entry = list_entry(queue->task_list.next, struct ExecTaskEntry, list);
list_del(&entry->list);
session = entry->session;
if (!list_empty(&queue->task_list))
{
struct thrdpool_task task = {
.routine = Executor::executor_thread_routine,
.context = queue
};
__thrdpool_schedule(&task, entry, entry->thrdpool);
}
else
free(entry);
pthread_mutex_unlock(&queue->mutex);
session->execute();
session->handle(ES_STATE_FINISHED, 0);
}
void Executor::executor_cancel_tasks(const struct thrdpool_task *task)
{
ExecQueue *queue = (ExecQueue *)task->context;
struct ExecTaskEntry *entry;
struct list_head *pos, *tmp;
ExecSession *session;
list_for_each_safe(pos, tmp, &queue->task_list)
{
entry = list_entry(pos, struct ExecTaskEntry, list);
list_del(pos);
session = entry->session;
free(entry);
session->handle(ES_STATE_CANCELED, 0);
}
}
int Executor::request(ExecSession *session, ExecQueue *queue)
{
struct ExecTaskEntry *entry;
session->queue = queue;
entry = (struct ExecTaskEntry *)malloc(sizeof (struct ExecTaskEntry));
if (entry)
{
entry->session = session;
entry->thrdpool = this->thrdpool;
pthread_mutex_lock(&queue->mutex);
list_add_tail(&entry->list, &queue->task_list);
if (queue->task_list.next == &entry->list)
{
struct thrdpool_task task = {
.routine = Executor::executor_thread_routine,
.context = queue
};
if (thrdpool_schedule(&task, this->thrdpool) < 0)
{
list_del(&entry->list);
free(entry);
entry = NULL;
}
}
pthread_mutex_unlock(&queue->mutex);
}
return -!entry;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/andrewgithub/workflow.git
git@gitee.com:andrewgithub/workflow.git
andrewgithub
workflow
workflow
master

搜索帮助