📄 bthread.h
字号:
/*
* Copyright (C) 2003 Radu Braniste (rbraniste@epicad.com)
* All Rights Reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Author nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef _brewthread_h_
#define _brewthread_h_
#include "AEEThreadUtil.h"
#include "AEERScPool.h"
struct BThreadPolicy
{
static int getStackSize()
{
return 100;
}
};
template <class R, class P = BThreadPolicy>
class BThread
{
public:
~BThread()
{
//int err = IRSCPOOL_ReleaseRsc(pool_,(IBase*)thread_);
//if (thread_)
// ITHREAD_Release(thread_);
}
static BThread* createThread(IShell* shell, IRscPool *rsc, Writer* writer=0)
{
void* v = IRSCPOOL_Malloc(rsc,sizeof(BThread<R>));
BThread<R>* bthread = new(v) BThread<R>(shell, rsc, writer);
if (bthread->allocate()!= SUCCESS)
{
IRSCPOOL_Free(rsc,v);
bthread->deallocate();
return 0;
}
return bthread;
}
int start()
{
return ITHREAD_Start(thread_, P::getStackSize(), runImpl, this);
}
void sleep(int ms)
{
AEECallback *pcb = ITHREAD_GetResumeCBK(thread_);
ISHELL_SetTimerEx(shell_,ms,pcb);
ITHREAD_Suspend(thread_);
}
private:
BThread(IShell* shell, IRscPool *rsc, Writer* writer) :
shell_(shell), thread_(0), pool_(rsc), runnable_(0), writer_(writer)
{
}
int allocate()
{
int err = SUCCESS;
if ((err = ISHELL_CreateInstance(shell_, AEECLSID_THREAD, (void**)(&thread_))) != SUCCESS)
return err;
err = IRSCPOOL_HoldRsc(pool_,(IBase*)thread_);
void* v = ITHREAD_Malloc(thread_,sizeof(R));
runnable_ = new(v) R(shell_, writer_);
if (!runnable_)
return 1;
return err;
}
void deallocate()
{
IRSCPOOL_ReleaseRsc(pool_,(IBase*)thread_);
ITHREAD_Free(thread_,runnable_);
}
static int runImpl(IThread* t, void* v)
{
BThread* r = static_cast<BThread*>(v);
(*(r->runnable_))(r);
return SUCCESS;
}
private:
IShell* shell_;
IThread* thread_;
R* runnable_;
IRscPool* pool_;
Writer* writer_;
private:
BThread( const BThread &Value );
const BThread &operator = ( const BThread &Rhs );
};
struct RunnableImpl
{
RunnableImpl(IShell* shell, void*) : shell_(shell), cursor_(0)
{
}
void operator()(BThread<RunnableImpl>* t)
{
Writer writer (shell_);
while (cursor_ < 30)
{
String e ((long)cursor_);
writer.DisplayOutput(1,e.toCharArray());
t->sleep(100);
int i = cursor_++;
}
}
private:
IShell* shell_;
int cursor_;
};
struct RunnableImpl1
{
RunnableImpl1(IShell* shell, Writer* writer) : shell_(shell), cursor_(0), writer_(writer)
{
}
void operator()(BThread<RunnableImpl1>* t)
{
while (cursor_ < 30)
{
writer_->WriteLine(cursor_);
t->sleep(100);
int i = cursor_++;
}
}
private:
IShell* shell_;
int cursor_;
Writer* writer_;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -