Forgot to attach the file

2015-01-31 20:36 GMT+01:00 Jan Mareš <[email protected]>:

> Hello,
>
> I'm working on porting the QEMU to the HelenOS and I need the pthread
> functionality. What I thought I could do was base pthread implementation on
> existing thead_ functions and extend them to support join, detach and
> return values. Sadly I discovered the function thread_get_id is behaving
> rather strangely. It seems like it sometimes returns a different id.
> Another possibility is that the stack is corrupted. Is this still a
> supported functionality, or are fibril_ functions supposed to substitute
> those? Please find attached a test which is failing for me. Correct me if
> my assumptions are wrong.
>
> Jan Mares
>
/*
 * Copyright (c) 2015 Jan Mares
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#define __POSIX_DEF__(x) posix_##x

#include <thread.h>
#include <stdio.h>

#include <pcut/pcut.h>
#include <fibril_synch.h>


#define    THREAD_NUM  	20
#define    LOOPS     	4



FIBRIL_MUTEX_INITIALIZE(mutex);
int                value;	// value protected by mutex

static void mutex_worker(void *parm)
{
    int   i, tmp;
    uint64_t self = (uint64_t) thread_get_id();

    // Increase the value protected by mutex LOOPS times

    for (i=0; i<LOOPS; ++i) {
        fibril_mutex_lock(&mutex);
        fprintf(stdout,"+");
        uint64_t testSelf = (uint64_t) thread_get_id();
        fprintf(stdout,"Slave: %"PRIu64" -> %"PRIu64"\n", self, testSelf);
        PCUT_ASSERT_EQUALS(testSelf, self);

        tmp = value;
        tmp = tmp+1;
        usleep(1000);	  // increase probability of race condition
        value = tmp;

        fibril_mutex_lock(&mutex);
        usleep(1000);
    }
    thread_exit(0);
}


PCUT_INIT

PCUT_TEST_SUITE(mutex);

PCUT_TEST(mutex_lock,
        PCUT_TEST_SET_TIMEOUT(30))
{
    int                   i, rc;
    thread_id_t           threads[THREAD_NUM];

    uint64_t self = (uint64_t) thread_get_id();

    // Create threads
    fprintf(stdout,"Creating %d threads\n", THREAD_NUM);
    for (i=0; i<THREAD_NUM; ++i) {
        char *name;
        asprintf(&name,"mutex_worker_%d",i);
        rc = thread_create(mutex_worker, NULL, name, &threads[i]);
        PCUT_ASSERT_EQUALS(rc,0);
        free(name);
    }
    // Test if id stays the same
    for(i=0; i<10; i++){
        uint64_t testSelf = (uint64_t) thread_get_id();
        fprintf(stdout,"Main: %"PRIu64" -> %"PRIu64"\n", self, testSelf);
        PCUT_ASSERT_EQUALS(testSelf, self);
        usleep(100);
    }

    // Wait to join all threads
    for (i=0; i<THREAD_NUM; ++i)
        thread_join(threads[i]);

    // join is not working for the time being, sleep
    sleep(10);

    fprintf(stdout,"All joined\n");

    // Check if the final value is as expected
    PCUT_ASSERT_EQUALS(value,(THREAD_NUM) * LOOPS);
}

PCUT_EXPORT(mutex);
_______________________________________________
HelenOS-devel mailing list
[email protected]
http://lists.modry.cz/listinfo/helenos-devel

Reply via email to