📄 thread.cc
字号:
//// Copyright (c) 2003 by Istv醤 V醨adi//// This file is part of dxr3Player, a DVD player written specifically // for the DXR3 (aka Hollywood+) decoder card.// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//------------------------------------------------------------------------------#include "Thread.h"#include "Scheduler.h"#include "util/POSIX.h"#include <cassert>//------------------------------------------------------------------------------using sched::Thread;//------------------------------------------------------------------------------//------------------------------------------------------------------------------void* Thread::thread_function(void* arg){ Thread* thread = reinterpret_cast<Thread*>(arg); thread->run(); return 0;}//------------------------------------------------------------------------------Thread::Thread(const char* name) : fromThreadFDReadable(name, "fromThreadFDReadable"), quit(false){ pipe_tothread[0] = pipe_tothread[1] = -1; pipe_fromthread[0] = pipe_fromthread[1] = -1;}//------------------------------------------------------------------------------Thread::~Thread(){ if (!quit) { stop(); }}//------------------------------------------------------------------------------void Thread::start(){ POSIX::pipe(pipe_tothread); POSIX::pipe(pipe_fromthread); int flags = POSIX::fcntl(pipe_fromthread[0], F_GETFL); flags |= O_NONBLOCK; POSIX::fcntl(pipe_fromthread[0], F_SETFL, flags); fromThreadFDReadable.setFileDescriptor(pipe_fromthread[0]); pthread_create(&pthread, 0, &thread_function, reinterpret_cast<void*>(this));}//------------------------------------------------------------------------------void Thread::asyncTrigger(){ char c = 0; POSIX::write(pipe_tothread[1], &c, sizeof(c));}//------------------------------------------------------------------------------void Thread::finishTrigger(){ char c = 0; do { Scheduler::wait(fromThreadFDReadable); } while(POSIX::read(pipe_fromthread[0], &c, sizeof(c))!=sizeof(c));}//------------------------------------------------------------------------------void Thread::trigger(){ asyncTrigger(); finishTrigger();}//------------------------------------------------------------------------------void Thread::stop(){ quit = true; trigger(); POSIX::close(pipe_fromthread[0]); POSIX::close(pipe_fromthread[1]); POSIX::close(pipe_tothread[0]); POSIX::close(pipe_tothread[1]); pipe_fromthread[0] = -1; pthread_join(pthread, 0);}//------------------------------------------------------------------------------void Thread::run(){ bool doQuit = false; do { char c; POSIX::read(pipe_tothread[0], &c, sizeof(c)); doQuit = quit; if (!doQuit) { perform(); } POSIX::write(pipe_fromthread[1], &c, sizeof(c)); } while(!doQuit);}//------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -