Ejemplo n.º 1
0
void global_names()
{
    Block* block = find_or_create_block(global_root_block(), "names_test");

    Term* a = block->compile("a = 1");

    circa::Value globalName;
    get_global_name(a, &globalName);
    test_equals(&globalName, "names_test:a");

    Term* a_2 = block->compile("a = 2");
    get_global_name(a, &globalName);
    test_equals(&globalName, "names_test:a#1");

    get_global_name(a_2, &globalName);
    test_equals(&globalName, "names_test:a#2");

    Block* block2 = create_block(block, "block2");
    Term* b = block2->compile("b = 3");
    Term* b_2 = block2->compile("b = 4");

    get_global_name(b, &globalName);
    test_equals(&globalName, "names_test:block2:b#1");
    get_global_name(b_2, &globalName);
    test_equals(&globalName, "names_test:block2:b#2");

    // Now try finding terms via their global name.
    test_assert(find_from_global_name(global_world(), "names_test:a") == a_2);
    test_assert(find_from_global_name(global_world(), "names_test:a#1") == a);
    test_assert(find_from_global_name(global_world(), "names_test:a#2") == a_2);
    test_assert(find_from_global_name(global_world(), "names_test:block2:b") == b_2);
    test_assert(find_from_global_name(global_world(), "names_test:block2:b#1") == b);
    test_assert(find_from_global_name(global_world(), "names_test:block2:b#2") == b_2);
}
Ejemplo n.º 2
0
int am_agent_instance_init_init(int id) {
    int status = AM_ERROR;
#if defined(_WIN32)
    ic_sem = CreateSemaphoreA(NULL, 1, 1, get_global_name("Global\\"AM_CONFIG_INIT_NAME, id));
    if (ic_sem != NULL) {
        status = AM_SUCCESS;
    }
#elif defined(__APPLE__)
    kern_return_t rv = semaphore_create(mach_task_self(), &ic_sem, SYNC_POLICY_FIFO, 1);
    if (rv == KERN_SUCCESS) {
        status = AM_SUCCESS;
    }
#else
    ic_sem = sem_open(get_global_name(AM_CONFIG_INIT_NAME, id), O_CREAT, 0600, 1);
    if (ic_sem != SEM_FAILED) {
        status = AM_SUCCESS;
    }
#endif
    return status;
}
Ejemplo n.º 3
0
void am_agent_instance_init_release(int id, char unlink) {
#if defined(_WIN32)
    CloseHandle(ic_sem);
#elif defined(__APPLE__)
    semaphore_destroy(mach_task_self(), ic_sem);
#else
    sem_close(ic_sem);
    if (unlink) {
        sem_unlink(get_global_name(AM_CONFIG_INIT_NAME, id));
    }
#endif
}
Ejemplo n.º 4
0
void module_possibly_patch_new_function(World* world, Block* function)
{
    NativePatchWorld* moduleWorld = world->nativePatchWorld;

    Value globalName;
    get_global_name(function, &globalName);

    // No global name: no patch.
    if (!is_string(&globalName))
        return;

    // Lookup in the global table.
    caValue* patchEntry = hashtable_get(&moduleWorld->everyPatchedFunction, &globalName);

    if (patchEntry == NULL) {
        // No patch for this function.
        return;
    }

    caValue* nativeModuleName = list_get(patchEntry, 0);
    caValue* functionName = list_get(patchEntry, 1);

    // Found a patch; apply it.
    NativePatch* module = get_existing_native_patch(world, as_cstring(nativeModuleName));

    if (module == NULL) {
        std::cout << "in module_possibly_patch_new_function, couldn't find module: "
            << as_cstring(nativeModuleName) << std::endl;
        return;
    }

    std::map<std::string, EvaluateFunc>::const_iterator it;
    it = module->patches.find(as_cstring(functionName));

    if (it == module->patches.end()) {
        std::cout << "in module_possibly_patch_new_function, couldn't find function: "
            << as_cstring(functionName) << std::endl;
        return;
    }
    
    EvaluateFunc evaluateFunc = it->second;

    install_function(function->owningTerm, evaluateFunc);
}
Ejemplo n.º 5
0
void search_every_global_name()
{
    // This test is brave. We go through every single term in the world, find its
    // global name (if it exists), then see if we can find the original term using
    // the global name.

    circa::Value globalName;
    for (BlockIterator it(global_root_block()); it.unfinished(); it.advance()) {
        get_global_name(*it, &globalName);

        if (!is_string(&globalName))
            continue;

        Term* searchResult = find_from_global_name(global_world(), as_cstring(&globalName));

        if (searchResult != *it) {
            std::cout << "Global name search failed for term: " << global_id(*it)
                << ", with global name: " << as_cstring(&globalName) << std::endl;
            declare_current_test_failed();
        }
    }
}
Ejemplo n.º 6
0
void am_log_init(int id, int status) {
    int i;
    char opened = 0;

    am_agent_instance_init_init(id);

    if (am_log_handle == NULL) {
        am_log_handle = (struct am_shared_log *) malloc(sizeof (struct am_shared_log));
        if (am_log_handle == NULL) {
            return;
        }
    } else if (am_log_handle->reader_pid == getpid()) {
        return;
    }

    am_log_handle->reader_pid = getpid();
    snprintf(am_log_handle->area_file_name, sizeof (am_log_handle->area_file_name),
#ifdef __sun
            "/am_log_%d"
#else
            AM_GLOBAL_PREFIX"am_log_%d"
#endif
            , id);
    am_log_handle->area_size = page_size(sizeof (struct am_log));

#ifdef _WIN32
    am_log_handle->area_file_id = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
            0, (DWORD) am_log_handle->area_size, am_log_handle->area_file_name);

    if (am_log_handle->area_file_id == NULL) return;
    if (NULL != am_log_handle->area_file_id && GetLastError() == ERROR_ALREADY_EXISTS) {
        opened = 1;
    }

    if (am_log_handle->area_file_id != NULL) {
        am_log_handle->area = MapViewOfFile(am_log_handle->area_file_id, FILE_MAP_ALL_ACCESS,
                0, 0, am_log_handle->area_size);
    }
    if (am_log_handle->area != NULL) {
        if (status == AM_SUCCESS || status == AM_EAGAIN) {
            struct am_log *log = (struct am_log *) am_log_handle->area;

            memset(log, 0, am_log_handle->area_size);

            log->bucket_count = AM_LOG_QUEUE_SIZE;
            log->bucket_size = AM_LOG_MESSAGE_SIZE;
            log->in = log->out = log->read_count = log->write_count = 0;

            for (i = 0; i < AM_MAX_INSTANCES; i++) {
                struct log_files *f = &log->files[i];
                f->fd_audit = f->fd_debug = -1;
                f->used = AM_FALSE;
                f->instance_id = 0;
                f->level_debug = f->level_audit = AM_LOG_LEVEL_NONE;
                f->max_size_debug = f->max_size_audit = 0;
            }

            am_log_lck.exit = CreateEvent(NULL, FALSE, FALSE, get_global_name(AM_GLOBAL_PREFIX"am_log_exit", id));
            if (am_log_lck.exit == NULL && GetLastError() == ERROR_ACCESS_DENIED) {
                am_log_lck.exit = OpenEventA(SYNCHRONIZE, TRUE, get_global_name(AM_GLOBAL_PREFIX"am_log_exit", id));
            }
            am_log_lck.lock = CreateMutex(NULL, FALSE, get_global_name(AM_GLOBAL_PREFIX"am_log_lock", id));
            if (am_log_lck.lock == NULL && GetLastError() == ERROR_ACCESS_DENIED) {
                am_log_lck.lock = OpenMutexA(SYNCHRONIZE, TRUE, get_global_name(AM_GLOBAL_PREFIX"am_log_lock", id));
            }
            am_log_lck.new_data_cond = CreateEvent(NULL, FALSE, FALSE, get_global_name(AM_GLOBAL_PREFIX"am_log_queue_empty", id));
            if (am_log_lck.new_data_cond == NULL && GetLastError() == ERROR_ACCESS_DENIED) {
                am_log_lck.new_data_cond = OpenEventA(SYNCHRONIZE, TRUE, get_global_name(AM_GLOBAL_PREFIX"am_log_queue_empty", id));
            }
            am_log_lck.new_space_cond = CreateEvent(NULL, FALSE, FALSE, get_global_name(AM_GLOBAL_PREFIX"am_log_queue_overflow", id));
            if (am_log_lck.new_space_cond == NULL && GetLastError() == ERROR_ACCESS_DENIED) {
                am_log_lck.new_space_cond = OpenEventA(SYNCHRONIZE, TRUE, get_global_name(AM_GLOBAL_PREFIX"am_log_queue_overflow", id));
            }

            log->owner = getpid();
            am_log_handle->reader_thr = CreateThread(NULL, 0,
                    (LPTHREAD_START_ROUTINE) am_log_worker, NULL, 0, NULL);
        }
    }

