Skip to content

Commit b362d1e

Browse files
author
test
committed
Merge branch 'dev'
2 parents 44b1d9d + 09ca44f commit b362d1e

File tree

11 files changed

+39
-288
lines changed

11 files changed

+39
-288
lines changed

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,20 @@ script:
2020
- make -j4
2121
- sudo make install
2222
- make test_small
23+
- make samples
2324
- make run_test
25+
# - sudo rm * -rf
26+
# - cmake .. -DENABLE_BOOST_COROUTINE=ON
27+
# - make -j4
28+
# - sudo make install
29+
# - make test_small
30+
# - make run_test
31+
# - sudo rm * -rf
32+
# - cmake .. -DENABLE_SHARED_STACK=ON
33+
# - make -j4
34+
# - sudo make install
35+
# - make test_small
36+
# - make run_test
2437
- popd;
2538

2639
after_success:

src/coroutine.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ struct __async_wait<void>
8989
#define go_stack(size) ::co::__go_stack(size)-
9090
#define co_yield do { g_Scheduler.CoYield(); } while (0)
9191

92-
// (uint32_t type, uint64_t id)
93-
#define co_wait(type, id) do { g_Scheduler.UserBlockWait(type, id); } while (0)
94-
#define co_wakeup(type, id) do { g_Scheduler.UserBlockWakeup(type, id); } while (0)
95-
9692
// coroutine sleep, never blocks current thread.
9793
#define co_sleep(milliseconds) do { g_Scheduler.SleepSwitch(milliseconds); } while (0)
9894

src/ctx_boost_coroutine/context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace co
6666
#if defined(ENABLE_SHARED_STACK)
6767
, boost::coroutines::attributes(shared_stack_cap), shared_stack_allocator(shared_stack, shared_stack_cap)
6868
#else
69-
, boost::coroutines::attributes(stack_size_)
69+
, boost::coroutines::attributes(std::max<std::size_t>(stack_size_, boost::coroutines::stack_traits::minimum_size()))
7070
#endif
7171
);
7272
if (!c) return false;

src/ctx_win_fiber/context.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <context.h>
22
#include <scheduler.h>
33
#include <Windows.h>
4+
#include <algorithm>
45

56
namespace co
67
{
@@ -48,8 +49,9 @@ namespace co
4849
bool Context::Init(std::function<void()> const& fn, char* shared_stack, uint32_t shared_stack_cap)
4950
{
5051
impl_->fn_ = fn;
51-
impl_->native_ = CreateFiberEx(g_Scheduler.GetOptions().init_commit_stack_size,
52-
stack_size_, FIBER_FLAG_FLOAT_SWITCH,
52+
SIZE_T commit_size = g_Scheduler.GetOptions().init_commit_stack_size;
53+
impl_->native_ = CreateFiberEx(commit_size,
54+
(std::max)(stack_size_, commit_size), FIBER_FLAG_FLOAT_SWITCH,
5355
(LPFIBER_START_ROUTINE)FiberFunc, &impl_->fn_);
5456
return !!impl_->native_;
5557
}

src/processer.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,13 @@ uint32_t Processer::Run(ThreadLocalInfo &info, uint32_t &done_count)
8585
break;
8686

8787
case TaskState::sys_block:
88-
case TaskState::user_block:
8988
{
89+
assert(tk->block_);
9090
if (tk->block_) {
9191
it = slist.erase(it);
9292
if (!tk->block_->AddWaitTask(tk))
9393
runnable_list_.push(tk);
9494
tk->block_ = NULL;
95-
} else {
96-
std::unique_lock<LFLock> lock(g_Scheduler.user_wait_lock_);
97-
auto &zone = g_Scheduler.user_wait_tasks_[tk->user_wait_type_];
98-
auto &wait_pair = zone[tk->user_wait_id_];
99-
auto &task_queue = wait_pair.second;
100-
if (wait_pair.first) {
101-
--wait_pair.first;
102-
tk->state_ = TaskState::runnable;
103-
++it;
104-
} else {
105-
it = slist.erase(it);
106-
task_queue.push(tk);
107-
}
108-
g_Scheduler.ClearWaitPairWithoutLock(tk->user_wait_type_,
109-
tk->user_wait_id_, zone, wait_pair);
11095
}
11196
}
11297
break;

src/scheduler.cpp

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -255,21 +255,6 @@ void Scheduler::SleepSwitch(int timeout_ms)
255255
sleep_wait_.CoSwitch(timeout_ms);
256256
}
257257

258-
bool Scheduler::UserBlockWait(uint32_t type, uint64_t wait_id)
259-
{
260-
return BlockWait((int64_t)type, wait_id);
261-
}
262-
263-
bool Scheduler::TryUserBlockWait(uint32_t type, uint64_t wait_id)
264-
{
265-
return TryBlockWait((int64_t)type, wait_id);
266-
}
267-
268-
uint32_t Scheduler::UserBlockWakeup(uint32_t type, uint64_t wait_id, uint32_t wakeup_count)
269-
{
270-
return BlockWakeup((int64_t)type, wait_id, wakeup_count);
271-
}
272-
273258
TimerId Scheduler::ExpireAt(CoTimerMgr::TimePoint const& time_point,
274259
CoTimer::fn_t const& fn)
275260
{
@@ -299,78 +284,4 @@ ThreadPool& Scheduler::GetThreadPool()
299284
return *thread_pool_;
300285
}
301286

302-
bool Scheduler::BlockWait(int64_t type, uint64_t wait_id)
303-
{
304-
if (!IsCoroutine()) return false;
305-
Task* tk = GetLocalInfo().current_task;
306-
tk->user_wait_type_ = type;
307-
tk->user_wait_id_ = wait_id;
308-
tk->state_ = type < 0 ? TaskState::sys_block : TaskState::user_block;
309-
DebugPrint(dbg_wait, "task(%s) %s. wait_type=%lld, wait_id=%llu",
310-
tk->DebugInfo(), type < 0 ? "sys_block" : "user_block",
311-
(long long int)tk->user_wait_type_, (long long unsigned)tk->user_wait_id_);
312-
CoYield();
313-
return true;
314-
}
315-
316-
bool Scheduler::TryBlockWait(int64_t type, uint64_t wait_id)
317-
{
318-
std::unique_lock<LFLock> locker(user_wait_lock_);
319-
auto it = user_wait_tasks_.find(type);
320-
if (user_wait_tasks_.end() == it) return false;
321-
322-
auto &zone = it->second;
323-
auto it2 = zone.find(wait_id);
324-
if (zone.end() == it2) return false;
325-
326-
auto &wait_pair = it2->second;
327-
if (wait_pair.first > 0) {
328-
--wait_pair.first;
329-
ClearWaitPairWithoutLock(type, wait_id, zone, wait_pair);
330-
return true;
331-
}
332-
333-
return false;
334-
}
335-
336-
uint32_t Scheduler::BlockWakeup(int64_t type, uint64_t wait_id, uint32_t wakeup_count)
337-
{
338-
std::unique_lock<LFLock> locker(user_wait_lock_);
339-
auto &zone = user_wait_tasks_[type];
340-
auto &wait_pair = zone[wait_id];
341-
auto &task_queue = wait_pair.second;
342-
SList<Task> tasks = task_queue.pop(wakeup_count);
343-
std::size_t c = tasks.size();
344-
if (c < wakeup_count) // 允许提前设置唤醒标志, 以便多线程同步。
345-
wait_pair.first += wakeup_count - c;
346-
ClearWaitPairWithoutLock(type, wait_id, zone, wait_pair);
347-
uint32_t domain_wakeup = wait_pair.first;
348-
locker.unlock();
349-
350-
for (auto &task: tasks)
351-
{
352-
++c;
353-
Task *tk = &task;
354-
DebugPrint(dbg_wait, "%s wakeup task(%s). wait_type=%lld, wait_id=%llu",
355-
type < 0 ? "sys_block" : "user_block", tk->DebugInfo(), (long long int)type, (long long unsigned)wait_id);
356-
AddTaskRunnable(tk);
357-
}
358-
359-
DebugPrint(dbg_wait, "%s wakeup %u tasks, domain wakeup=%u. wait_type=%lld, wait_id=%llu",
360-
type < 0 ? "sys_block" : "user_block", (unsigned)c, domain_wakeup, (long long int)type, (long long unsigned)wait_id);
361-
return c;
362-
}
363-
364-
void Scheduler::ClearWaitPairWithoutLock(int64_t type,
365-
uint64_t wait_id, WaitZone& zone, WaitPair& wait_pair)
366-
{
367-
if (wait_pair.second.empty() && wait_pair.first == 0) {
368-
if (zone.size() > 1) {
369-
zone.erase(wait_id);
370-
} else {
371-
user_wait_tasks_.erase(type);
372-
}
373-
}
374-
}
375-
376287
} //namespace co

src/scheduler.h

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,6 @@ class Scheduler
160160
// \timeout_ms min value is 0.
161161
void SleepSwitch(int timeout_ms);
162162

163-
/// ------------------------------------------------------------------------
164-
// @{ 以计数的方式模拟实现的协程同步方式.
165-
// 初始计数为0, Wait减少计数, Wakeup增加计数.
166-
// UserBlockWait将阻塞式(yield)地等待计数大于0, 等待成功后将计数减一,
167-
// 并将协程切换回可执行状态. 如果不在协程中调用, 则返回false, 且不做任何事.
168-
// TryBlockWait检查当前计数, 如果计数等于0, 则返回false; 否则计数减一并返回true.
169-
// UserBlockWakeup检查当前等待队列, 将等待队列中的前面最多wakeup_count个
170-
// 协程唤醒(设置为可执行状态), 累加剩余计数(wakeup_count减去唤醒的协程数量)
171-
//
172-
// 用户自定义的阻塞切换, type范围限定为: [0, 0xffffffff]
173-
bool UserBlockWait(uint32_t type, uint64_t wait_id);
174-
bool TryUserBlockWait(uint32_t type, uint64_t wait_id);
175-
uint32_t UserBlockWakeup(uint32_t type, uint64_t wait_id, uint32_t wakeup_count = 1);
176-
// }@
177-
/// ------------------------------------------------------------------------
178-
179163
/// ------------------------------------------------------------------------
180164
// @{ 定时器
181165
TimerId ExpireAt(CoTimerMgr::TimePoint const& time_point, CoTimer::fn_t const& fn);
@@ -216,23 +200,6 @@ class Scheduler
216200
// 将一个协程加入可执行队列中
217201
void AddTaskRunnable(Task* tk);
218202

