112953Sgabeblack@google.com/*
212953Sgabeblack@google.com * Copyright 2018 Google, Inc.
312953Sgabeblack@google.com *
412953Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512953Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612953Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712953Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812953Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912953Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012953Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112953Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212953Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312953Sgabeblack@google.com * this software without specific prior written permission.
1412953Sgabeblack@google.com *
1512953Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612953Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712953Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812953Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912953Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012953Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112953Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212953Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312953Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412953Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512953Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612953Sgabeblack@google.com *
2712953Sgabeblack@google.com * Authors: Gabe Black
2812953Sgabeblack@google.com */
2912953Sgabeblack@google.com
3012953Sgabeblack@google.com#ifndef __SYSTEMC_CORE_PROCESS_TYPES_HH__
3112953Sgabeblack@google.com#define __SYSTEMC_CORE_PROCESS_TYPES_HH__
3212953Sgabeblack@google.com
3312953Sgabeblack@google.com#include "systemc/core/process.hh"
3412953Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3512953Sgabeblack@google.com
3612953Sgabeblack@google.comnamespace sc_gem5
3712953Sgabeblack@google.com{
3812953Sgabeblack@google.com
3912953Sgabeblack@google.comclass Method : public Process
4012953Sgabeblack@google.com{
4112953Sgabeblack@google.com  public:
4213180Sgabeblack@google.com    Method(const char *name, ProcessFuncWrapper *func, bool internal=false) :
4313180Sgabeblack@google.com        Process(name, func, internal)
4413180Sgabeblack@google.com    {}
4512953Sgabeblack@google.com
4612953Sgabeblack@google.com    const char *kind() const override { return "sc_method_process"; }
4712953Sgabeblack@google.com
4812953Sgabeblack@google.com    sc_core::sc_curr_proc_kind
4912953Sgabeblack@google.com    procKind() const override
5012953Sgabeblack@google.com    {
5112953Sgabeblack@google.com        return sc_core::SC_METHOD_PROC_;
5212953Sgabeblack@google.com    }
5312953Sgabeblack@google.com};
5412953Sgabeblack@google.com
5512953Sgabeblack@google.comclass Thread : public Process
5612953Sgabeblack@google.com{
5712953Sgabeblack@google.com  public:
5813180Sgabeblack@google.com    Thread(const char *name, ProcessFuncWrapper *func, bool internal=false) :
5913180Sgabeblack@google.com        Process(name, func, internal), ctx(nullptr)
6012953Sgabeblack@google.com    {}
6112953Sgabeblack@google.com
6212953Sgabeblack@google.com    ~Thread() { delete ctx; }
6312953Sgabeblack@google.com
6412953Sgabeblack@google.com    const char *kind() const override { return "sc_thread_process"; }
6512953Sgabeblack@google.com
6612953Sgabeblack@google.com    sc_core::sc_curr_proc_kind
6712953Sgabeblack@google.com    procKind() const override
6812953Sgabeblack@google.com    {
6912953Sgabeblack@google.com        return sc_core::SC_THREAD_PROC_;
7012953Sgabeblack@google.com    }
7112953Sgabeblack@google.com
7212953Sgabeblack@google.com    Fiber *
7312953Sgabeblack@google.com    fiber() override
7412953Sgabeblack@google.com    {
7512953Sgabeblack@google.com        if (!ctx)
7612953Sgabeblack@google.com            ctx = new Context(this, stackSize);
7712953Sgabeblack@google.com        return ctx;
7812953Sgabeblack@google.com    }
7912953Sgabeblack@google.com
8012953Sgabeblack@google.com  private:
8112953Sgabeblack@google.com    class Context : public Fiber
8212953Sgabeblack@google.com    {
8312953Sgabeblack@google.com      public:
8412953Sgabeblack@google.com        Context(Thread *thread, size_t size) : Fiber(size), thread(thread) {}
8512953Sgabeblack@google.com
8612953Sgabeblack@google.com      private:
8712953Sgabeblack@google.com        Thread *thread;
8812953Sgabeblack@google.com
8912998Sgabeblack@google.com        void
9012998Sgabeblack@google.com        main() override
9112998Sgabeblack@google.com        {
9212998Sgabeblack@google.com            thread->_needsStart = false;
9313182Sgabeblack@google.com            try {
9413182Sgabeblack@google.com                thread->run();
9513182Sgabeblack@google.com            } catch (...) {
9613182Sgabeblack@google.com                thread->terminate();
9713701Sgabeblack@google.com                scheduler.throwUp();
9813182Sgabeblack@google.com                return;
9913182Sgabeblack@google.com            }
10012998Sgabeblack@google.com            thread->terminate();
10113092Sgabeblack@google.com            scheduler.yield();
10212998Sgabeblack@google.com        }
10312953Sgabeblack@google.com    };
10412998Sgabeblack@google.com    friend class Context;
10512953Sgabeblack@google.com
10612953Sgabeblack@google.com    Context *ctx;
10712953Sgabeblack@google.com};
10812953Sgabeblack@google.com
10912953Sgabeblack@google.comclass CThread : public Thread
11012953Sgabeblack@google.com{
11112953Sgabeblack@google.com  public:
11213180Sgabeblack@google.com    CThread(const char *name, ProcessFuncWrapper *func, bool internal=false) :
11313180Sgabeblack@google.com        Thread(name, func, internal)
11412953Sgabeblack@google.com    {
11512953Sgabeblack@google.com        // We'll be in the initialization list now, but we shouldn't be.
11612953Sgabeblack@google.com        popListNode();
11712953Sgabeblack@google.com    }
11812953Sgabeblack@google.com
11912953Sgabeblack@google.com    const char *kind() const override { return "sc_cthread_process"; }
12012953Sgabeblack@google.com
12112953Sgabeblack@google.com    sc_core::sc_curr_proc_kind
12212953Sgabeblack@google.com    procKind() const override
12312953Sgabeblack@google.com    {
12412953Sgabeblack@google.com        return sc_core::SC_CTHREAD_PROC_;
12512953Sgabeblack@google.com    }
12612953Sgabeblack@google.com};
12712953Sgabeblack@google.com
12812953Sgabeblack@google.com} // namespace sc_gem5
12912953Sgabeblack@google.com
13012953Sgabeblack@google.com#endif  //__SYSTEMC_CORE_PROCESS_TYPES_HH__
131