ChillMagic / TinyGC

Lightweight GC in C++
Apache License 2.0
4 stars 2 forks source link

Safepoint, GC threshold and insertions #4

Open NIC0NIC0NI opened 6 years ago

NIC0NIC0NI commented 6 years ago

实现了一个Safe Point

class SafePoint {
public:
    SafePoint();
    bool check();    // returns true if encounters a safe point
    bool start();     // returns true if success
    void end();

    bool addThreads(int num);       // returns true if success
    bool removeThreads(int num); // returns true if success
    bool setThreadNum(int num);  // returns true if success
    int getThreadNum();
};

当某线程调用start(),会尝试阻塞所有线程,也就是等待其他threadNum - 1个线程调用check()。其他线程调用check()的时候,如果发现遇到了safe point,则阻塞,直到end()被调用;如果没有遇到 safe point,则直接返回。

另一方面。是否发起GC也可以设定阈值。

设想:用TinyGC的时候在所有线程里到处插一些GC::checkPoint()

void GC::checkPoint() {
    if(std::this_thread::get_id() == GCThreadId) {  // privileged thread
        if(objectNum > threshold) {   // other thresholds such as last GC time
            safePoint.startSafePoint();
            collect();
            safePoint.endSafePoint();
        }
    }
    else {
        safePoint.checkSafePoint();
    }
}
ChillMagic commented 6 years ago

已开分支experimental,所有关于多线程方面的内容,都可以push到那里