#else
    am_log_handle->area_file_id = shm_open(am_log_handle->area_file_name, O_CREAT | O_EXCL | O_RDWR, 0666);
    if (am_log_handle->area_file_id == -1 && EEXIST != errno) {
        return;
    }
    if (am_log_handle->area_file_id == -1) {
        /* already there, open without O_EXCL and go; if
         * something goes wrong, close and cleanup */
        am_log_handle->area_file_id = shm_open(am_log_handle->area_file_name, O_RDWR, 0666);
        if (am_log_handle->area_file_id == -1) {
            fprintf(stderr, "am_log_init() shm_open failed (%d)\n", errno);
            free(am_log_handle);
            am_log_handle = NULL;
            return;
        } else {
            opened = 1;
        }
    } else {
        /* we just created the shm area, must setup; if
         * something goes wrong, delete the shm area and
         * cleanup
         */
        if (ftruncate(am_log_handle->area_file_id, am_log_handle->area_size) == -1) {
            fprintf(stderr, "am_log_init() ftruncate failed\n");
            return;
        }
    }
    if (am_log_handle->area_file_id != -1) {
        am_log_handle->area = mmap(NULL, am_log_handle->area_size,
                PROT_READ | PROT_WRITE, MAP_SHARED, am_log_handle->area_file_id, 0);
        if (am_log_handle->area == MAP_FAILED) {
            fprintf(stderr, "am_log_init() mmap failed (%d)\n", errno);
            free(am_log_handle);
            am_log_handle = NULL;
        } else {
            pthread_mutexattr_t exit_attr, lock_attr;
            pthread_condattr_t new_data_attr, new_space_attr;

            struct am_log *log = (struct am_log *) am_log_handle->area;

            pthread_mutexattr_init(&exit_attr);
            pthread_mutexattr_init(&lock_attr);
            pthread_condattr_init(&new_data_attr);
            pthread_condattr_init(&new_space_attr);
            pthread_mutexattr_setpshared(&exit_attr, PTHREAD_PROCESS_SHARED);
            pthread_mutexattr_setpshared(&lock_attr, PTHREAD_PROCESS_SHARED);
            pthread_condattr_setpshared(&new_data_attr, PTHREAD_PROCESS_SHARED);
            pthread_condattr_setpshared(&new_space_attr, PTHREAD_PROCESS_SHARED);

            if (status == AM_SUCCESS || status == AM_EAGAIN) {

                memset(log, 0, am_log_handle->area_size);

                log->bucket_count = AM_LOG_QUEUE_SIZE;
                log->bucket_size = AM_LOG_MESSAGE_SIZE;
                log->in = log->out = log->read_count = log->write_count = 0;

                for (i = 0; i < AM_MAX_INSTANCES; i++) {
                    struct log_files *f = &log->files[i];
                    f->fd_audit = f->fd_debug = -1;
                    f->used = AM_FALSE;
                    f->instance_id = 0;
                    f->level_debug = f->level_audit = AM_LOG_LEVEL_NONE;
                    f->max_size_debug = f->max_size_audit = 0;
                }

                pthread_mutex_init(&log->exit, &exit_attr);
                pthread_mutex_init(&log->lock, &lock_attr);
                pthread_cond_init(&log->new_data_cond, &new_data_attr);
                pthread_cond_init(&log->new_space_cond, &new_space_attr);

                pthread_mutex_lock(&log->exit);
                pthread_create(&am_log_handle->reader_thr, NULL, am_log_worker, NULL);
                log->owner = getpid();
            }

            pthread_mutexattr_destroy(&exit_attr);
            pthread_mutexattr_destroy(&lock_attr);
            pthread_condattr_destroy(&new_data_attr);
            pthread_condattr_destroy(&new_space_attr);
        }
    }

#endif
}