219-
/// ------------------------------------------------------------------------
220-
// 协程框架定义的阻塞切换, type范围不可与用户自定义范围重叠, 指定为:[-xxxxx, -1]
221-
// 如果不在协程中调用, 则返回false, 且不做任何事.
222-
bool BlockWait(int64_t type, uint64_t wait_id);
223-
224-
// 尝试等待某个事件发生, 功能等同于try_lock, 可在协程外调用.
225-
bool TryBlockWait(int64_t type, uint64_t wait_id);
226-
227-
// 唤醒对某个时间等待的协程.
228-
uint32_t BlockWakeup(int64_t type, uint64_t wait_id, uint32_t wakeup_count = 1);
229-
// @
230-
/// ------------------------------------------------------------------------
231-
232-
// 清理没有等待也没有被等待的WaitPair.
233-
void ClearWaitPairWithoutLock(int64_t type, uint64_t wait_id, WaitZone& zone, WaitPair& wait_pair);
234-
235-
private:
236203
// Run函数的一部分, 处理runnable状态的协程
237204
uint32_t DoRunnable();
238205

@@ -263,10 +230,6 @@ class Scheduler
263230
// sleep block waiter.
264231
SleepWait sleep_wait_;
265232

266-
// User define wait tasks table.
267-
WaitTable user_wait_tasks_;
268-
LFLock user_wait_lock_;
269-
270233
// Timer manager.
271234
CoTimerMgr timer_mgr_;
272235

@@ -276,11 +239,12 @@ class Scheduler
276239
std::atomic<uint8_t> sleep_ms_{0};
277240
std::atomic<uint32_t> thread_id_{0};
278241

279-
friend class CoMutex;
280-
friend class BlockObject;
281-
friend class IoWait;
282-
friend class SleepWait;
283-
friend class Processer;
242+
private:
243+
friend class CoMutex;
244+
friend class BlockObject;
245+
friend class IoWait;
246+
friend class SleepWait;
247+
friend class Processer;
284248
};
285249

286250
} //namespace co

src/task.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ enum class TaskState
1717
runnable,
1818
io_block, // write, writev, read, select, poll, ...
1919
sys_block, // co_mutex, ...
20-
user_block, // user switch it.
21-
sleep, // sleep nanosleep poll(NULL, 0, timeout)
20+
sleep, // sleep, nanosleep, poll(NULL, 0, timeout)
2221
done,
2322
fatal,
2423
};
@@ -69,8 +68,6 @@ struct Task
6968
int io_block_timeout_ = 0;
7069
CoTimerPtr io_block_timer_;
7170

72-
int64_t user_wait_type_ = 0; // user_block等待的类型
73-
uint64_t user_wait_id_ = 0; // user_block等待的id
7471
BlockObject* block_ = NULL; // sys_block等待的block对象
7572

7673
int sleep_ms_ = 0; // 睡眠时间

test/gtest_unit/pinfo.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
#ifndef _WIN32
33
#include <unistd.h>
44
#include <boost/algorithm/string.hpp>
5+
#else
6+
#include <Windows.h>
7+
#include <Psapi.h>
8+
#pragma comment(lib,"Psapi.lib")
59
#endif
10+
611
#include <fstream>
712
#include <string>
813
#include <vector>
@@ -51,6 +56,12 @@ struct pinfo
5156
sscanf(s.c_str(), "VmSwap: %llu KB", (long long unsigned int*)&swap);
5257
}
5358
}
59+
#else
60+
PROCESS_MEMORY_COUNTERS meminfo;
61+
GetProcessMemoryInfo(GetCurrentProcess(), &meminfo, sizeof(meminfo));
62+
rss_high = meminfo.PeakWorkingSetSize / 1024;
63+
rss = meminfo.WorkingSetSize / 1024;
64+
virt_high = virt = 0;
5465
#endif
5566
}
5667

test/gtest_unit/run_test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ runs=`ls test/*.t | wc -l`
1919
pass=`grep "^\[\s*PASSED" result | wc -l`
2020
if [ "${pass}" -eq "${runs}" ];
2121
then
22+
echo "all test were PASSED"
2223
exit 0
2324
else
24-
exit 1
25+
echo "some test were FAIL"
2526
fi

0 commit comments

Comments
 (0)