void timer_handler (int signum) 
{
	if (!interruptsAreDisabled)
	{
		printf(" interrupting\n");
		threadYield();
	}
} 
//this function will be called
//everytime that the timer fires
//...then I call yield.
void timer_handler (int signum) 
{
	if (!interruptsAreDisabled)
	{
		//printf("--------->$$$$ interrupting\n");
		//printThreadIDs();
                threadYield();
	}
} 
Example #3
0
void objectUnlock(Object *obj) {
    Thread *self = threadSelf();
    uintptr_t lockword = LOCKWORD_READ(&obj->lock);
    uintptr_t thin_locked = self->id<<TID_SHIFT;

    TRACE("Thread %p unlock on obj %p...\n", self, obj);

    if(lockword == thin_locked) {
        /* This barrier is not needed for the thin-locking implementation;
           it's a requirement of the Java memory model. */
        JMM_UNLOCK_MBARRIER();
        LOCKWORD_WRITE(&obj->lock, 0);

        /* Required by thin-locking mechanism. */
        MBARRIER();

retry:
        if(testFlcBit(obj)) {
            Monitor *mon = findMonitor(obj);

            if(!monitorTryLock(mon, self)) {
                threadYield(self);
                goto retry;
            }

            if(testFlcBit(obj) && (mon->obj == obj))
                monitorNotify(mon, self);

            monitorUnlock(mon, self);
        }
    } else {
        if((lockword & (TID_MASK|SHAPE_BIT)) == thin_locked)
            LOCKWORD_WRITE(&obj->lock, lockword - (1<<COUNT_SHIFT));
        else
            if((lockword & SHAPE_BIT) != 0) {
                Monitor *mon = (Monitor*) (lockword & ~SHAPE_BIT);

                if((mon->count == 0) && (LOCKWORD_READ(&mon->entering) == 0) &&
                                (mon->in_wait == 0)) {
                    TRACE("Thread %p is deflating obj %p...\n", self, obj);

                    /* This barrier is not needed for the thin-locking
                       implementation; it's a requirement of the Java
                       memory model. */
                    JMM_UNLOCK_MBARRIER();

                    LOCKWORD_WRITE(&obj->lock, 0);
                    LOCKWORD_COMPARE_AND_SWAP(&mon->entering, 0, UN_USED);
                }

                monitorUnlock(mon, self);
            }
    }
}
void threadLockInternal(int lockNum) {
    // Attempt to grab the lock
    int gotlock = 0;
    while (!gotlock) {
        if (!locks[lockNum]) {
            // Lock has been established
            locks[lockNum] = 1;
            gotlock = 1;
        } else {
            // Switch threads until it can be grabbed
            threadYield();
        }
    }
}
extern void threadWait(int lockNum, int conditionNum) {
    interruptDisable();

    // Prevent erroroneous calls
    if (!locks[lockNum]) {
        printf("threadWait called with unlocked mutex\n");
        exit(1);
    }

    threadUnlock(lockNum);
    // Take the current thread out of the rotation
    node_t *old = current;
    remove_node(current);
    // Wait for a condition variable signal
    while (!conditions[lockNum][conditionNum]) {
        threadYield();
    }
    // Put the thread back into the rotation
    append_node(old);
    threadLockInternal(lockNum);

    interruptEnable();
}
Example #6
0
void SeatShuffler::shuffleSeat () {
    if (EnvTable::Instantiate()->GameMode == EnvTable::Server)
        for (PlayerID i = 0; i < Players; ++i)
            if (EnvTable::Instantiate()->PlayerDat[i].RemotePlayerFlag > 1)
                mihajong_socket::listen(SOCK_CHAT - 1 + EnvTable::Instantiate()->PlayerDat[i].RemotePlayerFlag,
                                        PORT_CHAT - 1 + EnvTable::Instantiate()->PlayerDat[i].RemotePlayerFlag);
    // 退避
    InfoByPlayer<EnvTable::PlayerLabel> TmpPlayerDat;
    for (PlayerID i = 0; i < Players; i++) {
        TmpPlayerDat[i].PlayerName = EnvTable::Instantiate()->PlayerDat[i].PlayerName;
        TmpPlayerDat[i].RemotePlayerFlag = EnvTable::Instantiate()->PlayerDat[i].RemotePlayerFlag;
    }
    std::vector<PlayerID> TmpPosition;
    for (PlayerID i = 0; i < ACTUAL_PLAYERS; i++) TmpPosition.push_back(i);
    // 場決め
    if (EnvTable::Instantiate()->GameMode != EnvTable::Client) {
        // 場決め処理
        std::random_shuffle(TmpPosition.begin(), TmpPosition.end(),
        [] (unsigned max) {
            return RndNum::rnd(max);
        });
        // サーバーであれば結果を送信
        if (EnvTable::Instantiate()->GameMode == EnvTable::Server)
            for (PlayerID i = 0; i < ACTUAL_PLAYERS; i++)
                mihajong_socket::server::send(TmpPosition[i]);
    } else {
        // クライアントであれば受信する
        for (PlayerID i = 0; i < ACTUAL_PLAYERS; i++) {
            int receivedByte;
            while ((receivedByte = mihajong_socket::getc(0)) == -1) // 受信待ち
                threadYield();
            TmpPosition[i] = receivedByte;
        }
    }
    // シャッフル結果を書き込み
    for (PlayerID i = 0; i < ACTUAL_PLAYERS; i++) {
        EnvTable::Instantiate()->PlayerDat[TmpPosition[i]].PlayerName = TmpPlayerDat[i].PlayerName;
        EnvTable::Instantiate()->PlayerDat[TmpPosition[i]].RemotePlayerFlag = TmpPlayerDat[i].RemotePlayerFlag;
        posarry[i] = TmpPosition[i];
    }

    // リモートとしてマーク
    /*PlayerID tmpPlayer = TmpPosition[ClientNumber];
    if (EnvTable::Instantiate()->GameMode == EnvTable::Client)
    	for (PlayerID i = 0; i < ACTUAL_PLAYERS; i++)
    		EnvTable::Instantiate()->PlayerDat[TmpPosition[i]].RemotePlayerFlag =
    		(i != tmpPlayer) ? 1 : 0;*/

    {
        CodeConv::tostringstream o;
        o << _T("ClientNumber [") << (int)ClientNumber << _T("]");
        debug(o.str().c_str());
    }
    {
        CodeConv::tostringstream o;
        o << _T("TmpPosition ");
        for (PlayerID i = 0; i < ACTUAL_PLAYERS; i++)
            o << (i ? _T(" ") : _T("[")) << (int)TmpPosition[i];
        o << _T("]");
        debug(o.str().c_str());
    }
    {
        CodeConv::tostringstream o;
        o << _T("Remote? ");
        for (PlayerID i = 0; i < ACTUAL_PLAYERS; i++)
            o << (i ? _T(" ") : _T("[")) << (int)EnvTable::Instantiate()->PlayerDat[i].RemotePlayerFlag;
        o << _T("]");
        debug(o.str().c_str());
    }
    {
        CodeConv::tostringstream o;
        o << _T("Name ");
        for (PlayerID i = 0; i < ACTUAL_PLAYERS; i++)
            o << (i ? _T(" [") : _T("[")) << EnvTable::Instantiate()->PlayerDat[i].PlayerName << _T("]");
        debug(o.str().c_str());
    }

    return;
}