示例#1
0
文件: sandbox.c 项目: LiuCihang/OJ
bool
sandbox_check(sandbox_t * psbox)
{
    FUNC_BEGIN("sandbox_check(%p)", psbox);

    assert(psbox);

    if (psbox == NULL)
    {
        WARNING("psbox: bad pointer");
        FUNC_RET(false, "sandbox_check()");
    }

    P(&psbox->mutex);

    /* Don't change the state of a running sandbox */
    if (IS_RUNNING(psbox) || IS_BLOCKED(psbox))
    {
        V(&psbox->mutex);
        FUNC_RET(false, "sandbox_check()");
    }
    DBG("passed sandbox status check");

    /* Clear previous statistics and status */
    if (IS_FINISHED(psbox))
    {
        __sandbox_stat_fini(&psbox->stat);
        __sandbox_stat_init(&psbox->stat);
        psbox->result = S_RESULT_PD;
    }

    /* Update status to PRE */
    if (psbox->status != S_STATUS_PRE)
    {
        psbox->status = S_STATUS_PRE;
        pthread_cond_broadcast(&psbox->update);
    }

    if (!__sandbox_task_check(&psbox->task))
    {
        V(&psbox->mutex);
        FUNC_RET(false, "sandbox_check()");
    }
    DBG("passed task spec validation");

    if (psbox->ctrl.tracer == NULL)
    {
        V(&psbox->mutex);
        FUNC_RET(false, "sandbox_check()");
    }
    DBG("passed ctrl tracer validation");

#ifdef WITH_CUSTOM_MONITOR
    if (psbox->ctrl.monitor == NULL)
    {
        V(&psbox->mutex);
        FUNC_RET(false, "sandbox_check()");
    }
    DBG("passed ctrl monitor validation");
#endif /* WITH_CUSTOM_MONITOR */

    /* Update status to RDY */
    if (psbox->status != S_STATUS_RDY)
    {
        psbox->status = S_STATUS_RDY;
        pthread_cond_broadcast(&psbox->update);
    }
    V(&psbox->mutex);

    FUNC_RET(true, "sandbox_check()");
}
示例#2
0
文件: stereogc.cpp 项目: 353/viewercv
static int64 icvAlphaExpand(int64 Eprev, int alpha, CvStereoGCState* state, CvStereoGCState2* state2) {
    GCVtx* var, *var1;
    int64 E = 0;
    int delta, E00 = 0, E0a = 0, Ea0 = 0, Eaa = 0;
    int k, a, d, d1, x, y, x1, y1, rows = state->left->rows, cols = state->left->cols;
    int nvtx = 0, nedges = 2;
    GCVtx* vbuf = (GCVtx*)state->vtxBuf->data.ptr;
    GCEdge* ebuf = (GCEdge*)state->edgeBuf->data.ptr;
    int maxR = state2->interactionRadius;
    const int* dtab = state2->dataCostFuncTab;
    const int* stabR = state2->smoothnessR + CUTOFF;
    const int* stabI = state2->smoothnessGrayDiff + 255;
    const uchar* left0 = state->left->data.ptr;
    const uchar* right0 = state->right->data.ptr;
    short* dleft0 = state->dispLeft->data.s;
    short* dright0 = state->dispRight->data.s;
    GCVtx** pleft0 = (GCVtx**)state->ptrLeft->data.ptr;
    GCVtx** pright0 = (GCVtx**)state->ptrRight->data.ptr;
    int step = state->left->step;
    int dstep = (int)(state->dispLeft->step / sizeof(short));
    int pstep = (int)(state->ptrLeft->step / sizeof(GCVtx*));
    int aa[] = { alpha, -alpha };

    //double t = (double)cvGetTickCount();

    assert(state->left->step == state->right->step &&
           state->dispLeft->step == state->dispRight->step &&
           state->ptrLeft->step == state->ptrRight->step);
    for (k = 0; k < 2; k++) {
        ebuf[k].dst = 0;
        ebuf[k].next = 0;
        ebuf[k].weight = 0;
    }

    for (y = 0; y < rows; y++) {
        const uchar* left = left0 + step * y;
        const uchar* right = right0 + step * y;
        const short* dleft = dleft0 + dstep * y;
        const short* dright = dright0 + dstep * y;
        GCVtx** pleft = pleft0 + pstep * y;
        GCVtx** pright = pright0 + pstep * y;
        const uchar* lr[] = { left, right };
        const short* dlr[] = { dleft, dright };
        GCVtx** plr[] = { pleft, pright };

        for (k = 0; k < 2; k++) {
            a = aa[k];
            for (y1 = y + (y > 0); y1 <= y + (y < rows - 1); y1++) {
                const short* disp = (k == 0 ? dleft0 : dright0) + y1 * dstep;
                GCVtx** ptr = (k == 0 ? pleft0 : pright0) + y1 * pstep;
                for (x = 0; x < cols; x++) {
                    GCVtx* v = ptr[x] = &vbuf[nvtx++];
                    v->first = 0;
                    v->weight = disp[x] == (short)(OCCLUDED ? -OCCLUSION_PENALTY2 : 0);
                }
            }
        }

        for (x = 0; x < cols; x++) {
            d = dleft[x];
            x1 = x + d;
            var = pleft[x];

            // (left + x, right + x + d)
            if (d != alpha && d != OCCLUDED && (unsigned)x1 < (unsigned)cols) {
                var1 = pright[x1];
                d1 = dright[x1];
                if (d == -d1) {
                    assert(var1 != 0);
                    delta = IS_BLOCKED(alpha, d) ? INFINITY : 0;
                    //add inter edge
                    E += icvAddTerm(var, var1,
                                    dtab[icvDataCostFuncGraySubpix(left + x*3, right + x1*3)],
                                    delta, delta, 0, ebuf, nedges);
                } else if (IS_BLOCKED(alpha, d)) {
                    E += icvAddTerm(var, var1, 0, INFINITY, 0, 0, ebuf, nedges);
                }
            }

            // (left + x, right + x + alpha)
            x1 = x + alpha;
            if ((unsigned)x1 < (unsigned)cols) {
                var1 = pright[x1];
                d1 = dright[x1];

                E0a = IS_BLOCKED(d, alpha) ? INFINITY : 0;
                Ea0 = IS_BLOCKED(-d1, alpha) ? INFINITY : 0;
                Eaa = dtab[icvDataCostFuncGraySubpix(left + x*3, right + x1*3)];
                E += icvAddTerm(var, var1, 0, E0a, Ea0, Eaa, ebuf, nedges);
            }

            // smoothness
            for (k = 0; k < 2; k++) {
                GCVtx** p = plr[k];
                const short* disp = dlr[k];
                const uchar* img = lr[k] + x * 3;
                int scale;
                var = p[x];
                d = disp[x];
                a = aa[k];

                if (x < cols - 1) {
                    var1 = p[x+1];
                    d1 = disp[x+1];
                    scale = stabI[img[0] - img[3]];
                    E0a = icvSmoothnessCostFunc(d, a, maxR, stabR, scale);
                    Ea0 = icvSmoothnessCostFunc(a, d1, maxR, stabR, scale);
                    E00 = icvSmoothnessCostFunc(d, d1, maxR, stabR, scale);
                    E += icvAddTerm(var, var1, E00, E0a, Ea0, 0, ebuf, nedges);
                }

                if (y < rows - 1) {
                    var1 = p[x+pstep];
                    d1 = disp[x+dstep];
                    scale = stabI[img[0] - img[step]];
                    E0a = icvSmoothnessCostFunc(d, a, maxR, stabR, scale);
                    Ea0 = icvSmoothnessCostFunc(a, d1, maxR, stabR, scale);
                    E00 = icvSmoothnessCostFunc(d, d1, maxR, stabR, scale);
                    E += icvAddTerm(var, var1, E00, E0a, Ea0, 0, ebuf, nedges);
                }
            }

            // visibility term
            if (d != OCCLUDED && IS_BLOCKED(alpha, -d)) {
                x1 = x + d;
                if ((unsigned)x1 < (unsigned)cols) {
                    if (d != -dleft[x1]) {
                        var1 = pleft[x1];
                        E += icvAddTerm(var, var1, 0, INFINITY, 0, 0, ebuf, nedges);
                    }
                }
            }
        }
    }

    //t = (double)cvGetTickCount() - t;
    ebuf[0].weight = ebuf[1].weight = 0;
    E += icvGCMaxFlow(vbuf, nvtx, ebuf, state2->orphans, state2->maxOrphans);

    if (E < Eprev) {
        for (y = 0; y < rows; y++) {
            short* dleft = dleft0 + dstep * y;
            short* dright = dright0 + dstep * y;
            GCVtx** pleft = pleft0 + pstep * y;
            GCVtx** pright = pright0 + pstep * y;
            for (x = 0; x < cols; x++) {
                GCVtx* var = pleft[x];
                if (var && var->parent && var->t) {
                    dleft[x] = (short)alpha;
                }

                var = pright[x];
                if (var && var->parent && var->t) {
                    dright[x] = (short) - alpha;
                }
            }
        }
    }

    return MIN(E, Eprev);
}
示例#3
0
文件: sandbox.c 项目: LiuCihang/OJ
static void *
__sandbox_monitor(sandbox_t * psbox)
{
    FUNC_BEGIN("sandbox_monitor(%p)", psbox);

    assert(psbox);

    /* Temporary variables */
    ctrl_t * pctrl = &psbox->ctrl;

    /* Enter the main monitoring loop */
    P(&pctrl->mutex);
    DBG("entering: the monitoring thread");

    /* Wait for the sandbox to start */
    P(&psbox->mutex);
    while (NOT_STARTED(psbox))
    {
        pthread_cond_wait(&psbox->update, &psbox->mutex);
    }
    V(&psbox->mutex);

    /* Detect and handle events while the sandbox is running */
    P(&psbox->mutex);
    while (IS_RUNNING(psbox) || IS_BLOCKED(psbox))
    {
        V(&psbox->mutex);

        /* An event might have already been posted */
        if (IS_IDLE(pctrl))
        {
            pthread_cond_wait(&pctrl->sched, &pctrl->mutex);
            if (IS_IDLE(pctrl))
            {
                P(&psbox->mutex);
                continue;
            }
        }

        /* Start investigating the event */
        DBG("detected event: %s {%lu %lu %lu %lu %lu %lu %lu}",
            s_event_type_name(pctrl->event.type),
            pctrl->event.data.__bitmap__.A,
            pctrl->event.data.__bitmap__.B,
            pctrl->event.data.__bitmap__.C,
            pctrl->event.data.__bitmap__.D,
            pctrl->event.data.__bitmap__.E,
            pctrl->event.data.__bitmap__.F,
            pctrl->event.data.__bitmap__.G);

        /* Consult the sandbox policy to determine next action */
        ((policy_entry_t)pctrl->policy.entry)(&pctrl->policy, &pctrl->event,
                                              &pctrl->action);

        DBG("decided action: %s {%lu %lu}",
            s_action_type_name(pctrl->action.type),
            pctrl->action.data.__bitmap__.A,
            pctrl->action.data.__bitmap__.B);

        P(&psbox->mutex);

        /* Notify the tracer to perform the decided action */
        pctrl->idle = true;
        pthread_cond_broadcast(&pctrl->sched);
    }
    V(&psbox->mutex);

    DBG("leaving: the monitoring thread");
    V(&pctrl->mutex);

    FUNC_RET((void *)&psbox->result, "sandbox_monitor()");
}